From 1d1c27701dd5d41a2d895be3d034c48699a35c91 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 31 Oct 2012 14:25:00 +1100 Subject: [PATCH] Add admin interface for enterprises --- .../admin/enterprises_controller.rb | 30 +++++ .../{distributor_set.rb => enterprise_set.rb} | 16 +-- app/overrides/enterprises_admin_tab.rb | 4 + app/views/admin/enterprises/_form.html.haml | 54 ++++++++ app/views/admin/enterprises/edit.html.erb | 6 + app/views/admin/enterprises/index.html.erb | 40 ++++++ app/views/admin/enterprises/new.html.erb | 6 + app/views/admin/enterprises/show.html.haml | 51 ++++++++ config/routes.rb | 3 + db/schema.rb | 8 +- spec/requests/admin/enterprises_spec.rb | 122 ++++++++++++++++++ 11 files changed, 328 insertions(+), 12 deletions(-) create mode 100644 app/controllers/admin/enterprises_controller.rb rename app/models/{distributor_set.rb => enterprise_set.rb} (55%) create mode 100644 app/overrides/enterprises_admin_tab.rb create mode 100644 app/views/admin/enterprises/_form.html.haml create mode 100644 app/views/admin/enterprises/edit.html.erb create mode 100644 app/views/admin/enterprises/index.html.erb create mode 100644 app/views/admin/enterprises/new.html.erb create mode 100644 app/views/admin/enterprises/show.html.haml create mode 100644 spec/requests/admin/enterprises_spec.rb diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb new file mode 100644 index 0000000000..bc9b39ef35 --- /dev/null +++ b/app/controllers/admin/enterprises_controller.rb @@ -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 diff --git a/app/models/distributor_set.rb b/app/models/enterprise_set.rb similarity index 55% rename from app/models/distributor_set.rb rename to app/models/enterprise_set.rb index 04fbd02db4..b741167dfe 100644 --- a/app/models/distributor_set.rb +++ b/app/models/enterprise_set.rb @@ -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? diff --git a/app/overrides/enterprises_admin_tab.rb b/app/overrides/enterprises_admin_tab.rb new file mode 100644 index 0000000000..254f9d7d25 --- /dev/null +++ b/app/overrides/enterprises_admin_tab.rb @@ -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) %>") diff --git a/app/views/admin/enterprises/_form.html.haml b/app/views/admin/enterprises/_form.html.haml new file mode 100644 index 0000000000..1cd6efa5e0 --- /dev/null +++ b/app/views/admin/enterprises/_form.html.haml @@ -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 diff --git a/app/views/admin/enterprises/edit.html.erb b/app/views/admin/enterprises/edit.html.erb new file mode 100644 index 0000000000..f4302f4ef9 --- /dev/null +++ b/app/views/admin/enterprises/edit.html.erb @@ -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 %> diff --git a/app/views/admin/enterprises/index.html.erb b/app/views/admin/enterprises/index.html.erb new file mode 100644 index 0000000000..d31be7b2b8 --- /dev/null +++ b/app/views/admin/enterprises/index.html.erb @@ -0,0 +1,40 @@ +
+ +
+
+ +<%= form_for @enterprise_set, :url => main_app.bulk_update_admin_enterprises_path do |f| %> + + + + + + + + + + + <%= f.fields_for :enterprises do |enterprise_form| %> + <% enterprise = enterprise_form.object %> + + + + + + + <% end %> + <% if @enterprises.empty? %> + + <% end %> + +
NameNext Collection Date/TimeDescription
<%= link_to enterprise.name, main_app.admin_enterprise_path(enterprise) %><%= enterprise_form.text_field :next_collection_at %><%= enterprise.description %> + <%= link_to_edit enterprise, :class => 'edit' %>   + <%= link_to_delete enterprise %> +
<%= t(:none) %>
+ + <%= f.submit 'Update' %> +<% end %> diff --git a/app/views/admin/enterprises/new.html.erb b/app/views/admin/enterprises/new.html.erb new file mode 100644 index 0000000000..92682f64d8 --- /dev/null +++ b/app/views/admin/enterprises/new.html.erb @@ -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 %> diff --git a/app/views/admin/enterprises/show.html.haml b/app/views/admin/enterprises/show.html.haml new file mode 100644 index 0000000000..a2eb9027fa --- /dev/null +++ b/app/views/admin/enterprises/show.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 3d5a29e0bb..7fb03b6950 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 7863a818d1..6267fefdd9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/requests/admin/enterprises_spec.rb b/spec/requests/admin/enterprises_spec.rb new file mode 100644 index 0000000000..19edb6d772 --- /dev/null +++ b/spec/requests/admin/enterprises_spec.rb @@ -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