Add admin interface for enterprises

This commit is contained in:
Rohan Mitchell
2012-10-31 14:25:00 +11:00
parent 077098e6c1
commit 1d1c27701d
11 changed files with 328 additions and 12 deletions

View File

@@ -0,0 +1,30 @@
module Admin
class EnterprisesController < ResourceController
before_filter :load_enterprise_set, :only => :index
before_filter :load_countries, :except => :index
helper 'spree/products'
def bulk_update
@enterprise_set = EnterpriseSet.new(params[:enterprise_set])
if @enterprise_set.save
redirect_to main_app.admin_enterprises_path, :notice => 'Distributor collection times updated.'
else
render :index
end
end
private
def load_enterprise_set
@enterprise_set = EnterpriseSet.new :enterprises => collection
end
def load_countries
@countries = Spree::Country.order(:name)
end
def collection
super.order(:name)
end
end
end

View File

@@ -1,30 +1,30 @@
# Tableless model to handle updating multiple distributors at once from a
# 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 DistributorSet
class EnterpriseSet
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :distributors
attr_accessor :enterprises
def initialize(attributes={})
@distributors = Distributor.all
@enterprises = Enterprise.all
attributes.each do |name, value|
send("#{name}=", value)
end
end
def distributors_attributes=(attributes)
def enterprises_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))
e = @enterprises.detect { |e| e.id.to_s == attributes[:id].to_s }
e.assign_attributes(attributes.except(:id))
end
end
def save
distributors.all?(&:save)
enterprises.all?(&:save)
end
def persisted?

View File

@@ -0,0 +1,4 @@
Deface::Override.new(:virtual_path => "spree/layouts/admin",
:name => "enterprises_admin_tabs",
:insert_bottom => "[data-hook='admin_tabs'], #admin_tabs[data-hook]",
:text => "<%= tab(:enterprises, :url => main_app.admin_enterprises_path) %>")

View File

@@ -0,0 +1,54 @@
- content_for :head do
= render 'shared/cms_elrte_head'
%table{"data-hook" => "distributors"}
%tr{"data-hook" => "name"}
%td Name:
%td= f.text_field :name
%tr{"data-hook" => "description"}
%td Description:
%td= f.text_field :description
%tr{'data-hook' => "long_description"}
%td Extended Description:
%td= f.text_area :long_description, :class => 'rich_text'
%tr{'data-hook' => "is_primary_producer"}
%td Primary Producer?
%td= f.check_box :is_primary_producer
%tr{'data-hook' => "is_distributor"}
%td Distributor?
%td= f.check_box :is_distributor
%tr{"data-hook" => "contact"}
%td Contact Person:
%td= f.text_field :contact
%tr{"data-hook" => "phone"}
%td Phone:
%td= f.text_field :phone
%tr{"data-hook" => "email"}
%td Email:
%td= f.text_field :email
%tr{"data-hook" => "website"}
%td Website:
%td= f.text_field :website
%tr{"data-hook" => "twitter"}
%td Twitter:
%td= f.text_field :twitter
%tr{"data-hook" => "abn"}
%td ABN:
%td= f.text_field :abn
%tr{"data-hook" => "acn"}
%td ACN:
%td= f.text_field :acn
%fieldset
%legend Address
%table
= f.fields_for :address do |address_form|
= render 'spree/admin/shared/address_form', :f => address_form
%fieldset
%legend Pickup details
%table{"data-hook" => "distributors_pickup_details"}
%tr{"data-hook" => "next_collection_at"}
%td Next collection date/time:
%td= f.text_field :next_collection_at
%tr{"data-hook" => "pickup_times"}
%td Regular pickup times:
%td= f.text_field :pickup_times

View File

@@ -0,0 +1,6 @@
<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @enterprise } %>
<%= form_for [main_app, :admin, @enterprise] do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= render :partial => 'spree/admin/shared/edit_resource_links' %>
<% end %>

View File

