Add distributor next_collection_at field, bulk edit in admin backend

This commit is contained in:
Rohan Mitchell
2012-07-02 14:23:13 +10:00
parent cb56e9f658
commit 718e54f933
7 changed files with 136 additions and 56 deletions

View File

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

View File

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

View File

@@ -1,4 +1,3 @@
<div class="toolbar" data-hook="toolbar">
<ul class="actions">
<li>
@@ -8,30 +7,34 @@
<br class="clear" />
</div>
<table class="index" id='listing_distributors'>
<thead>
<tr data-hook="distributors_header">
<th>Name</th>
<th>
Description
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<% @distributors.each do |distributor| %>
<tr >
<td><%= link_to distributor.name, spree.admin_distributor_path(distributor) %></td>
<td><%=distributor.description %></td>
<td data-hook="admin_users_index_row_actions">
<%= link_to_edit distributor, :class => 'edit' %> &nbsp;
<%= link_to_delete distributor %>
</td>
<%= form_for @distributor_set, :url => bulk_update_admin_distributors_path do |f| %>
<table class="index" id="listing_distributors">
<thead>
<tr data-hook="distributors_header">
<th>Name</th>
<th>Next Collection Date/Time</th>
<th>Description</th>
<th></th>
</tr>
<% end %>
<% if @distributors.empty? %>
<tr><td colspan="2"><%= t(:none) %></td></tr>
<% end %>
</tbody>
</table>
</thead>
<tbody>
<%= f.fields_for :distributors do |distributor_form| %>
<% distributor = distributor_form.object %>
<tr>
<td><%= link_to distributor.name, spree.admin_distributor_path(distributor) %></td>
<td><%= distributor_form.text_field :next_collection_at %></td>
<td><%= distributor.description %></td>
<td data-hook="admin_users_index_row_actions">
<%= link_to_edit distributor, :class => 'edit' %> &nbsp;
<%= link_to_delete distributor %>
</td>
</tr>
<% end %>
<% if @distributors.empty? %>
<tr><td colspan="4"><%= t(:none) %></td></tr>
<% end %>
</tbody>
</table>
<%= f.submit 'Update' %>
<% end %>

View File

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

View File

@@ -0,0 +1,5 @@
class AddNextCollectionAtToDistributors < ActiveRecord::Migration
def change
add_column :distributors, :next_collection_at, :string
end
end

View File

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

View File

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