From e575b0e4909623763af0961efbb3da44ef7370e8 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 11 Jun 2015 10:38:05 +0800 Subject: [PATCH] Switching enterprise index to its own dedicated serializer, so we can spit out ownership --- .../admin/panels/enterprise_package.html.haml | 12 +++---- .../panels/enterprise_producer.html.haml | 6 ++-- .../admin/enterprise_index_panels.css.scss | 3 ++ .../admin/enterprises_controller.rb | 4 +-- .../api/admin/basic_enterprise_serializer.rb | 2 +- .../api/admin/index_enterprise_serializer.rb | 8 +++++ .../admin/index_enterprise_serializer_spec.rb | 34 +++++++++++++++++++ 7 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 app/serializers/api/admin/index_enterprise_serializer.rb create mode 100644 spec/serializers/admin/index_enterprise_serializer_spec.rb diff --git a/app/assets/javascripts/templates/admin/panels/enterprise_package.html.haml b/app/assets/javascripts/templates/admin/panels/enterprise_package.html.haml index 5b472f624a..593d69bb91 100644 --- a/app/assets/javascripts/templates/admin/panels/enterprise_package.html.haml +++ b/app/assets/javascripts/templates/admin/panels/enterprise_package.html.haml @@ -61,23 +61,23 @@ .omega.eight.columns %div{ ng: { if: "!enterprise.is_primary_producer"} } - %a.button.selector{ ng: { click: "enterprise.sells='none'", class: "{selected: enterprise.sells=='none'}" } } + %a.button.selector{ ng: { click: "enterprise.owned && (enterprise.sells='none')", class: "{selected: enterprise.sells=='none', disabled: !enterprise.owned}" } } .top %h3 Profile Only %p Connect through OFN .bottom ALWAYS FREE - %a.button.selector{ ng: { click: "enterprise.sells='any'", class: "{selected: enterprise.sells=='any'}" } } + %a.button.selector{ ng: { click: "enterprise.owned && (enterprise.sells='any')", class: "{selected: enterprise.sells=='any', disabled: !enterprise.owned}" } } .top %h3 Hub Shop %p Sell produce from others .bottom ALWAYS FREE %div{ ng: { if: "enterprise.is_primary_producer"} } - %a.button.selector{ ng: { click: "enterprise.sells='none'", class: "{selected: enterprise.sells=='none'}" } } + %a.button.selector{ ng: { click: "enterprise.owned && (enterprise.sells='none')", class: "{selected: enterprise.sells=='none', disabled: !enterprise.owned}" } } .top %h3 Profile Only .bottom ALWAYS FREE - %a.button.selector{ ng: { click: "enterprise.sells='own'", class: "{selected: enterprise.sells=='own'}" } } + %a.button.selector{ ng: { click: "enterprise.owned && (enterprise.sells='own')", class: "{selected: enterprise.sells=='own', disabled: !enterprise.owned}" } } .top %h3 Producer Shop %p Sell your own produce @@ -85,7 +85,7 @@ \%2 OF SALES %br CAPPED AT $50 PER MONTH - %a.button.selector{ ng: { click: "enterprise.sells='any';", class: "{selected: enterprise.sells=='any'}" } } + %a.button.selector{ ng: { click: "enterprise.owned && (enterprise.sells='any')", class: "{selected: enterprise.sells=='any', disabled: !enterprise.owned}" } } .top %h3 Producer Hub %p Sell produce from self and others @@ -94,7 +94,7 @@ %br CAPPED AT $50 PER MONTH - %a.button.update.fullwidth{ ng: { class: "{disabled: saved() && !saving, saving: saving}", click: "save()" } } + %a.button.update.fullwidth{ ng: { show: "enterprise.owned", class: "{disabled: saved() && !saving, saving: saving}", click: "save()" } } %span{ ng: {hide: "saved() || saving" } } SAVE %i.icon-save diff --git a/app/assets/javascripts/templates/admin/panels/enterprise_producer.html.haml b/app/assets/javascripts/templates/admin/panels/enterprise_producer.html.haml index b0f7473e06..c12e6a3af4 100644 --- a/app/assets/javascripts/templates/admin/panels/enterprise_producer.html.haml +++ b/app/assets/javascripts/templates/admin/panels/enterprise_producer.html.haml @@ -20,19 +20,19 @@ %p Instead, non-producers specialise in linking producers to the end eater, whether it be by aggregating, grading, packing, selling or delivering food. .omega.eight.columns - %a.button.selector{ ng: { click: 'changeToProducer()', class: "{selected: enterprise.is_primary_producer==true}" } } + %a.button.selector{ ng: { click: 'enterprise.owned && changeToProducer()', class: "{selected: enterprise.is_primary_producer==true, disabled: !enterprise.owned}" } } .top %h3 PRODUCER %p Primary producers of food .bottom eg. GROWERS, BAKERS, BREWERS, MAKERS - %a.button.selector{ ng: { click: 'changeToNonProducer()', class: "{selected: enterprise.is_primary_producer==false}" } } + %a.button.selector{ ng: { click: 'enterprise.owned && changeToNonProducer()', class: "{selected: enterprise.is_primary_producer==false, disabled: !enterprise.owned}" } } .top %h3 Non-Producer %p All other food enterprises .bottom eg. Grocery stores, Food co-ops, Buying groups - %a.button.update.fullwidth{ ng: { class: "{disabled: saved() && !saving, saving: saving}", click: "save()" } } + %a.button.update.fullwidth{ ng: { show: "enterprise.owned", class: "{disabled: saved() && !saving, saving: saving}", click: "save()" } } %span{ ng: {hide: "saved() || saving" } } SAVE %i.icon-save diff --git a/app/assets/stylesheets/admin/enterprise_index_panels.css.scss b/app/assets/stylesheets/admin/enterprise_index_panels.css.scss index faabf144d9..25a968e0d1 100644 --- a/app/assets/stylesheets/admin/enterprise_index_panels.css.scss +++ b/app/assets/stylesheets/admin/enterprise_index_panels.css.scss @@ -68,6 +68,9 @@ border-top-color: #9fc820; } } + &.disabled{ + background-color: #C1C1C1; + } .bottom { background: repeating-linear-gradient(60deg, rgba(84, 152, 218, 0), rgba(84, 152, 218, 0) 5px, rgba(255, 255, 255, 0.25) 5px, rgba(255, 255, 255, 0.25) 10px); margin-top: 1em; diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index cd13055a02..ffb74aa825 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -27,7 +27,7 @@ module Admin respond_to do |format| format.html format.json do - render json: @collection, each_serializer: Api::Admin::BasicEnterpriseSerializer + render json: @collection, each_serializer: Api::Admin::IndexEnterpriseSerializer, spree_current_user: spree_current_user end end end @@ -40,7 +40,7 @@ module Admin respond_with(@object) do |format| format.html { redirect_to location_after_save } format.js { render :layout => false } - format.json { render json: @object, serializer: Api::Admin::BasicEnterpriseSerializer } + format.json { render json: @object, serializer: Api::Admin::IndexEnterpriseSerializer, spree_current_user: spree_current_user } end else invoke_callbacks(:update, :fails) diff --git a/app/serializers/api/admin/basic_enterprise_serializer.rb b/app/serializers/api/admin/basic_enterprise_serializer.rb index 854b9b4019..f060dc4166 100644 --- a/app/serializers/api/admin/basic_enterprise_serializer.rb +++ b/app/serializers/api/admin/basic_enterprise_serializer.rb @@ -1,4 +1,4 @@ class Api::Admin::BasicEnterpriseSerializer < ActiveModel::Serializer attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :category, :payment_method_ids, :shipping_method_ids - attributes :producer_profile_only, :permalink + attributes :producer_profile_only end diff --git a/app/serializers/api/admin/index_enterprise_serializer.rb b/app/serializers/api/admin/index_enterprise_serializer.rb new file mode 100644 index 0000000000..189e6a08b5 --- /dev/null +++ b/app/serializers/api/admin/index_enterprise_serializer.rb @@ -0,0 +1,8 @@ +class Api::Admin::IndexEnterpriseSerializer < ActiveModel::Serializer + attributes :name, :id, :permalink, :is_primary_producer, :sells, :producer_profile_only, :owned + + def owned + return true if options[:spree_current_user].admin? + object.owner == options[:spree_current_user] + end +end diff --git a/spec/serializers/admin/index_enterprise_serializer_spec.rb b/spec/serializers/admin/index_enterprise_serializer_spec.rb new file mode 100644 index 0000000000..3651f53f8d --- /dev/null +++ b/spec/serializers/admin/index_enterprise_serializer_spec.rb @@ -0,0 +1,34 @@ +describe Api::Admin::IndexEnterpriseSerializer do + include AuthenticationWorkflow + + let(:enterprise) { create(:distributor_enterprise) } + context "when spree_current_user is a manager" do + let(:user) { create_enterprise_user } + before do + user.enterprise_roles.create(enterprise: enterprise) + end + + it "sets 'owned' to false" do + serializer = Api::Admin::IndexEnterpriseSerializer.new enterprise, spree_current_user: user + serializer.to_json.should match "\"owned\":false" + end + end + + context "when spree_current_user is " do + let(:user) { enterprise.owner } + + it "sets 'owned' to true" do + serializer = Api::Admin::IndexEnterpriseSerializer.new enterprise, spree_current_user: user + serializer.to_json.should match "\"owned\":true" + end + end + + context "when spree_current_user is the owner" do + let(:user) { create(:admin_user) } + + it "sets 'owned' to true" do + serializer = Api::Admin::IndexEnterpriseSerializer.new enterprise, spree_current_user: user + serializer.to_json.should match "\"owned\":true" + end + end +end