Sha256: d6706ac5868ec6a5c50211940d2654aa7bb9bfcd58482b6a1a2ced32d98a329e

Contents?: true

Size: 1.45 KB

Versions: 25

Compression:

Stored size: 1.45 KB

Contents

module Para
  module Inputs
    class NestedBaseInput < SimpleForm::Inputs::Base
      GLOBAL_NESTED_FIELD_KEY = "para.nested_field.parent"

      private

      def dom_identifier
        @dom_identifier ||= begin
          name = attribute_name
          id = @builder.object.id || "_new_#{ parent_nested_field&.attribute_name }_"
          time = (Time.now.to_f * 1000).to_i
          random = (rand * 1000).to_i
          [name, id, time, random].join('-')
        end
      end

      def subclass
        @subclass ||= options.fetch(:subclass, subclasses.presence)
      end

      def subclasses
        options.fetch(:subclasses, [])
      end
      
      def add_button_label
        options.fetch(:add_button_label) { I18n.t('para.form.nested.add') }
      end

      def add_button_class
        options.fetch(:add_button_class) { 'btn-primary' }
      end

      # This allows to access the parent nested field from a child nested field
      # and fetch some of its data. This is useful for deeply nested cocoon
      # fields.
      #
      def with_global_nested_field(&block)
        @parent_nested_field = RequestStore.store[GLOBAL_NESTED_FIELD_KEY]
        RequestStore.store[GLOBAL_NESTED_FIELD_KEY] = self

        block.call.tap do
          RequestStore.store[GLOBAL_NESTED_FIELD_KEY] = @parent_nested_field
        end
      end

      def parent_nested_field
        @parent_nested_field || RequestStore.store[GLOBAL_NESTED_FIELD_KEY]
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
para-0.11.1 lib/para/inputs/nested_base_input.rb
para-0.11.0 lib/para/inputs/nested_base_input.rb
para-0.10.0 lib/para/inputs/nested_base_input.rb
para-0.9.4 lib/para/inputs/nested_base_input.rb
para-0.9.3.3 lib/para/inputs/nested_base_input.rb
para-0.9.3.2 lib/para/inputs/nested_base_input.rb
para-0.9.3.1 lib/para/inputs/nested_base_input.rb
para-0.9.2 lib/para/inputs/nested_base_input.rb
para-0.9.0 lib/para/inputs/nested_base_input.rb
para-0.8.15 lib/para/inputs/nested_base_input.rb
para-0.8.14 lib/para/inputs/nested_base_input.rb
para-0.8.13 lib/para/inputs/nested_base_input.rb
para-0.8.12 lib/para/inputs/nested_base_input.rb
para-0.8.11 lib/para/inputs/nested_base_input.rb
para-0.8.10 lib/para/inputs/nested_base_input.rb
para-0.8.9 lib/para/inputs/nested_base_input.rb
para-0.8.8 lib/para/inputs/nested_base_input.rb
para-0.8.7 lib/para/inputs/nested_base_input.rb
para-0.8.5 lib/para/inputs/nested_base_input.rb
para-0.8.3.3 lib/para/inputs/nested_base_input.rb