hobo_files/plugin/lib/hobo/scopes/automatic_scopes.rb in hobo-0.7.3 vs hobo_files/plugin/lib/hobo/scopes/automatic_scopes.rb in hobo-0.7.4
- old
+ new
@@ -150,21 +150,21 @@
def_scope do |str|
{ :conditions => ["#{column_sql(col)} NOT LIKE ?", "%#{str}"] }
end
# published
- elsif (col = column($1)) && (col.type == :boolean)
+ elsif (col = column(name)) && (col.type == :boolean)
def_scope do
- { :conditions => "#{column_sql(col)} = 1" }
+ { :conditions => "#{column_sql(col)}" }
end
# not_published
- elsif (col = column($1)) && (col.type == :boolean)
+ elsif name =~ /^not_(.*)$/ && (col = column($1)) && (col.type == :boolean)
def_scope do
- { :conditions => "#{column_sql(col)} <> 1" }
+ { :conditions => "NOT #{column_sql(col)}" }
end
# published_before(time)
elsif name =~ /^(.*)_before$/ && (col = column("#{$1}_at")) && col.type.in?([:date, :datetime, :time, :timestamp])
@@ -198,17 +198,44 @@
when "limit"
def_scope do |count|
{ :limit => count }
end
-
+
when "order_by"
+ klass = @klass
def_scope do |*args|
field, asc = args
- { :order => "#{field} #{asc._?.upcase}" }
+ type = klass.attr_type(field)
+ if type.respond_to?(:table_name) && (name = type.name_attribute)
+ include = field
+ colspec = "#{type.table_name}.#{name}"
+ else
+ colspec = "#{klass.table_name}.#{field}"
+ end
+ { :order => "#{colspec} #{asc._?.upcase}", :include => include }
end
+
+ when "include"
+ def_scope do |inclusions|
+ { :include => inclusions }
+ end
+
+ when "search"
+ def_scope do |query, *fields|
+ words = query.split
+ args = []
+ word_queries = words.map do |word|
+ field_query = '(' + fields.map { |field| "(#{@klass.table_name}.#{field} like ?)" }.join(" OR ") + ')'
+ args += ["%#{word}%"] * fields.length
+ field_query
+ end
+
+ { :conditions => [word_queries.join(" OR ")] + args }
+ end
+
else
matched_scope = false
end
end
@@ -234,10 +261,10 @@
related = reflection.klass
foreign_key = reflection.primary_key_name
"EXISTS (SELECT * FROM #{related.table_name} " +
"WHERE #{related.table_name}.#{foreign_key} = #{owner_primary_key} AND " +
- "#{related.table_name}.#{related.primary_key} = ?"
+ "#{related.table_name}.#{related.primary_key} = ?)"
end
end
def find_if_named(reflection, string_or_record)