mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-22 05:18:51 +00:00
Enterprise validates owner enterprise_limit
This commit is contained in:
@@ -16,7 +16,7 @@ class Enterprise < ActiveRecord::Base
|
||||
has_many :enterprise_fees
|
||||
has_many :enterprise_roles, :dependent => :destroy
|
||||
has_many :users, through: :enterprise_roles
|
||||
belongs_to :owner, class_name: 'Spree::User', foreign_key: :owner_id
|
||||
belongs_to :owner, class_name: 'Spree::User', foreign_key: :owner_id, inverse_of: :owned_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
|
||||
@@ -47,7 +47,8 @@ class Enterprise < ActiveRecord::Base
|
||||
validates :name, presence: true
|
||||
validates :type, presence: true, inclusion: {in: TYPES}
|
||||
validates :address, presence: true, associated: true
|
||||
validates :owner, presence: true
|
||||
validates_presence_of :owner
|
||||
validate :enforce_ownership_limit, if: lambda { owner_id_changed? }
|
||||
|
||||
before_validation :ensure_owner_is_manager, if: lambda { owner_id_changed? }
|
||||
before_validation :set_unused_address_fields
|
||||
@@ -241,4 +242,10 @@ class Enterprise < ActiveRecord::Base
|
||||
def ensure_owner_is_manager
|
||||
users << owner unless users.include?(owner) || owner.admin?
|
||||
end
|
||||
|
||||
def enforce_ownership_limit
|
||||
unless owner.can_own_more_enterprises?
|
||||
errors.add(:owner, "^You are not permitted to own own any more enterprises (limit is #{owner.enterprise_limit}).")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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
|
||||
has_many :owned_enterprises, class_name: 'Enterprise', foreign_key: :owner_id, inverse_of: :owner
|
||||
has_one :cart
|
||||
|
||||
accepts_nested_attributes_for :enterprise_roles, :allow_destroy => true
|
||||
@@ -9,7 +9,7 @@ Spree.user_class.class_eval do
|
||||
attr_accessible :enterprise_ids, :enterprise_roles_attributes
|
||||
after_create :send_signup_confirmation
|
||||
|
||||
validate :owned_enterprises_count
|
||||
validate :limit_owned_enterprises
|
||||
|
||||
def build_enterprise_roles
|
||||
Enterprise.all.each do |enterprise|
|
||||
@@ -23,11 +23,15 @@ Spree.user_class.class_eval do
|
||||
Spree::UserMailer.signup_confirmation(self).deliver
|
||||
end
|
||||
|
||||
def can_own_more_enterprises?
|
||||
owned_enterprises(:reload).size < enterprise_limit
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def owned_enterprises_count
|
||||
def limit_owned_enterprises
|
||||
if owned_enterprises.size > enterprise_limit
|
||||
errors.add(:owned_enterprises, "^The nominated user is not permitted to own own any more enterprises.")
|
||||
errors.add(:owned_enterprises, "^The nominated user is not permitted to own own any more enterprises (limit is #{enterprise_limit}).")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user