diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 08f6e2133f..d0ad4bfac6 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -6,7 +6,7 @@ module Admin helper 'spree/products' def bulk_update - @enterprise_set = EnterpriseSet.new(params[:enterprise_set]) + @enterprise_set = ModelSet.new(Enterprise.all, params[:model_set]) if @enterprise_set.save redirect_to main_app.admin_enterprises_path, :notice => 'Distributor collection times updated.' else @@ -16,7 +16,7 @@ module Admin private def load_enterprise_set - @enterprise_set = EnterpriseSet.new :enterprises => collection + @enterprise_set = ModelSet.new Enterprise.all, :collection => collection end def load_countries diff --git a/app/models/enterprise_set.rb b/app/models/enterprise_set.rb deleted file mode 100644 index b741167dfe..0000000000 --- a/app/models/enterprise_set.rb +++ /dev/null @@ -1,34 +0,0 @@ -# Tableless model to handle updating multiple enterprises at once from a -# single form. Used to update next_collection_at field for all distributors in -# admin backend. -class EnterpriseSet - include ActiveModel::Conversion - extend ActiveModel::Naming - - attr_accessor :enterprises - - def initialize(attributes={}) - @enterprises = Enterprise.all - - attributes.each do |name, value| - send("#{name}=", value) - end - end - - def enterprises_attributes=(attributes) - attributes.each do |k, attributes| - # attributes == {:id => 123, :next_collection_at => '...'} - e = @enterprises.detect { |e| e.id.to_s == attributes[:id].to_s } - e.assign_attributes(attributes.except(:id)) - end - end - - def save - enterprises.all?(&:save) - end - - def persisted? - false - end - -end diff --git a/app/models/model_set.rb b/app/models/model_set.rb new file mode 100644 index 0000000000..e806e44014 --- /dev/null +++ b/app/models/model_set.rb @@ -0,0 +1,34 @@ +# 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 diff --git a/app/views/admin/enterprises/index.html.erb b/app/views/admin/enterprises/index.html.erb index 75dad0f85f..c1e0869b86 100644 --- a/app/views/admin/enterprises/index.html.erb +++ b/app/views/admin/enterprises/index.html.erb @@ -19,7 +19,7 @@
- <%= f.fields_for :enterprises do |enterprise_form| %> + <%= f.fields_for :collection do |enterprise_form| %> <% enterprise = enterprise_form.object %>