Sha256: 3907c575913806263a77e028fd0d625f293e7416b614fcce4624d683c768623e

Contents?: true

Size: 700 Bytes

Versions: 9

Compression:

Stored size: 700 Bytes

Contents

module Para
  module AttributeField
    class BelongsToField < RelationField
      register :belongs_to, self

      field_option :collection, :relation_options

      def field_name
        reflection.name
      end

      def value_for(instance)
        if (resource = instance.send(name))
          resource_name(resource)
        end
      end

      def relation_options
        reflection.klass.all
      end

      def parse_input(params)
        if (id = params[reflection.foreign_key].presence) && !reflection.klass.exists?(id: id)
          on_the_fly_creation(id) do |resource|
            params[reflection.foreign_key] = resource.id
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
para-0.6.9 lib/para/attribute_field/belongs_to.rb
para-0.6.8 lib/para/attribute_field/belongs_to.rb
para-0.6.7 lib/para/attribute_field/belongs_to.rb
para-0.6.3 lib/para/attribute_field/belongs_to.rb
para-0.6.2 lib/para/attribute_field/belongs_to.rb
para-0.5.4 lib/para/attribute_field/belongs_to.rb
para-0.5.3 lib/para/attribute_field/belongs_to.rb
para-0.5.1 lib/para/attribute_field/belongs_to.rb
para-0.5.0 lib/para/attribute_field/belongs_to.rb