vanilla-nested icon indicating copy to clipboard operation
vanilla-nested copied to clipboard

Added partial_extra_params to link_to_add_nested helper

Open gregogalante opened this issue 3 years ago • 3 comments

I have added a new parameter "partial_extra_params" on link_to_add_nested helper to permit user to send other extra parameters to the nested fields attributes.

In my example i have a form for the model Strategy with different Strategies::Rule for every typology. Now i can pass the typology value to an hidden field on rules attributes using partial_extra_params.

# Models
class Strategy < ApplicationRecord
  has_many :strategies_rules, dependent: :destroy, class_name: 'Strategies::Rule'
  accepts_nested_attributes_for :strategies_rules, allow_destroy: true, limit: 100
end

class Strategies::Rule < ApplicationRecord
  enum typology: {
    tbd: 0,
    entry: 1,
    take_profit: 2,
    stop_loss: 3,
    trading_hours: 4
  }, _suffix: true

  belongs_to :strategy
end

# Controller
class StrategiesController < ApplicationController
  # ...
  
  def strategy_params
    params.require(:strategy).permit(:name, :default_pair_id, strategies_rules_attributes: %i[id name typology _destroy])
  end
end

# View helper
link_to_add_nested(form, :strategies_rules, "#rule-fields-#{dom_id(strategy)}-#{typology}",
  partial_extra_params: { typology: typology },
  link_text: 'Add rule' to typology,
)

gregogalante avatar Aug 25 '22 13:08 gregogalante

hey! thanks! sorry I've been slow to reply, I'll check this one of these days, I've been super busy lately

arielj avatar Aug 30 '22 21:08 arielj

Hey @arielj, have you checked?

gregogalante avatar Sep 13 '22 08:09 gregogalante

Hey! sorry for the long response, I did make some time to check the PR and I like the feature, I have some suggestions and some requests.

It would be great to have a test for this (I imagine something simple, like adding a partial using one variable that you then pass in the method call and assert the content is present in the rendered html). You can check tests/VanillaNestedTests/test/helpers/vanilla_nested_helpers_test.rb for examples on testing that method (if a unit test doesn't work we can also have a system test).

We should include also an update in the README.md to document this new param.

Lastly, maybe the argument can be named partial_locals instead? I think it makes it more clear

thanks for the collaboration!!

arielj avatar Sep 14 '22 03:09 arielj

thanks! I'm closing this PR because I had to merge things manually locally (there are conflicts after merging other PRs).

This was merged into main here https://github.com/arielj/vanilla-nested/commit/4121c9148e155754467cd1d02eb6b2c1759da757

arielj avatar May 25 '23 20:05 arielj