diff --git a/app/controllers/spree/admin/distributors_controller.rb b/app/controllers/spree/admin/distributors_controller.rb index 03a5c6a967..cfbc3dd22f 100644 --- a/app/controllers/spree/admin/distributors_controller.rb +++ b/app/controllers/spree/admin/distributors_controller.rb @@ -1,16 +1,30 @@ module Spree module Admin class DistributorsController < ResourceController - before_filter :load_data, :except => [:index] + before_filter :load_distributor_set, :only => :index + before_filter :load_countries, :except => :index + + def bulk_update + @distributor_set = DistributorSet.new(params[:distributor_set]) + if @distributor_set.save + redirect_to admin_distributors_path, :notice => 'Distributor collection times updated.' + else + render :index + end + end private - def load_data - @countries = Country.order(:name) + def load_distributor_set + @distributor_set = Spree::DistributorSet.new :distributors => collection + end + + def load_countries + @countries = Country.order(:name) end def collection - super.order(:name) + super.order(:name) end end end -end \ No newline at end of file +end diff --git a/app/models/spree/distributor_set.rb b/app/models/spree/distributor_set.rb new file mode 100644 index 0000000000..4ef6ae5b27 --- /dev/null +++ b/app/models/spree/distributor_set.rb @@ -0,0 +1,36 @@ +# Tableless model to handle updating multiple distributors at once from a +# single form. Used to update next_collection_at field for all distributors in +# admin backend. +module Spree + class DistributorSet + include ActiveModel::Conversion + extend ActiveModel::Naming + + attr_accessor :distributors + + def initialize(attributes={}) + @distributors = Spree::Distributor.all + + attributes.each do |name, value| + send("#{name}=", value) + end + end + + def distributors_attributes=(attributes) + attributes.each do |k, attributes| + # attributes == {:id => 123, :next_collection_at => '...'} + d = @distributors.detect { |d| d.id.to_s == attributes[:id].to_s } + d.assign_attributes(attributes.except(:id)) + end + end + + def save + distributors.all?(&:save) + end + + def persisted? + false + end + + end +end diff --git a/app/views/spree/admin/distributors/index.html.erb b/app/views/spree/admin/distributors/index.html.erb index 4307e02b6d..0042604d1f 100644 --- a/app/views/spree/admin/distributors/index.html.erb +++ b/app/views/spree/admin/distributors/index.html.erb @@ -1,4 +1,3 @@ -
- - - - - - - - - - <% @distributors.each do |distributor| %> - - - - +<%= form_for @distributor_set, :url => bulk_update_admin_distributors_path do |f| %> +
Name - Description - -
<%= link_to distributor.name, spree.admin_distributor_path(distributor) %><%=distributor.description %> - <%= link_to_edit distributor, :class => 'edit' %>   - <%= link_to_delete distributor %> -
+ + + + + + - <% end %> - <% if @distributors.empty? %> - - <% end %> - -
NameNext Collection Date/TimeDescription
<%= t(:none) %>
\ No newline at end of file + + + <%= f.fields_for :distributors do |distributor_form| %> + <% distributor = distributor_form.object %> + + <%= link_to distributor.name, spree.admin_distributor_path(distributor) %> + <%= distributor_form.text_field :next_collection_at %> + <%= distributor.description %> + + <%= link_to_edit distributor, :class => 'edit' %>   + <%= link_to_delete distributor %> + + + <% end %> + <% if @distributors.empty? %> + <%= t(:none) %> + <% end %> + + + + <%= f.submit 'Update' %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 60cec94135..8e557032a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,7 +12,9 @@ Spree::Core::Engine.routes.prepend do end namespace :admin do - resources :distributors + resources :distributors do + post :bulk_update, :on => :collection, :as => :bulk_update + end resources :suppliers end end diff --git a/db/migrate/20120702020402_add_next_collection_at_to_distributors.rb b/db/migrate/20120702020402_add_next_collection_at_to_distributors.rb new file mode 100644 index 0000000000..1e30199de8 --- /dev/null +++ b/db/migrate/20120702020402_add_next_collection_at_to_distributors.rb @@ -0,0 +1,5 @@ +class AddNextCollectionAtToDistributors < ActiveRecord::Migration + def change + add_column :distributors, :next_collection_at, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index cbf694ab85..334400b91a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120629043648) do +ActiveRecord::Schema.define(:version => 20120702020402) do create_table "distributors", :force => true do |t| t.string "name" @@ -26,6 +26,7 @@ ActiveRecord::Schema.define(:version => 20120629043648) do t.datetime "created_at" t.datetime "updated_at" t.integer "pickup_address_id" + t.string "next_collection_at" end create_table "product_distributions", :force => true do |t| diff --git a/spec/requests/admin/distributors_spec.rb b/spec/requests/admin/distributors_spec.rb index b9fc1d06b6..beaac1a628 100644 --- a/spec/requests/admin/distributors_spec.rb +++ b/spec/requests/admin/distributors_spec.rb @@ -8,33 +8,52 @@ feature %q{ include WebHelper - context "setting up distributors" do - scenario "creating a new distributor" do - login_to_admin_section + scenario "creating a new distributor" do + login_to_admin_section - click_link 'Distributors' - click_link 'New Distributor' + click_link 'Distributors' + click_link 'New Distributor' - fill_in 'distributor_name', :with => 'Eaterprises' - fill_in 'distributor_description', :with => 'Connecting farmers and eaters' - fill_in 'distributor_contact', :with => 'Kirsten or Ren' - fill_in 'distributor_phone', :with => '0413 897 321' + fill_in 'distributor_name', :with => 'Eaterprises' + fill_in 'distributor_description', :with => 'Connecting farmers and eaters' + fill_in 'distributor_contact', :with => 'Kirsten or Ren' + fill_in 'distributor_phone', :with => '0413 897 321' - fill_in 'distributor_pickup_address_attributes_address1', :with => '35 Ballantyne St' - fill_in 'distributor_pickup_address_attributes_city', :with => 'Thornbury' - fill_in 'distributor_pickup_address_attributes_zipcode', :with => '3072' - select('Australia', :from => 'distributor_pickup_address_attributes_country_id') - select('Victoria', :from => 'distributor_pickup_address_attributes_state_id') + fill_in 'distributor_pickup_address_attributes_address1', :with => '35 Ballantyne St' + fill_in 'distributor_pickup_address_attributes_city', :with => 'Thornbury' + fill_in 'distributor_pickup_address_attributes_zipcode', :with => '3072' + select('Australia', :from => 'distributor_pickup_address_attributes_country_id') + select('Victoria', :from => 'distributor_pickup_address_attributes_state_id') - fill_in 'distributor_pickup_times', :with => 'Thursday, 22nd Feb, 6 - 9 PM. Friday, 23nd Feb, 6 - 9 PM' - fill_in 'distributor_email', :with => 'info@eaterprises.com.au' - fill_in 'distributor_url', :with => 'http://eaterprises.com.au' - fill_in 'distributor_abn', :with => '09812309823' - fill_in 'distributor_acn', :with => '' + fill_in 'distributor_pickup_times', :with => 'Thursday, 22nd Feb, 6 - 9 PM. Friday, 23nd Feb, 6 - 9 PM' + fill_in 'distributor_email', :with => 'info@eaterprises.com.au' + fill_in 'distributor_url', :with => 'http://eaterprises.com.au' + fill_in 'distributor_abn', :with => '09812309823' + fill_in 'distributor_acn', :with => '' - click_button 'Create' + click_button 'Create' - flash_message.should == 'Distributor "Eaterprises" has been successfully created!' - end + flash_message.should == 'Distributor "Eaterprises" has been successfully created!' end + + + scenario "updating many distributor next collection times at once" do + # Given three distributors + 3.times { create(:distributor) } + + # When I go to the distributors page + login_to_admin_section + click_link 'Distributors' + + # And I fill in some new collection times and save them + fill_in 'distributor_set_distributors_attributes_0_next_collection_at', :with => 'One' + fill_in 'distributor_set_distributors_attributes_1_next_collection_at', :with => 'Two' + fill_in 'distributor_set_distributors_attributes_2_next_collection_at', :with => 'Three' + click_button 'Update' + + # Then my times should have been saved + flash_message.should == 'Distributor collection times updated.' + Spree::Distributor.all.map { |d| d.next_collection_at }.should == %w(One Two Three) + end + end