acts_as_span 0.0.6 → 1.2.1

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.
@@ -42,6 +42,29 @@ RSpec.describe ActsAsSpan::NoOverlapValidator do
42
42
 
43
43
  let(:all_siblings) { [brother, sister, brother_from_another_mother] }
44
44
 
45
+ describe 'instance_scope' do
46
+ let(:child_class) { OneParentChild }
47
+
48
+ let(:new_child_start_date) { Date.current - 2 }
49
+ let(:new_child_end_date) { Date.current + 3 }
50
+
51
+ before { new_child.favorite = favorite }
52
+
53
+ context 'when instance_scope evaluates to false' do
54
+ let(:favorite) { false }
55
+ it 'skips validation on the record for which instance_scope is false' do
56
+ expect(new_child).to be_valid
57
+ end
58
+ end
59
+
60
+ context 'when instance_scope evaluates to true' do
61
+ let(:favorite) { true }
62
+ it 'validates normally' do
63
+ expect(new_child).not_to be_valid
64
+ end
65
+ end
66
+ end
67
+
45
68
  describe 'an object with a single parent' do
46
69
  let(:child_class) { OneParentChild }
47
70
 
@@ -69,7 +92,7 @@ RSpec.describe ActsAsSpan::NoOverlapValidator do
69
92
 
70
93
  before do
71
94
  all_siblings.each do |sibling|
72
- sibling.update_attributes(papa: papa)
95
+ sibling.update(papa: papa)
73
96
  end
74
97
 
75
98
  new_child.papa = papa
@@ -93,4 +116,14 @@ RSpec.describe ActsAsSpan::NoOverlapValidator do
93
116
  end
94
117
  end
95
118
  end
119
+
120
+ describe 'error messages' do
121
+ let(:child_class) { OneParentChildCustom }
122
+ let(:new_child_start_date) { sister_start_date }
123
+ let(:new_child_end_date) { sister_end_date }
124
+ it 'allows using custom error messages' do
125
+ expect(new_child).not_to be_valid
126
+ expect(new_child.errors.messages[:base]).to include('Custom error message')
127
+ end
128
+ end
96
129
  end
@@ -12,5 +12,17 @@ RSpec.describe "Span" do
12
12
  it "should return the end_date" do
13
13
  expect(span.end_date).to eq(span_model.end_date)
14
14
  end
15
+
16
+ context "changed?" do
17
+ it 'returns true when start_date has changed' do
18
+ span_model.start_date = span_model.start_date + 5
19
+ expect(span.start_date_changed?).to eq(true)
20
+ end
21
+
22
+ it 'returns true when end_date has changed' do
23
+ span_model.end_date = span_model.end_date + 5
24
+ expect(span.end_date_changed?).to eq(true)
25
+ end
26
+ end
15
27
  end
16
28
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe ActsAsSpan::SpanKlass::Status do
6
+ let(:span_klass) { ::SpanModel }
7
+
8
+ let!(:today) { Date.current }
9
+
10
+ describe '.current' do
11
+ let!(:record) do
12
+ span_klass.create(
13
+ start_date: today - 1.week,
14
+ end_date: today + 1.week,
15
+ )
16
+ end
17
+
18
+ subject { span_klass.current(query_date) }
19
+
20
+ context "when query_date is within the record's span" do
21
+ let(:query_date) { record.start_date + 3.days }
22
+
23
+ it { is_expected.not_to be_empty }
24
+ end
25
+
26
+ context "when query_date is before the record's span" do
27
+ let(:query_date) { record.start_date - 1.week }
28
+
29
+ it { is_expected.to be_empty }
30
+ end
31
+
32
+ context "when query_date is after the record's span" do
33
+ let(:query_date) { record.end_date + 1.week }
34
+
35
+ it { is_expected.to be_empty }
36
+ end
37
+ end
38
+ end
@@ -112,4 +112,15 @@ RSpec.describe ActsAsSpan::WithinParentDateSpanValidator do
112
112
  end
113
113
  end
114
114
  end
115
+
116
+ describe 'error messages' do
117
+ let(:child_class) { OneParentChildCustom }
118
+ let(:new_child_start_date) { mama_start_date - 3.days }
119
+ let(:new_child_end_date) { mama_end_date + 5.days }
120
+
121
+ it 'allows using custom error messages' do
122
+ expect(new_child).not_to be_valid
123
+ expect(new_child.errors.messages[:base]).to include('Custom error message')
124
+ end
125
+ end
115
126
  end
data/spec/spec_models.rb CHANGED
@@ -4,6 +4,15 @@ require 'has_siblings'
4
4
  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
5
5
  ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV["DEBUG"]
6
6
 
7
+ Temping.create :spannable_model do
8
+ with_columns do |t|
9
+ t.date :starting_date
10
+ t.date :ending_date
11
+
12
+ t.integer :unique_by_date_range
13
+ end
14
+ end
15
+
7
16
  Temping.create :span_model do
