Created
November 1, 2012 06:25
-
-
Save denispeplin/3992175 to your computer and use it in GitHub Desktop.
Add display_filter option to simple_form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SimpleForm | |
class FormBuilder < ActionView::Helpers::FormBuilder | |
# tried to monkey patch without copy'n'pasting, broke associations | |
def input(attribute_name, options={}, &block) | |
options = @defaults.deep_dup.deep_merge(options) if @defaults | |
chosen = | |
if name = options[:wrapper] | |
name.respond_to?(:render) ? name : SimpleForm.wrapper(name) | |
else | |
wrapper | |
end | |
# here display_filter logic begins | |
display_filter = options.delete(:display_only) | |
if (! display_filter) || (display_filter && display_filter.include?(attribute_name)) | |
chosen.render find_input(attribute_name, options, &block) | |
end | |
end | |
end | |
end |
# display display_only attributes
if display_filter
super if display_filter.include?(attribute_name)
else
super
end
Working example here: https://github.com/denispeplin/display_filter_demo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There we go:
The issues:
options
argument indef input
, so we need to accessself.options
to access the builder options instead (I've also changed to*args
inside the input definition, since options are not being used there.display_only: [:name]
, not inside the:defaults
key. If you want to move it inside defaults, you have to access it the same way when calling the form.I tested the app and it worked fine for me, let me know how it goes.