class ActionView::Helpers::FormBuilder::PropertySetFormBuilderProxy

Attributes

object[R]
object_name[R]
property_set[R]
template[R]

Public Class Methods

new(property_set, template, object_name, object) click to toggle source
# File lib/property_sets/action_view_extension.rb, line 9
def initialize(property_set, template, object_name, object)
  @property_set = property_set
  @template = template
  @object_name = object_name
  @object = object
end

Public Instance Methods

check_box(property, options = {}, checked_value = "1", unchecked_value = "0") click to toggle source
# File lib/property_sets/action_view_extension.rb, line 16
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 = {}) click to toggle source
# File lib/property_sets/action_view_extension.rb, line 35
def hidden_field(property, options = {})
  options = prepare_id_name(property, options)
  unless options.key?(:value)
    options[:value] = cast_boolean(options[:object].send(property_set).send(property))
  end
  template.hidden_field(object_name, property, options)
end
radio_button(property, checked_value = "1", options = {}) click to toggle source
# File lib/property_sets/action_view_extension.rb, line 23
def radio_button(property, checked_value = "1", options = {})
  options[:id] ||= "#{object_name}_#{property_set}_#{property}_#{checked_value}"
  options = prepare_options(property, options) do |properties|
    properties.send(property.to_s) == checked_value
  end
  template.radio_button(object_name, property, checked_value, options)
end
select(property, choices, options = {}, html_options = {}) click to toggle source
# File lib/property_sets/action_view_extension.rb, line 43
def select(property, choices, options = {}, html_options = {})
  options = prepare_id_name(property, options)
  current_value = options[:object].send(property_set).send(property)
  template.select("#{object_name}[#{property_set}]", property, choices, {selected: current_value}, html_options)
end
text_field(property, options = {}) click to toggle source
# File lib/property_sets/action_view_extension.rb, line 31
def text_field(property, options = {})
  template.text_field(object_name, property, prepare_id_name(property, options))
end

Private Instance Methods

cast_boolean(value) click to toggle source
# File lib/property_sets/action_view_extension.rb, line 76
def cast_boolean(value)
  case value
  when TrueClass then "1"
  when FalseClass then "0"
  else value
  end
end
fetch_target_object() click to toggle source
# File lib/property_sets/action_view_extension.rb, line 61
def fetch_target_object
  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)

  instance
end
prepare_id_name(property, options) click to toggle source
# File lib/property_sets/action_view_extension.rb, line 51
def prepare_id_name(property, options)
  throw "Invalid options type #{options.inspect}" unless options.is_a?(Hash)

  options.clone.tap do |prepared_options|
    prepared_options[:object] = object || fetch_target_object
    prepared_options[:id] ||= "#{object_name}_#{property_set}_#{property}"
    prepared_options[:name] = "#{object_name}[#{property_set}][#{property}]"
  end
end
prepare_options(property, options) { |send| ... } click to toggle source
# File lib/property_sets/action_view_extension.rb, line 70
def prepare_options(property, options, &block)
  options = prepare_id_name(property, options)
  options[:checked] = yield(options[:object].send(property_set))
  options
end