Files
openfoodnetwork/app/models/customer.rb
Maikel Linke 17f168ed06 Merge branch 'master' of github.com:openfoodfoundation/openfoodnetwork into uk/account-balances
Applied code conventions.

Conflicts:
	config/locales/en.yml
2016-03-30 13:30:44 +11:00

21 lines
614 B
Ruby

class Customer < ActiveRecord::Base
acts_as_taggable
belongs_to :enterprise
belongs_to :user, class_name: Spree.user_class
validates :code, uniqueness: { scope: :enterprise_id, allow_blank: true, allow_nil: true }
validates :email, presence: true, uniqueness: { scope: :enterprise_id, message: I18n.t('validation_msg_is_associated_with_an_exising_customer') }
validates :enterprise_id, presence: true
scope :of, ->(enterprise) { where(enterprise_id: enterprise) }
before_create :associate_user
private
def associate_user
self.user = user || Spree::User.find_by_email(email)
end
end