mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merging master into this branch to make it easy to pull for staging push
This commit is contained in:
@@ -60,12 +60,13 @@ class Enterprise < ActiveRecord::Base
|
||||
validate :enforce_ownership_limit, if: lambda { owner_id_changed? && !owner_id.nil? }
|
||||
validates_length_of :description, :maximum => 255
|
||||
|
||||
before_save :confirmation_check, if: lambda{ email_changed? }
|
||||
before_save :confirmation_check, if: lambda { email_changed? }
|
||||
|
||||
before_validation :ensure_owner_is_manager, if: lambda { owner_id_changed? && !owner_id.nil? }
|
||||
before_validation :set_unused_address_fields
|
||||
after_validation :geocode_address
|
||||
|
||||
after_create :relate_to_owners_hubs
|
||||
# TODO: Later versions of devise have a dedicated after_confirmation callback, so use that
|
||||
after_update :welcome_after_confirm, if: lambda { confirmation_token_changed? && confirmation_token.nil? }
|
||||
after_create :send_welcome_email, if: lambda { email_is_known? }
|
||||
@@ -93,6 +94,7 @@ class Enterprise < ActiveRecord::Base
|
||||
}
|
||||
scope :is_primary_producer, where(:is_primary_producer => true)
|
||||
scope :is_distributor, where('sells != ?', 'none')
|
||||
scope :is_hub, where(sells: 'any')
|
||||
scope :supplying_variant_in, lambda { |variants| joins(:supplied_products => :variants_including_master).where('spree_variants.id IN (?)', variants).select('DISTINCT enterprises.*') }
|
||||
scope :with_supplied_active_products_on_hand, lambda {
|
||||
joins(:supplied_products)
|
||||
@@ -343,6 +345,18 @@ class Enterprise < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def relate_to_owners_hubs
|
||||
hubs = owner.owned_enterprises.is_hub.where('enterprises.id != ?', self)
|
||||
|
||||
hubs.each do |hub|
|
||||
EnterpriseRelationship.create!(parent: self,
|
||||
child: hub,
|
||||
permissions_list: [:add_to_order_cycle,
|
||||
:manage_products,
|
||||
:edit_profile])
|
||||
end
|
||||
end
|
||||
|
||||
def shopfront_taxons
|
||||
unless preferred_shopfront_taxon_order =~ /\A((\d+,)*\d+)?\z/
|
||||
errors.add(:shopfront_category_ordering, "must contain a list of taxons.")
|
||||
|
||||
@@ -580,6 +580,41 @@ describe Enterprise do
|
||||
end
|
||||
end
|
||||
|
||||
describe "callbacks" do
|
||||
describe "after creation" do
|
||||
let(:owner) { create(:user, enterprise_limit: 10) }
|
||||
|
||||
let(:hub1) { create(:distributor_enterprise, owner: owner) }
|
||||
let(:hub2) { create(:distributor_enterprise, owner: owner) }
|
||||
let(:producer) { create(:supplier_enterprise, owner: owner) }
|
||||
|
||||
let(:er1) { EnterpriseRelationship.where(child_id: hub1).last }
|
||||
let(:er2) { EnterpriseRelationship.where(child_id: hub2).last }
|
||||
|
||||
it "establishes relationships with the owner's hubs" do
|
||||
hub1
|
||||
hub2
|
||||
enterprise = nil
|
||||
|
||||
expect do
|
||||
enterprise = create(:enterprise, owner: owner)
|
||||
end.to change(EnterpriseRelationship, :count).by(2)
|
||||
|
||||
[er1, er2].each do |er|
|
||||
er.parent.should == enterprise
|
||||
er.permissions.map(&:name).sort.should == ['add_to_order_cycle', 'manage_products', 'edit_profile'].sort
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't relate to enterprises that aren't sells=='any'" do
|
||||
producer
|
||||
expect do
|
||||
enterprise = create(:enterprise, owner: owner)
|
||||
end.to change(EnterpriseRelationship, :count).by(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "has_supplied_products_on_hand?" do
|
||||
before :each do
|
||||
@supplier = create(:supplier_enterprise)
|
||||
|
||||
Reference in New Issue
Block a user