8
17
  with_columns do |t|
9
18
  t.date :start_date
@@ -19,17 +28,51 @@ Temping.create :one_parent_child do
19
28
 
20
29
  t.date :start_date
21
30
  t.date :end_date
31
+
32
+ # every one-parent child is a favorite! ...by default
33
+ t.boolean :favorite, default: true
22
34
  end
23
35
 
24
36
  acts_as_span
25
37
 
38
+ def favorite?
39
+ favorite
40
+ end
41
+
26
42
  belongs_to :mama
27
43
  has_siblings through: [:mama]
28
44
 
29
- validates_with ActsAsSpan::NoOverlapValidator, scope: proc { siblings }
45
+ validates_with ActsAsSpan::NoOverlapValidator,
46
+ scope: proc { siblings }, instance_scope: proc { favorite? }
30
47
  validates_with ActsAsSpan::WithinParentDateSpanValidator, parents: [:mama]
31
48
  end
32
49
 
50
+ Temping.create :one_parent_child_custom do
51
+ with_columns do |t|
52
+ t.belongs_to :mama
53
+
54
+ t.date :start_date
55
+ t.date :end_date
56
+
57
+ # every one-parent child is a favorite! ...by default
58
+ t.boolean :favorite, default: true
59
+ end
60
+
61
+ acts_as_span
62
+
63
+ def favorite?
64
+ favorite
65
+ end
66
+
67
+ belongs_to :mama
68
+ has_siblings through: [:mama]
69
+
70
+ validates_with ActsAsSpan::NoOverlapValidator,
71
+ scope: proc { siblings }, instance_scope: proc { favorite? }, message: 'Custom error message'
72
+ validates_with ActsAsSpan::WithinParentDateSpanValidator, parents: [:mama], message: 'Custom error message'
73
+ end
74
+
75
+
33
76
  Temping.create :two_parent_child do
34
77
  with_columns do |t|
35
78
  t.belongs_to :mama
@@ -55,8 +98,11 @@ Temping.create :mama do
55
98
  t.date :end_date
56
99
  end
57
100
 
101
+ acts_as_span
102
+
58
103
  has_many :one_parent_children
59
104
  has_many :two_parent_children
105
+ has_many :one_parent_child_customs
60
106
  end
61
107
 
62
108
  Temping.create :papa do
@@ -65,5 +111,116 @@ Temping.create :papa do
65
111
  t.date :end_date
66
112
  end
67
113
 
114
+ acts_as_span
115
+
68
116
  has_many :one_parent_children
69
117
  end
118
+
119
+ # fulfill association requirements for EndDatePropagator
120
+ Temping.create :base do
121
+ has_many :children, dependent: :destroy
122
+ has_many :dogs, dependent: :destroy
123
+ has_many :birds, through: :children
124
+ has_many :tales, dependent: :destroy
125
+
126
+ acts_as_span
127
+
128
+ with_columns do |t|
129
+ t.date :end_date
130
+ t.date :start_date
131
+ end
132
+ end
133
+
134
+ Temping.create :cat_owner do
135
+ has_many :cats, dependent: :destroy
136
+
137
+ acts_as_span
138
+
139
+ with_columns do |t|
140
+ t.date :start_date
141
+ t.date :end_date
142
+ end
143
+ end
144
+
145
+ Temping.create :cat do
146
+ belongs_to :cat_owner
147
+
148
+ with_columns do |t|
149
+ t.belongs_to :cat_owner
150
+ end
151
+ end
152
+
153
+ Temping.create :other_base do
154
+ has_many :children, dependent: :destroy
155
+
156
+ acts_as_span
157
+
158
+ with_columns do |t|
159
+ t.date :end_date
160
+ t.date :start_date
161
+ end
162
+ end
163
+
164
+ # has non-standard start_ and end_field names
165
+ Temping.create :child do
166
+ belongs_to :base
167
+ belongs_to :other_base
168
+ has_many :birds, dependent: :destroy
169
+
170
+ validates_with ActsAsSpan::WithinParentDateSpanValidator,
171
+ parents: [:base]
172
+
173
+ acts_as_span(
174
+ start_field: :date_of_birth,
175
+ end_field: :emancipation_date
176
+ )
177
+
178
+ with_columns do |t|
179
+ t.date :date_of_birth
180
+ t.date :emancipation_date
181
+ t.string :manual_invalidation
182
+ t.belongs_to :base
183
+ end
184
+ end
185
+
186
+ Temping.create :dog do
187
+ belongs_to :base
188
+
189
+ acts_as_span
190
+
191
+ with_columns do |t|
192
+ t.date :start_date
193
+ t.date :end_date
194
+ t.belongs_to :base
195
+ end
196
+ end
197
+
198
+
199
+ Temping.create :bird do
200
+ belongs_to :child
201
+
202
+ validates_with ActsAsSpan::WithinParentDateSpanValidator,
203
+ parents: [:child]
204
+
205
+ acts_as_span
206
+
207
+ with_columns do |t|
208
+ t.date :end_date
209
+ t.date :start_date
210
+ t.belongs_to :child
211
+ end
212
+ end
213
+
214
+ Temping.create :tale do
215
+ belongs_to :base
216
+
217
+ acts_as_span
218
+
219
+ with_columns do |t|
220
+ t.date :end_date
221
+ t.date :start_date
222
+ t.belongs_to :base
223
+ end
224
+ end
225
+
226
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_span
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Sullivan
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-23 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,86 +16,86 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.15'
19
+ version: 2.1.4
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.15'
26
+ version: 2.1.4
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: has_siblings
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 0.2.7
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 0.2.7
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: pry-byebug
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: sqlite3
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 12.3.3
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 12.3.3
69
69
  - !ruby/object:Gem::Dependency
