Skip to content

Commit

Permalink
Goodbye def_scope (Rails now has named_scope). Made automatic scopes …
Browse files Browse the repository at this point in the history
…work with named_scope
  • Loading branch information
tslocke committed Jun 17, 2008
1 parent a8dc5de commit ebfbdd6
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 225 deletions.
1 change: 0 additions & 1 deletion hobo/lib/hobo/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def self.included(base)
end

class << base
alias_method_chain :has_many, :defined_scopes
alias_method_chain :has_many, :join_record_management
alias_method_chain :belongs_to, :creator_metadata
alias_method_chain :attr_accessor, :creator_metadata
Expand Down
48 changes: 0 additions & 48 deletions hobo/lib/hobo/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module Scopes

def self.included_in_class(base)
base.extend(ClassMethods)

class << base
alias_method_chain :has_many, :defined_scopes
end
end

module ClassMethods
Expand All @@ -16,50 +12,6 @@ module ClassMethods

include ApplyScopes

def defined_scopes
@defined_scopes ||= {}
end


def def_scope(name, scope=nil, &block)
defined_scopes[name.to_sym] = block || scope

meta_def(name) do |*args|
ScopedProxy.new(self, block ? block.call(*args) : scope)
end
end


def apply_scopes(scopes)
result = self
scopes.each_pair do |scope, arg|
if arg.is_a?(Array)
result = result.send(scope, *arg) unless arg.first.blank?
else
result = result.send(scope, arg) unless arg.blank?
end
end
result
end


def alias_scope(new_name, old_name)
metaclass.send(:alias_method, new_name, old_name)
defined_scopes[new_name] = defined_scopes[old_name]
end


def has_many_with_defined_scopes(name, options={}, &block)
if options.has_key?(:extend) || block
# Normal has_many
has_many_without_defined_scopes(name, options, &block)
else
options[:extend] = Hobo::Scopes::DefinedScopeProxyExtender
has_many_without_defined_scopes(name, options, &block)
end
end


# --- monkey-patches to allow :scope key on has_many, has_one and belongs_to ---

def create_has_many_reflection(association_id, options, &extension)
Expand Down
2 changes: 1 addition & 1 deletion hobo/lib/hobo/scopes/association_proxy_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Add support for :scope => :my_scope to has_many and :belongs_to
# Add support for :scope => :my_scope to has_many and belongs_to

module Hobo

Expand Down
18 changes: 7 additions & 11 deletions hobo/lib/hobo/scopes/automatic_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ module AutomaticScopes

def create_automatic_scope(name)
ScopeBuilder.new(self, name).create_scope
rescue ActiveRecord::StatementInvalid
rescue ActiveRecord::StatementInvalid => e
# Problem with the database? Don't try to create automatic scopes
ActiveRecord::Base.logger.warn "!! Database exception during hobo auto-scope creation -- continuing automatic scopes"
ActiveRecord::Base.logger.warn "!! #{e.to_s}"
false
end

Expand Down Expand Up @@ -157,16 +159,12 @@ def create_scope
# published
elsif (col = column(name)) && (col.type == :boolean)

def_scope do
{ :conditions => ["#{column_sql(col)} = ?", true] }
end
def_scope :conditions => ["#{column_sql(col)} = ?", true]

# not_published
elsif name =~ /^not_(.*)$/ && (col = column($1)) && (col.type == :boolean)

def_scope do
{ :conditions => ["#{column_sql(col)} <> ?", true] }
end
def_scope :conditions => ["#{column_sql(col)} <> ?", true]

# published_before(time)
elsif name =~ /^(.*)_before$/ && (col = column("#{$1}_at")) && col.type.in?([:date, :datetime, :time, :timestamp])
Expand All @@ -192,9 +190,7 @@ def create_scope
# active (a lifecycle state)
elsif @klass.has_lifecycle? && name.in?(@klass::Lifecycle.state_names)

def_scope do
{ :conditions => ["#{@klass.table_name}.#{@klass::Lifecycle.state_field} = ?", name] }
end
def_scope :conditions => ["#{@klass.table_name}.#{@klass::Lifecycle.state_field} = ?", name]

# self is / is not
elsif name == "is"
Expand Down Expand Up @@ -312,7 +308,7 @@ def reflection(name)


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


Expand Down
90 changes: 0 additions & 90 deletions hobo/lib/hobo/scopes/defined_scope_proxy_extender.rb

This file was deleted.

18 changes: 0 additions & 18 deletions hobo/lib/hobo/scopes/scope_reflection.rb

This file was deleted.

55 changes: 0 additions & 55 deletions hobo/lib/hobo/scopes/scoped_proxy.rb

This file was deleted.

2 changes: 1 addition & 1 deletion hobo/rails_generators/hobo/hobo_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def manifest
if options[:add_routes]
routes_path = File.join(RAILS_ROOT, "config/routes.rb")

hobo_require = "gem 'hobo'\nrequire 'hobo'\n"
hobo_require = "(gem 'hobo'; require 'hobo') unless defined?(Hobo)\n"
route = " Hobo.add_routes(map)\n"

route_src = File.read(routes_path)
Expand Down

0 comments on commit ebfbdd6

Please sign in to comment.