mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Generalise EnterpriseSet to ModelSet
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
34
app/models/model_set.rb
Normal 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
|
||||
@@ -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>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user