Sha256: 1cd19f86d9c4cf994c45de68d126391b109518d2d9511455cc92d0ea5c272801

Contents?: true

Size: 808 Bytes

Versions: 9

Compression:

Stored size: 808 Bytes

Contents

module Para
  module AttributeField
    class HasManyField < RelationField
      register :has_many, self

      def field_name
        reflection.name
      end

      def value_for(instance)
        instance.send(name).map do |resource|
          resource_name(resource)
        end.join(', ')
      end

      def parse_input(params)
        if (ids = params[plural_foreign_key].presence) && String === ids
          # Format selectize value for Rails
          ids = params[plural_foreign_key] = ids.split(',')

          on_the_fly_creation(ids) do |resource, value|
            params[plural_foreign_key].delete(value)
            params[plural_foreign_key] << resource.id
          end
        end
      end

      def plural_foreign_key
        foreign_key.to_s.pluralize
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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