mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Add EnterpriseRelationship. An Enterprise can find its relatives.
This commit is contained in:
@@ -127,6 +127,15 @@ class Enterprise < ActiveRecord::Base
|
||||
"#{id}-#{name.parameterize}"
|
||||
end
|
||||
|
||||
def relatives
|
||||
Enterprise.where("
|
||||
enterprises.id IN
|
||||
(SELECT child_id FROM enterprise_relationships WHERE enterprise_relationships.parent_id=?)
|
||||
OR enterprises.id IN
|
||||
(SELECT parent_id FROM enterprise_relationships WHERE enterprise_relationships.child_id=?)
|
||||
", self.id, self.id)
|
||||
end
|
||||
|
||||
def distributed_variants
|
||||
Spree::Variant.joins(:product).merge(Spree::Product.in_distributor(self)).select('spree_variants.*')
|
||||
end
|
||||
|
||||
7
app/models/enterprise_relationship.rb
Normal file
7
app/models/enterprise_relationship.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class EnterpriseRelationship < ActiveRecord::Base
|
||||
belongs_to :parent, class_name: 'Enterprise'
|
||||
belongs_to :child, class_name: 'Enterprise'
|
||||
|
||||
validates_presence_of :parent_id, :child_id
|
||||
validates_uniqueness_of :child_id, scope: :parent_id
|
||||
end
|
||||
16
db/migrate/20140514044959_create_enterprise_relationships.rb
Normal file
16
db/migrate/20140514044959_create_enterprise_relationships.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class CreateEnterpriseRelationships < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :enterprise_relationships do |t|
|
||||
t.integer :parent_id
|
||||
t.integer :child_id
|
||||
end
|
||||
|
||||
add_index :enterprise_relationships, :parent_id
|
||||
add_index :enterprise_relationships, :child_id
|
||||
|
||||
add_index :enterprise_relationships, [:parent_id, :child_id], unique: true
|
||||
|
||||
add_foreign_key :enterprise_relationships, :enterprises, column: :parent_id
|
||||
add_foreign_key :enterprise_relationships, :enterprises, column: :child_id
|
||||
end
|
||||
end
|
||||
14
db/schema.rb
14
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20140430020639) do
|
||||
ActiveRecord::Schema.define(:version => 20140514044959) do
|
||||
|
||||
create_table "adjustment_metadata", :force => true do |t|
|
||||
t.integer "adjustment_id"
|
||||
@@ -195,6 +195,15 @@ ActiveRecord::Schema.define(:version => 20140430020639) do
|
||||
add_index "enterprise_groups_enterprises", ["enterprise_group_id"], :name => "index_enterprise_groups_enterprises_on_enterprise_group_id"
|
||||
add_index "enterprise_groups_enterprises", ["enterprise_id"], :name => "index_enterprise_groups_enterprises_on_enterprise_id"
|
||||
|
||||
create_table "enterprise_relationships", :force => true do |t|
|
||||
t.integer "parent_id"
|
||||
t.integer "child_id"
|
||||
end
|
||||
|
||||
add_index "enterprise_relationships", ["child_id"], :name => "index_enterprise_relationships_on_child_id"
|
||||
add_index "enterprise_relationships", ["parent_id", "child_id"], :name => "index_enterprise_relationships_on_parent_id_and_child_id", :unique => true
|
||||
add_index "enterprise_relationships", ["parent_id"], :name => "index_enterprise_relationships_on_parent_id"
|
||||
|
||||
create_table "enterprise_roles", :force => true do |t|
|
||||
t.integer "user_id"
|
||||
t.integer "enterprise_id"
|
||||
@@ -1005,6 +1014,9 @@ ActiveRecord::Schema.define(:version => 20140430020639) do
|
||||
add_foreign_key "enterprise_groups_enterprises", "enterprise_groups", name: "enterprise_groups_enterprises_enterprise_group_id_fk"
|
||||
add_foreign_key "enterprise_groups_enterprises", "enterprises", name: "enterprise_groups_enterprises_enterprise_id_fk"
|
||||
|
||||
add_foreign_key "enterprise_relationships", "enterprises", name: "enterprise_relationships_child_id_fk", column: "child_id"
|
||||
add_foreign_key "enterprise_relationships", "enterprises", name: "enterprise_relationships_parent_id_fk", column: "parent_id"
|
||||
|
||||
add_foreign_key "enterprise_roles", "enterprises", name: "enterprise_roles_enterprise_id_fk"
|
||||
add_foreign_key "enterprise_roles", "spree_users", name: "enterprise_roles_user_id_fk", column: "user_id"
|
||||
|
||||
|
||||
@@ -26,6 +26,17 @@ describe Enterprise do
|
||||
|
||||
Spree::Product.where(id: p.id).should be_empty
|
||||
end
|
||||
|
||||
describe "relationships to other enterprises" do
|
||||
it "finds relatives" do
|
||||
e, p, c = create(:enterprise), create(:enterprise), create(:enterprise)
|
||||
|
||||
EnterpriseRelationship.create! parent_id: p.id, child_id: e.id
|
||||
EnterpriseRelationship.create! parent_id: e.id, child_id: c.id
|
||||
|
||||
e.relatives.sort.should == [p, c].sort
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "validations" do
|
||||
|
||||
Reference in New Issue
Block a user