From 4d1148528313a14a721627dd3e4efea57f06e9fa Mon Sep 17 00:00:00 2001 From: Matt-Yorkley Date: Mon, 31 Jul 2017 16:18:43 +0100 Subject: [PATCH] Use receives_notifications flag for enterprise contact --- app/controllers/api/enterprises_controller.rb | 5 ----- app/models/enterprise.rb | 9 ++++++--- app/models/spree/user_decorator.rb | 1 - spec/features/consumer/registration_spec.rb | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/enterprises_controller.rb b/app/controllers/api/enterprises_controller.rb index 2971e648b6..c1f6fb3ce7 100644 --- a/app/controllers/api/enterprises_controller.rb +++ b/app/controllers/api/enterprises_controller.rb @@ -2,7 +2,6 @@ module Api class EnterprisesController < Spree::Api::BaseController before_filter :override_owner, only: [:create, :update] - before_filter :set_contact, only: [:create] before_filter :check_type, only: :update before_filter :override_sells, only: [:create, :update] before_filter :override_visible, only: [:create, :update] @@ -54,10 +53,6 @@ module Api params[:enterprise][:owner_id] = current_api_user.id end - def set_contact - params[:enterprise][:contact_id] = current_api_user.id - end - def check_type params[:enterprise].delete :type unless current_api_user.admin? end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 37539dff23..d842c51b90 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -33,7 +33,6 @@ class Enterprise < ActiveRecord::Base has_many :enterprise_roles, :dependent => :destroy has_many :users, through: :enterprise_roles belongs_to :owner, class_name: 'Spree::User', foreign_key: :owner_id, inverse_of: :owned_enterprises - belongs_to :contact, class_name: 'Spree::User', foreign_key: :contact_id, inverse_of: :contact_enterprises has_and_belongs_to_many :payment_methods, join_table: 'distributors_payment_methods', class_name: 'Spree::PaymentMethod', foreign_key: 'distributor_id' has_many :distributor_shipping_methods, foreign_key: :distributor_id has_many :shipping_methods, through: :distributor_shipping_methods @@ -82,7 +81,7 @@ class Enterprise < ActiveRecord::Base after_validation :geocode_address after_touch :touch_distributors - before_create :set_default_contact + after_create :set_default_contact after_create :relate_to_owners_enterprises after_create :send_welcome_email @@ -178,6 +177,10 @@ class Enterprise < ActiveRecord::Base count(distinct: true) end + def contact + EnterpriseRole.receives_notifications_for self.id + end + def activated? contact.confirmed? && sells != 'unspecified' end @@ -384,7 +387,7 @@ class Enterprise < ActiveRecord::Base end def set_default_contact - self.contact_id = self.owner_id if self.contact_id.nil? + EnterpriseRole.set_notification_user self.owner_id, self.id end def relate_to_owners_enterprises diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 92a020d20e..092afa0f6b 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -6,7 +6,6 @@ Spree.user_class.class_eval do has_many :enterprise_roles, :dependent => :destroy has_many :enterprises, through: :enterprise_roles has_many :owned_enterprises, class_name: 'Enterprise', foreign_key: :owner_id, inverse_of: :owner - has_many :contact_enterprises, class_name: 'Enterprise', foreign_key: :contact_id, inverse_of: :contact has_many :owned_groups, class_name: 'EnterpriseGroup', foreign_key: :owner_id, inverse_of: :owner has_many :account_invoices has_many :billable_periods, foreign_key: :owner_id, inverse_of: :owner diff --git a/spec/features/consumer/registration_spec.rb b/spec/features/consumer/registration_spec.rb index a192b672c2..37caa9123e 100644 --- a/spec/features/consumer/registration_spec.rb +++ b/spec/features/consumer/registration_spec.rb @@ -67,7 +67,7 @@ feature "Registration", js: true do expect(e.address.address1).to eq "123 Abc Street" expect(e.sells).to eq "unspecified" expect(e.is_primary_producer).to eq true - expect(e.contact_id).to eq e.owner_id + expect(e.contact.id).to eq e.owner_id # Filling in about fill_in 'enterprise_description', with: 'Short description'