From cc5d0c35dd32aa3690a2dad77a0edc731a69f383 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley Date: Sun, 31 Dec 2017 00:17:01 +0000 Subject: [PATCH] Refactor enterprise contact user assignment --- .../admin/enterprises_controller.rb | 7 +++---- app/models/enterprise.rb | 9 +++++++-- app/models/enterprise_role.rb | 20 ------------------- .../admin/enterprises/form/_users.html.haml | 4 ++-- .../admin/enterprises_controller_spec.rb | 10 ---------- spec/features/admin/enterprises_spec.rb | 2 +- 6 files changed, 13 insertions(+), 39 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 666db81e68..d5672eccda 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -188,7 +188,7 @@ module Admin def load_roles @enterprise_roles = EnterpriseRole.find_all_by_enterprise_id(@enterprise.id) - @notification_user = EnterpriseRole.receives_notifications_for(@enterprise.id).try(:id) || @enterprise.owner.id + @current_contact = @enterprise.contact.id end def update_tag_rules(tag_rules_attributes) @@ -206,9 +206,8 @@ module Admin end def update_enterprise_notifications - notify_user = params[:receives_notifications] - if notify_user.present? - EnterpriseRole.set_notification_user notify_user, @object.id + if params.key? :receives_notifications + @enterprise.update_contact params[:receives_notifications] end end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9fd5b6030c..8b0f3fe385 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -178,7 +178,12 @@ class Enterprise < ActiveRecord::Base end def contact - EnterpriseRole.receives_notifications_for id || owner + contact = users.where(enterprise_roles: {receives_notifications: true}).first + contact || owner + end + + def update_contact(user_id) + enterprise_roles.update_all(["receives_notifications=(user_id=?)", user_id]) end def activated? @@ -387,7 +392,7 @@ class Enterprise < ActiveRecord::Base end def set_default_contact - EnterpriseRole.set_notification_user self.owner_id, self.id + update_contact self.owner_id end def relate_to_owners_enterprises diff --git a/app/models/enterprise_role.rb b/app/models/enterprise_role.rb index c283a12b1d..e69d439c40 100644 --- a/app/models/enterprise_role.rb +++ b/app/models/enterprise_role.rb @@ -6,24 +6,4 @@ class EnterpriseRole < ActiveRecord::Base validates_uniqueness_of :enterprise_id, scope: :user_id, message: I18n.t(:enterprise_role_uniqueness_error) scope :by_user_email, joins(:user).order('spree_users.email ASC') - - def self.receives_notifications_for(enterprise_id) - manager = EnterpriseRole.find_by_enterprise_id_and_receives_notifications(enterprise_id, true) - return nil if manager.blank? - Spree::User.find(manager.user_id) - end - - def self.set_notification_user(user_id, enterprise_id) - managers_for(enterprise_id).map do |m| - if m.user_id == user_id.to_i - m.update_attributes receives_notifications: true - elsif m.receives_notifications - m.update_attributes receives_notifications: false - end - end - end - - def self.managers_for(enterprise_id) - EnterpriseRole.where(enterprise_id: enterprise_id) - end end diff --git a/app/views/admin/enterprises/form/_users.html.haml b/app/views/admin/enterprises/form/_users.html.haml index 443b01c483..e939778a80 100644 --- a/app/views/admin/enterprises/form/_users.html.haml +++ b/app/views/admin/enterprises/form/_users.html.haml @@ -24,8 +24,8 @@ - if full_permissions .eight.columns.omega - %select.select2.fullwidth{id: 'receives_notifications_dropdown', name: 'receives_notifications', ng: {model: 'receivesNotifications', init: "receivesNotifications = '#{@notification_user}'"}} - %option{ng: {repeat: 'user in Enterprise.users', selected: "user.id == #{@notification_user}", hide: '!user.confirmed'}, value: '{{user.id}}'} + %select.select2.fullwidth{id: 'receives_notifications_dropdown', name: 'receives_notifications', ng: {model: 'receivesNotifications', init: "receivesNotifications = '#{@current_contact}'"}} + %option{ng: {repeat: 'user in Enterprise.users', selected: "user.id == #{@current_contact}", hide: '!user.confirmed'}, value: '{{user.id}}'} {{user.email}} .row diff --git a/spec/controllers/admin/enterprises_controller_spec.rb b/spec/controllers/admin/enterprises_controller_spec.rb index c592fce4f3..e6ff6b1cea 100644 --- a/spec/controllers/admin/enterprises_controller_spec.rb +++ b/spec/controllers/admin/enterprises_controller_spec.rb @@ -32,16 +32,6 @@ module Admin distributor_manager.enterprise_roles.where(enterprise_id: enterprise).first.should be end - it "does not grant management permission to admins" do - controller.stub spree_current_user: admin_user - enterprise_params[:enterprise][:owner_id] = admin_user - - spree_put :create, enterprise_params - enterprise = Enterprise.find_by_name 'zzz' - response.should redirect_to edit_admin_enterprise_path enterprise - admin_user.enterprise_roles.where(enterprise_id: enterprise).should be_empty - end - it "overrides the owner_id submitted by the user (when not super admin)" do controller.stub spree_current_user: distributor_manager enterprise_params[:enterprise][:owner_id] = user diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index 282bbe0713..fb7a3be218 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -345,7 +345,7 @@ feature %q{ # And I should be managing it Enterprise.managed_by(enterprise_user).should include enterprise - expect(EnterpriseRole.receives_notifications_for(enterprise.id)).to eq enterprise.owner + expect(enterprise.contact).to eq enterprise.owner end context "overstepping my owned enterprises limit" do