acts_as_historical 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_historical}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hasclass"]
@@ -31,7 +31,8 @@ Gem::Specification.new do |s|
31
31
  "test/acts_as_historical_test.rb",
32
32
  "test/database.yml",
33
33
  "test/schema.rb",
34
- "test/test_helper.rb"
34
+ "test/test_helper.rb",
35
+ "test/weekday_test.rb"
35
36
  ]
36
37
  s.homepage = %q{http://github.com/hasclass/acts_as_historical}
37
38
  s.rdoc_options = ["--charset=UTF-8"]
@@ -41,7 +42,8 @@ Gem::Specification.new do |s|
41
42
  s.test_files = [
42
43
  "test/acts_as_historical_test.rb",
43
44
  "test/schema.rb",
44
- "test/test_helper.rb"
45
+ "test/test_helper.rb",
46
+ "test/weekday_test.rb"
45
47
  ]
46
48
 
47
49
  if s.respond_to? :specification_version then
@@ -69,9 +69,11 @@ module ActsAsHistorical
69
69
  :limit => 1
70
70
  }}
71
71
 
72
+ # between(older_date, newer_date)
73
+ #
72
74
  named_scope :between, lambda {|*args|
73
75
  from, to = args
74
- range = from.to_date..to.to_date
76
+ range = (from.to_date..to.to_date)
75
77
  { :conditions => {self.historical_date_col => range } }
76
78
  }
77
79
 
@@ -80,7 +82,7 @@ module ActsAsHistorical
80
82
  #
81
83
  named_scope :nearest, lambda {|*args|
82
84
  date, tolerance = args
83
- range = self.tolerance_to_range(date.to_date, tolerance)
85
+ range = self.tolerance_to_range(date, tolerance)
84
86
  {
85
87
  :conditions => {self.historical_date_col => range},
86
88
  :order => ["ABS(DATEDIFF(#{self.historical_date_col_sql}, '#{date.to_date.to_s(:db)}')) ASC"]
@@ -148,14 +150,21 @@ module ActsAsHistorical
148
150
  true
149
151
  end
150
152
 
151
- def previous; find_record_at(to_date - 1); end
152
- def next; find_record_at(to_date + 1); end
153
+ def previous; find_record_at(snapshot_date - 1); end
154
+ def next; find_record_at(snapshot_date + 1); end
155
+
156
+ # override with your date implementation. eg. Weekday
157
+ #
158
+ #def snapshot_date
159
+ # self[self.class.historical_date_col]
160
+ #end
153
161
 
154
162
  def to_date
155
- self.send(self.class.historical_date_col)
163
+ snapshot_date.to_date
156
164
  end
157
165
 
158
166
  private
167
+
159
168
  def find_record_at(date)
160
169
  self.class.on_date(date).same_scope(self).find(:first)
161
170
  end
data/test/schema.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :records, :force => true do |t|
3
- t.string :snapshot_date
3
+ t.date :snapshot_date
4
4
  end
5
5
  create_table :record_weekdays, :force => true do |t|
6
- t.string :snapshot_date
6
+ t.date :snapshot_date
7
7
  end
8
8
  end
@@ -0,0 +1,62 @@
1
+ require 'date_ext'
2
+ require File.dirname(__FILE__) + '/test_helper.rb'
3
+
4
+ class ActsAsHistoricalWeekdayTest < ActiveSupport::TestCase
5
+ load_schema
6
+
7
+
8
+ class Record < ActiveRecord::Base
9
+ acts_as_historical
10
+ def snapshot_date
11
+ date = super
12
+ date and date.to_weekday
13
+ end
14
+ end
15
+
16
+ context 'named_scopes with 5 weekdays' do
17
+ setup {
18
+ Record.delete_all
19
+ @fri = Weekday.new(2009,11,27)
20
+ @mon = Weekday.new(2009,11,30)
21
+ @tue = Weekday.new(2009,12,1)
22
+ @wed = Weekday.new(2009,12,2)
23
+ @thu = Weekday.new(2009,12,3)
24
+
25
+ @r_fri = Record.create! :snapshot_date => @fri
26
+ @r_mon = Record.create! :snapshot_date => @mon
27
+ @r_tue = Record.create! :snapshot_date => @tue
28
+ @r_wed = Record.create! :snapshot_date => @wed
29
+ @r_thu = Record.create! :snapshot_date => @thu
30
+ }
31
+
32
+ context "snapshot_date" do
33
+ should "return weekday" do
34
+ assert_equal @mon, Record.find(@r_mon).snapshot_date
35
+ end
36
+ end
37
+
38
+ context "snapshots over weekend" do
39
+ should "return friday as previous of monday" do
40
+ assert_equal @r_fri, @r_mon.previous
41
+ end
42
+
43
+ should "return monday as next of friday" do
44
+ assert_equal @r_mon, @r_fri.next
45
+ end
46
+
47
+ should "include friday, monday, tuesday for nearest(monday, 1)" do
48
+ results = Record.nearest(@mon,1)
49
+ assert_equal 3, results.length
50
+ assert results.include?(@r_mon)
51
+ assert results.include?(@r_tue)
52
+ assert results.include?(@r_fri)
53
+ end
54
+ end
55
+
56
+ context "tolerance over week" do
57
+ should "work" do
58
+ Record.tolerance_to_range(@mon, 1).include?(@fri)
59
+ end
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_historical
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - hasclass
@@ -47,6 +47,7 @@ files:
47
47
  - test/database.yml
48
48
  - test/schema.rb
49
49
  - test/test_helper.rb
50
+ - test/weekday_test.rb
50
51
  has_rdoc: true
51
52
  homepage: http://github.com/hasclass/acts_as_historical
52
53
  licenses: []
@@ -79,3 +80,4 @@ test_files:
79
80
  - test/acts_as_historical_test.rb
80
81
  - test/schema.rb
81
82
  - test/test_helper.rb
83
+ - test/weekday_test.rb