Class: ActionView::Helpers::FormBuilder::PropertySetFormBuilderProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/property_sets/action_view_extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject

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_setObject

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

#templateObject

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, options = {}, checked_value = "1", unchecked_value = "0")
  options = prepare_options(property, options) do |properties|
    properties.send("#{property}?")
  end
  template.check_box(object_name, property, options, checked_value, unchecked_value)
end

#hidden_field(property, options = {}) ⇒ Object



31
32
33
# File 'lib/property_sets/action_view_extension.rb', line 31

def hidden_field(property, options = {})
  template.hidden_field(object_name, property, prepare_id_name(property, options))
end

#prepare_id_name(property, options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/property_sets/action_view_extension.rb', line 35

def prepare_id_name(property, options)
  throw "Invalid options type #{options.inspect}" unless options.is_a?(Hash)

  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)

  options[:id]     ||= "#{object_name}_#{property_set}_#{property}"
  options[:name]     = "#{object_name}[#{property_set}][#{property}]"
  options[:object]   = instance

  options
end

#prepare_options(property, options, &block) ⇒ Object



50
51
52
53
54
# File 'lib/property_sets/action_view_extension.rb', line 50

def prepare_options(property, options, &block)
  options = prepare_id_name(property, options)
  options[:checked] = yield(options[:object].send(property_set))
  options
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 radio_button(property, checked_value = "1", options = {})
  options = prepare_options(property, options) do |properties|
    properties.send("#{property}") == checked_value.to_s
  end
  template.radio_button(object_name, property, checked_value, options)
end