Sha256: 94893b97f825480b9c1629b57cdf3e5bb3abf19875c89d88fe5b62a348fbe499

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'action_view'
require 'delegate'

module ActionView
  module Helpers
    # property_set_check_box(:account, :property_association, :property_key, options)
    def property_set_check_box(model_name, property_set, property, options = {}, checked_value = "1", unchecked_value = "0")
      the_model = @template.instance_variable_get("@#{model_name}")

      throw "No @#{model_name} in scope" if the_model.nil?
      throw "The property_set_check_box only works on models with property set #{property_set}" unless the_model.respond_to?(property_set)

      options[:checked] = the_model.send(property_set).send("#{property}?")
      options[:id]    ||= "#{model_name}_#{property_set}_#{property}"
      options[:name]    = "#{model_name}[#{property_set}][#{property}]"
      @template.check_box(model_name, "#{property_set}_#{property}", options, checked_value, unchecked_value)
    end

    class FormBuilder
      class PropertySetFormBuilderProxy < Delegator
        attr_accessor :builder
        attr_accessor :property_set

        def initialize(property_set, builder)
          self.property_set = property_set
          self.builder      = builder
        end

        def __getobj__
          builder
        end

        def check_box(property, options = {}, checked_value = "1", unchecked_value = "0")
          builder.property_set_check_box(property_set, property, options, checked_value, unchecked_value)
        end
      end

      def property_set(identifier)
        PropertySetFormBuilderProxy.new(identifier, self)
      end

      def property_set_check_box(property_set, property, options, checked_value, unchecked_value)
        @template.property_set_check_box(@object_name, property_set, property, objectify_options(options), checked_value, unchecked_value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
property_sets-0.2.0 lib/property_sets/action_view_extension.rb