Files
openfoodnetwork/app/models/model_set.rb
2012-11-15 14:29:38 +11:00

35 lines
831 B
Ruby

# Tableless model to handle updating multiple models at once from a
# single form. For example, it is used to update the enterprise next_collection_at
# field for all distributors in the admin backend.
class ModelSet
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :collection
def initialize(collection, attributes={})
@collection = collection
attributes.each do |name, value|
send("#{name}=", value)
end
end
def collection_attributes=(attributes)
attributes.each do |k, attributes|
# attributes == {:id => 123, :next_collection_at => '...'}
e = @collection.detect { |e| e.id.to_s == attributes[:id].to_s }
e.assign_attributes(attributes.except(:id))
end
end
def save
collection.all?(&:save)
end
def persisted?
false
end
end