Sha256: 14bda902c53c51dda85d1ac666c7edbc510e3ddaad73ad532c982846d1d469e6

Contents?: true

Size: 776 Bytes

Versions: 1

Compression:

Stored size: 776 Bytes

Contents

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

1 entries across 1 versions & 1 rubygems

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