From 84cd56cfdce1859ec9e0e746d9f04a396c023553 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 18 Jun 2014 12:20:01 +1000 Subject: [PATCH] Admin can list and create producer properties --- .../admin/enterprises_controller.rb | 9 +++++ .../admin/producer_properties_controller.rb | 20 ++++++++++ app/models/enterprise.rb | 2 +- app/models/producer_property.rb | 11 ++++++ app/views/admin/enterprises/index.html.haml | 6 ++- .../_producer_property_fields.html.haml | 11 ++++++ .../admin/producer_properties/index.html.haml | 39 +++++++++++++++++++ config/routes.rb | 4 ++ spec/features/admin/enterprises_spec.rb | 28 ++++++++++++- 9 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/producer_properties_controller.rb create mode 100644 app/views/admin/producer_properties/_producer_property_fields.html.haml create mode 100644 app/views/admin/producer_properties/index.html.haml diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 3171f4c5bc..a184cf99d6 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -7,6 +7,15 @@ module Admin helper 'spree/products' + respond_override update: { html: { success: lambda { + if params[:enterprise].key? :producer_properties_attributes + redirect_to main_app.admin_enterprise_producer_properties_path(@enterprise) + else + redirect_to main_app.admin_enterprises_path + end + } } } + + def bulk_update @enterprise_set = EnterpriseSet.new(params[:enterprise_set]) if @enterprise_set.save diff --git a/app/controllers/admin/producer_properties_controller.rb b/app/controllers/admin/producer_properties_controller.rb new file mode 100644 index 0000000000..bc12141b52 --- /dev/null +++ b/app/controllers/admin/producer_properties_controller.rb @@ -0,0 +1,20 @@ +module Admin + class ProducerPropertiesController < ResourceController + before_filter :find_properties + + before_filter :setup_property, only: [:index] + + + + private + + def find_properties + @properties = Spree::Property.pluck(:name) + end + + def setup_property + @enterprise = Enterprise.find params[:enterprise_id] + @enterprise.producer_properties.build + end + end +end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 910851e76f..1fc09d2880 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -19,7 +19,7 @@ class Enterprise < ActiveRecord::Base delegate :latitude, :longitude, :city, :state_name, :to => :address - accepts_nested_attributes_for :address + accepts_nested_attributes_for :address, :producer_properties has_attached_file :logo, :styles => { :medium => "300x300>", :thumb => "100x100>" } has_attached_file :promo_image, :styles => { :large => "1200x260#", :thumb => "100x100>" } diff --git a/app/models/producer_property.rb b/app/models/producer_property.rb index cda2928632..ac99c88013 100644 --- a/app/models/producer_property.rb +++ b/app/models/producer_property.rb @@ -1,3 +1,14 @@ class ProducerProperty < ActiveRecord::Base belongs_to :property, class_name: 'Spree::Property' + + def property_name + property.name if property + end + + def property_name=(name) + unless name.blank? + self.property = Spree::Property.find_by_name(name) || + Spree::Property.create(name: name, presentation: name) + end + end end diff --git a/app/views/admin/enterprises/index.html.haml b/app/views/admin/enterprises/index.html.haml index e92f87c231..b358011d89 100644 --- a/app/views/admin/enterprises/index.html.haml +++ b/app/views/admin/enterprises/index.html.haml @@ -43,6 +43,10 @@ %br/ = link_to_delete_enterprise enterprise %br/ + - if enterprise.is_primary_producer + = link_to_with_icon 'icon-dashboard', 'Properties', main_app.admin_enterprise_producer_properties_path(enterprise_id: enterprise.id) + (#{enterprise.producer_properties.count}) + %br/ - if enterprise.is_distributor = link_to_with_icon 'icon-chevron-right', 'Payment Methods', spree.admin_payment_methods_path(enterprise_id: enterprise.id) (#{enterprise.payment_methods.count}) @@ -61,4 +65,4 @@ - if @enterprises.empty? %tr %td{colspan: "4"}= t(:none) - = f.submit 'Update' \ No newline at end of file + = f.submit 'Update' diff --git a/app/views/admin/producer_properties/_producer_property_fields.html.haml b/app/views/admin/producer_properties/_producer_property_fields.html.haml new file mode 100644 index 0000000000..0732bffa75 --- /dev/null +++ b/app/views/admin/producer_properties/_producer_property_fields.html.haml @@ -0,0 +1,11 @@ +%tr.product_property.fields{"data-hook" => "producer_property", id: "#{dom_id(f.object)}"} + %td.no-border + %span.handle + = f.hidden_field :id + %td.property_name + = f.text_field :property_name, :class => 'autocomplete' + %td.value + = f.text_field :value, :class => 'autocomplete' + %td.actions + - unless @enterprise.producer_properties.empty? + -#= link_to_remove_fields t(:remove), f, :no_text => true diff --git a/app/views/admin/producer_properties/index.html.haml b/app/views/admin/producer_properties/index.html.haml new file mode 100644 index 0000000000..b92c191e7d --- /dev/null +++ b/app/views/admin/producer_properties/index.html.haml @@ -0,0 +1,39 @@ +- content_for :page_title do + Producer Properties + + +- content_for :page_actions do + %ul.tollbar.inline-menu + %li + = link_to_add_fields 'Add Producer Property', 'tbody#producer_properties', class: 'icon-plus button' + + += render 'spree/shared/error_messages', target: @enterprise + + += form_for @enterprise, url: main_app.admin_enterprise_path(@enterprise), method: :put do |f| + %fieldset.no-border-top + .add_producer_properties{"data-hook" => "add_producer_properties"} + = image_tag 'spinner.gif', :plugin => 'spree', :style => 'display:none;', :id => 'busy_indicator' + %table.index.sortable{"data-hook" => "", "data-sortable-link" => main_app.update_positions_admin_enterprise_producer_properties_url(@enterprise)} + %thead + %tr{"data-hook" => "producer_properties_header"} + %th{colspan: "2"} Property + %th Value + %th.actions + %tbody#producer_properties{"data-hook" => ""} + = f.fields_for :producer_properties do |pp_form| + = render 'producer_property_fields', f: pp_form + = render 'spree/admin/shared/edit_resource_links', collection_url: main_app.admin_enterprise_producer_properties_path(@enterprise) + = hidden_field_tag 'clear_producer_properties', 'true' + +:javascript + var properties = #{raw(@properties.to_json)}; + + $("#producer_properties input.autocomplete").live("keydown", function() { + already_auto_completed = $(this).is('ac_input'); + if (!already_auto_completed) { + $(this).autocomplete({source: properties}); + $(this).focus(); + } + }); diff --git a/config/routes.rb b/config/routes.rb index 0622acb050..b151e4bae8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,6 +45,10 @@ Openfoodnetwork::Application.routes.draw do resources :enterprises do post :bulk_update, :on => :collection, :as => :bulk_update + + resources :producer_properties do + post :update_positions, on: :collection + end end resources :enterprise_relationships diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index 989d44f181..7a143d59fb 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -160,7 +160,33 @@ feature %q{ page.should have_selector "a.list-item", text: enterprise_fee.name end - context 'as an Enterprise user' do + describe "producer properties" do + it "creates producer properties" do + # Given a producer enterprise + s = create(:supplier_enterprise) + + # When I go to its properties page + login_to_admin_section + click_link 'Enterprises' + within(".enterprise-#{s.id}") { click_link 'Properties' } + + # And I create a property + fill_in 'enterprise_producer_properties_attributes_0_property_name', with: "Certified Organic" + fill_in 'enterprise_producer_properties_attributes_0_value', with: "NASAA 12345" + click_button 'Update' + + # Then I should be returned to the producer properties page + page.should have_selector 'h1.page-title', text: "Producer Properties" + + # And the producer should have the property + s.producer_properties(true).count.should == 1 + s.producer_properties.first.property.presentation.should == "Certified Organic" + s.producer_properties.first.value.should == "NASAA 12345" + end + end + + + context "as an Enterprise user" do let(:supplier1) { create(:supplier_enterprise, name: 'First Supplier') } let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') } let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') }