Skip to content

Commit

Permalink
removed :including, deprecated :order_by and normalized :create_scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Nov 28, 2010
1 parent 6456009 commit f1c11d9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 97 deletions.
12 changes: 4 additions & 8 deletions hobo/doctests/hobo/scopes.rdoctest
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,14 @@ Gives the N most recent items:

## include

DEPRECATED: Automatic scope :include has been deprecated: use :including instead.
DEPRECATED: Automatic scope :include has been deprecated: use :includes instead.

## including
## includes

Adding the including function to your query chain has the same effect as
the `:include` option to the `find` method.

>> Person.search("B", :name).including(:friends).*.name # test LH#839
>> Person.search("B", :name).includes(:friends).*.name # test LH#839
=> ["Bryan", "Bethany"]
.hidden

>> Person.including(:friends).*.name
>> Person.includes(:friends).*.name
=> ["Bryan", "Bethany"]

## search
Expand Down
168 changes: 79 additions & 89 deletions hobo/lib/hobo/model/scopes/automatic_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def initialize(klass, name)
def create_scope(check_only=false)
matched_scope = true


case
# --- Association Queries --- #

# with_players(player1, player2)
if name =~ /^with_(.*)/ && (refl = reflection($1))
when name =~ /^with_(.*)/ && (refl = reflection($1))
return true if check_only

def_scope do |*records|
Expand All @@ -49,7 +49,7 @@ def create_scope(check_only=false)
end

# with_player(a_player)
elsif name =~ /^with_(.*)/ && (refl = reflection($1.pluralize))
when name =~ /^with_(.*)/ && (refl = reflection($1.pluralize))
return true if check_only

exists_sql = exists_sql_condition(refl)
Expand All @@ -59,7 +59,7 @@ def create_scope(check_only=false)
end

# any_of_players(player1, player2)
elsif name =~ /^any_of_(.*)/ && (refl = reflection($1))
when name =~ /^any_of_(.*)/ && (refl = reflection($1))
return true if check_only

def_scope do |*records|
Expand All @@ -73,7 +73,7 @@ def create_scope(check_only=false)
end

# without_players(player1, player2)
elsif name =~ /^without_(.*)/ && (refl = reflection($1))
when name =~ /^without_(.*)/ && (refl = reflection($1))
return true if check_only

def_scope do |*records|
Expand All @@ -87,7 +87,7 @@ def create_scope(check_only=false)
end

# without_player(a_player)
elsif name =~ /^without_(.*)/ && (refl = reflection($1.pluralize))
when name =~ /^without_(.*)/ && (refl = reflection($1.pluralize))
return true if check_only

exists_sql = exists_sql_condition(refl)
Expand All @@ -97,7 +97,7 @@ def create_scope(check_only=false)
end

# team_is(a_team)
elsif name =~ /^(.*)_is$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
when name =~ /^(.*)_is$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
return true if check_only

if refl.options[:polymorphic]
Expand All @@ -113,7 +113,7 @@ def create_scope(check_only=false)
end

# team_is_not(a_team)
elsif name =~ /^(.*)_is_not$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
when name =~ /^(.*)_is_not$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
return true if check_only

if refl.options[:polymorphic]
Expand All @@ -132,116 +132,116 @@ def create_scope(check_only=false)
# --- Column Queries --- #

# name_is(str)
elsif name =~ /^(.*)_is$/ && (col = column($1))
when name =~ /^(.*)_is$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} = ?", str
end

# name_is_not(str)
elsif name =~ /^(.*)_is_not$/ && (col = column($1))
when name =~ /^(.*)_is_not$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} <> ?", str
end

# name_contains(str)
elsif name =~ /^(.*)_contains$/ && (col = column($1))
when name =~ /^(.*)_contains$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} LIKE ?", "%#{str}%"
end

# name_does_not_contain
elsif name =~ /^(.*)_does_not_contain$/ && (col = column($1))
when name =~ /^(.*)_does_not_contain$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} NOT LIKE ?", "%#{str}%"
end

# name_starts(str)
elsif name =~ /^(.*)_starts$/ && (col = column($1))
when name =~ /^(.*)_starts$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} LIKE ?", "#{str}%"
end

# name_does_not_start
elsif name =~ /^(.*)_does_not_start$/ && (col = column($1))
when name =~ /^(.*)_does_not_start$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} NOT LIKE ?", "#{str}%"
end

# name_ends(str)
elsif name =~ /^(.*)_ends$/ && (col = column($1))
when name =~ /^(.*)_ends$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} LIKE ?", "%#{str}"
end

# name_does_not_end(str)
elsif name =~ /^(.*)_does_not_end$/ && (col = column($1))
when name =~ /^(.*)_does_not_end$/ && (col = column($1))
return true if check_only

def_scope do |str|
@klass.where "#{column_sql(col)} NOT LIKE ?", "%#{str}"
end

# published (a boolean column)
elsif (col = column(name)) && (col.type == :boolean)
when (col = column(name)) && (col.type == :boolean)
return true if check_only

def_scope do
@klass.where "#{column_sql(col)} = ?", true
end

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

def_scope do
@klass.where "#{column_sql(col)} <> ?", true
end

# published_before(time)
elsif name =~ /^(.*)_before$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
when name =~ /^(.*)_before$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
return true if check_only

