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

update the extending models guide for rails 4 and strong paramaters #2565

Merged
merged 1 commit into from
Mar 17, 2014

Conversation

jess
Copy link
Contributor

@jess jess commented Mar 14, 2014

Is this how you'd recommend doing it?

@parndt
Copy link
Member

parndt commented Mar 15, 2014

@ugisozols what do you think?

@ugisozols
Copy link
Member

I think we need some kind of extra_params method which then gets merged into page_params. This would allow users to only add stuff they need by overriding that method instead of the whole page_params.

@parndt
Copy link
Member

parndt commented Mar 16, 2014

What about

def page_params
  super.permit(:extra_param)
end

Does that work? It wouldn't allow the user to remove existing params, just add new ones.

@jess
Copy link
Contributor Author

jess commented Mar 16, 2014

I'd prefer the extra method too.

I tried the super.permit(:extra_params) and get:

super: no superclass method `page_params' for #<Refinery::Admin::PagesController:0x007fdbdbaa6630>

I guess because we're actually overriding that method as opposed to sub classing it??

What if did the extra_params like this:

  def page_params
    params.require(:page).permit(
      *extra_params, :browser_title, :draft, :link_url, :menu_title, :meta_description,
      :parent_id, :skip_to_first_child, :show_in_menu, :title, :view_template,
      :layout_template, :main_photo_id, parts_attributes: [:id, :title, :body, :position]
      ).merge(extra_params)
  end

  def extra_params
    []
  end

Then we'd just need to overwrite extra_params.

The only problem with that might be if another engine/gem needed to also permit params, how could it be done from more than one place?

It'd be cool if we could just call a method from our decorator like add_extra_params :extra_param and that method would just continue adding to an instance variable. Similar to a before filter. But if I try to define that method in the main admin pages controller class, it won't work because our decorator gets loaded first and I get a no method error.

@parndt
Copy link
Member

parndt commented Mar 16, 2014

Ah, of course.. it's not a subclass.

This is the sort of thing that alias_method_chain was invented for.. even though it's deemed an anti-pattern.

def page_params_with_my_ones
  page_params_without_my_ones.permit(:extra_params)
end

alias_method_chain :page_params, :my_ones

I'd rather not have to add even more "magic method" APIs to Refinery if we can help it.

Sometimes I can see, with Devise, why you have to inherit all of their controllers to modify how it works at all.

@jess
Copy link
Contributor Author

jess commented Mar 16, 2014

cool, I wasn't that familiar with alias_method_chain.

I got this to work:

Refinery::Admin::PagesController.class_eval do
  def page_params_with_my_params
    page_params_without_my_params.merge(params.require(:page).permit(:main_photo_id))
  end

  alias_method_chain :page_params, :my_params
end

From reading a few posts, it does seem to be abused, but under the right circumstances, it's has a valid use. I'd say this is a good recommendation for it since we don't want to completely overwrite the method and we cannot subclass it.

@ugisozols
Copy link
Member

Ah yes, I totally forgot about alias_method_chain. 👍

@jess keen to update guide mentioning alias_method_chain?

@jess
Copy link
Contributor Author

jess commented Mar 17, 2014

yes, I'll be glad to...

@jess
Copy link
Contributor Author

jess commented Mar 17, 2014

what do you think?

@ugisozols
Copy link
Member

Looks good. @parndt?

@parndt
Copy link
Member

parndt commented Mar 17, 2014

@jess looks good, can you please squash into a single commit for this change?

make background image code format

instead of overwriting the page_params method, recommend using
alias_method_chain
@jess
Copy link
Contributor Author

jess commented Mar 17, 2014

@parndt sure, I've done that here cd407e3, do I need to issue a new PR? Or, can I force over these changes?

@parndt
Copy link
Member

parndt commented Mar 17, 2014

@jess you just need to force push to this branch guide_update on your fork.

@jess
Copy link
Contributor Author

jess commented Mar 17, 2014

@parndt thanks for the tip, all done

parndt added a commit that referenced this pull request Mar 17, 2014
update the extending models guide for rails 4 and strong paramaters
@parndt parndt merged commit 5a22d7a into refinery:master Mar 17, 2014
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.

3 participants