@@ -5,17 +5,18 @@ def self.types
55 [ "timestamp" , "datetime" , "date" ] . freeze
66 end
77
8- def self . create_scopes_for_column ( model , column )
9- full_name = "#{ model . quoted_table_name } .#{ column } "
10- short_name = shorten_column_name ( column )
11- append_scope ( model , :"#{ short_name } _to" , lambda { |date | where ( "#{ full_name } <= ?" , date ) } )
12- append_scope ( model , :"#{ short_name } _from" , lambda { |date | where ( "#{ full_name } >= ?" , date ) } )
13- append_scope ( model , :"#{ short_name } _after" , lambda { |date | where ( "#{ full_name } > ?" , date ) } )
14- append_scope ( model , :"#{ short_name } _before" , lambda { |date | where ( "#{ full_name } < ?" , date ) } )
15- append_scope ( model , :"#{ short_name } _between" , lambda { |from , to | where ( "#{ full_name } BETWEEN ? AND ?" , from , to ) } )
16- append_scope ( model , :"#{ short_name } _not_between" , lambda { |from , to | where ( "#{ full_name } NOT BETWEEN ? AND ?" , from , to ) } )
17- append_scope ( model , :"#{ short_name } _within" , lambda { |from , to | where ( "#{ full_name } > ? AND #{ full_name } < ?" , from , to ) } )
18- append_scope ( model , :"#{ short_name } _not_within" , lambda { |from , to | where ( "#{ full_name } <= ? OR #{ full_name } >= ?" , from , to ) } )
8+ def self . create_scopes_for_column ( model , name )
9+ short_name = shorten_column_name ( name )
10+ column = model . arel_table [ name ]
11+
12+ append_scope ( model , :"#{ short_name } _to" , lambda { |date | where ( column . lteq ( date ) ) } )
13+ append_scope ( model , :"#{ short_name } _from" , lambda { |date | where ( column . gteq ( date ) ) } )
14+ append_scope ( model , :"#{ short_name } _after" , lambda { |date | where ( column . gt ( date ) ) } )
15+ append_scope ( model , :"#{ short_name } _before" , lambda { |date | where ( column . lt ( date ) ) } )
16+ append_scope ( model , :"#{ short_name } _between" , lambda { |from , to | where ( name => from ..to ) } )
17+ append_scope ( model , :"#{ short_name } _not_between" , lambda { |from , to | where . not ( name => from ..to ) } )
18+ append_scope ( model , :"#{ short_name } _within" , lambda { |from , to | where ( column . gt ( from ) ) . where ( column . lt ( to ) ) } )
19+ append_scope ( model , :"#{ short_name } _not_within" , lambda { |from , to | where ( column . lteq ( from ) . or ( column . gteq ( to ) ) ) } )
1920 end
2021
2122 def self . shorten_column_name ( name )
0 commit comments