def_scope do |time|
@klass.where "#{column_sql(col)} < ?", time
end

# published_after(time)
elsif name =~ /^(.*)_after$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
when name =~ /^(.*)_after$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
return true if check_only

def_scope do |time|
@klass.where "#{column_sql(col)} > ?", time
end

# published_between(time1, time2)
elsif name =~ /^(.*)_between$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
when name =~ /^(.*)_between$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
return true if check_only

def_scope do |time1, time2|
@klass.where "#{column_sql(col)} >= ? AND #{column_sql(col)} <= ?", time1, time2
end

# active (a lifecycle state)
elsif @klass.has_lifecycle? && name.to_sym.in?(@klass::Lifecycle.state_names)
when @klass.has_lifecycle? && name.to_sym.in?(@klass::Lifecycle.state_names)
return true if check_only

if @klass::Lifecycle.state_names.length == 1
# nothing to check for - create a dummy scope
def_scope { @klass.where '' }
def_scope { @klass.scoped }
true
else
def_scope do
Expand All @@ -250,104 +250,94 @@ def create_scope(check_only=false)
end

# self is / is not
elsif name == "is"
when name == "is"
return true if check_only

def_scope do |record|
@klass.where "#{@klass.table_name}.#{@klass.primary_key} = ?", record
end

elsif name == "is_not"
when name == "is_not"
return true if check_only

def_scope do |record|
@klass.where "#{@klass.table_name}.#{@klass.primary_key} <> ?", record
end

else

case name

when "by_most_recent"
return true if check_only
when name == "by_most_recent"
return true if check_only

def_scope do
@klass.order "#{@klass.table_name}.created_at DESC"
end
def_scope do
@klass.order "#{@klass.table_name}.created_at DESC"
end

when "recent"
return true if check_only
when name == "recent"
return true if check_only

if "created_at".in?(@klass.columns.*.name)
def_scope do |*args|
count = args.first || 6
@klass.order("#{@klass.table_name}.created_at DESC").limit(count)
end
else
def_scope do |*args|
count = args.first || 6
limit(count)
end
if "created_at".in?(@klass.columns.*.name)
def_scope do |*args|
count = args.first || 6
@klass.order("#{@klass.table_name}.created_at DESC").limit(count)
end

when "order_by"
return true if check_only

klass = @klass
else
def_scope do |*args|
field, asc = args
type = klass.attr_type(field)
if type.nil? #a virtual attribute from an SQL alias, e.g., 'total' from 'COUNT(*) AS total'
colspec = "#{field}" # don't prepend the table name
elsif type.respond_to?(:name_attribute) && (name = type.name_attribute)
include = field
colspec = "#{type.table_name}.#{name}"
else
colspec = "#{klass.table_name}.#{field}"
end
@klass.includes(include).order("#{colspec} #{asc._?.upcase}")
count = args.first || 6
limit(count)
end
end

when name == "order_by"
# DEPRECATED: use :order instead.
Rails.logger.warn "Automatic scope :order_by has been deprecated: use :order instead."
return true if check_only

when "include"
# DEPRECATED: it clashes with Module.include when called on an ActiveRecord::Relation
# after a scope chain, if you didn't call it on the class itself first
Rails.logger.warn "Automatic scope :include has been deprecated: use :including instead."
return true if check_only

def_scope do |inclusions|
@klass.includes(inclusions)
klass = @klass
def_scope do |*args|
field, asc = args
type = klass.attr_type(field)
if type.nil? #a virtual attribute from an SQL alias, e.g., 'total' from 'COUNT(*) AS total'
colspec = "#{field}" # don't prepend the table name
elsif type.respond_to?(:name_attribute) && (name = type.name_attribute)
include = field
colspec = "#{type.table_name}.#{name}"
else
colspec = "#{klass.table_name}.#{field}"
end
@klass.includes(include).order("#{colspec} #{asc._?.upcase}")
end

when "including"
return true if check_only

def_scope do |inclusions|
@klass.includes(inclusions)
end
when name == "include"
# DEPRECATED: it clashes with Module.include when called on an ActiveRecord::Relation
# after a scope chain, if you didn't call it on the class itself first
Rails.logger.warn "Automatic scope :include has been deprecated: use :includes instead."
return true if check_only

when "search"
return true if check_only
def_scope do |inclusions|
@klass.includes(inclusions)
end

def_scope do |query, *fields|
match_keyword = ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"
when name == "search"
return true if check_only

words = query.split
args = []
word_queries = words.map do |word|
field_query = '(' + fields.map { |field| "(#{@klass.table_name}.#{field} #{match_keyword} ?)" }.join(" OR ") + ')'
args += ["%#{word}%"] * fields.length
field_query
end
def_scope do |query, *fields|
match_keyword = ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"

@klass.where *([word_queries.join(" AND ")] + args)
words = query.split
args = []
word_queries = words.map do |word|
field_query = '(' + fields.map { |field| "(#{@klass.table_name}.#{field} #{match_keyword} ?)" }.join(" OR ") + ')'
args += ["%#{word}%"] * fields.length
field_query
end

else
matched_scope = false
@klass.where *([word_queries.join(" AND ")] + args)
end

else
matched_scope = false
end

matched_scope
end

Expand Down

0 comments on commit f1c11d9

Please sign in to comment.