Sort enterprise relationships by enterprise name

This commit is contained in:
Rohan Mitchell
2014-05-15 14:24:35 +10:00
parent f7de504d89
commit 1a854b89ec
2 changed files with 21 additions and 0 deletions

View File

@@ -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

View 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