forked from metaskills/grouped_scope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
311ad5b
commit 2bdfe3b
Showing
6 changed files
with
138 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,17 @@ http://metaskills.net/2008/09/28/jack-has_many-things/ | |
|
||
|
||
|
||
## Installation & Usage | ||
## Installation | ||
|
||
Install the gem with bundler. We follow a semantic versioning format that tracks ActiveRecord's minor version. So this means to use the latest 3.1.x version of GroupedScope with any ActiveRecord 3.1 version. | ||
|
||
```ruby | ||
gem 'grouped_scope', '~> 3.1.0' | ||
``` | ||
|
||
|
||
## Setup | ||
|
||
To use GroupedScope on a model it must have a `:group_id` column. | ||
|
||
```ruby | ||
|
@@ -30,6 +33,9 @@ class AddGroupId < ActiveRecord::Migration | |
end | ||
``` | ||
|
||
|
||
## General Usage | ||
|
||
Assume the following model. | ||
|
||
```ruby | ||
|
@@ -79,6 +85,30 @@ defined on the original association. For instance: | |
``` | ||
|
||
|
||
## Advanced Usage | ||
|
||
The object returned by the `#group` method is an ActiveRecord relation on the targets class, | ||
in this case `Employee`. Given this, you can further scope the grouped proxy if needed. Below, | ||
we use the `:email_present` scope to refine the group down. | ||
|
||
```ruby | ||
class Employee < ActiveRecord::Base | ||
has_many :reports | ||
grouped_scope :reports | ||
scope :email_present, where("email IS NOT NULL") | ||
end | ||
|
||
@employee_one = Employee.create :group_id => 5, :name => 'Ken' | ||
@employee_two = Employee.create :group_id => 5, :name => 'MetaSkills', :email => '[email protected]' | ||
|
||
# Only one employee is returned now. | ||
@employee_one.group.email_present # => [#<Employee id: 1, group_id: 5, name: 'MetaSkills', email: '[email protected]'] | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
## Todo List | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,11 @@ class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase | |
@employee = FactoryGirl.create(:employee) | ||
end | ||
|
||
it 'return an array' do | ||
assert_instance_of Array, @employee.group | ||
end | ||
|
||
it 'return #ids array' do | ||
assert_equal [@employee.id], @employee.group.ids | ||
e1 = FactoryGirl.create :employee, :group_id => 3 | ||
e2 = FactoryGirl.create :employee, :group_id => 3 | ||
assert_same_elements [e1.id, e2.id], e1.group.ids | ||
end | ||
|
||
it 'return #quoted_ids string for use in sql statments' do | ||
|
@@ -35,6 +34,28 @@ class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase | |
end | ||
end | ||
|
||
describe 'for #with_reflection' do | ||
|
||
before { @reflection = Employee.reflections[:reports] } | ||
|
||
it 'will set a reflection and always set it back to nil' do | ||
assert_nil @employee.group.reflection | ||
@employee.group.with_reflection(@reflection) do | ||
assert_equal @reflection, @employee.group.reflection | ||
end | ||
assert_nil @employee.group.reflection | ||
end | ||
|
||
it 'will use the primary key of the reflection' do | ||
pk = 'association_primary_key' | ||
@reflection.stubs :association_primary_key => pk | ||
@employee.group.with_reflection(@reflection) do | ||
assert_equal pk, @employee.group.send(:primary_key) | ||
end | ||
end | ||
|
||
end | ||
|
||
describe 'for Array delegates' do | ||
|
||
it 'respond to first/last' do | ||
|
@@ -56,8 +77,8 @@ class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase | |
|
||
describe 'Calling #group' do | ||
|
||
it 'return an array' do | ||
assert_instance_of Array, FactoryGirl.create(:employee).group | ||
it 'returns a active record relation' do | ||
assert_instance_of ActiveRecord::Relation, FactoryGirl.create(:employee).group | ||
end | ||
|
||
describe 'with a NIL group_id' do | ||
|
@@ -74,6 +95,11 @@ class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase | |
assert @employee.group.include?(@employee) | ||
end | ||
|
||
it 'returns a sql literal for #ids_sql scoped to single record' do | ||
@employee.group.ids_sql.must_be_instance_of Arel::Nodes::SqlLiteral | ||
@employee.group.ids_sql.must_match %r{SELECT \"employees\".\"id\" FROM \"employees\" WHERE \"employees\".\"id\" = #{@employee.id}} | ||
end | ||
|
||
end | ||
|
||
describe 'with a set group_id' do | ||
|
@@ -90,6 +116,22 @@ class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase | |
assert @employee.group.include?(@employee) | ||
end | ||
|
||
it 'returns a sql literal for #ids_sql scoped to group' do | ||
new_group_id = 420 | ||
e1 = FactoryGirl.create :employee, :group_id => new_group_id | ||
e2 = FactoryGirl.create :employee, :group_id => new_group_id | ||
e1.group.ids_sql.must_be_instance_of Arel::Nodes::SqlLiteral | ||
e1.group.ids_sql.must_match %r{SELECT \"employees\".\"id\" FROM \"employees\" WHERE \"employees\".\"group_id\" = #{new_group_id}} | ||
end | ||
|
||
it 'allows the group to be further scoped' do | ||
new_group_id = 420 | ||
e1 = FactoryGirl.create :employee, :group_id => new_group_id, :name => 'Ken', :email => '[email protected]' | ||
e2 = FactoryGirl.create :employee, :group_id => new_group_id, :name => 'Hostmaster', :email => '[email protected]' | ||
e3 = FactoryGirl.create :employee, :group_id => new_group_id, :name => 'Ken', :email => '[email protected]' | ||
assert_same_elements [e1, e2], e1.group.email_for_actionmoniker | ||
end | ||
|
||
end | ||
|
||
describe 'with different groups available' do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters