time_scopes 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,8 +6,8 @@ module TimeScopes
6
6
  module Version
7
7
 
8
8
  MAJOR = 0
9
- MINOR = 0
10
- TINY = 1
9
+ MINOR = 1
10
+ TINY = 0
11
11
 
12
12
  def self.to_s # :nodoc:
13
13
  [MAJOR, MINOR, TINY].join('.')
@@ -22,7 +22,7 @@ spec = Gem::Specification.new do |s|
22
22
  s.has_rdoc = false
23
23
  s.extra_rdoc_files = %w(README.rdoc)
24
24
  s.rdoc_options = %w(--main README.rdoc)
25
- s.summary = "scopes for ActiveRecord 3"
25
+ s.summary = "Useful scopes for ActiveRecord based on Date/DateTime columns"
26
26
  s.author = 'Tomasz Mazur'
27
27
  s.email = 'defkode@gmail.com'
28
28
  s.homepage = 'http://trix.pl'
@@ -1,109 +1,42 @@
1
- module TrixyScopes
1
+ module TimeScopes
2
2
 
3
3
  def self.included(klass)
4
+
4
5
  klass.class_eval do
5
6
  return unless klass.table_exists?
6
-
7
+
7
8
  connection = ActiveRecord::Base.connection
8
9
  adapter = connection.adapter_name
9
-
10
+
10
11
  table_name = klass.table_name
11
12
  quoted_table_name = klass.quoted_table_name
12
-
13
- named_scope :random, :order => adapter == "SQLite" ? "RANDOM()" : "RAND()"
14
- named_scope :order_by, lambda {|sort_by, sort_order| { :order => "#{sort_by} #{sort_order}" } }
15
- named_scope :inner_join, lambda { |reflection| { :joins => reflection } }
16
- named_scope :left_join, lambda { |reflection|
17
- r = klass.reflections[reflection]
18
- reflection_table_name = r.class_name.constantize.quoted_table_name
19
- reflection_primary_key = r.class_name.constantize.primary_key
20
-
21
- {
22
- :joins => "LEFT JOIN #{reflection_table_name} ON #{reflection_table_name}.#{reflection_primary_key} = #{table_name}.#{r.association_foreign_key}"
23
- }
24
- }
25
- named_scope :limit, lambda { |limit| { :limit => limit } }
26
- named_scope :latest, lambda { |limit| { :limit => limit, :order => "#{quoted_table_name}.`created_at` DESC" } }
27
- named_scope :earliest, lambda { |limit| { :limit => limit, :order => "#{quoted_table_name}.`created_at` ASC" } }
28
-
13
+
29
14
  klass.columns.each do |column|
30
15
  column_name = column.name
31
-
16
+
32
17
  quoted_column_name = [quoted_table_name, connection.quote_column_name(column_name)].join(".")
33
-
34
- named_scope "#{column_name}_is", lambda { |value| { :conditions => { column_name => value } } }
35
- named_scope "#{column_name}_is_not", lambda { |value| { :conditions => ["#{quoted_column_name} != ?", value] } }
36
- named_scope "#{column_name}_is_nil", :conditions => { column_name => nil }
37
- named_scope "#{column_name}_is_not_nil", :conditions => "#{quoted_column_name} IS NOT NULL"
38
-
39
- named_scope "#{column_name.pluralize}_are", lambda { |*ids| { :conditions => { column_name => ids.flatten } } }
40
- named_scope "#{column_name.pluralize}_are_not", lambda { |*ids| { :conditions => ["#{quoted_column_name} NOT IN(?)", ids.flatten] } }
41
-
42
- unless column.type == :boolean
43
- named_scope "#{column_name}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
44
- named_scope "#{column_name}_not_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
45
- end
46
-
47
- unless column.type == :boolean || column.type == :text
48
- named_scope "#{column_name}_in", lambda { |*elements| { :conditions => { column_name => elements.flatten } } }
49
- named_scope "#{column_name}_not_in", lambda { |*elements| { :conditions => ["#{quoted_column_name} NOT IN(?)", elements.flatten] } }
50
- end
51
-
52
- if column.name == "expires_at"
53
- named_scope "expired", lambda { { :conditions => ["#{quoted_column_name} < ?", Time.zone.now] } }
54
- named_scope "valid", lambda { { :conditions => ["#{quoted_column_name} >= ?", Time.zone.now] } }
55
- end
56
-
18
+
57
19
  if column.type == :datetime
58
- named_scope "#{column_name}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
59
- named_scope "#{column_name}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
60
-
61
20
  if column_name.last(3) == "_at"
62
- column_name_alias = column_name.chomp("_at")
63
- named_scope "#{column_name_alias}_today", lambda { { :conditions => ["#{quoted_column_name} > ?", Time.zone.now.beginning_of_day] } }
64
- 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] } }
65
-
66
- named_scope "#{column_name_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", from, to] } }
67
- named_scope "not_#{column_name_alias}_between", lambda { |from, to| { :conditions => ["#{quoted_column_name} NOT BETWEEN ? AND ?", from, to] } }
68
- named_scope "#{column_name_alias}_before", lambda { |datetime| { :conditions => ["#{quoted_column_name} < ?", datetime] } }
69
- named_scope "#{column_name_alias}_after", lambda { |datetime| { :conditions => ["#{quoted_column_name} > ?", datetime] } }
70
-
71
- named_scope "#{column_name_alias}_in_last_week", lambda { { :conditions => ["#{quoted_column_name} > ?", 1.week.ago] } }
72
- named_scope "#{column_name_alias}_in_last_month", lambda { { :conditions => ["#{quoted_column_name} > ?", 1.month.ago] } }
73
- named_scope "#{column_name_alias}_in_last_year", lambda { { :conditions => ["#{quoted_column_name} > ?", 1.year.ago] } }
74
- named_scope "#{column_name_alias}_in_month", lambda { |datetime| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", datetime.beginning_of_month, datetime.end_of_month] } }
75
- named_scope "#{column_name_alias}_in_day", lambda { |datetime| { :conditions => ["#{quoted_column_name} BETWEEN ? AND ?", datetime.beginning_of_day, datetime.end_of_day] } }
21
+ aliased = column_name.chomp("_at")
22
+
23
+ scope "#{aliased}_today", lambda { where("#{quoted_column_name} > ?", Time.zone.now.beginning_of_day) }
24
+ scope "#{aliased}_yesterday", lambda { where("#{quoted_column_name} BETWEEN ? AND ?", Time.zone.now.yesterday.beginning_of_day, Time.zone.now.yesterday.end_of_day) }
25
+ scope "#{aliased}_between", lambda { |from, to| where("#{quoted_column_name} BETWEEN ? AND ?", from, to) }
26
+ scope "not_#{aliased}_between", lambda { |from, to| where("#{quoted_column_name} NOT BETWEEN ? AND ?", from, to) }
27
+ scope "#{aliased}_before", lambda { |datetime| where("#{quoted_column_name} < ?", datetime) }
28
+ scope "#{aliased}_after", lambda { |datetime| where("#{quoted_column_name} > ?", datetime) }
76
29
 
77
- named_scope "#{column_name_alias}_ago", lambda { |period_type, period_count| { :conditions => ["#{quoted_column_name} > ?", period_count.send(period_type).ago] } }
30
+ scope "#{aliased}_in_last_week", lambda { where("#{quoted_column_name} > ?", 1.week.ago) }
31
+ scope "#{aliased}_in_last_month", lambda { where("#{quoted_column_name} > ?", 1.month.ago) }
32
+ scope "#{aliased}_in_last_year", lambda { where("#{quoted_column_name} > ?", 1.year.ago) }
33
+ scope "#{aliased}_in_month", lambda { |datetime| where("#{quoted_column_name} BETWEEN ? AND ?", datetime.beginning_of_month, datetime.end_of_month) }
34
+ scope "#{aliased}_in_day", lambda { |datetime| where("#{quoted_column_name} BETWEEN ? AND ?", datetime.beginning_of_day, datetime.end_of_day) }
35
+ scope "#{aliased}_ago", lambda { |period_count, period_type| where("#{quoted_column_name} > ?", period_count.send(period_type).ago) }
78
36
  end
79
37
  end
80
-
81
- if column.type == :integer || column.type == :float
82
- named_scope "#{column_name}_eql", lambda { |value| { :conditions => { column_name => value } } }
83
- named_scope "#{column_name}_gt", lambda { |value| { :conditions => ["#{quoted_column_name} > ?", value] } }
84
- named_scope "#{column_name}_gte", lambda { |value| { :conditions => ["#{quoted_column_name} >= ?", value] } }
85
- named_scope "#{column_name}_lt", lambda { |value| { :conditions => ["#{quoted_column_name} < ?", value] } }
86
- named_scope "#{column_name}_lte", lambda { |value| { :conditions => ["#{quoted_column_name} <= ?", value] } }
87
- end
88
-
89
- if column.type == :string
90
- named_scope "#{column_name}_starts_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "#{string}%"] } }
91
- named_scope "#{column_name}_ends_with", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}"] } }
92
- named_scope "#{column_name}_includes", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", "%#{string}%"] } }
93
- end
94
-
95
- if column.type == :text || column.type == :string
96
- named_scope "#{column_name}_like", lambda { |string| { :conditions => ["#{quoted_column_name} LIKE ?", string] } }
97
- named_scope "#{column_name}_not_like", lambda { |string| { :conditions => ["#{quoted_column_name} NOT LIKE ?", string] } }
98
- end
99
-
100
- if column.type == :boolean
101
- named_scope "#{column_name}", :conditions => { column_name => true }
102
- named_scope "not_#{column_name}", :conditions => { column_name => false }
103
- end
104
38
  end
105
-
106
39
  end
107
- end
108
40
 
109
- end
41
+ end
42
+ end
data/lib/time_scopes.rb CHANGED
@@ -1,3 +1 @@
1
- $:.unshift File.dirname(__FILE__)
2
-
3
1
  require 'time_scopes/time_scopes'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_scopes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tomasz Mazur
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-01 00:00:00 +02:00
18
+ date: 2010-07-28 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -81,6 +81,6 @@ rubyforge_project:
81
81
  rubygems_version: 1.3.7
82
82
  signing_key:
83
83
  specification_version: 3
84
- summary: scopes for ActiveRecord 3
84
+ summary: Useful scopes for ActiveRecord based on Date/DateTime columns
85
85
  test_files: []
86
86