Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: Feature or Already possible ? Pass Page params to field-nested_has_many - hide field if not necessary #39

Open
v2lrf opened this issue May 22, 2020 · 1 comment

Comments

@v2lrf
Copy link

v2lrf commented May 22, 2020

  • 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),
@v2lrf v2lrf changed the title Issue: Feature Pass Page params to field-nested_has_many - Already possible ? Issue: Feature or Already possible ? Pass Page params to field-nested_has_many - hide field if not necessary May 23, 2020
@pablobm
Copy link
Contributor

pablobm commented Jun 1, 2020

I think that this is not really an issue with this plugin, but rather with Administrate itself, which doesn't give information to each field about their context. Does that sound right?

If this is the case, there isn't a better solution at the moment. We'd have to make some changes to Administrate, but this is slow going. I'm already working on something in that general area (but not the same thing), so any change like this would have to wait, to avoid conflicts and allow for a cleaner upgrade path :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants