Skip to content

Commit

Permalink
Scopes - fixes to problems with automatic scopes in the new named_sco…
Browse files Browse the repository at this point in the history
…pe world
  • Loading branch information
tslocke committed Jun 26, 2008
1 parent 507b5de commit fde301f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hobo/lib/hobo/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def method_missing(name, *args, &block)
# FIXME: Do we need this now?
call_method_chain(name, args, &block)
elsif create_automatic_scope(name)
send(name, *args, &block)
send(name.to_sym, *args, &block)
else
super(name.to_sym, *args, &block)
end
Expand Down
1 change: 1 addition & 0 deletions hobo/lib/hobo/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ def create_belongs_to_reflection(association_id, options)

ActiveRecord::Associations::AssociationProxy.send(:include, Hobo::Scopes::AssociationProxyExtensions)
ActiveRecord::Associations::HasManyThroughAssociation.send(:include, Hobo::Scopes::HasManyThroughAssociationExtensions)
require "hobo/scopes/named_scope_extensions"
10 changes: 4 additions & 6 deletions hobo/lib/hobo/scopes/automatic_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def create_scope
{ :conditions => ["#{column_sql(col)} NOT LIKE ?", "%#{str}"] }
end

# published
# published (a boolean column)
elsif (col = column(name)) && (col.type == :boolean)

def_scope :conditions => ["#{column_sql(col)} = ?", true]
Expand Down Expand Up @@ -210,9 +210,7 @@ def create_scope
case name

when "by_most_recent"
def_scope do
{ :order => "#{@klass.table_name}.created_at DESC" }
end
def_scope :order => "#{@klass.table_name}.created_at DESC"

when "recent"
def_scope do |*args|
Expand Down Expand Up @@ -297,7 +295,7 @@ def find_if_named(reflection, string_or_record)
name = string_or_record
reflection.klass.named(name)
else
string_or_record
string_or_record # a record
end
end

Expand All @@ -313,7 +311,7 @@ def reflection(name)


def def_scope(options={}, &block)
@klass.send(:named_scope, name, block || options)
@klass.named_scope(name.to_sym, block || options)
end


Expand Down
23 changes: 23 additions & 0 deletions hobo/lib/hobo/scopes/named_scope_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ActiveRecord
module NamedScope
class Scope

def respond_to?(method)
super || scopes.include?(method) || proxy_scope.respond_to?(method)
end

private

def method_missing(method, *args, &block)
if scopes.include?(method) || (!respond_to?(method) && proxy_scope.create_automatic_scope(method))
scopes[method].call(self, *args)
else
with_scope :find => proxy_options do
proxy_scope.send(method, *args, &block)
end
end
end

end
end
end

0 comments on commit fde301f

Please sign in to comment.