acts_as_span 1.0.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -28,17 +28,51 @@ Temping.create :one_parent_child do
28
28
 
29
29
  t.date :start_date
30
30
  t.date :end_date
31
+
32
+ # every one-parent child is a favorite! ...by default
33
+ t.boolean :favorite, default: true
31
34
  end
32
35
 
33
36
  acts_as_span
34
37
 
38
+ def favorite?
39
+ favorite
40
+ end
41
+
35
42
  belongs_to :mama
36
43
  has_siblings through: [:mama]
37
44
 
38
- validates_with ActsAsSpan::NoOverlapValidator, scope: proc { siblings }
45
+ validates_with ActsAsSpan::NoOverlapValidator,
46
+ scope: proc { siblings }, instance_scope: proc { favorite? }
39
47
  validates_with ActsAsSpan::WithinParentDateSpanValidator, parents: [:mama]
40
48
  end
41
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
+
42
76
  Temping.create :two_parent_child do
43
77
  with_columns do |t|
44
78
  t.belongs_to :mama
@@ -64,8 +98,11 @@ Temping.create :mama do
64
98
  t.date :end_date
65
99
  end
66
100
 
101
+ acts_as_span
102
+
67
103
  has_many :one_parent_children
68
104
  has_many :two_parent_children
105
+ has_many :one_parent_child_customs
69
106
  end
70
107
 
71
108
  Temping.create :papa do
@@ -74,5 +111,124 @@ Temping.create :papa do
74
111
  t.date :end_date
75
112
  end
76
113
 
114
+ acts_as_span
115
+
77
116
  has_many :one_parent_children
78
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
+ validate :not_manually_invalidated
174
+
175
+ acts_as_span(
176
+ start_field: :date_of_birth,
177
+ end_field: :emancipation_date,
178
+ )
179
+
180
+ with_columns do |t|
181
+ t.date :date_of_birth
182
+ t.date :emancipation_date
183
+ t.string :manual_invalidation
184
+ t.belongs_to :base
185
+ end
186
+
187
+ def not_manually_invalidated
188
+ return if manual_invalidation.blank? || manual_invalidation == false
189
+
190
+ errors.add(:base, 'Child is bad')
191
+ end
192
+ end
193
+
194
+ Temping.create :dog do
195
+ belongs_to :base
196
+
197
+ acts_as_span
198
+
199
+ with_columns do |t|
200
+ t.date :start_date
201
+ t.date :end_date
202
+ t.belongs_to :base
203
+ end
204
+ end
205
+
206
+
207
+ Temping.create :bird do
208
+ belongs_to :child
209
+
210
+ validates_with ActsAsSpan::WithinParentDateSpanValidator,
211
+ parents: [:child]
212
+
213
+ acts_as_span
214
+
215
+ with_columns do |t|
216
+ t.date :end_date
217
+ t.date :start_date
218
+ t.belongs_to :child
219
+ end
220
+ end
221
+
222
+ Temping.create :tale do
223
+ belongs_to :base
224
+
225
+ acts_as_span
226
+
227
+ with_columns do |t|
228
+ t.date :end_date
229
+ t.date :start_date
230
+ t.belongs_to :base
231
+ end
232
+ end
233
+
234
+
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: 1.0.0
4
+ version: 1.2.2
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: 2019-03-12 00:00:00.000000000 Z
11
+ date: 2021-03-15 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: 2.0.1
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: 2.0.1
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: 1.3.6
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: 1.3.6
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,18 @@ extra_rdoc_files: []
145
145
  files:
146
146
  - ".gitignore"
147
147
  - ".rspec"
148
+ - ".tool-versions"
148
149
  - ".travis.yml"
150
+ - CHANGELOG.md
149
151
  - Gemfile
150
152
  - LICENSE
153
+ - PULL_REQUEST_TEMPLATE.md
151
154
  - README.rdoc
152
155
  - Rakefile
153
156
  - acts_as_span.gemspec
157
+ - config/locales/en/acts_as_span.yml
154
158
  - lib/acts_as_span.rb
159
+ - lib/acts_as_span/end_date_propagator.rb
155
160
  - lib/acts_as_span/no_overlap_validator.rb
156
161
  - lib/acts_as_span/span_instance.rb
157
162
  - lib/acts_as_span/span_instance/status.rb
@@ -162,6 +167,7 @@ files:
162
167
  - lib/acts_as_span/within_parent_date_span_validator.rb
163
168
  - spec/lib/acts_as_span_spec.rb
164
169
  - spec/lib/delegation_spec.rb
170
+ - spec/lib/end_date_propagator_spec.rb
165
171
  - spec/lib/no_overlap_validator_spec.rb
166
172
  - spec/lib/span_instance/named_scopes_on_spec.rb
167
173
  - spec/lib/span_instance/named_scopes_spec.rb
@@ -169,6 +175,7 @@ files:
169
175
  - spec/lib/span_instance/status_spec.rb
170
176
  - spec/lib/span_instance/validations_spec.rb
171
177
  - spec/lib/span_instance_spec.rb
178
+ - spec/lib/span_klass/status_spec.rb
172
179
  - spec/lib/within_parent_date_span_validator_spec.rb
173
180
  - spec/spec_helper.rb
174
181
  - spec/spec_models.rb
@@ -177,7 +184,7 @@ licenses:
177
184
  - MIT
178
185
  metadata:
179
186
  allowed_push_host: https://rubygems.org
180
- post_install_message:
187
+ post_install_message:
181
188
  rdoc_options: []
182
189
  require_paths:
183
190
  - lib
@@ -192,9 +199,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
199
  - !ruby/object:Gem::Version
193
200
  version: '0'
194
201
  requirements: []
195
- rubyforge_project:
196
- rubygems_version: 2.7.6
197
- signing_key:
202
+ rubygems_version: 3.0.3
203
+ signing_key:
198
204
  specification_version: 4
199
- summary: acts_as_span 1.0.0
200
- test_files: []
205
+ summary: acts_as_span 1.2.2
206
+ test_files:
207
+ - spec/lib/acts_as_span_spec.rb
208
+ - spec/lib/delegation_spec.rb
209
+ - spec/lib/end_date_propagator_spec.rb
210
+ - spec/lib/no_overlap_validator_spec.rb
211
+ - spec/lib/span_instance/named_scopes_on_spec.rb
212
+ - spec/lib/span_instance/named_scopes_spec.rb
213
+ - spec/lib/span_instance/overlap_spec.rb
214
+ - spec/lib/span_instance/status_spec.rb
215
+ - spec/lib/span_instance/validations_spec.rb
216
+ - spec/lib/span_instance_spec.rb
217
+ - spec/lib/span_klass/status_spec.rb
218
+ - spec/lib/within_parent_date_span_validator_spec.rb
219
+ - spec/spec_helper.rb
220
+ - spec/spec_models.rb