hobo_files/plugin/lib/hobo/model_queries.rb in hobo-0.6.1 vs hobo_files/plugin/lib/hobo/model_queries.rb in hobo-0.6.2
- old
+ new
@@ -25,11 +25,28 @@
def not_in(association)
not_(is_in(association))
end
+
+
+ def sql(s)
+ WhereFragment.new(s)
+ end
+
+ def block(b)
+ b && instance_eval(&b)
+ end
+
+
+ def all?(*args)
+ args.inject { |m, a| m & a }
+ end
+ def any?(*args)
+ args.inject { |m, a| m | a }
+ end
def method_missing(name, *args)
check_column = proc do |col|
raise HoboError.new("no such column '#{col}' in query") unless
model.columns.every(:name).include? col
@@ -44,33 +61,37 @@
else
check_column[field]
val = args[0]
end
return (if val.nil?
- WhereFragment.new("#{field} IS NULL")
+ WhereFragment.new("#{_query_table}.#{field} IS NULL")
else
- WhereFragment.new("#{field} = ?", val)
+ WhereFragment.new("#{_query_table}.#{field} = ?", val)
end)
end
m, field = *name.to_s.match(/^(.*)_contains$/)
if m
check_column[field]
- return WhereFragment.new("#{field} like ?", "%#{args[0]}%")
+ return WhereFragment.new("#{_query_table}.#{field} like ?", "%#{args[0]}%")
end
m, field = *name.to_s.match(/^(.*)_starts$/)
if m
check_column[field]
- return WhereFragment.new("#{field} like ?", "#{args[0]}%")
+ return WhereFragment.new("#{_query_table}.#{field} like ?", "#{args[0]}%")
end
m, field = *name.to_s.match(/^(.*)_ends$/)
if m
check_column[field]
- return WhereFragment.new("#{field} like ?", "%#{args[0]}")
+ return WhereFragment.new("#{_query_table}.#{field} like ?", "%#{args[0]}")
end
+
+ if (refl = model.reflections[name.to_sym]) && refl.macro == :belongs_to
+ return ModelQueries.new(refl.klass)
+ end
return WhereFragment.new(@model.send(name, *args))
end
def _association_finder_sql(assoc)
@@ -83,9 +104,13 @@
table = "#{model.table_name}"
"id in (select #{table}.id from #{table} #{joins} where #{conditions})"
else
Object.instance_method(:instance_variable_get).bind(assoc).call("@finder_sql")
end
+ end
+
+ def _query_table
+ model.table_name
end
end
end