Sha256: 87d2131591f0133cd4983dfd378893821ae40abbeba6b91fb6bf40df126a6572

Contents?: true

Size: 1.57 KB

Versions: 24

Compression:

Stored size: 1.57 KB

Contents

module Para
  module Inputs
    class NestedOneInput < NestedBaseInput
      attr_reader :model
      
      def input(wrapper_options = nil)
        input_html_options[:class] << "nested-one"

        parent_model = object.class
        association = object.association(attribute_name)
        relation = parent_model.reflect_on_association(attribute_name)

        resource = object.send(attribute_name)
        @model = (resource && resource.class) || relation.klass

        unless resource
          # Build association without trying to save the new record
          resource = case association
          when ActiveRecord::Associations::HasOneThroughAssociation
            association.replace(model.new)
          when ActiveRecord::Associations::HasOneAssociation
            association.replace(model.new, false)
          else
            association.replace(model.new)
          end
        end

        locals = options.fetch(:locals, {})

        template.render(
          partial: 'para/inputs/nested_one',
          locals: {
            form: @builder,
            model: model,
            resource: resource,
            attribute_name: attribute_name,
            nested_locals: locals,
            collapsible: collapsible || subclass,
            dom_identifier: dom_identifier,
            subclass: subclass,
            subclasses: subclasses,
            add_button_label: add_button_label,
            add_button_class: add_button_class
          }
        )
      end

      private

      def collapsible
        options.fetch(:collapsible, false)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
para-0.9.4 lib/para/inputs/nested_one_input.rb
para-0.9.3.3 lib/para/inputs/nested_one_input.rb
para-0.9.3.2 lib/para/inputs/nested_one_input.rb
para-0.9.3.1 lib/para/inputs/nested_one_input.rb
para-0.9.2 lib/para/inputs/nested_one_input.rb
para-0.9.0 lib/para/inputs/nested_one_input.rb
para-0.8.15 lib/para/inputs/nested_one_input.rb
para-0.8.14 lib/para/inputs/nested_one_input.rb
para-0.8.13 lib/para/inputs/nested_one_input.rb
para-0.8.12 lib/para/inputs/nested_one_input.rb
para-0.8.11 lib/para/inputs/nested_one_input.rb
para-0.8.10 lib/para/inputs/nested_one_input.rb
para-0.8.9 lib/para/inputs/nested_one_input.rb
para-0.8.8 lib/para/inputs/nested_one_input.rb
para-0.8.7 lib/para/inputs/nested_one_input.rb
para-0.8.5 lib/para/inputs/nested_one_input.rb
para-0.8.3.3 lib/para/inputs/nested_one_input.rb
para-0.8.3.2 lib/para/inputs/nested_one_input.rb
para-0.8.3.1 lib/para/inputs/nested_one_input.rb
para-0.8.3 lib/para/inputs/nested_one_input.rb