state_of_the_nation 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 611580ac0c208f088555adadcd019dc5a6b424c0
4
- data.tar.gz: f1d1819db563048a4b7a980dbe9db17b72989735
2
+ SHA256:
3
+ metadata.gz: 97a7482f3c135b954fe912e2bb6b8948f4ba14596e282a91687aec9cbcb06b3d
4
+ data.tar.gz: 4577435dca22e84ded9236bc52b94cc24d02681973697623d5bca3c884034d6b
5
5
  SHA512:
6
- metadata.gz: 71c4a3084eeae10dae78e09c1139132671d3d5e5bafe47f4f72deaeac8ecbcb0543d6d10f80d77166ea47ea7ec74d1b275ebf44390f1639383da1c830cb6896e
7
- data.tar.gz: aec17ea9744fa0e7dc764944f4084922a4d4e2bed263892273047077a277d21936c36dc4d397057e2bbf00dff8ad2f7a0e81610a25a6bcdaf606bb3ec9402c26
6
+ metadata.gz: d00dd78d49cd44f1c79ad9c316d1e4843bb255f5b7728f0cb813bd656d8b3ff794c3d5dffa4013bf3d9e4805dda858f01bace09557c241e0b69d3869b06fd3f7
7
+ data.tar.gz: 11dbf26dbddd36bc93c408f6679c9980044c990c4ba4990298dd86ca1633687d5bfc0df9b13b38ad067d12645a3b001989e2356a175405ea3ed34a1cc354f5ed
@@ -0,0 +1,26 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ environment:
5
+ RAILS_ENV: test
6
+
7
+ docker:
8
+ - image: circleci/ruby:2.5.5
9
+
10
+ steps:
11
+ - checkout
12
+
13
+ - restore_cache:
14
+ keys:
15
+ - v2-dependencies-{{ .Branch }}
16
+ - v2-dependencies-master
17
+ - v2-dependencies-
18
+
19
+ - run: bundle install --path=vendor/bundle
20
+
21
+ - save_cache:
22
+ key: v2-dependencies-{{ .Branch }}
23
+ paths:
24
+ - vendor/bundle
25
+
26
+ - run: bundle exec rspec --format progress spec
@@ -0,0 +1 @@
1
+ 2.5.5
data/Gemfile CHANGED
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in state_of_the_nation.gemspec
4
4
  gemspec
5
-
6
- gem 'sqlite3', '~> 1.3.6'
data/README.md CHANGED
@@ -40,7 +40,7 @@ usa.senators.create!(name: "Ron Wyden", entered_office_at: Date.new(1996, 2, 6))
40
40
  usa.senators.create!(name: "Barbara Boxer", entered_office_at: Date.new(1993, 1, 3))
41
41
  usa.senators.create!(name: "Alan Cranston", entered_office_at: Date.new(1969, 1, 3), left_office_at: Date.new(1993, 1, 3))
42
42
 
43
- usa.active_president(Date.new(2015))
43
+ usa.active_president(Date.new(2015))
44
44
  # => President(id: 1, name: "Barack Obama", …)
45
45
 
46
46
  obama.active?
@@ -52,10 +52,9 @@ usa.active_senators(Date.new(2015))
52
52
  usa.presidents.create!(name: "Mitt Romney", entered_office_at: Date.new(2013, 1, 20))
53
53
  # => StateOfTheNation::ConflictError
