Sha256: 79175e08a9fe5effb091b025e81f17da94c359be0fd8282e3cc8aaec0ae06a54

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

module RailsCoreExtensions
  module HasManyExtensions
    module Tags
      def hm_check_box(object_name, method, options = {})
        object = options.delete(:object)
        parent = options.delete(:parent) || instance_variable_get(object_name)
        objects = options.delete(:objects) || parent.send(method)
        check_box_tag("#{object_name}[#{method}][]", object, objects.include?(object), options.merge(:id => "#{object_name}_#{method}"))
      end

      def hm_empty_array(object_name, method)
        hidden_field_tag "#{object_name}[#{method}][]"
      end
    end

    module FormBuilder
      def hm_empty_array(method)
        @habtm_fields ||= {}
        @habtm_fields[method] = @object.send(method)
        @template.hm_empty_array(@object_name, method)
      end

      def hm_check_box(method, object, options = {})
        empty = (hm_empty_array(method) unless @habtm_fields && @habtm_fields[method])
        (empty || '').html_safe + @template.hm_check_box(@object_name, method, options.merge(:object => object, :parent => @object, :objects => @habtm_fields[method]))
      end
    end
  end
end

ActiveSupport.on_load :action_view do
  ActionView::Base.send(:include, RailsCoreExtensions::HasManyExtensions::Tags)
  ActionView::Helpers::FormBuilder.send(:include, RailsCoreExtensions::HasManyExtensions::FormBuilder)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_core_extensions-0.15.0 lib/rails_core_extensions/action_view_has_many_extensions.rb
rails_core_extensions-0.14.0 lib/rails_core_extensions/action_view_has_many_extensions.rb
rails_core_extensions-0.13.2 lib/rails_core_extensions/action_view_has_many_extensions.rb
rails_core_extensions-0.13.1 lib/rails_core_extensions/action_view_has_many_extensions.rb
rails_core_extensions-0.13.0 lib/rails_core_extensions/action_view_has_many_extensions.rb