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
d4f3633
commit 50f03a2
Showing
18 changed files
with
275 additions
and
269 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,63 @@ | ||
|
||
* Rails 3.1 Implementation | ||
|
||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/reflection.rb - [24, 159, 331] | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations.rb - [154, 1171] | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations/builder/association.rb - [] | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations/builder/collection_association.rb - [] | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations/builder/has_many.rb - | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations/has_many_association.rb - | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations/collection_association.rb - [370] | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations/association.rb - [97] | ||
/Users/kencollins/Repositories/rails/activerecord/lib/active_record/associations/association_scope.rb - [48] | ||
|
||
Notes: | ||
|
||
equalities = wheres.grep(Arel::Nodes::Equality) | ||
|
||
>> ActiveRecord::Associations::AssociationScope.new(User.first.association(:columns)).scope.where_values | ||
User Load (0.5ms) SELECT "users".* FROM "users" LIMIT 1 | ||
=> [#<Arel::Nodes::Equality:0x007ffa41eb9c70 @left=#<struct Arel::Attributes::Attribute relation=#<Arel::Table:0x007ffa41eb9f40 @name="columns", @engine=ActiveRecord::Base, @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>, name="user_id">, @right=8>] | ||
|
||
>> ActiveRecord::Associations::AssociationScope.new(User.first.association(:columns)).send(:construct_tables) | ||
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1 | ||
=> [#<Arel::Table:0x007ffa41fccbf8 @name="columns", @engine=ActiveRecord::Base, @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>] | ||
|
||
>> ActiveRecord::Associations::AssociationScope.new(User.first.association(:columns)) | ||
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1 | ||
Column Load (0.7ms) SELECT "columns".* FROM "columns" WHERE "columns"."user_id" = 8 ORDER BY position | ||
=> #<ActiveRecord::Associations::AssociationScope:0x007ffa458fe9d0 @association=#<ActiveRecord::Associations::HasManyAssociation:0x007ffa458fea70 @target=[], @reflection=#<ActiveRecord::Reflection::AssociationReflection:0x007ffa46855820 @macro=:has_many, @name=:columns, @options={:order=>"position", :extend=>[]}, @active_record=User(14 columns), @plural_name="columns", @collection=true, @class_name="Column", @klass=Column(3 columns), @foreign_key="user_id", @active_record_primary_key="id", @type=nil, @table_name="columns", @association_foreign_key="column_id">, @owner=#<User id: 8, email: "[email protected]", verified: true, security_token: "1b8ed73c7e8600a82bd8824d2a5b243063665b9a", token_expiry: "2010-02-07 20:44:20", created_at: "2006-12-07 17:39:27", updated_at: "2011-09-24 16:28:41", logged_in_at: "2010-02-07 00:45:11", deleted: false, delete_after: nil, uuid: "b8281ad06871012903870016cba2a0b6", iphone_verified: false, tab_links: false, password_digest: "52eb36143a4c782e202a62d5b7d64f7ce4e6ec11">, @updated=false, @loaded=false, @association_scope=nil, @proxy=[#<Column id: 3635, user_id: 8, position: 1>, #<Column id: 10, user_id: 8, position: 2>], @stale_state=nil>, @alias_tracker=#<ActiveRecord::Associations::AliasTracker:0x007ffa458fe9a8 @aliases={}, @table_joins=[]>> | ||
|
||
>> User.first.columns.unscoped { Column.where(:position => 99) } | ||
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1 | ||
Column Load (0.9ms) SELECT "columns".* FROM "columns" WHERE "columns"."position" = 99 | ||
=> [] | ||
|
||
>> User.first.columns.unscoped { Column.where(:user_id => [1,2,3]) } | ||
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1 | ||
Column Load (0.9ms) SELECT "columns".* FROM "columns" WHERE "columns"."user_id" IN (1, 2, 3) | ||
=> [#<Column id: 1, user_id: 1, position: 5>, #<Column id: 2, user_id: 1, position: 1>, #<Column id: 8, user_id: 1, position: 6>, #<Column id: 9, user_id: 1, position: 4>, #<Column id: 809, user_id: 1, position: 2>, #<Column id: 3395, user_id: 1, position: 3>] | ||
|
||
>> User.reflections[:columns].association_class | ||
=> ActiveRecord::Associations::HasManyAssociation | ||
|
||
>> User.reflections[:columns].association_foreign_key | ||
=> "column_id" | ||
|
||
>> User.reflect_on_all_associations(:has_many).detect { |a| a.name == :columns } | ||
=> #<ActiveRecord::Reflection::AssociationReflection:0x007ffa46855820 @macro=:has_many, @name=:columns, @options={:order=>"position", :extend=>[]}, @active_record=User(14 columns), @plural_name="columns", @collection=true, @class_name="Column", @klass=Column(3 columns), @foreign_key="user_id", @active_record_primary_key="id", @type=nil, @table_name="columns", @association_foreign_key="column_id"> | ||
|
||
>> User.reflections[:columns] | ||
=> #<ActiveRecord::Reflection::AssociationReflection:0x007ffa46855820 @macro=:has_many, @name=:columns, @options={:order=>"position", :extend=>[]}, @active_record=User(14 columns), @plural_name="columns", @collection=true, @class_name="Column", @klass=Column(3 columns), @foreign_key="user_id", @active_record_primary_key="id", @type=nil> | ||
|
||
>> User.reflect_on_association(:columns) | ||
=> #<ActiveRecord::Reflection::AssociationReflection:0x007ffa46855820 @macro=:has_many, @name=:columns, @options={:order=>"position", :extend=>[]}, @active_record=User(14 columns), @plural_name="columns", @collection=true, @class_name="Column", @klass=Column(3 columns), @foreign_key="user_id", @active_record_primary_key="id", @type=nil> | ||
|
||
>> User.first.association(:columns) | ||
=> #<ActiveRecord::Associations::HasManyAssociation:0x007ffa464a7480 @target=[], @reflection=#<ActiveRecord::Reflection::AssociationReflection:0x007ffa46855820 @macro=:has_many, @name=:columns, @options={:order=>"position", :extend=>[]}, @active_record=User(14 columns), @plural_name="columns", @collection=true, @class_name="Column", @klass=Column(3 columns), @foreign_key="user_id", @active_record_primary_key="id", @type=nil>, @owner=#<User id: 8, email: "[email protected]", verified: true, security_token: "1b8ed73c7e8600a82bd8824d2a5b243063665b9a", token_expiry: "2010-02-07 20:44:20", created_at: "2006-12-07 17:39:27", updated_at: "2011-09-24 16:28:41", logged_in_at: "2010-02-07 00:45:11", deleted: false, delete_after: nil, uuid: "b8281ad06871012903870016cba2a0b6", iphone_verified: false, tab_links: false, password_digest: "52eb36143a4c782e202a62d5b7d64f7ce4e6ec11">, @updated=false, @loaded=false, @association_scope=nil, @proxy=[#<Column id: 3635, user_id: 8, position: 1>, #<Column id: 10, user_id: 8, position: 2>], @stale_state=nil> | ||
|
||
|
||
|
||
* Use appraisal for rails dep testing. | ||
https://github.com/thoughtbot/appraisal | ||
|
||
|
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,24 @@ | ||
module GroupedScope | ||
module Arish | ||
module Associations | ||
class AssociationScope < ActiveRecord::Associations::AssociationScope | ||
|
||
|
||
private | ||
|
||
def add_constraints(scope) | ||
super(scope).tap do |s| | ||
case reflection.macro | ||
when :has_many | ||
|
||
# s.where_values | ||
# [#<Arel::Nodes::Equality:0x007ffa41eb9c70 @left=#<struct Arel::Attributes::Attribute relation=#<Arel::Table:0x007ffa41eb9f40 @name="columns", @engine=ActiveRecord::Base, @columns=nil, @aliases=[], @table_alias=nil, @primary_key=nil>, name="user_id">, @right=8>] | ||
|
||
end if reflection.grouped_scope? | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
lib/grouped_scope/arish/associations/builder/grouped_scope.rb
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,42 @@ | ||
module GroupedScope | ||
module Arish | ||
module Associations | ||
module Builder | ||
class GroupedScope | ||
|
||
class << self | ||
|
||
def build(model, *associations) | ||
association_names.each do |name| | ||
ungrouped_reflection = find_ungrouped_reflection(model, name) | ||
grouped_reflection = model.send ungrouped_reflection.macro, :"grouped_scope_#{name}", ungrouped_reflection.options | ||
grouped_reflection.grouped_scope = true | ||
end | ||
define_grouped_scope_reader | ||
end | ||
|
||
private | ||
|
||
def define_grouped_scope_reader | ||
model.send(:define_method, :group) do | ||
@group ||= GroupedScope::SelfGroupping.new(self) | ||
end | ||
end | ||
|
||
def find_ungrouped_reflection(model, name) | ||
reflection = model.reflections[name.to_sym] | ||
if reflection.blank? || [:has_many, :has_and_belongs_to_many].exclude?(reflection.macro) | ||
msg = "Cannot create a group scope for #{name.inspect}. Either the reflection is blank or not supported." + | ||
"Make sure to call grouped_scope after the association you are trying to extend has been defined." | ||
raise ArgumentError, msg | ||
end | ||
reflection | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
lib/grouped_scope/arish/associations/collection_association.rb
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,23 @@ | ||
module GroupedScope | ||
module Arish | ||
module Associations | ||
module CollectionAssociation | ||
|
||
extend ActiveSupport::Concern | ||
|
||
module InstanceMethods | ||
|
||
def association_scope | ||
if klass | ||
@association_scope ||= GroupedScope::Associations::AssociationScope.new(self).scope | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Associations::CollectionAssociation.send :include, GroupedScope::Arish::Associations::CollectionAssociation |
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,24 @@ | ||
module GroupedScope | ||
module Arish | ||
module Base | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :grouped_scopes, :instance_reader => false, :instance_writer => false | ||
self.grouped_scopes = {} | ||
end | ||
|
||
module ClassMethods | ||
|
||
def grouped_scope(*association_names) | ||
GroupedScope::Arish::Associations::Builder::GroupedScope.build(self, *association_names) | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Base.send :include, GroupedScope::Arish::Base |
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,18 @@ | ||
module GroupedScope | ||
module Arish | ||
module Reflection | ||
module AssociationReflection | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
attr_accessor :grouped_scope | ||
alias :grouped_scope? :grouped_scope | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Reflection::AssociationReflection.send :include, GroupedScope::Arish::Reflection::AssociationReflection |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.