70
- name: has_siblings
70
+ name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.2.7
75
+ version: '3.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.2.7
82
+ version: '3.0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: temping
84
+ name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '1.4'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '1.4'
97
97
  - !ruby/object:Gem::Dependency
98
- name: pry-byebug
98
+ name: temping
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -114,28 +114,28 @@ dependencies:
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: 4.2.0
117
+ version: 5.0.0
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: 4.2.0
124
+ version: 5.0.0
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: activesupport
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: 4.2.0
131
+ version: 5.0.0
132
132
  type: :runtime
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 4.2.0
138
+ version: 5.0.0
139
139
  description: ActiveRecord model w/ a start_date and an end_date == ActsAsSpan
140
140
  email:
141
141
  - eric.sullivan@annkissam.com
@@ -145,13 +145,16 @@ extra_rdoc_files: []
145
145
  files:
146
146
  - ".gitignore"
147
147
  - ".rspec"
148
+ - ".tool-versions"
148
149
  - ".travis.yml"
149
150
  - Gemfile
150
151
  - LICENSE
151
152
  - README.rdoc
152
153
  - Rakefile
153
154
  - acts_as_span.gemspec
155
+ - config/locales/en/acts_as_span.yml
154
156
  - lib/acts_as_span.rb
157
+ - lib/acts_as_span/end_date_propagator.rb
155
158
  - lib/acts_as_span/no_overlap_validator.rb
156
159
  - lib/acts_as_span/span_instance.rb
157
160
  - lib/acts_as_span/span_instance/status.rb
@@ -162,6 +165,7 @@ files:
162
165
  - lib/acts_as_span/within_parent_date_span_validator.rb
163
166
  - spec/lib/acts_as_span_spec.rb
164
167
  - spec/lib/delegation_spec.rb
168
+ - spec/lib/end_date_propagator_spec.rb
165
169
  - spec/lib/no_overlap_validator_spec.rb
166
170
  - spec/lib/span_instance/named_scopes_on_spec.rb
167
171
  - spec/lib/span_instance/named_scopes_spec.rb
@@ -169,6 +173,7 @@ files:
169
173
  - spec/lib/span_instance/status_spec.rb
170
174
  - spec/lib/span_instance/validations_spec.rb
171
175
  - spec/lib/span_instance_spec.rb
176
+ - spec/lib/span_klass/status_spec.rb
172
177
  - spec/lib/within_parent_date_span_validator_spec.rb
173
178
  - spec/spec_helper.rb
174
179
  - spec/spec_models.rb
@@ -177,7 +182,7 @@ licenses:
177
182
  - MIT
178
183
  metadata:
179
184
  allowed_push_host: https://rubygems.org
180
- post_install_message:
185
+ post_install_message:
181
186
  rdoc_options: []
182
187
  require_paths:
183
188
  - lib
@@ -192,14 +197,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
197
  - !ruby/object:Gem::Version
193
198
  version: '0'
194
199
  requirements: []
195
- rubyforge_project:
196
- rubygems_version: 2.6.14
197
- signing_key:
200
+ rubygems_version: 3.0.3
201
+ signing_key:
198
202
  specification_version: 4
199
- summary: acts_as_span 0.0.6
203
+ summary: acts_as_span 1.2.1
200
204
  test_files:
201
205
  - spec/lib/acts_as_span_spec.rb
202
206
  - spec/lib/delegation_spec.rb
207
+ - spec/lib/end_date_propagator_spec.rb
203
208
  - spec/lib/no_overlap_validator_spec.rb
204
209
  - spec/lib/span_instance/named_scopes_on_spec.rb
205
210
  - spec/lib/span_instance/named_scopes_spec.rb
@@ -207,6 +212,7 @@ test_files:
207
212
  - spec/lib/span_instance/status_spec.rb
208
213
  - spec/lib/span_instance/validations_spec.rb
209
214
  - spec/lib/span_instance_spec.rb
215
+ - spec/lib/span_klass/status_spec.rb
210
216
  - spec/lib/within_parent_date_span_validator_spec.rb
211
217
  - spec/spec_helper.rb
212
218
  - spec/spec_models.rb