Sha256: d36070cacf82b5c571221e02b9b3f8f4b7badeb262451fe3872e8ca9611c2ef9

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 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
      ensure
        RequestStore.store[GLOBAL_NESTED_FIELD_KEY] = @parent_nested_field
      end

      def parent_nested_field(fallback_to_self: true)
        @parent_nested_field || (RequestStore.store[GLOBAL_NESTED_FIELD_KEY] if fallback_to_self)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
para-0.12.5 lib/para/inputs/nested_base_input.rb
para-0.12.4 lib/para/inputs/nested_base_input.rb
para-0.12.3 lib/para/inputs/nested_base_input.rb
para-0.12.2 lib/para/inputs/nested_base_input.rb
para-0.12.1 lib/para/inputs/nested_base_input.rb
para-0.12.0 lib/para/inputs/nested_base_input.rb
para-0.11.4 lib/para/inputs/nested_base_input.rb
para-0.11.3 lib/para/inputs/nested_base_input.rb
para-0.11.2 lib/para/inputs/nested_base_input.rb