validates_overlap 0.8.0 → 0.8.1

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
2
  SHA1:
3
- metadata.gz: 51a9d0225831152c676f511f8bb393d9a8b3ed5f
4
- data.tar.gz: 2352b75e04fa67608a16d08b1e1fd304352fbab8
3
+ metadata.gz: 03d095c00004db43b1b61ae30b0de3df1f7ca9b6
4
+ data.tar.gz: 0f1df2c05348e4e972011eb36a9a969b02907f34
5
5
  SHA512:
6
- metadata.gz: 070384cc4d4447894c1131f4077c691ec8b3bb73a6f52eb1665cbba0a1f15646161bd77200a88feff96bebdb598b9dbf87ba5c3efba91ae7778f443d3a388b0d
7
- data.tar.gz: 823d335905e96d0bc560c0a30c485fa26d004b2e1840fb00545e6478f4feeb9485a20fa249b254a604f41969e728e2ebf8a03e8fb503af83d17221fc050ac428
6
+ metadata.gz: 0730c4937d1ac05b221c96328bbd9dee9795352a0c62792921202ef7ba435ac395e01f5e66ffe7ec03c159848003642811330f17456b957e2c44e891d70771e5
7
+ data.tar.gz: ec70192d3ba6a7633395a5ef3f036de336730fc9dc8d52655574de76b24e4af3328b9a85d27c5678fa03a82dfe975a2ff190aface62b023002aaaa9cc774a02e
data/.travis.yml CHANGED
@@ -1,11 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.0
3
+ - 2.2.2
6
4
  gemfile:
7
5
  - Gemfile.rails30
8
6
  - Gemfile.rails32
9
7
  - Gemfile.rails40
10
8
  - Gemfile.rails41
11
9
  - Gemfile.rails42
10
+ - Gemfile.rails50
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.0
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 = resolve_scope_value(record, attr_name, 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 resolve_scope_value(record, attr_name, value = nil)
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
@@ -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: 20_150_707_155_107) do
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', null: false
20
- t.datetime 'updated_at', null: false
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 'end_overlap_meetings', force: :cascade do |t|
24
- t.date 'starts_at'
25
- t.date 'ends_at'
26
- t.datetime 'created_at', null: false
27
- t.datetime 'updated_at', null: false
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 'meetings', force: :cascade do |t|
31
- t.date 'starts_at'
32
- t.date 'ends_at'
33
- t.datetime 'created_at', null: false
34
- t.datetime 'updated_at', null: false
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 'positions', force: :cascade do |t|
38
- t.integer 'user_id'
39
- t.integer 'time_slot_id'
40
- t.datetime 'created_at', null: false
41
- t.datetime 'updated_at', null: false
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 'secure_meetings', force: :cascade do |t|
45
- t.date 'starts_at'
46
- t.date 'ends_at'
47
- t.datetime 'created_at'
48
- t.datetime 'updated_at'
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 'start_end_overlap_meetings', force: :cascade do |t|
52
- t.date 'starts_at'
53
- t.date 'ends_at'
54
- t.datetime 'created_at', null: false
55
- t.datetime 'updated_at', null: false
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 'start_overlap_meetings', force: :cascade do |t|
59
- t.date 'starts_at'
60
- t.date 'ends_at'
61
- t.datetime 'created_at', null: false
62
- t.datetime 'updated_at', null: false
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 'time_slots', force: :cascade do |t|
66
- t.date 'starts_at'
67
- t.date 'ends_at'
68
- t.datetime 'created_at', null: false
69
- t.datetime 'updated_at', null: false
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 'user_meetings', force: :cascade do |t|
73
- t.integer 'user_id'
74
- t.date 'starts_at'
75
- t.date 'ends_at'
76
- t.datetime 'created_at', null: false
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 'users', force: :cascade do |t|
81
- t.string 'name'
82
- t.datetime 'created_at', null: false
83
- t.datetime 'updated_at', null: false
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,7 @@
1
+ FactoryGirl.define do
2
+ factory :document_with_enum do |d|
3
+ d.valid_from '2011-01-05'.to_date
4
+ d.valid_until '2011-01-08'.to_date
5
+ d.kind :draft
6
+ end
7
+ end
@@ -1,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/active_meeting'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe ActiveMeeting do
5
4
  context 'scoping' do
@@ -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,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/end_overlap_meeting'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe EndOverlapMeeting do
5
4
  it 'create meeting' do
@@ -1,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/meeting'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe Meeting do
5
4
  context 'Validation' do
@@ -1,7 +1,4 @@
1
- require_relative '../../../spec_helper'
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,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/secure_meeting'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe SecureMeeting do
5
4
  context 'A model with a UUID as a primary key' do
@@ -1,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/shift'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe Meeting do
5
4
  context 'Validation' do
@@ -1,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/start_end_overlap_meeting'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe StartEndOverlapMeeting do
5
4
  it 'create meeting' do
@@ -1,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/start_overlap_meeting'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe StartOverlapMeeting do
5
4
  it 'create meeting' do
@@ -1,7 +1,4 @@
1
- require_relative '../../../spec_helper'
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
@@ -1,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/user_meeting'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe UserMeeting do
5
4
  it 'create johns meeting' do
@@ -1,5 +1,4 @@
1
- require_relative '../../../spec_helper'
2
- require_relative '../factories/user'
1
+ require "#{File.dirname(__FILE__)}/../../../spec_helper"
3
2
 
4
3
  describe User do
5
4
  it 'create user' 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
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
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
@@ -27,4 +27,5 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency 'rb-readline'
28
28
  s.add_development_dependency 'database_cleaner'
29
29
  s.add_development_dependency 'rubocop'
30
+ s.add_development_dependency 'test-unit'
30
31
  end
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.0
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