Sha256: edd5c1dd95109e0bc34bee0c0727e6aee74b0ac582e5675cceea6abed0bd207e

Contents?: true

Size: 666 Bytes

Versions: 1

Compression:

Stored size: 666 Bytes

Contents

module Para
  module AttributeField
    class BelongsToField < RelationField
      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

1 entries across 1 versions & 1 rubygems

Version Path
para-0.4.0 lib/para/attribute_field/belongs_to.rb