-
Notifications
You must be signed in to change notification settings - Fork 106
Description
- What would you like to be able to do? Can you provide some examples?
I use Field-nested-has_many a lot. And this time in order to make the experience better when rendering fields within a nested has many way.
For instance My page is a 'Period' and this Object has been build through a callBack after user creation. i have something like this
class User < ApplicationRecord
after_create :set_period_pack
def set_period_pack
self.numeric_seasonality == 0 ? # ANNUAL
Periodcreate(user:self, weekly:false, yearly:true) :
Period.create(user:self, weekly:true, yearly:false)
end
end
So the Period is build through a callback.
Then when the user want to fill out the Period form, in fact it's an edit view.
So there are some fields that it's not necessary to display.
As we can see Period object has already some set attributes like weekly:false, yearly:true.
These attributes should alow me to display only fields that matter to display for a 'period yearly:true'.
I would like to hide inside form some fields base on the page attribute already existing .
So the objective is to pass the page object to the a Field-nested-has_many.
#<Administrate::Page::Form:0x00007ff4c258ade0>
Basically as the gem is designed right now, it's seams to be not possible to pass any page params object to the final field like I did initially through the render
send page parameter to the final field
inside the gem administrate-field-nested_has_many inside #views/fields/nested_has_many/_fields.html.erb
<div class="nested-fields nested-fields_<%= field.attribute %>">
<div class="card light-shadow">
<div class="card-body">
<% field.nested_fields_for_builder(f).each do |attribute| -%>
<div class="form-group field-unit field-unit--<%= attribute.html_class %>">
<%= render_field attribute, f: f , page: page%> # ππ½ I need that page object in within a field
</div>
<% end -%>
<%# some code%>
</div>
</div>
</div>
receive the page parameter from the gem
inside #app/views/fields/multiple_select_field/_form.html.erb
<%= "π₯ page.resource.weekly : #{ page.resource.weekly}"%>
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select(
field.attribute,
options_from_collection_for_select(
field.selectable_options,
:to_s,
:to_s,
field.data.presence,
),
{}, { :multiple => true, :class => 'selectpicker' }#
) %>
</div>
then we have ActionView::Template::Error => undefined local variable or method `page' for ...
and that normal cause page is not defined yet.
- How could we go about implementing that?
I founded a way to do this but it's kinda not very safe I think.
inside #app/views/admin/period/_form.html.erb
at the top of the form i just added that line
<%= @page = page.resource %>
<%= form_for([namespace, page.resource], html: {
class: "form",
id: "#{action_name}_#{controller_name.singularize}_#{page.resource.id}" }) do |f| %>
<%# rest of the code%>
<% end %>
inside the gem administrate-field-nested_has_many inside #views/fields/nested_has_many/_fields.html.erb
<div class="nested-fields nested-fields_<%= field.attribute %>">
<div class="card light-shadow">
<div class="card-body">
<% field.nested_fields_for_builder(f).each do |attribute| -%>
<div class="form-group field-unit field-unit--<%= attribute.html_class %>">
<%= render_field attribute, f: f , page: @page%> # π₯
</div>
<% end -%>
<%# some code%>
</div>
</div>
</div>
inside the gem administrate-field-nested_has_many inside #app/views/fields/nested_has_many/_form.html.erb
I know that it's kinda snaky but here the thing how hide properly fields if we needn't ?
I can put now some logic inside the Gem like
<% if @page.class == Period %>
<% if @page.weekly %>
<!-- / page is Period and weekly -->
<% if @page.refund_on_period %>
<!-- / page is PeriodPack and weekly and period_refunds -->
<% if field.attribute != :period_selected_ranges%>
<%= render(
partial: "fields/nested_has_many/fieldset",
locals: {
field: field,
f: f
},
) %>
<% end %>
<% else %>
#....
<% end %>
<% end %>
<!-- / page is Period but not weekly -->
<%else %>
<% if @page.refund_on_period %>
#....
<% else %>
#....
<% end %>
<% end %>
<%else %>
#....
<% end %>
and then inside my final field i can check if the Period is weekly or not before display the field
<%if @page.weekly?%>
# the field
<%end%>
- Can you think of other approaches to the problem?
Maybe rethink the Gem and implement the following approach option and propose a PR for that feature if it's not existing yet at all. Because clearly i do not know if there is an easiest way to suround my objective which is to hide some field when i need to.
=> pass params page to the gem
=> Add some conditions inside hash option.
I guess omething like this
foo: Field::NestedHasMany.with_options(skip: :foo, {
:page => page,
:hide_taz_fied_name_if => [page.weekly? && page.monthly? || other_conditions ],
:hide_bar_field_name_if => [page_other_boolean?]
}, limit: 1000),