Permalink
Browse files

fixed table and column name quoting

  • Loading branch information...
1 parent e142b61 commit 078e4ef9a9df8f71985b08d4c320c81ff3fd7b41 Tomasz Mazur committed Oct 6, 2009
Showing with 45 additions and 46 deletions.
  1. +45 −46 lib/trixy_scopes.rb
View
@@ -1,22 +1,17 @@
module TrixyScopes
-
- # TODO
- # latest, earliest, random - limit should be optional
-
def self.included(klass)
klass.class_eval do
return unless klass.table_exists?
- table_name = klass.quoted_table_name
- primary_key = klass.primary_key
connection = ActiveRecord::Base.connection
adapter = connection.adapter_name
- quoted_primary_key = connection.quote_table_name("#{table_name}.#{primary_key}")
-
-
+
+ table_name = klass.table_name
+ quoted_table_name = klass.quoted_table_name
+
named_scope :random, :order => adapter == "SQLite" ? "RANDOM()" : "RAND()"
- named_scope :order_by, lambda {|sort_by, sort_order| {:order => "#{sort_by} #{sort_order}" } }
+ named_scope :order_by, lambda {|sort_by, sort_order| { :order => "#{sort_by} #{sort_order}" } }
named_scope :inner_join, lambda { |reflection| { :joins => reflection } }
named_scope :left_join, lambda { |reflection|
r = klass.reflections[reflection]
@@ -28,69 +23,73 @@ def self.included(klass)
}
}
named_scope :limit, lambda { |limit| { :limit => limit } }
- named_scope :latest, lambda { |limit| { :limit => limit, :order => "#{table_name}.`created_at` DESC" } }
- named_scope :earliest, lambda { |limit| { :limit => limit, :order => "#{table_name}.`created_at` ASC" } }
- named_scope :before, lambda { |datetime| { :conditions => ["#{table_name}.`created_at` < ?", datetime] } }
- named_scope :after, lambda { |datetime| { :conditions => ["#{table_name}.`created_at` > ?", datetime] } }
+ named_scope :latest, lambda { |limit| { :limit => limit, :order => "#{quoted_table_name}.`created_at` DESC" } }
+ named_scope :earliest, lambda { |limit| { :limit => limit, :order => "#{quoted_table_name}.`created_at` ASC" } }
+ named_scope :before, lambda { |datetime| { :conditions => ["#{quoted_table_name}.`created_at` < ?", datetime] } }
+ named_scope :after, lambda { |datetime| { :conditions => ["#{quoted_table_name}.`created_at` > ?", datetime] } }
klass.columns.each do |column|
- attribute = column.name
- quoted_column_name = connection.quote_table_name("#{klass.table_name}.#{attribute}")
+ column_name = column.name
+
+ quoted_column_name = [quoted_table_name, connection.quote_column_name(column_name)].join(".")
- named_scope "#{attribute}_is", lambda { |value| { :conditions => { attribute => value } } }
- named_scope "#{attribute}_is_not", lambda { |value| { :conditions => ["#{quoted_column_name} != ?", value] } }
- named_scope "#{attribute}_is_nil", :conditions => { attribute => nil }
- named_scope "#{attribute}_is_not_nil", :conditions => "#{quoted_column_name} IS NOT NULL"
+ named_scope "#{column_name}_is", lambda { |value| { :conditions => { column_name => value } } }
+ named_scope "#{column_name}_is_not", lambda { |value| { :conditions => ["#{quoted_column_name} != ?", value] } }
+ named_scope "#{column_name}_is_nil", :conditions => { column_name => nil }
+ named_scope "#{column_name}_is_not_nil", :conditions => "#{quoted_column_name} IS NOT NULL"
- named_scope "#{attribute.pluralize}_are", lambda { |*ids| { :conditions => { attribute => ids.flatten } } }
- named_scope "#{attribute.pluralize}_are_not", lambda { |*ids| { :conditions => ["#{quoted_column_name} NOT IN(?)", ids.flatten] } }
+ named_scope "#{column_name.pluralize}_are", lambda { |*ids| { :conditions => { column_name => ids.flatten } } }
+ named_scope "#{column_name.pluralize}_are_not", lambda { |*ids| { :conditions => ["#{quoted_column_name} NOT IN(?)", ids.flatten] } }
unless column.type == :boolean
- named_scope "#{attribute}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
- named_scope "#{attribute}_not_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
+ named_scope "#{column_name}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
+ named_scope "#{column_name}_not_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
end
unless column.type == :boolean || column.type == :text
- named_scope "#{attribute}_in", lambda { |*elements| { :conditions => { attribute => elements.flatten } } }
- named_scope "#{attribute}_not_in", lambda { |*elements| { :conditions => ["#{quoted_column_name} NOT IN(?)", elements.flatten] } }
+ named_scope "#{column_name}_in", lambda { |*elements| { :conditions => { column_name => elements.flatten } } }
+ named_scope "#{column_name}_not_in", lambda { |*elements| { :conditions => ["#{quoted_column_name} NOT IN(?)", elements.flatten] } }
end
if column.type == :datetime
- named_scope "#{attribute}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
- named_scope "#{attribute}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
+ named_scope "#{column_name}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
+ named_scope "#{column_name}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
- if attribute.last(3) == "_at"
- attribute_alias = attribute.chomp("_at")
- named_scope "#{attribute_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
- named_scope "not_#{attribute_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
- named_scope "#{attribute_alias}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
- named_scope "#{attribute_alias}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
+ if column_name.last(3) == "_at"
+ column_name_alias = column_name.chomp("_at")
+ named_scope "#{column_name_alias}_today", lambda { { :conditions => ["#{quoted_column_name} > ?", Time.zone.now.beginning_of_day] } }
+ named_scope "#{column_name_alias}_yesterday", lambda { { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", Time.zone.now.yesterday.beginning_of_day, Time.zone.now.yesterday.end_of_day] } }
+
+ named_scope "#{column_name_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
+ named_scope "not_#{column_name_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
+ named_scope "#{column_name_alias}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
+ named_scope "#{column_name_alias}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
end
end
if column.type == :integer || column.type == :float
- named_scope "#{attribute}_greater_than", lambda { |value| { :conditions => ["#{quoted_column_name} > ?", value] } }
- named_scope "#{attribute}_greater_or_equal_to", lambda { |value| { :conditions => ["#{quoted_column_name} >= ?", value] } }
- named_scope "#{attribute}_less_than", lambda { |value| { :conditions => ["#{quoted_column_name} < ?", value] } }
- named_scope "#{attribute}_less_or_equal_to", lambda { |value| { :conditions => ["#{quoted_column_name} <= ?", value] } }
+ named_scope "#{column_name}_greater_than", lambda { |value| { :conditions => ["#{quoted_column_name} > ?", value] } }
+ named_scope "#{column_name}_greater_or_equal_to", lambda { |value| { :conditions => ["#{quoted_column_name} >= ?", value] } }
+ named_scope "#{column_name}_less_than", lambda { |value| { :conditions => ["#{quoted_column_name} < ?", value] } }
+ named_scope "#{column_name}_less_or_equal_to", lambda { |value| { :conditions => ["#{quoted_column_name} <= ?", value] } }
end
if column.type == :string
- named_scope "#{attribute}_starts_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "#{string}%"] } }
- named_scope "#{attribute}_ends_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}"] } }
- named_scope "#{attribute}_includes", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}%"] } }
- named_scope "#{attribute}_matches", lambda { |regexp| { :conditions => ["#{quoted_column_name} REGEXP ?", regexp] } }
+ named_scope "#{column_name}_starts_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "#{string}%"] } }
+ named_scope "#{column_name}_ends_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}"] } }
+ named_scope "#{column_name}_includes", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}%"] } }
+ named_scope "#{column_name}_matches", lambda { |regexp| { :conditions => ["#{quoted_column_name} REGEXP ?", regexp] } }
end
if column.type == :text || column.type == :string
- named_scope "#{attribute}_like", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", string] } }
- named_scope "#{attribute}_not_like", lambda { |string| { :conditions => ["#{quoted_column_name} NOT LIKE ?", string] } }
+ named_scope "#{column_name}_like", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", string] } }
+ named_scope "#{column_name}_not_like", lambda { |string| { :conditions => ["#{quoted_column_name} NOT LIKE ?", string] } }
end
if column.type == :boolean
- named_scope "#{attribute}", :conditions => { attribute => true }
- named_scope "not_#{attribute}", :conditions => { attribute => false }
+ named_scope "#{column_name}", :conditions => { column_name => true }
+ named_scope "not_#{column_name}", :conditions => { column_name => false }
end
end

0 comments on commit 078e4ef

Please sign in to comment.