@@ -0,0 +1,40 @@
<div class="toolbar" data-hook="toolbar">
<ul class="actions">
<li>
<%= button_link_to "New Enterprise", main_app.new_admin_enterprise_path, :icon => 'add', :id => 'admin_new_enterprise_link' %>
</li>
</ul>
<br class="clear" />
</div>
<%= form_for @enterprise_set, :url => main_app.bulk_update_admin_enterprises_path do |f| %>
<table class="index" id="listing_enterprises">
<thead>
<tr data-hook="enterprises_header">
<th>Name</th>
<th>Next Collection Date/Time</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody>
<%= f.fields_for :enterprises do |enterprise_form| %>
<% enterprise = enterprise_form.object %>
<tr>
<td><%= link_to enterprise.name, main_app.admin_enterprise_path(enterprise) %></td>
<td><%= enterprise_form.text_field :next_collection_at %></td>
<td><%= enterprise.description %></td>
<td data-hook="admin_users_index_row_actions">
<%= link_to_edit enterprise, :class => 'edit' %> &nbsp;
<%= link_to_delete enterprise %>
</td>
</tr>
<% end %>
<% if @enterprises.empty? %>
<tr><td colspan="4"><%= t(:none) %></td></tr>
<% end %>
</tbody>
</table>
<%= f.submit 'Update' %>
<% end %>

View File

@@ -0,0 +1,6 @@
<%= render :partial => 'spree/shared/error_messages', :locals => { :target => @enterprise } %>
<%= form_for [main_app, :admin, @enterprise] do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= render :partial => 'spree/admin/shared/new_resource_links' %>
<% end %>

View File

@@ -0,0 +1,51 @@
%h1 Enterprise
%table
%tr
%th Name:
%td= @enterprise.name
%tr
%th Description:
%td= @enterprise.description
%tr
%th Extended Description:
%td= @enterprise.long_description.andand.html_safe
%tr
%th Primary producer?
%td= @enterprise.is_primary_producer ? 'Yes' : 'No'
%tr
%th Distributor?
%td= @enterprise.is_distributor ? 'Yes' : 'No'
%tr
%th Contact person:
%td= @enterprise.contact
%tr
%th Phone number:
%td= @enterprise.phone
%tr
%th Email:
%td= @enterprise.email
%tr
%th Website:
%td= @enterprise.website
%tr
%th Twitter:
%td= @enterprise.twitter
%tr
%th ABN:
%td= @enterprise.abn
%tr
%th ACN:
%td= @enterprise.acn
%tr
%th Address:
%td= render 'spree/shared/address', :address => @enterprise.address
%tr
%th Regular pickup times:
%td= @enterprise.pickup_times
%tr
%th Next collection date/time:
%td= @enterprise.next_collection_at
%p
= link_to :Edit, main_app.edit_admin_enterprise_path(@enterprise), :class => 'edit_enterprise'
= t(:or)
= link_to t(:back), main_app.admin_enterprises_path

View File

@@ -8,6 +8,9 @@ Openfoodweb::Application.routes.draw do
end
namespace :admin do
resources :enterprises do
post :bulk_update, :on => :collection, :as => :bulk_update
end
resources :distributors do
post :bulk_update, :on => :collection, :as => :bulk_update
end

View File

@@ -282,8 +282,8 @@ ActiveRecord::Schema.define(:version => 20121028070200) do
t.string "start_year"
t.string "issue_number"
t.integer "address_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "gateway_customer_profile_id"
t.string "gateway_payment_profile_id"
end
@@ -799,10 +799,10 @@ ActiveRecord::Schema.define(:version => 20121028070200) do
t.string "email"
t.string "twitter"
t.string "website"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "address_id"
t.text "long_description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end

View File

