Skip to content

Added partial_extra_params to link_to_add_nested helper - #64

Closed
gregogalante wants to merge 2 commits into
arielj:mainfrom
1d3o:main
Closed

Added partial_extra_params to link_to_add_nested helper#64
gregogalante wants to merge 2 commits into
arielj:mainfrom
1d3o:main

Conversation

@gregogalante

Copy link
Copy Markdown

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,
)

@arielj

arielj commented Aug 30, 2022

Copy link
Copy Markdown
Owner

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

@gregogalante

Copy link
Copy Markdown
Author

Hey @arielj, have you checked?

@arielj

arielj commented Sep 14, 2022

Copy link
Copy Markdown
Owner

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 arielj left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! sorry for the long delay responding here

@arielj

arielj commented May 25, 2023

Copy link
Copy Markdown
Owner

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 4121c91

@arielj arielj closed this May 25, 2023
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

Successfully merging this pull request may close these issues.

2 participants