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.
Breaking up lib modules for class and instance methods. Also make dyn…
…amic belongs_to :grouping check for association first.
- Loading branch information
1 parent
813effd
commit e3adcd6
Showing
7 changed files
with
109 additions
and
64 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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module GroupedScope | ||
module ClassMethods | ||
|
||
def grouped_scopes | ||
read_inheritable_attribute(:grouped_scopes) || write_inheritable_attribute(:grouped_scopes, {}) | ||
end | ||
|
||
def grouped_scope(*args) | ||
belongs_to_grouped_scope_grouping | ||
args.each do |association| | ||
ung_assoc = reflect_on_association(association) | ||
unless ung_assoc && ung_assoc.macro == :has_many | ||
raise ArgumentError, "Cannot create a group scope for :#{association} because it is not a has_many association." | ||
end | ||
grouped_scopes[association] = true | ||
group_association_options = {:class_name => ung_assoc.class_name, :foreign_key => ung_assoc.primary_key_name} | ||
group_association_options.merge!(ung_assoc.options) | ||
has_many "group_#{association}".to_sym, group_association_options | ||
end | ||
include InstanceMethods | ||
end | ||
|
||
private | ||
|
||
def belongs_to_grouped_scope_grouping | ||
grouping_class_name = 'GroupedScope::Grouping' | ||
existing_grouping = reflect_on_association(:grouping) | ||
return false if existing_grouping && existing_grouping.macro == :belongs_to && existing_grouping.options[:class_name] == grouping_class_name | ||
belongs_to :grouping, :foreign_key => 'group_id', :class_name => grouping_class_name | ||
end | ||
|
||
|
||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module GroupedScope | ||
module HasManyAssociation | ||
|
||
def self.included(klass) | ||
klass.class_eval do | ||
alias_method_chain :construct_sql, :group_scope | ||
end | ||
end | ||
|
||
def construct_sql_with_group_scope | ||
if @owner.class.grouped_scopes[@reflection.name] | ||
@finder_sql = "#{@reflection.klass.table_name}.#{@reflection.primary_key_name} IN (#{@owner.group.quoted_ids})" | ||
@finder_sql << " AND (#{conditions})" if conditions | ||
@counter_sql = @finder_sql | ||
else | ||
construct_sql_without_group_scope | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module GroupedScope | ||
module InstanceMethods | ||
|
||
def group | ||
@group ||= Group.new(self) | ||
end | ||
|
||
|
||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require File.dirname(__FILE__) + '/../helper' | ||
|
||
class ClassMethodsTest < Test::Unit::TestCase | ||
|
||
def setup | ||
setup_environment | ||
end | ||
|
||
context 'For .grouped_scopes' do | ||
|
||
should 'have a inheritable attribute hash' do | ||
assert_instance_of Hash, Employee.grouped_scopes | ||
end | ||
|
||
should 'add to inheritable attributes with new grouped_scope' do | ||
assert_nil Employee.grouped_scopes[:foobars] | ||
Employee.class_eval { has_many(:foobars) ; grouped_scope(:foobars) } | ||
assert Employee.grouped_scopes[:foobars] | ||
end | ||
|
||
end | ||
|
||
context 'For .grouped_scope' do | ||
|
||
should 'create a belongs_to :grouping association' do | ||
assert Employee.reflect_on_association(:grouping) | ||
end | ||
|
||
should 'not create more belongs_to :grouping on additional calls' do | ||
Employee.expects(:has_many).with(:grouping).never | ||
Employee.class_eval { has_many(:foobars) ; grouped_scope(:foobars) } | ||
end | ||
|
||
end | ||
|
||
|
||
end |