@@ -0,0 +1,122 @@
require "spec_helper"
feature %q{
As an administration
I want manage enterprises
} do
include AuthenticationWorkflow
include WebHelper
scenario "listing enterprises" do
e = create(:enterprise)
login_to_admin_section
click_link 'Enterprises'
page.should have_content e.name
end
scenario "viewing an enterprise" do
e = create(:enterprise)
login_to_admin_section
click_link 'Enterprises'
click_link e.name
page.should have_content e.name
end
scenario "creating a new enterprise" do
login_to_admin_section
click_link 'Enterprises'
click_link 'New Enterprise'
fill_in 'enterprise_name', :with => 'Eaterprises'
fill_in 'enterprise_description', :with => 'Connecting farmers and eaters'
fill_in 'enterprise_long_description', :with => 'Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro.'
uncheck 'enterprise_is_primary_producer'
check 'enterprise_is_distributor'
fill_in 'enterprise_contact', :with => 'Kirsten or Ren'
fill_in 'enterprise_phone', :with => '0413 897 321'
fill_in 'enterprise_email', :with => 'info@eaterprises.com.au'
fill_in 'enterprise_website', :with => 'http://eaterprises.com.au'
fill_in 'enterprise_twitter', :with => '@eaterprises'
fill_in 'enterprise_abn', :with => '09812309823'
fill_in 'enterprise_acn', :with => ''
fill_in 'enterprise_address_attributes_address1', :with => '35 Ballantyne St'
fill_in 'enterprise_address_attributes_city', :with => 'Thornbury'
fill_in 'enterprise_address_attributes_zipcode', :with => '3072'
select('Australia', :from => 'enterprise_address_attributes_country_id')
select('Victoria', :from => 'enterprise_address_attributes_state_id')
fill_in 'enterprise_pickup_times', :with => 'Thursday, 22nd Feb, 6 - 9 PM. Friday, 23nd Feb, 6 - 9 PM'
fill_in 'enterprise_next_collection_at', :with => 'Thursday, 22nd Feb, 6 - 9 PM'
click_button 'Create'
flash_message.should == 'Enterprise "Eaterprises" has been successfully created!'
end
scenario "editing an existing enterprise" do
@enterprise = create(:enterprise)
login_to_admin_section
click_link 'Enterprises'
click_link 'Edit'
fill_in 'enterprise_name', :with => 'Eaterprises'
fill_in 'enterprise_description', :with => 'Connecting farmers and eaters'
fill_in 'enterprise_long_description', :with => 'Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro.'
uncheck 'enterprise_is_primary_producer'
check 'enterprise_is_distributor'
fill_in 'enterprise_contact', :with => 'Kirsten or Ren'
fill_in 'enterprise_phone', :with => '0413 897 321'
fill_in 'enterprise_email', :with => 'info@eaterprises.com.au'
fill_in 'enterprise_website', :with => 'http://eaterprises.com.au'
fill_in 'enterprise_twitter', :with => '@eaterprises'
fill_in 'enterprise_abn', :with => '09812309823'
fill_in 'enterprise_acn', :with => ''
fill_in 'enterprise_address_attributes_address1', :with => '35 Ballantyne St'
fill_in 'enterprise_address_attributes_city', :with => 'Thornbury'
fill_in 'enterprise_address_attributes_zipcode', :with => '3072'
select('Australia', :from => 'enterprise_address_attributes_country_id')
select('Victoria', :from => 'enterprise_address_attributes_state_id')
fill_in 'enterprise_pickup_times', :with => 'Thursday, 22nd Feb, 6 - 9 PM. Friday, 23nd Feb, 6 - 9 PM'
fill_in 'enterprise_next_collection_at', :with => 'Thursday, 22nd Feb, 6 - 9 PM'
click_button 'Update'
flash_message.should == 'Enterprise "Eaterprises" has been successfully updated!'
end
scenario "updating many distributor next collection times at once" do
# Given three distributors
3.times { create(:distributor_enterprise) }
# When I go to the enterprises page
login_to_admin_section
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'
click_button 'Update'
# Then my times should have been saved
flash_message.should == 'Distributor collection times updated.'
Enterprise.is_distributor.map { |d| d.next_collection_at }.should == %w(One Two Three)
end
end