riveter 0.2.1 → 0.3.0
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/.ruby-version +1 -1
- data/.travis.yml +3 -6
- data/Gemfile.lock +6 -6
- data/config/locales/validators.en.yml +0 -3
- data/lib/generators/riveter/query_filter/templates/query_filter.rb +22 -18
- data/lib/riveter.rb +0 -2
- data/lib/riveter/attributes.rb +55 -15
- data/lib/riveter/version.rb +1 -1
- data/riveter.gemspec +1 -1
- data/spec/riveter/attributes_spec.rb +0 -1
- metadata +5 -10
- data/lib/riveter/date_range_validator.rb +0 -34
- data/spec/riveter/date_range_validator_spec.rb +0 -97
- data/spec/support/validate_date_range_of_matcher.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac7829381fcfecbda57c395c23653edc8df876b3
|
4
|
+
data.tar.gz: 57005498bcf3833e4b487f8d1e174cfd6adb5b77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cda2c9c10fa00fef707ed7d8bff72b40684ed8f3f088fb8fecb6879988d30a4ce02d0b57056da324bb8c1a77ac5a3fe9ba822bfa9082ad75ee767d00bbade38
|
7
|
+
data.tar.gz: e0853c278c745da742bf13bd9e6d6b2a1319f0297b09b0a9864eccf77b7b397eaa979025f7848ddd683fbaf335f2ab4bbef621c63003747385a72f21722ddcd7
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.2.
|
1
|
+
ruby-2.2.4
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
riveter (0.
|
4
|
+
riveter (0.3.0)
|
5
5
|
activemodel (>= 4.0.0)
|
6
6
|
railties (>= 4.0.0)
|
7
7
|
validates_timeliness (>= 3.0.0)
|
@@ -173,22 +173,22 @@ GEM
|
|
173
173
|
thor (0.19.1)
|
174
174
|
thread_safe (0.3.5)
|
175
175
|
tilt (2.0.1)
|
176
|
-
timeliness (0.3.
|
176
|
+
timeliness (0.3.8)
|
177
177
|
tins (1.5.2)
|
178
178
|
tzinfo (1.2.2)
|
179
179
|
thread_safe (~> 0.1)
|
180
180
|
unf (0.1.4)
|
181
181
|
unf_ext
|
182
182
|
unf_ext (0.0.7.1)
|
183
|
-
validates_timeliness (
|
184
|
-
timeliness (~> 0.3.
|
183
|
+
validates_timeliness (4.0.2)
|
184
|
+
timeliness (~> 0.3.7)
|
185
185
|
|
186
186
|
PLATFORMS
|
187
187
|
ruby
|
188
188
|
|
189
189
|
DEPENDENCIES
|
190
190
|
ammeter (~> 1.1.2)
|
191
|
-
bundler (~> 1.
|
191
|
+
bundler (~> 1.11.2)
|
192
192
|
coveralls (~> 0.8.1)
|
193
193
|
haml-rails (~> 0.9.0)
|
194
194
|
pry (~> 0.10.1)
|
@@ -200,4 +200,4 @@ DEPENDENCIES
|
|
200
200
|
shoulda-matchers (~> 2.8.0)
|
201
201
|
|
202
202
|
BUNDLED WITH
|
203
|
-
1.
|
203
|
+
1.11.2
|
@@ -11,30 +11,34 @@ class <%= class_name %>QueryFilter < Riveter::QueryFilter::Base
|
|
11
11
|
#
|
12
12
|
# E.g.
|
13
13
|
#
|
14
|
-
# attr_string :name[, options]
|
14
|
+
# attr_string :name[, options] [do |value|
|
15
|
+
# # optional converter logic
|
16
|
+
# value.to_i
|
17
|
+
# end]
|
15
18
|
#
|
16
19
|
# options include:
|
17
|
-
# :required => true|false
|
18
|
-
# :default => 'a value' # default is nil
|
19
|
-
# :validate => true|false
|
20
|
+
# :required => true|false # default is false
|
21
|
+
# :default => 'a value' or lambda # default is nil
|
22
|
+
# :validate => true|false # default is true
|
23
|
+
#
|
24
|
+
# optionally, provide a block which takes one argument and returns the converted value
|
20
25
|
#
|
21
26
|
# supported attributes:
|
22
27
|
#
|
23
|
-
# attr_string
|
24
|
-
# attr_text
|
25
|
-
# attr_integer
|
26
|
-
# attr_decimal
|
27
|
-
# attr_date
|
28
|
-
# attr_date_range
|
29
|
-
# attr_time
|
30
|
-
# attr_boolean
|
31
|
-
# attr_enum
|
32
|
-
# attr_array
|
33
|
-
# attr_hash
|
34
|
-
#
|
28
|
+
# attr_string :attr_name
|
29
|
+
# attr_text :attr_name
|
30
|
+
# attr_integer :attr_name
|
31
|
+
# attr_decimal :attr_name
|
32
|
+
# attr_date :attr_name
|
33
|
+
# attr_date_range :attr_name[, :min => <date>, :max => <date>]
|
34
|
+
# attr_time :attr_name
|
35
|
+
# attr_boolean :attr_name
|
36
|
+
# attr_enum :attr_name, <enum_module>
|
37
|
+
# attr_array :attr_name[, :data_type => :integer]
|
38
|
+
# attr_hash :attr_name[, :data_type => :integer]
|
39
|
+
#
|
40
|
+
# optionally, define additional validations and helper methods
|
35
41
|
#
|
36
|
-
|
37
|
-
# optionally, define additional validations
|
38
42
|
<% end -%>
|
39
43
|
|
40
44
|
end
|
data/lib/riveter.rb
CHANGED
@@ -14,7 +14,6 @@ module Riveter
|
|
14
14
|
autoload :BooleanessValidator, 'riveter/booleaness_validator'
|
15
15
|
autoload :Command, 'riveter/command'
|
16
16
|
autoload :CommandController, 'riveter/command_controller'
|
17
|
-
autoload :DateRangeValidator, 'riveter/date_range_validator'
|
18
17
|
autoload :EmailValidator, 'riveter/email_validator'
|
19
18
|
autoload :Enquiry, 'riveter/enquiry'
|
20
19
|
autoload :EnquiryController, 'riveter/enquiry_controller'
|
@@ -30,7 +29,6 @@ end
|
|
30
29
|
module ActiveModel
|
31
30
|
module Validations
|
32
31
|
autoload :BooleanessValidator, 'riveter/booleaness_validator'
|
33
|
-
autoload :DateRangeValidator, 'riveter/date_range_validator'
|
34
32
|
autoload :EmailValidator, 'riveter/email_validator'
|
35
33
|
end
|
36
34
|
end
|
data/lib/riveter/attributes.rb
CHANGED
@@ -61,15 +61,13 @@ module Riveter
|
|
61
61
|
|
62
62
|
required = (true == options.delete(:required))
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
:to => default.last
|
72
|
-
}
|
64
|
+
[:min, :max].each do |limit|
|
65
|
+
limit_value = options.delete(limit)
|
66
|
+
|
67
|
+
define_method :"#{name}_#{limit}" do
|
68
|
+
limit_value.respond_to?(:call) ? limit_value.call : limit_value
|
69
|
+
end
|
70
|
+
end
|
73
71
|
|
74
72
|
converter = block_given? ? block : Converters.converter_for(:date)
|
75
73
|
|
@@ -79,18 +77,31 @@ module Riveter
|
|
79
77
|
send(:"#{name}_from")..send(:"#{name}_to") rescue nil
|
80
78
|
end
|
81
79
|
|
82
|
-
define_method "#{name}=" do |value|
|
80
|
+
define_method :"#{name}=" do |value|
|
83
81
|
value ||= nil..nil
|
84
82
|
range = value.is_a?(Range) ? value : value..value
|
85
83
|
send(:"#{name}_from=", range.first)
|
86
84
|
send(:"#{name}_to=", range.last)
|
87
85
|
end
|
88
86
|
|
89
|
-
|
90
|
-
|
91
|
-
|
87
|
+
add_attr(name, :date_range, converter, options)
|
88
|
+
|
89
|
+
if options[:validate]
|
90
|
+
|
91
|
+
validate :"#{name}_validation"
|
92
|
+
|
93
|
+
define_method :"#{name}_validation" do
|
94
|
+
date_from = send(:"#{name}_from")
|
95
|
+
date_to = send(:"#{name}_to")
|
96
|
+
|
97
|
+
errors.add(name, :blank) if
|
98
|
+
required && (date_from.blank? || date_to.blank?)
|
99
|
+
|
100
|
+
errors.add(name, :invalid) if
|
101
|
+
!(date_from.blank? || date_to.blank?) && (date_from > date_to || date_to < date_from)
|
102
|
+
end
|
92
103
|
|
93
|
-
|
104
|
+
end
|
94
105
|
|
95
106
|
# break down into parts
|
96
107
|
[:from, :to].each do |part|
|
@@ -106,8 +117,32 @@ module Riveter
|
|
106
117
|
:allow_nil => !required,
|
107
118
|
:timeliness => { :type => :date } if options[:validate]
|
108
119
|
|
109
|
-
add_attr(:"#{name}_#{part}", :date, converter
|
120
|
+
add_attr(:"#{name}_#{part}", :date, converter)
|
121
|
+
end
|
122
|
+
|
123
|
+
# helper for determining if both from and to dates have been provided
|
124
|
+
define_method :"#{name}_present?" do
|
125
|
+
date_from = send(:"#{name}_from")
|
126
|
+
date_to = send(:"#{name}_to")
|
127
|
+
date_from && date_to
|
128
|
+
end
|
129
|
+
|
130
|
+
# return from and to as range in UTC
|
131
|
+
define_method :"#{name}_utc" do
|
132
|
+
date_from = send(:"#{name}_from")
|
133
|
+
date_to = send(:"#{name}_to")
|
134
|
+
if date_from && date_to
|
135
|
+
if date_from == date_to
|
136
|
+
date = date_from.to_utc_date
|
137
|
+
DateTime.new(date.year, date.month, date.day, 0, 0, 0)..DateTime.new(date.year, date.month, date.day, 23, 59, 59)
|
138
|
+
else
|
139
|
+
date_from.to_utc_date..date_to.to_utc_date
|
140
|
+
end
|
141
|
+
else
|
142
|
+
nil
|
143
|
+
end
|
110
144
|
end
|
145
|
+
|
111
146
|
end
|
112
147
|
|
113
148
|
def attr_boolean(name, options={}, &block)
|
@@ -311,6 +346,7 @@ module Riveter
|
|
311
346
|
|
312
347
|
def default
|
313
348
|
@default ||= options[:default]
|
349
|
+
@default.respond_to?(:call) ? @default.call : @default
|
314
350
|
end
|
315
351
|
|
316
352
|
def default?
|
@@ -354,6 +390,10 @@ module Riveter
|
|
354
390
|
false
|
355
391
|
end
|
356
392
|
|
393
|
+
def has_attribute?(column)
|
394
|
+
self.class._attributes.key?(column)
|
395
|
+
end
|
396
|
+
|
357
397
|
# forms use this for getting column meta data
|
358
398
|
def column_for_attribute(column)
|
359
399
|
attribute_info = self.class._attributes[column]
|
data/lib/riveter/version.rb
CHANGED
data/riveter.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency 'activemodel' , '>= 4.0.0'
|
25
25
|
spec.add_dependency 'validates_timeliness' , '>= 3.0.0'
|
26
26
|
|
27
|
-
spec.add_development_dependency 'bundler' , '~> 1.
|
27
|
+
spec.add_development_dependency 'bundler' , '~> 1.11.2'
|
28
28
|
spec.add_development_dependency 'rake' , '~> 10.4.2'
|
29
29
|
spec.add_development_dependency 'rails' , '~> 4.2.1'
|
30
30
|
spec.add_development_dependency 'haml-rails' , '~> 0.9.0'
|
@@ -67,7 +67,6 @@ describe Riveter::Attributes do
|
|
67
67
|
end
|
68
68
|
let(:instance) { subject.new() }
|
69
69
|
|
70
|
-
it { instance.should validate_date_range_of(:an_attribute) }
|
71
70
|
it { instance.should validate_timeliness_of(:an_attribute_from) }
|
72
71
|
it { instance.should validate_timeliness_of(:an_attribute_to) }
|
73
72
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: riveter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Stefano
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1.
|
61
|
+
version: 1.11.2
|
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.
|
68
|
+
version: 1.11.2
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -304,7 +304,6 @@ files:
|
|
304
304
|
- lib/riveter/command_controller.rb
|
305
305
|
- lib/riveter/command_routes.rb
|
306
306
|
- lib/riveter/core_extensions.rb
|
307
|
-
- lib/riveter/date_range_validator.rb
|
308
307
|
- lib/riveter/email_validator.rb
|
309
308
|
- lib/riveter/enquiry.rb
|
310
309
|
- lib/riveter/enquiry_controller.rb
|
@@ -357,7 +356,6 @@ files:
|
|
357
356
|
- spec/riveter/command_routes_spec.rb
|
358
357
|
- spec/riveter/command_spec.rb
|
359
358
|
- spec/riveter/core_extensions_spec.rb
|
360
|
-
- spec/riveter/date_range_validator_spec.rb
|
361
359
|
- spec/riveter/email_validator_spec.rb
|
362
360
|
- spec/riveter/enquiry_controller_spec.rb
|
363
361
|
- spec/riveter/enquiry_routes_spec.rb
|
@@ -387,7 +385,6 @@ files:
|
|
387
385
|
- spec/support/test_query_filter.rb
|
388
386
|
- spec/support/test_service.rb
|
389
387
|
- spec/support/validate_booleaness_of_matcher.rb
|
390
|
-
- spec/support/validate_date_range_of_matcher.rb
|
391
388
|
- spec/support/validate_timeliness_of_matcher.rb
|
392
389
|
- spec/support/validator_detector.rb
|
393
390
|
homepage: https://github.com/virtualstaticvoid/riveter
|
@@ -410,7 +407,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
410
407
|
version: '0'
|
411
408
|
requirements: []
|
412
409
|
rubyforge_project:
|
413
|
-
rubygems_version: 2.4.
|
410
|
+
rubygems_version: 2.4.8
|
414
411
|
signing_key:
|
415
412
|
specification_version: 4
|
416
413
|
summary: Provides several useful patterns, packaged in a gem, for use in Rails.
|
@@ -449,7 +446,6 @@ test_files:
|
|
449
446
|
- spec/riveter/command_routes_spec.rb
|
450
447
|
- spec/riveter/command_spec.rb
|
451
448
|
- spec/riveter/core_extensions_spec.rb
|
452
|
-
- spec/riveter/date_range_validator_spec.rb
|
453
449
|
- spec/riveter/email_validator_spec.rb
|
454
450
|
- spec/riveter/enquiry_controller_spec.rb
|
455
451
|
- spec/riveter/enquiry_routes_spec.rb
|
@@ -479,6 +475,5 @@ test_files:
|
|
479
475
|
- spec/support/test_query_filter.rb
|
480
476
|
- spec/support/test_service.rb
|
481
477
|
- spec/support/validate_booleaness_of_matcher.rb
|
482
|
-
- spec/support/validate_date_range_of_matcher.rb
|
483
478
|
- spec/support/validate_timeliness_of_matcher.rb
|
484
479
|
- spec/support/validator_detector.rb
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Riveter
|
2
|
-
class DateRangeValidator < ActiveModel::EachValidator
|
3
|
-
def initialize(options={})
|
4
|
-
@date_from_attr = options.delete(:date_from_attr)
|
5
|
-
@date_to_attr = options.delete(:date_to_attr)
|
6
|
-
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
def validate_each(record, attribute, value)
|
11
|
-
# NOTE: validate_each usually isn't called when the value is nil
|
12
|
-
# however it is possible to have a nil..nil range, so check for this
|
13
|
-
if value.is_a?(Range) && value.first && value.last
|
14
|
-
date_from = value.first
|
15
|
-
date_to = value.last
|
16
|
-
if date_from > date_to
|
17
|
-
record.errors.add(attribute, :date_range)
|
18
|
-
record.errors.add(@date_from_attr || :"#{attribute}_from", :date_range_date_from, :date_to => date_to)
|
19
|
-
record.errors.add(@date_to_attr || :"#{attribute}_to", :date_range_date_to, :date_from => date_from)
|
20
|
-
end
|
21
|
-
else
|
22
|
-
unless options[:allow_nil] || options[:allow_blank]
|
23
|
-
record.errors.add(attribute, :blank)
|
24
|
-
record.errors.add(@date_from_attr || :"#{attribute}_from", :blank)
|
25
|
-
record.errors.add(@date_to_attr || :"#{attribute}_to", :blank)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# add compatibility with ActiveModel validates method which
|
33
|
-
# matches option keys to their validator class
|
34
|
-
ActiveModel::Validations::DateRangeValidator = Riveter::DateRangeValidator
|
@@ -1,97 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Riveter::DateRangeValidator do
|
4
|
-
subject { Riveter::DateRangeValidator }
|
5
|
-
|
6
|
-
describe "#validate_each" do
|
7
|
-
def record_for(value)
|
8
|
-
record = double()
|
9
|
-
allow(record).to receive(:read_attribute_for_validation) { value }
|
10
|
-
record
|
11
|
-
end
|
12
|
-
|
13
|
-
def errors
|
14
|
-
errors = double
|
15
|
-
allow(errors).to receive(:add)
|
16
|
-
errors
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "with defaults" do
|
20
|
-
it {
|
21
|
-
validator = subject.new(:attributes => [:test])
|
22
|
-
record = record_for('not_a_date_range')
|
23
|
-
expect(record).to receive(:errors).at_least(:once).and_return(errors)
|
24
|
-
|
25
|
-
validator.validate(record)
|
26
|
-
}
|
27
|
-
|
28
|
-
it {
|
29
|
-
validator = subject.new(:attributes => [:test])
|
30
|
-
record = record_for(nil..nil)
|
31
|
-
expect(record).to receive(:errors).at_least(:once).and_return(errors)
|
32
|
-
|
33
|
-
validator.validate(record)
|
34
|
-
}
|
35
|
-
|
36
|
-
it {
|
37
|
-
validator = subject.new(:attributes => [:test])
|
38
|
-
record = record_for(Date.new(2010, 1, 1)..Date.new(2011, 1, 1))
|
39
|
-
expect(record).to_not receive(:errors)
|
40
|
-
|
41
|
-
validator.validate(record)
|
42
|
-
}
|
43
|
-
|
44
|
-
it {
|
45
|
-
validator = subject.new(:attributes => [:test])
|
46
|
-
record = record_for(Date.new(2012, 1, 1)..Date.new(2011, 1, 1))
|
47
|
-
expect(record).to receive(:errors).at_least(:once).and_return(errors)
|
48
|
-
|
49
|
-
validator.validate(record)
|
50
|
-
}
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "with allow_nil" do
|
54
|
-
it {
|
55
|
-
validator = subject.new(:attributes => [:test], :allow_nil => false)
|
56
|
-
record = record_for(nil)
|
57
|
-
expect(record).to receive(:errors).at_least(:once).and_return(errors)
|
58
|
-
|
59
|
-
validator.validate(record)
|
60
|
-
}
|
61
|
-
|
62
|
-
it {
|
63
|
-
validator = subject.new(:attributes => [:test], :allow_nil => true)
|
64
|
-
record = record_for(nil)
|
65
|
-
expect(record).to_not receive(:errors)
|
66
|
-
|
67
|
-
validator.validate(record)
|
68
|
-
}
|
69
|
-
|
70
|
-
it {
|
71
|
-
validator = subject.new(:attributes => [:test], :allow_nil => true)
|
72
|
-
record = record_for(nil..nil)
|
73
|
-
expect(record).to_not receive(:errors)
|
74
|
-
|
75
|
-
validator.validate(record)
|
76
|
-
}
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "with allow_blank" do
|
80
|
-
it {
|
81
|
-
validator = subject.new(:attributes => [:test], :allow_blank => false)
|
82
|
-
record = record_for('')
|
83
|
-
expect(record).to receive(:errors).at_least(:once).and_return(errors)
|
84
|
-
|
85
|
-
validator.validate(record)
|
86
|
-
}
|
87
|
-
|
88
|
-
it {
|
89
|
-
validator = subject.new(:attributes => [:test], :allow_blank => true)
|
90
|
-
record = record_for('')
|
91
|
-
expect(record).to_not receive(:errors)
|
92
|
-
|
93
|
-
validator.validate(record)
|
94
|
-
}
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
RSpec::Matchers.define :validate_date_range_of do |attribute|
|
2
|
-
match do |model|
|
3
|
-
ValidatorDetector.detect(model, attribute, Riveter::DateRangeValidator)
|
4
|
-
end
|
5
|
-
|
6
|
-
failure_message do |actual|
|
7
|
-
"expect #{attribute} to validate date range of"
|
8
|
-
end
|
9
|
-
|
10
|
-
failure_message_when_negated do |actual|
|
11
|
-
"expect #{attribute} to not validate date range of"
|
12
|
-
end
|
13
|
-
|
14
|
-
description do
|
15
|
-
"expect #{attribute} to validate date range of"
|
16
|
-
end
|
17
|
-
end
|