54
54
  ```
55
-
56
55
  ## IdentityCache Support
57
56
 
58
- StateOfTheNation optionally supports fetching records through [IdentityCache](https://github.com/Shopify/identity_cache) instead of reading directly from the database.
57
+ StateOfTheNation optionally supports fetching records through [IdentityCache](https://github.com/Shopify/identity_cache) instead of reading directly from the database.
59
58
 
60
59
  For example if the `Country` model uses IdentityCache to cache the `has_many` relationship to `President`, you can instruct StateOfTheNation to fetch from the cache by calling `.with_identity_cache` on your `has_active` or `has_uniquely_active` definitions:
61
60
 
@@ -63,7 +62,7 @@ For example if the `Country` model uses IdentityCache to cache the `has_many` re
63
62
  class Country
64
63
  include IdentityCache
65
64
  include StateOfTheNation
66
-
65
+
67
66
  has_many(:presidents)
68
67
  cache_has_many(:presidents)
69
68
  has_uniquely_active(:president).with_identity_cache
@@ -95,3 +94,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
95
94
  ## Contributing
96
95
 
97
96
  Bug reports and pull requests are welcome on GitHub at https://github.com/intercom/state_of_the_nation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
97
+
@@ -13,9 +13,11 @@ module StateOfTheNation
13
13
  end
14
14
 
15
15
  module ClassMethods
16
- attr_accessor :prevent_multiple_active, :parent_association, :start_key, :finish_key
16
+ attr_accessor :prevent_multiple_active, :parent_association, :start_key, :finish_key, :ignore_empty
17
+
18
+ def considered_active(ignore_empty: false)
19
+ @ignore_empty = ignore_empty
17
20
 
18
- def considered_active
19
21
  def from(start_key)
20
22
  @start_key = start_key
21
23
  self
@@ -33,6 +35,24 @@ module StateOfTheNation
33
35
  }
34
36
  end
35
37
 
38
+ private def round_if_should(time)
39
+ return time if !should_round_timestamps?
40
+ time.respond_to?(:round) ? time.round : time
41
+ end
42
+
43
+ private def should_round_timestamps?
44
+ # MySQL datetime fields do not support millisecond resolution while
45
+ # PostgreSQL's do. To prevent issues with near identical timestamps not
46
+ # comparing as expected in .active? methods we'll choose the resolution
47
+ # appropriate for the database adapter backing the model.
48
+ case self.connection.adapter_name
49
+ when /PostgreSQL/
50
+ false
51
+ else
52
+ true
53
+ end
54
+ end
55
+
36
56
  self
37
57
  end
38
58
 
@@ -76,24 +96,6 @@ module StateOfTheNation
76
96
  single ? collection.first : collection
77
97
  end
78
98
  end
79
-
80
- def round_if_should(time)
81
- return time if !should_round_timestamps?
82
- time.respond_to?(:round) ? time.round : time
83
- end
84
-
85
- def should_round_timestamps?
86
- # MySQL datetime fields do not support millisecond resolution while
87
- # PostgreSQL's do. To prevent issues with near identical timestamps not
88
- # comparing as expected in .active? methods we'll choose the resolution
89
- # appropriate for the database adapter backing the model.
90
- case self.connection.adapter_name
91
- when /PostgreSQL/
92
- false
93
- else
94
- true
95
- end
96
- end
97
99
  end
98
100
 
99
101
  private
@@ -127,6 +129,9 @@ module StateOfTheNation
127
129
  # find competing records which *finish* being active AFTER this record *starts* being active
128
130
  # (or ones which are not set to finish being active)
129
131
 
132
+ records = records.where(QueryString.query_for(:start_and_finish_not_equal, self.class)) if ignore_empty
133
+ # exclude records where there is no difference between start and finish dates
134
+
130
135
  records
131
136
  end
132
137
 
@@ -167,6 +172,10 @@ module StateOfTheNation
167
172
  self.class.finish_key
168
173
  end
169
174
 
175
+ def ignore_empty
176
+ self.class.ignore_empty
177
+ end
178
+
170
179
  def should_round_timestamps?
171
180
  self.class.send(:should_round_timestamps?)
172
181
  end
@@ -10,11 +10,13 @@ module StateOfTheNation
10
10
  active_scope: "(%{finish_key} IS NULL OR %{finish_key} > ?::timestamp) AND %{start_key} <= ?::timestamp",
11
11
  less_than: "(%{start_key} < ?::timestamp)",
12
12
  greater_than_or_null: "(%{finish_key} > ?::timestamp) OR (%{finish_key} IS NULL)",
13
+ start_and_finish_not_equal: "(%{start_key} != %{finish_key})",
13
14
  },
14
15
  mysql: {
15
16
  active_scope: "(%{finish_key} IS NULL OR %{finish_key} > ?) AND %{start_key} <= ?",
16
17
  less_than: "(%{start_key} < ?)",
17
- greater_than_or_null: "(%{finish_key} > ?) OR (%{finish_key} IS NULL)"
18
+ greater_than_or_null: "(%{finish_key} > ?) OR (%{finish_key} IS NULL)",
19
+ start_and_finish_not_equal: "(%{start_key} != %{finish_key})"
18
20
  }
19
21
  }[appropriate_db_type(klass)]
20
22
  end
@@ -1,3 +1,3 @@
1
1
  module StateOfTheNation
2
- VERSION = "1.1.3"
2
+ VERSION = "1.1.4"
3
3
  end
@@ -24,16 +24,15 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler"
26
26
  spec.add_development_dependency "database_cleaner"
27
- # spec.add_development_dependency "mysql"
27
+ spec.add_development_dependency "mysql2"
28
28
  spec.add_development_dependency "pg"
29
- spec.add_development_dependency "pry", "0.12.2"
30
- spec.add_development_dependency "rb-readline"
29
+ spec.add_development_dependency "pry"
31
30
  spec.add_development_dependency "rake", "~> 10.0"
32
31
  spec.add_development_dependency "rspec", "~> 3.0"
33
32
  spec.add_development_dependency "rspec-rails"
34
33
  spec.add_development_dependency "shoulda-matchers"
35
- spec.add_development_dependency "sqlite3", "1.4.0"
34
+ spec.add_development_dependency "sqlite3"
36
35
 
37
- spec.add_runtime_dependency "activesupport", ">= 5.0.0"
38
- spec.add_runtime_dependency "activerecord", ">= 5.0.0"
36
+ spec.add_runtime_dependency "activesupport", ">= 4.0.0"
37
+ spec.add_runtime_dependency "activerecord", ">= 4.0.0"
39
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_of_the_nation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick O'Doherty
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-03-02 00:00:00.000000000 Z
12
+ date: 2020-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -40,7 +40,7 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: pg
43
+ name: mysql2
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
@@ -54,21 +54,21 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: pry
57
+ name: pg
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - '='
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: 0.12.2
62
+ version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - '='
67
+ - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: 0.12.2
69
+ version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
- name: rb-readline
71
+ name: pry
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - ">="
@@ -141,44 +141,44 @@ dependencies:
141
141
  name: sqlite3
142
142
  requirement: !ruby/object:Gem::Requirement
143
143
  requirements:
144
- - - '='
144
+ - - ">="
145
145
  - !ruby/object:Gem::Version
146
- version: 1.4.0
146
+ version: '0'
147
147
  type: :development
148
148
  prerelease: false
149
149
  version_requirements: !ruby/object:Gem::Requirement
150
150
  requirements:
151
- - - '='
151
+ - - ">="
152
152
  - !ruby/object:Gem::Version
153
- version: 1.4.0
153
+ version: '0'
154
154
  - !ruby/object:Gem::Dependency
155
155
  name: activesupport
156
156
  requirement: !ruby/object:Gem::Requirement
157
157
  requirements:
158
158
  - - ">="
159
159
  - !ruby/object:Gem::Version
160
- version: 5.0.0
160
+ version: 4.0.0
161
161
  type: :runtime
162
162
  prerelease: false
163
163
  version_requirements: !ruby/object:Gem::Requirement
164
164
  requirements:
165
165
  - - ">="
166
166
  - !ruby/object:Gem::Version
167
- version: 5.0.0
167
+ version: 4.0.0
168
168
  - !ruby/object:Gem::Dependency
169
169
  name: activerecord
170
170
  requirement: !ruby/object:Gem::Requirement
171
171
  requirements:
172
172
  - - ">="
173
173
  - !ruby/object:Gem::Version
174
- version: 5.0.0
174
+ version: 4.0.0
175
175
  type: :runtime
176
176
  prerelease: false
177
177
  version_requirements: !ruby/object:Gem::Requirement
178
178
  requirements:
179
179
  - - ">="
180
180
  - !ruby/object:Gem::Version
181
- version: 5.0.0
181
+ version: 4.0.0
182
182
  description: State of the Nation makes modeling object history easy.
183
183
  email:
184
184
  - patrick@intercom.io
@@ -187,7 +187,9 @@ executables: []
187
187
  extensions: []
188
188
  extra_rdoc_files: []
189
189
  files:
190
+ - ".circleci/config.yml"
190
191
  - ".gitignore"
192
+ - ".ruby-version"
191
193
  - ".travis.yml"
192
194
  - CODE_OF_CONDUCT.md
193
195
  - Gemfile
@@ -222,7 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
224
  version: '0'
223
225
  requirements: []
224
226
  rubyforge_project:
225
- rubygems_version: 2.6.14.1
227
+ rubygems_version: 2.7.6.2
226
228
  signing_key:
227
229
  specification_version: 4
228
230
  summary: An easy way to model state that changes over time with ActiveRecord