Sha256: ccdf4775bf7127ee99e7161236208484839b5c3015e6033fb325dd67910a8062

Contents?: true

Size: 1.91 KB

Versions: 17

Compression:

Stored size: 1.91 KB

Contents

module Eac
  module CommonFormHelper
    class FormBuilder
      class SearchableAssociationField
        def initialize(form_builder, field_name, options)
          @form_builder = form_builder
          @field_name = field_name
          @options = options
        end

        def hidden_input
          @form_builder.form.hidden_field(hidden_input_name, id: hidden_input_id)
        end

        def visible_input
          @form_builder.helper.text_field_tag(visible_input_name, '', id: visible_input_id,
                                                                      class: 'form-control')
        end

        def javascript_tag
          @form_builder.helper.content_tag(:script) do
            @form_builder.helper.raw("new InputSearchable(#{json_options});")
          end
        end

        private

        def hidden_input_id
          @hidden_input_id ||= SecureRandom.hex(5)
        end

        def hidden_input_name
          "#{@field_name}_id"
        end

        def visible_input_id
          @visible_input_id ||= SecureRandom.hex(5)
        end

        def visible_input_name
          "#{@field_name}_search"
        end

        def association_column
          @form_builder.model_instance && @form_builder.model_instance.send(@field_name)
        end

        def initial_id
          return association_column.id if association_column
          ''
        end

        def initial_label
          return association_column.to_s if association_column
          return params[visible_input_name] if params.key?(visible_input_name)
          ''
        end

        def params
          @form_builder.helper.params
        end

        def url
          @options[:url]
        end

        def json_options
          r = {}
          [:hidden_input_id, :visible_input_id, :initial_id, :initial_label, :url].each do |k|
            r[k] = send(k)
          end
          r.to_json
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
eac_rails_utils-0.8.0 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.7.1 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.7.0 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.6.0 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.5.0 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.4.0 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.3.0 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.2.2 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.2.1 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.2.0 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.1.15 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.1.14 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.1.13 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.1.12 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.1.11 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.1.10 lib/eac/common_form_helper/form_builder/searchable_association_field.rb
eac_rails_utils-0.0.1 lib/eac/common_form_helper/form_builder/searchable_association_field.rb