Class: ActionView::Helpers::FormBuilder::PropertySetFormBuilderProxy
- Inherits:
-
Object
- Object
- ActionView::Helpers::FormBuilder::PropertySetFormBuilderProxy
- Defined in:
- lib/property_sets/action_view_extension.rb
Instance Attribute Summary collapse
-
#object_name ⇒ Object
Returns the value of attribute object_name.
-
#property_set ⇒ Object
Returns the value of attribute property_set.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #check_box(property, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #hidden_field(property, options = {}) ⇒ Object
-
#initialize(property_set, template, object_name) ⇒ PropertySetFormBuilderProxy
constructor
A new instance of PropertySetFormBuilderProxy.
- #prepare_id_name(property, options) ⇒ Object
- #prepare_options(property, options, &block) ⇒ Object
- #radio_button(property, checked_value = "1", options = {}) ⇒ Object
- #select(property, choices, options = {}, html_options = {}) ⇒ Object
- #text_field(property, options = {}) ⇒ Object
Constructor Details
#initialize(property_set, template, object_name) ⇒ PropertySetFormBuilderProxy
Returns a new instance of PropertySetFormBuilderProxy.
11 12 13 14 15 |
# File 'lib/property_sets/action_view_extension.rb', line 11 def initialize(property_set, template, object_name) self.property_set = property_set self.template = template self.object_name = object_name end |
Instance Attribute Details
#object_name ⇒ Object
Returns the value of attribute object_name.
9 10 11 |
# File 'lib/property_sets/action_view_extension.rb', line 9 def object_name @object_name end |
#property_set ⇒ Object
Returns the value of attribute property_set.
7 8 9 |
# File 'lib/property_sets/action_view_extension.rb', line 7 def property_set @property_set end |
#template ⇒ Object
Returns the value of attribute template.
8 9 10 |
# File 'lib/property_sets/action_view_extension.rb', line 8 def template @template end |
Instance Method Details
#check_box(property, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
17 18 19 20 21 22 |
# File 'lib/property_sets/action_view_extension.rb', line 17 def check_box(property, = {}, checked_value = "1", unchecked_value = "0") = (property, ) do |properties| properties.send("#{property}?") end template.check_box(object_name, property, , checked_value, unchecked_value) end |
#hidden_field(property, options = {}) ⇒ Object
35 36 37 |
# File 'lib/property_sets/action_view_extension.rb', line 35 def hidden_field(property, = {}) template.hidden_field(object_name, property, prepare_id_name(property, )) end |
#prepare_id_name(property, options) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/property_sets/action_view_extension.rb', line 45 def prepare_id_name(property, ) throw "Invalid options type #{.inspect}" unless .is_a?(Hash) .clone.tap do || instance = template.instance_variable_get("@#{object_name}") throw "No @#{object_name} in scope" if instance.nil? throw "The property_set_check_box only works on models with property set #{property_set}" unless instance.respond_to?(property_set) [:id] ||= "#{object_name}_#{property_set}_#{property}" [:name] = "#{object_name}[#{property_set}][#{property}]" [:object] = instance end end |
#prepare_options(property, options, &block) ⇒ Object
60 61 62 63 64 |
# File 'lib/property_sets/action_view_extension.rb', line 60 def (property, , &block) = prepare_id_name(property, ) [:checked] = yield([:object].send(property_set)) end |
#radio_button(property, checked_value = "1", options = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/property_sets/action_view_extension.rb', line 24 def (property, checked_value = "1", = {}) = (property, ) do |properties| properties.send("#{property}") == checked_value.to_s end template.(object_name, property, checked_value, ) end |
#select(property, choices, options = {}, html_options = {}) ⇒ Object
39 40 41 42 43 |
# File 'lib/property_sets/action_view_extension.rb', line 39 def select(property, choices, = {}, = {}) = prepare_id_name(property, ) current_value = [:object].send(property_set).send(property) template.select("#{object_name}[#{property_set}]", property, choices, { :selected => current_value }, ) end |
#text_field(property, options = {}) ⇒ Object
31 32 33 |
# File 'lib/property_sets/action_view_extension.rb', line 31 def text_field(property, = {}) template.text_field(object_name, property, prepare_id_name(property, )) end |