Skip to content

Commit

Permalink
Current work.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Dec 3, 2011
1 parent d4f3633 commit 50f03a2
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 269 deletions.
59 changes: 59 additions & 0 deletions TODO
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

Expand Down
10 changes: 4 additions & 6 deletions lib/grouped_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
require 'grouped_scope/errors'
require 'grouped_scope/grouping'
require 'grouped_scope/self_grouping'
require 'grouped_scope/association_reflection'
require 'grouped_scope/class_methods'
require 'grouped_scope/has_many_association'
require 'grouped_scope/has_many_through_association'
require 'grouped_scope/core_ext'
require 'grouped_scope/version'

require 'grouped_scope/arish/associations/association_scope'
require 'grouped_scope/arish/associations/collection_association'
require 'grouped_scope/arish/base'

24 changes: 24 additions & 0 deletions lib/grouped_scope/arish/associations/association_scope.rb
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 lib/grouped_scope/arish/associations/builder/grouped_scope.rb
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 lib/grouped_scope/arish/associations/collection_association.rb
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
24 changes: 24 additions & 0 deletions lib/grouped_scope/arish/base.rb
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
18 changes: 18 additions & 0 deletions lib/grouped_scope/arish/reflection.rb
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
54 changes: 0 additions & 54 deletions lib/grouped_scope/association_reflection.rb

This file was deleted.

32 changes: 0 additions & 32 deletions lib/grouped_scope/class_methods.rb

This file was deleted.

29 changes: 0 additions & 29 deletions lib/grouped_scope/core_ext.rb

This file was deleted.

5 changes: 2 additions & 3 deletions lib/grouped_scope/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ class NoGroupIdError < GroupedScopeError #:nodoc:
def initialize(owner) ; @owner = owner ; end
def message ; %|The #{@owner.class} class does not have a "group_id" attribute.| ; end
end


end

end
9 changes: 0 additions & 9 deletions lib/grouped_scope/grouping.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/grouped_scope/has_many_association.rb

This file was deleted.

Loading

0 comments on commit 50f03a2

Please sign in to comment.