validates_overlap 0.8.0 → 0.8.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.
- checksums.yaml +4 -4
- data/.travis.yml +2 -3
- data/Rakefile +12 -0
- data/VERSION +1 -1
- data/lib/validates_overlap/overlap_validator.rb +17 -3
- data/spec/dummy/app/models/document_with_enum.rb +26 -0
- data/spec/dummy/db/migrate/20170102162952_add_documents_with_enum.rb +14 -0
- data/spec/dummy/db/schema.rb +69 -53
- data/spec/dummy/spec/factories/document_with_enum.rb +7 -0
- data/spec/dummy/spec/models/active_meetings_spec.rb +1 -2
- data/spec/dummy/spec/models/document_with_enum_spec.rb +53 -0
- data/spec/dummy/spec/models/end_overlap_meeting_spec.rb +1 -2
- data/spec/dummy/spec/models/meeting_spec.rb +1 -2
- data/spec/dummy/spec/models/position_spec.rb +1 -4
- data/spec/dummy/spec/models/secure_meeting_spec.rb +1 -2
- data/spec/dummy/spec/models/shift_spec.rb +1 -2
- data/spec/dummy/spec/models/start_end_overlap_meeting_spec.rb +1 -2
- data/spec/dummy/spec/models/start_overlap_meeting_spec.rb +1 -2
- data/spec/dummy/spec/models/time_slot_spec.rb +1 -4
- data/spec/dummy/spec/models/user_meeting_spec.rb +1 -2
- data/spec/dummy/spec/models/user_spec.rb +1 -2
- data/spec/spec_helper.rb +2 -1
- data/validates_overlap.gemspec +1 -0
- metadata +21 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03d095c00004db43b1b61ae30b0de3df1f7ca9b6
|
4
|
+
data.tar.gz: 0f1df2c05348e4e972011eb36a9a969b02907f34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0730c4937d1ac05b221c96328bbd9dee9795352a0c62792921202ef7ba435ac395e01f5e66ffe7ec03c159848003642811330f17456b957e2c44e891d70771e5
|
7
|
+
data.tar.gz: ec70192d3ba6a7633395a5ef3f036de336730fc9dc8d52655574de76b24e4af3328b9a85d27c5678fa03a82dfe975a2ff190aface62b023002aaaa9cc774a02e
|
data/.travis.yml
CHANGED
data/Rakefile
CHANGED
@@ -5,6 +5,17 @@ require 'rspec/core'
|
|
5
5
|
require 'rspec/core/rake_task'
|
6
6
|
require 'rdoc/task'
|
7
7
|
|
8
|
+
|
9
|
+
# Temporary fix of rake issue
|
10
|
+
# http://stackoverflow.com/questions/35893584/nomethoderror-undefined-method-last-comment-after-upgrading-to-rake-11/35893941
|
11
|
+
module TempFixForRakeLastComment
|
12
|
+
def last_comment
|
13
|
+
last_description
|
14
|
+
end
|
15
|
+
end
|
16
|
+
Rake::Application.send :include, TempFixForRakeLastComment
|
17
|
+
|
18
|
+
|
8
19
|
begin
|
9
20
|
Bundler::GemHelper.install_tasks
|
10
21
|
Bundler.setup(:default, :development)
|
@@ -14,6 +25,7 @@ rescue Bundler::BundlerError => e
|
|
14
25
|
exit e.status_code
|
15
26
|
end
|
16
27
|
|
28
|
+
|
17
29
|
RSpec::Core::RakeTask.new(:spec)
|
18
30
|
|
19
31
|
task default: :spec
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.1
|
@@ -150,7 +150,7 @@ class OverlapValidator < ActiveModel::EachValidator
|
|
150
150
|
|
151
151
|
# Add attribute and his value to sql condition
|
152
152
|
def add_attribute(record, attr_name, value = nil)
|
153
|
-
_value =
|
153
|
+
_value = resolve_attribute_value(record, attr_name, value)
|
154
154
|
operator = if _value.nil?
|
155
155
|
' IS NULL'
|
156
156
|
elsif _value.is_a?(Array)
|
@@ -168,14 +168,28 @@ class OverlapValidator < ActiveModel::EachValidator
|
|
168
168
|
name + '_value'
|
169
169
|
end
|
170
170
|
|
171
|
-
def
|
171
|
+
def resolve_attribute_value(record, attr_name, value = nil)
|
172
172
|
if value
|
173
173
|
value.is_a?(Proc) ? value.call(record) : value
|
174
174
|
else
|
175
|
-
record.read_attribute(attr_name)
|
175
|
+
value = record.read_attribute(attr_name)
|
176
|
+
|
177
|
+
if is_enum_attribute?(record, attr_name)
|
178
|
+
value = record.class.defined_enums[attr_name][value]
|
179
|
+
end
|
180
|
+
|
181
|
+
value
|
176
182
|
end
|
177
183
|
end
|
178
184
|
|
185
|
+
def is_enum_attribute?(record, attr_name)
|
186
|
+
implement_enum? && record.class.defined_enums[attr_name].present?
|
187
|
+
end
|
188
|
+
|
189
|
+
def implement_enum?
|
190
|
+
(ActiveRecord::VERSION::MAJOR > 5) || (ActiveRecord::VERSION::MAJOR > 4 && ActiveRecord::VERSION::MINOR > 1)
|
191
|
+
end
|
192
|
+
|
179
193
|
# Allow to use scope, joins, includes methods before querying
|
180
194
|
# == Example:
|
181
195
|
# validates_overlap :date_from, :date_to, :query_options => {:includes => "visits"}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class DocumentWithEnum < ActiveRecord::Base
|
2
|
+
self.table_name = 'documents_with_enum'
|
3
|
+
KINDS = [:contract, :fact, :draft]
|
4
|
+
|
5
|
+
|
6
|
+
if (ActiveRecord::VERSION::MAJOR > 5) || (ActiveRecord::VERSION::MAJOR > 4 && ActiveRecord::VERSION::MINOR > 1)
|
7
|
+
enum kind: KINDS
|
8
|
+
else
|
9
|
+
# Simulate enum functionality implemented in rails 4.1 and 5
|
10
|
+
# For rails version < 4.1 we always read read attribute by using read_attribute method
|
11
|
+
def kind=(value)
|
12
|
+
write_attribute(:kind, KINDS.index(value))
|
13
|
+
end
|
14
|
+
|
15
|
+
def kind
|
16
|
+
return nil if read_attribute(:kind).nil?
|
17
|
+
KINDS[read_attribute(:kind).to_i]
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
validates :valid_from, :valid_until, overlap: {
|
23
|
+
exclude_edges: ['valid_from', 'valid_until'],
|
24
|
+
scope: ['kind']
|
25
|
+
}
|
26
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class AddDocumentsWithEnum < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :documents_with_enum do |t|
|
4
|
+
t.date :valid_from
|
5
|
+
t.date :valid_until
|
6
|
+
t.integer :kind
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
drop_table :documents_with_enum
|
13
|
+
end
|
14
|
+
end
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
# This file is auto-generated from the current state of the database. Instead
|
3
2
|
# of editing this file, please use the migrations feature of Active Record to
|
4
3
|
# incrementally modify your database, and then regenerate this schema definition.
|
@@ -11,75 +10,92 @@
|
|
11
10
|
#
|
12
11
|
# It's strongly recommended that you check this file into your version control system.
|
13
12
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
15
|
-
|
16
|
-
|
17
|
-
t.date
|
18
|
-
t.
|
19
|
-
t.
|
20
|
-
t.datetime
|
13
|
+
ActiveRecord::Schema.define(version: 20170102162952) do
|
14
|
+
|
15
|
+
create_table "active_meetings", force: :cascade do |t|
|
16
|
+
t.date "starts_at"
|
17
|
+
t.date "ends_at"
|
18
|
+
t.boolean "is_active"
|
19
|
+
t.datetime "created_at"
|
20
|
+
t.datetime "updated_at"
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table "documents_with_enum", force: :cascade do |t|
|
24
|
+
t.date "valid_from"
|
25
|
+
t.date "valid_until"
|
26
|
+
t.integer "kind"
|
27
|
+
t.datetime "created_at"
|
28
|
+
t.datetime "updated_at"
|
21
29
|
end
|
22
30
|
|
23
|
-
create_table
|
24
|
-
t.date
|
25
|
-
t.date
|
26
|
-
t.datetime
|
27
|
-
t.datetime
|
31
|
+
create_table "end_overlap_meetings", force: :cascade do |t|
|
32
|
+
t.date "starts_at"
|
33
|
+
t.date "ends_at"
|
34
|
+
t.datetime "created_at"
|
35
|
+
t.datetime "updated_at"
|
28
36
|
end
|
29
37
|
|
30
|
-
create_table
|
31
|
-
t.date
|
32
|
-
t.date
|
33
|
-
t.datetime
|
34
|
-
t.datetime
|
38
|
+
create_table "meetings", force: :cascade do |t|
|
39
|
+
t.date "starts_at"
|
40
|
+
t.date "ends_at"
|
41
|
+
t.datetime "created_at"
|
42
|
+
t.datetime "updated_at"
|
35
43
|
end
|
36
44
|
|
37
|
-
create_table
|
38
|
-
t.integer
|
39
|
-
t.integer
|
40
|
-
t.datetime
|
41
|
-
t.datetime
|
45
|
+
create_table "positions", force: :cascade do |t|
|
46
|
+
t.integer "user_id"
|
47
|
+
t.integer "time_slot_id"
|
48
|
+
t.datetime "created_at"
|
49
|
+
t.datetime "updated_at"
|
42
50
|
end
|
43
51
|
|
44
|
-
create_table
|
45
|
-
t.date
|
46
|
-
t.date
|
47
|
-
t.datetime
|
48
|
-
t.datetime
|
52
|
+
create_table "secure_meetings", force: :cascade do |t|
|
53
|
+
t.date "starts_at"
|
54
|
+
t.date "ends_at"
|
55
|
+
t.datetime "created_at"
|
56
|
+
t.datetime "updated_at"
|
49
57
|
end
|
50
58
|
|
51
|
-
create_table
|
52
|
-
t.date
|
53
|
-
t.date
|
54
|
-
t.datetime
|
55
|
-
t.datetime
|
59
|
+
create_table "shifts", force: :cascade do |t|
|
60
|
+
t.date "starts_at"
|
61
|
+
t.date "ends_at"
|
62
|
+
t.datetime "created_at"
|
63
|
+
t.datetime "updated_at"
|
56
64
|
end
|
57
65
|
|
58
|
-
create_table
|
59
|
-
t.date
|
60
|
-
t.date
|
61
|
-
t.datetime
|
62
|
-
t.datetime
|
66
|
+
create_table "start_end_overlap_meetings", force: :cascade do |t|
|
67
|
+
t.date "starts_at"
|
68
|
+
t.date "ends_at"
|
69
|
+
t.datetime "created_at"
|
70
|
+
t.datetime "updated_at"
|
63
71
|
end
|
64
72
|
|
65
|
-
create_table
|
66
|
-
t.date
|
67
|
-
t.date
|
68
|
-
t.datetime
|
69
|
-
t.datetime
|
73
|
+
create_table "start_overlap_meetings", force: :cascade do |t|
|
74
|
+
t.date "starts_at"
|
75
|
+
t.date "ends_at"
|
76
|
+
t.datetime "created_at"
|
77
|
+
t.datetime "updated_at"
|
70
78
|
end
|
71
79
|
|
72
|
-
create_table
|
73
|
-
t.
|
74
|
-
t.date
|
75
|
-
t.
|
76
|
-
t.datetime
|
77
|
-
t.datetime 'updated_at', null: false
|
80
|
+
create_table "time_slots", force: :cascade do |t|
|
81
|
+
t.date "starts_at"
|
82
|
+
t.date "ends_at"
|
83
|
+
t.datetime "created_at"
|
84
|
+
t.datetime "updated_at"
|
78
85
|
end
|
79
86
|
|
80
|
-
create_table
|
81
|
-
t.
|
82
|
-
t.
|
83
|
-
t.
|
87
|
+
create_table "user_meetings", force: :cascade do |t|
|
88
|
+
t.integer "user_id"
|
89
|
+
t.date "starts_at"
|
90
|
+
t.date "ends_at"
|
91
|
+
t.datetime "created_at"
|
92
|
+
t.datetime "updated_at"
|
84
93
|
end
|
94
|
+
|
95
|
+
create_table "users", force: :cascade do |t|
|
96
|
+
t.string "name"
|
97
|
+
t.datetime "created_at"
|
98
|
+
t.datetime "updated_at"
|
99
|
+
end
|
100
|
+
|
85
101
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/../../../spec_helper"
|
2
|
+
|
3
|
+
describe DocumentWithEnum do
|
4
|
+
context '2 overlapping documents with same kind' do
|
5
|
+
it 'are invalid' do
|
6
|
+
|
7
|
+
document_1 = FactoryGirl.create(
|
8
|
+
:document_with_enum,
|
9
|
+
kind: :draft,
|
10
|
+
valid_from: '2011-01-05'.to_date,
|
11
|
+
valid_until: '2011-01-08'.to_date
|
12
|
+
)
|
13
|
+
|
14
|
+
document_2 = FactoryGirl.build(
|
15
|
+
:document_with_enum,
|
16
|
+
kind: :draft,
|
17
|
+
valid_from: '2011-01-06'.to_date,
|
18
|
+
valid_until: '2011-01-07'.to_date
|
19
|
+
)
|
20
|
+
|
21
|
+
expect(document_2).not_to be_valid
|
22
|
+
expect(document_2.errors[:valid_from]).to eq ["overlaps with another record"]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context '2 overlapping documents with different kind' do
|
27
|
+
it 'are valid' do
|
28
|
+
document_1 = FactoryGirl.create(
|
29
|
+
:document_with_enum,
|
30
|
+
kind: :draft,
|
31
|
+
valid_from: '2011-01-05'.to_date,
|
32
|
+
valid_until: '2011-01-08'.to_date
|
33
|
+
)
|
34
|
+
|
35
|
+
document_2 = FactoryGirl.build(
|
36
|
+
:document_with_enum,
|
37
|
+
kind: :contract,
|
38
|
+
valid_from: '2011-01-06'.to_date,
|
39
|
+
valid_until: '2011-01-07'.to_date
|
40
|
+
)
|
41
|
+
|
42
|
+
expect(document_2).to be_valid
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#kind' do
|
47
|
+
it 'save and read attribute properly' do
|
48
|
+
docuemnt = FactoryGirl.create(:document_with_enum, kind: :contract)
|
49
|
+
docuemnt.reload
|
50
|
+
expect(docuemnt.kind).to eq :contract
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,7 +1,4 @@
|
|
1
|
-
|
2
|
-
require_relative '../factories/position'
|
3
|
-
require_relative '../factories/time_slot'
|
4
|
-
require_relative '../factories/user'
|
1
|
+
require "#{File.dirname(__FILE__)}/../../../spec_helper"
|
5
2
|
|
6
3
|
describe Position do
|
7
4
|
it 'create position' do
|
@@ -1,7 +1,4 @@
|
|
1
|
-
|
2
|
-
require_relative '../factories/position'
|
3
|
-
require_relative '../factories/time_slot'
|
4
|
-
require_relative '../factories/user'
|
1
|
+
require "#{File.dirname(__FILE__)}/../../../spec_helper"
|
5
2
|
|
6
3
|
describe TimeSlot do
|
7
4
|
it 'create time_slot' do
|
data/spec/spec_helper.rb
CHANGED
@@ -18,7 +18,8 @@ Rails.backtrace_cleaner.remove_silencers!
|
|
18
18
|
ActiveRecord::Migrator.migrate File.expand_path('../dummy/db/migrate/', __FILE__)
|
19
19
|
|
20
20
|
# Load support files
|
21
|
-
|
21
|
+
FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), '/dummy/spec/factories')
|
22
|
+
FactoryGirl.find_definitions
|
22
23
|
|
23
24
|
RSpec.configure do |config|
|
24
25
|
# Remove this line if you don't want RSpec's should and should_not
|
data/validates_overlap.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_overlap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Bortlik
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: test-unit
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
description: It can be useful when you you are developing some app where you will
|
140
154
|
work with meetings, events etc.
|
141
155
|
email: robinbortlik@gmail.com
|
@@ -168,6 +182,7 @@ files:
|
|
168
182
|
- spec/dummy/app/controllers/application_controller.rb
|
169
183
|
- spec/dummy/app/helpers/application_helper.rb
|
170
184
|
- spec/dummy/app/models/active_meeting.rb
|
185
|
+
- spec/dummy/app/models/document_with_enum.rb
|
171
186
|
- spec/dummy/app/models/end_overlap_meeting.rb
|
172
187
|
- spec/dummy/app/models/meeting.rb
|
173
188
|
- spec/dummy/app/models/position.rb
|
@@ -204,6 +219,7 @@ files:
|
|
204
219
|
- spec/dummy/db/migrate/20130826155107_create_active_meetings.rb
|
205
220
|
- spec/dummy/db/migrate/20150707155107_create_secure_meetings.rb
|
206
221
|
- spec/dummy/db/migrate/20151203153132_create_shifts.rb
|
222
|
+
- spec/dummy/db/migrate/20170102162952_add_documents_with_enum.rb
|
207
223
|
- spec/dummy/db/schema.rb
|
208
224
|
- spec/dummy/public/404.html
|
209
225
|
- spec/dummy/public/422.html
|
@@ -218,6 +234,7 @@ files:
|
|
218
234
|
- spec/dummy/public/stylesheets/.gitkeep
|
219
235
|
- spec/dummy/script/rails
|
220
236
|
- spec/dummy/spec/factories/active_meeting.rb
|
237
|
+
- spec/dummy/spec/factories/document_with_enum.rb
|
221
238
|
- spec/dummy/spec/factories/end_overlap_meeting.rb
|
222
239
|
- spec/dummy/spec/factories/meeting.rb
|
223
240
|
- spec/dummy/spec/factories/position.rb
|
@@ -229,6 +246,7 @@ files:
|
|
229
246
|
- spec/dummy/spec/factories/user.rb
|
230
247
|
- spec/dummy/spec/factories/user_meeting.rb
|
231
248
|
- spec/dummy/spec/models/active_meetings_spec.rb
|
249
|
+
- spec/dummy/spec/models/document_with_enum_spec.rb
|
232
250
|
- spec/dummy/spec/models/end_overlap_meeting_spec.rb
|
233
251
|
- spec/dummy/spec/models/meeting_spec.rb
|
234
252
|
- spec/dummy/spec/models/position_spec.rb
|
@@ -268,6 +286,7 @@ specification_version: 4
|
|
268
286
|
summary: This gem helps validate records with time overlap.
|
269
287
|
test_files:
|
270
288
|
- spec/dummy/spec/factories/active_meeting.rb
|
289
|
+
- spec/dummy/spec/factories/document_with_enum.rb
|
271
290
|
- spec/dummy/spec/factories/end_overlap_meeting.rb
|
272
291
|
- spec/dummy/spec/factories/meeting.rb
|
273
292
|
- spec/dummy/spec/factories/position.rb
|
@@ -279,6 +298,7 @@ test_files:
|
|
279
298
|
- spec/dummy/spec/factories/user.rb
|
280
299
|
- spec/dummy/spec/factories/user_meeting.rb
|
281
300
|
- spec/dummy/spec/models/active_meetings_spec.rb
|
301
|
+
- spec/dummy/spec/models/document_with_enum_spec.rb
|
282
302
|
- spec/dummy/spec/models/end_overlap_meeting_spec.rb
|
283
303
|
- spec/dummy/spec/models/meeting_spec.rb
|
284
304
|
- spec/dummy/spec/models/position_spec.rb
|