Sha256: 919f7656ec72443a864fded97fd7bb1cfc2528203d0c2d34f89359934f5f35e7

Contents?: true

Size: 1.31 KB

Versions: 62

Compression:

Stored size: 1.31 KB

Contents

module ExpressTemplates
  module Components
    module Forms
      # Provides a form Select component based on the Rails *collection_select* helper.
      class SelectCollection < Select

        has_option :multiple, "Allow multiple selections.", default: true

        contains -> {
          label_tag(label_name, label_text)

          # need this because the collection_select helper does not provide
          # the hidden_field_tag trick (see rails api docs for select)
          hidden_field_tag(multi_field_name, '')
          collection_select(*collection_select_tag_args)
        }

        def collection_select_tag_args
          [ resource_name,
            multi_field_name,
            related_collection, :id, :name,
            field_options,
            html_options ]
        end

        def field_options
          {include_blank: !!input_attributes.delete(:include_blank)}
        end

        def html_options
          input_attributes.reject {|k,v| k.eql?(:include_blank)}.merge(multiple: config[:multiple])
        end

        def multi_field_name
          if has_many_through_association
            "#{field_name.singularize}_ids"
          else
            raise "Only use select_collection for has_many :through.  #{field_name} is not has_many :through"
          end
        end

      end
    end
  end
end

Version data entries

62 entries across 62 versions & 2 rubygems

Version Path
express_templates-0.11.20 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.20.rc1 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.19 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.18 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.17 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.16 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.16.rc1 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.15 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.14 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.13 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.11 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.10 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.9 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.8 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.7 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.6 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.5 lib/express_templates/components/forms/select_collection.rb
express_templates-0.11.4 lib/express_templates/components/forms/select_collection.rb
express_admin-1.7.5 vendor/gems/express_templates/lib/express_templates/components/forms/select_collection.rb
express_admin-1.7.4 vendor/gems/express_templates/express_templates/lib/express_templates/components/forms/select_collection.rb