mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-31 21:37:16 +00:00
Sort enterprise relationships by enterprise name
This commit is contained in:
@@ -4,4 +4,9 @@ class EnterpriseRelationship < ActiveRecord::Base
|
||||
|
||||
validates_presence_of :parent_id, :child_id
|
||||
validates_uniqueness_of :child_id, scope: :parent_id
|
||||
|
||||
scope :with_enterprises,
|
||||
joins('LEFT JOIN enterprises AS parent_enterprises ON parent_enterprises.id = enterprise_relationships.parent_id').
|
||||
joins('LEFT JOIN enterprises AS child_enterprises ON child_enterprises.id = enterprise_relationships.child_id')
|
||||
scope :by_name, with_enterprises.order('parent_enterprises.name, child_enterprises.name')
|
||||
end
|
||||
|
||||
16
spec/models/enterprise_relationship_spec.rb
Normal file
16
spec/models/enterprise_relationship_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe EnterpriseRelationship do
|
||||
describe "scopes" do
|
||||
it "sorts by parent, child enterprise name" do
|
||||
e1 = create(:enterprise, name: 'A')
|
||||
e2 = create(:enterprise, name: 'B')
|
||||
e3 = create(:enterprise, name: 'C')
|
||||
er1 = create(:enterprise_relationship, parent: e1, child: e3)
|
||||
er2 = create(:enterprise_relationship, parent: e2, child: e1)
|
||||
er3 = create(:enterprise_relationship, parent: e1, child: e2)
|
||||
|
||||
EnterpriseRelationship.by_name.should == [er3, er1, er2]
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user