Sha256: 41671390a16362c8f90757be2716930e2df0d1eb0e569daecac633fd774eb92e

Contents?: true

Size: 759 Bytes

Versions: 47

Compression:

Stored size: 759 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, resource)
        if reflection &&
           (id = params[reflection.foreign_key].presence) &&
           !reflection.klass.exists?(id: id)
        then
          on_the_fly_creation(id) do |resource|
            params[reflection.foreign_key] = resource.id
          end
        end
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
para-0.12.4 lib/para/attribute_field/belongs_to.rb
para-0.12.3 lib/para/attribute_field/belongs_to.rb
para-0.12.2 lib/para/attribute_field/belongs_to.rb
para-0.12.1 lib/para/attribute_field/belongs_to.rb
para-0.12.0 lib/para/attribute_field/belongs_to.rb
para-0.11.4 lib/para/attribute_field/belongs_to.rb
para-0.11.3 lib/para/attribute_field/belongs_to.rb
para-0.11.2 lib/para/attribute_field/belongs_to.rb
para-0.11.1 lib/para/attribute_field/belongs_to.rb
para-0.11.0 lib/para/attribute_field/belongs_to.rb
para-0.10.0 lib/para/attribute_field/belongs_to.rb
para-0.9.4 lib/para/attribute_field/belongs_to.rb
para-0.9.3.3 lib/para/attribute_field/belongs_to.rb
para-0.9.3.2 lib/para/attribute_field/belongs_to.rb
para-0.9.3.1 lib/para/attribute_field/belongs_to.rb
para-0.9.2 lib/para/attribute_field/belongs_to.rb
para-0.9.0 lib/para/attribute_field/belongs_to.rb
para-0.8.15 lib/para/attribute_field/belongs_to.rb
para-0.8.14 lib/para/attribute_field/belongs_to.rb
para-0.8.13 lib/para/attribute_field/belongs_to.rb