Add User#customer_of to look up customers

This commit is contained in:
Rohan Mitchell
2015-04-10 14:24:25 +10:00
parent 34d1841d22
commit 7f6c02ca29
4 changed files with 24 additions and 0 deletions

View File

@@ -1,7 +1,10 @@
class Customer < ActiveRecord::Base
belongs_to :enterprise
belongs_to :user, :class_name => Spree.user_class
validates :code, presence: true, uniqueness: {scope: :enterprise_id}
validates :email, presence: true
validates :enterprise_id, presence: true
scope :of, ->(enterprise) { where(enterprise_id: enterprise) }
end

View File

@@ -4,6 +4,7 @@ Spree.user_class.class_eval do
has_many :owned_enterprises, class_name: 'Enterprise', foreign_key: :owner_id, inverse_of: :owner
has_many :owned_groups, class_name: 'EnterpriseGroup', foreign_key: :owner_id, inverse_of: :owner
has_one :cart
has_many :customers
accepts_nested_attributes_for :enterprise_roles, :allow_destroy => true
@@ -12,6 +13,7 @@ Spree.user_class.class_eval do
validate :limit_owned_enterprises
def known_users
if admin?
Spree::User.scoped
@@ -30,6 +32,10 @@ Spree.user_class.class_eval do
end
end
def customer_of(enterprise)
customers.of(enterprise).first
end
def send_signup_confirmation
Spree::UserMailer.signup_confirmation(self).deliver
end
@@ -38,6 +44,7 @@ Spree.user_class.class_eval do
owned_enterprises(:reload).size < enterprise_limit
end
private
def limit_owned_enterprises

View File

@@ -203,6 +203,13 @@ FactoryGirl.define do
create(:tax_rate, amount: proxy.tax_rate_amount, tax_category: product.tax_category, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, zone: proxy.zone)
end
end
factory :customer, :class => Customer do
email { Faker::Internet.email }
enterprise
code 'abc123'
user
end
end

View File

@@ -36,7 +36,14 @@ describe Spree.user_class do
expect(u1.owned_groups(:reload)).to match_array([g1, g2])
expect(u2.owned_groups(:reload)).to match_array([g3])
end
end
it "loads a user's customer representation at a particular enterprise" do
u = create(:user)
e = create(:enterprise)
c = create(:customer, user: u, enterprise: e)
u.customer_of(e).should == c
end
end