Generalise EnterpriseSet to ModelSet

This commit is contained in:
Rohan Mitchell
2012-11-15 14:29:38 +11:00
parent ec74396659
commit 03764881c8
5 changed files with 41 additions and 41 deletions

View File

@@ -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

View File

@@ -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

34
app/models/model_set.rb Normal file
View File

@@ -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

View File

@@ -19,7 +19,7 @@
</tr>
</thead>
<tbody>
<%= f.fields_for :enterprises do |enterprise_form| %>
<%= f.fields_for :collection do |enterprise_form| %>
<% enterprise = enterprise_form.object %>
<tr>
<td><%= link_to enterprise.name, main_app.admin_enterprise_path(enterprise) %></td>

View File

@@ -2,7 +2,7 @@ require "spec_helper"
feature %q{
As an administrator
I want manage enterprises
I want to manage enterprises
} do
include AuthenticationWorkflow
include WebHelper
@@ -109,9 +109,9 @@ feature %q{
click_link 'Enterprises'
# And I fill in some new collection times and save them
fill_in 'enterprise_set_enterprises_attributes_0_next_collection_at', :with => 'One'
fill_in 'enterprise_set_enterprises_attributes_1_next_collection_at', :with => 'Two'
fill_in 'enterprise_set_enterprises_attributes_2_next_collection_at', :with => 'Three'
fill_in 'model_set_collection_attributes_0_next_collection_at', :with => 'One'
fill_in 'model_set_collection_attributes_1_next_collection_at', :with => 'Two'
fill_in 'model_set_collection_attributes_2_next_collection_at', :with => 'Three'
click_button 'Update'
# Then my times should have been saved