riveter 0.0.6 → 0.0.7
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/Gemfile.lock +21 -21
- data/README.md +233 -16
- data/app/helpers/riveter/enquiry_form_helper.rb +4 -0
- data/config/locales/validators.en.yml +5 -0
- data/lib/generators/riveter/command/templates/command.rb +1 -0
- data/lib/generators/riveter/query_filter/templates/query_filter.rb +2 -1
- data/lib/riveter.rb +5 -3
- data/lib/riveter/attributes.rb +58 -7
- data/lib/riveter/date_range_validator.rb +34 -0
- data/lib/riveter/enumerated.rb +1 -6
- data/lib/riveter/version.rb +1 -1
- data/riveter.gemspec +15 -15
- data/spec/examples/attribute_examples.rb +8 -6
- data/spec/helpers/riveter/enquiry_form_helper_spec.rb +16 -0
- data/spec/riveter/attributes_spec.rb +44 -10
- data/spec/riveter/booleaness_validator_spec.rb +60 -62
- data/spec/riveter/date_range_validator_spec.rb +97 -0
- data/spec/riveter/email_validator_spec.rb +73 -0
- data/spec/riveter/enumerated_spec.rb +2 -2
- data/spec/support/test_class_with_attributes.rb +1 -0
- data/spec/support/validate_date_range_of_matcher.rb +17 -0
- metadata +63 -56
@@ -0,0 +1,34 @@
|
|
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
|
data/lib/riveter/enumerated.rb
CHANGED
@@ -25,13 +25,8 @@ module Riveter
|
|
25
25
|
const_names
|
26
26
|
end
|
27
27
|
|
28
|
-
# returns an array of the constant names as symbols
|
29
|
-
define_singleton_method :values do
|
30
|
-
const_values
|
31
|
-
end
|
32
|
-
|
33
28
|
# returns an array of constant values
|
34
|
-
define_singleton_method :
|
29
|
+
define_singleton_method :values do
|
35
30
|
const_values
|
36
31
|
end
|
37
32
|
|
data/lib/riveter/version.rb
CHANGED
data/riveter.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['Chris Stefano']
|
10
10
|
spec.email = ['virtualstaticvoid@gmail.com']
|
11
11
|
spec.summary = %q{Provides several useful patterns, packaged in a gem, for use in Rails.}
|
12
|
-
spec.description = %q{Provides several useful patterns, such as Enumerated, Command, Enquiry, Query, QueryFilter, Service
|
12
|
+
spec.description = %q{Provides several useful patterns, such as Enumerated, Command, Enquiry, Query, QueryFilter, Service and Presenter, packaged in a gem, for use in Rails and other Ruby applications.}
|
13
13
|
spec.homepage = 'https://github.com/virtualstaticvoid/riveter'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
@@ -21,20 +21,20 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.required_ruby_version = '>= 2.0.0'
|
22
22
|
|
23
23
|
spec.add_dependency 'railties', '~> 4.0.0'
|
24
|
-
spec.add_dependency 'actionpack'
|
25
|
-
spec.add_dependency 'activemodel'
|
26
|
-
spec.add_dependency 'activerecord'
|
27
|
-
spec.add_dependency 'activesupport'
|
28
|
-
spec.add_dependency 'validates_timeliness'
|
24
|
+
spec.add_dependency 'actionpack', '~> 4.0.0'
|
25
|
+
spec.add_dependency 'activemodel', '~> 4.0.0'
|
26
|
+
spec.add_dependency 'activerecord', '~> 4.0.0'
|
27
|
+
spec.add_dependency 'activesupport', '~> 4.0.0'
|
28
|
+
spec.add_dependency 'validates_timeliness', '~> 3.0.0'
|
29
29
|
|
30
|
-
spec.add_development_dependency 'bundler', '~> 1.
|
31
|
-
spec.add_development_dependency 'rake'
|
32
|
-
spec.add_development_dependency 'rails', '~> 4.0.
|
33
|
-
spec.add_development_dependency 'haml-rails'
|
34
|
-
spec.add_development_dependency 'rspec-rails'
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.6.0'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.3.0'
|
32
|
+
spec.add_development_dependency 'rails', '~> 4.0.0'
|
33
|
+
spec.add_development_dependency 'haml-rails', '~> 0.5.0'
|
34
|
+
spec.add_development_dependency 'rspec-rails', '~> 2.14.0'
|
35
35
|
spec.add_development_dependency 'shoulda-matchers', '~> 2.6.1'
|
36
|
-
spec.add_development_dependency 'ammeter'
|
37
|
-
spec.add_development_dependency
|
38
|
-
spec.add_development_dependency 'pry'
|
39
|
-
spec.add_development_dependency 'pry-byebug'
|
36
|
+
spec.add_development_dependency 'ammeter', '~> 1.0.0'
|
37
|
+
spec.add_development_dependency 'coveralls', '~> 0.7.0'
|
38
|
+
spec.add_development_dependency 'pry', '~> 0.9.0'
|
39
|
+
spec.add_development_dependency 'pry-byebug', '~> 1.3.0'
|
40
40
|
end
|
@@ -7,34 +7,36 @@ shared_examples_for "an attribute" do |type, default_value, *args, &block|
|
|
7
7
|
let(:assigned_value) { default_value }
|
8
8
|
let(:expected_value) { assigned_value }
|
9
9
|
|
10
|
-
describe type do
|
10
|
+
describe "of type '#{type}'" do
|
11
11
|
let(:name) { "a_#{type}" }
|
12
12
|
let(:instance) { subject.new() }
|
13
13
|
|
14
|
-
describe "
|
14
|
+
describe "with" do
|
15
15
|
before do
|
16
|
-
subject.send :"attr_#{type}", name, *args
|
16
|
+
subject.send :"attr_#{type}", name, *args
|
17
17
|
end
|
18
18
|
|
19
19
|
it { subject.attributes.should include(name.to_s) }
|
20
20
|
it { instance.attributes.should include(name.to_s) }
|
21
|
+
|
21
22
|
it { instance.should respond_to(name)}
|
22
23
|
it { instance.should respond_to("#{name}=")}
|
23
24
|
|
24
|
-
it "
|
25
|
+
it "assigned via initializer" do
|
25
26
|
a = subject.new(name => assigned_value)
|
26
27
|
a.send(name).should eq(expected_value)
|
27
28
|
a.attributes[name].should eq(expected_value)
|
28
29
|
end
|
29
30
|
|
30
|
-
it "
|
31
|
+
it "assigned via writer" do
|
31
32
|
a = subject.new()
|
32
33
|
a.send("#{name}=", assigned_value)
|
34
|
+
a.send(name).should eq(expected_value)
|
33
35
|
a.attributes[name].should eq(expected_value)
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
37
|
-
describe "
|
39
|
+
describe "required" do
|
38
40
|
before do
|
39
41
|
subject.send :"attr_#{type}", name, *args, options.merge(:required => true)
|
40
42
|
end
|
@@ -23,4 +23,20 @@ describe Riveter::EnquiryFormHelper do
|
|
23
23
|
subject.enquiry_form_for(enquiry)
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
describe "#paginate_enquiry" do
|
28
|
+
it "delegates to paginate if available" do
|
29
|
+
enquiry = TestEnquiry.new()
|
30
|
+
allow(enquiry).to receive(:query) { :query }
|
31
|
+
|
32
|
+
expect(subject).to receive(:paginate).with(:query, {:a => :b})
|
33
|
+
subject.paginate_enquiry(enquiry, {:a => :b})
|
34
|
+
end
|
35
|
+
|
36
|
+
it "does nothing unless paginate method exists" do
|
37
|
+
enquiry = TestEnquiry.new()
|
38
|
+
|
39
|
+
subject.paginate_enquiry(enquiry).should be_nil
|
40
|
+
end
|
41
|
+
end
|
26
42
|
end
|
@@ -60,6 +60,43 @@ describe Riveter::Attributes do
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
+
it_should_behave_like "an attribute", :date_range, Date.new(2010, 1, 12)..Date.new(2012, 1, 11) do
|
64
|
+
describe "additional" do
|
65
|
+
before do
|
66
|
+
subject.attr_date_range :an_attribute
|
67
|
+
end
|
68
|
+
let(:instance) { subject.new() }
|
69
|
+
|
70
|
+
it { instance.should validate_date_range_of(:an_attribute) }
|
71
|
+
it { instance.should validate_timeliness_of(:an_attribute_from) }
|
72
|
+
it { instance.should validate_timeliness_of(:an_attribute_to) }
|
73
|
+
|
74
|
+
it { instance.should respond_to(:an_attribute_from?) }
|
75
|
+
|
76
|
+
it {
|
77
|
+
instance.an_attribute = nil
|
78
|
+
instance.an_attribute_from?.should be_false
|
79
|
+
}
|
80
|
+
|
81
|
+
it {
|
82
|
+
instance.an_attribute_from = Date.today
|
83
|
+
instance.an_attribute_from?.should be_true
|
84
|
+
}
|
85
|
+
|
86
|
+
it { instance.should respond_to(:an_attribute_to?) }
|
87
|
+
|
88
|
+
it {
|
89
|
+
instance.an_attribute = nil
|
90
|
+
instance.an_attribute_to?.should be_false
|
91
|
+
}
|
92
|
+
|
93
|
+
it {
|
94
|
+
instance.an_attribute_to = Date.today
|
95
|
+
instance.an_attribute_to?.should be_true
|
96
|
+
}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
63
100
|
it_should_behave_like "an attribute", :time, Time.new(2010, 1, 12, 8, 4, 45) do
|
64
101
|
let(:assigned_value) { '2010-01-12 08:04:12' }
|
65
102
|
let(:expected_value) { Time.new(2010, 1, 12, 8, 4, 12) }
|
@@ -77,15 +114,6 @@ describe Riveter::Attributes do
|
|
77
114
|
it_should_behave_like "an attribute", :boolean, true do
|
78
115
|
let(:assigned_value) { '0' }
|
79
116
|
let(:expected_value) { false }
|
80
|
-
|
81
|
-
describe "additional" do
|
82
|
-
before do
|
83
|
-
subject.attr_boolean :an_attribute
|
84
|
-
end
|
85
|
-
let(:instance) { subject.new() }
|
86
|
-
|
87
|
-
it { instance.should validate_booleaness_of(:an_attribute) }
|
88
|
-
end
|
89
117
|
end
|
90
118
|
|
91
119
|
it_should_behave_like "an attribute", :enum, TestEnum::Member1, TestEnum do
|
@@ -98,7 +126,7 @@ describe Riveter::Attributes do
|
|
98
126
|
end
|
99
127
|
let(:instance) { subject.new() }
|
100
128
|
|
101
|
-
it { instance.should ensure_inclusion_of(:product_type).in_array(TestEnum.
|
129
|
+
it { instance.should ensure_inclusion_of(:product_type).in_array(TestEnum.values) }
|
102
130
|
|
103
131
|
it { should respond_to(:product_type_enum)}
|
104
132
|
it { subject.product_type_enum.should eq(TestEnum) }
|
@@ -167,6 +195,9 @@ describe Riveter::Attributes do
|
|
167
195
|
subject.integer.should eq(1)
|
168
196
|
subject.decimal.should eq(9.998)
|
169
197
|
subject.date.should eq(Date.new(2010, 1, 12))
|
198
|
+
subject.date_range.should eq(Date.new(2010, 1, 12)..Date.new(2011, 1, 12))
|
199
|
+
subject.date_range_from.should eq(Date.new(2010, 1, 12))
|
200
|
+
subject.date_range_to.should eq(Date.new(2011, 1, 12))
|
170
201
|
subject.time.should eq(Time.new(2010, 1, 12, 14, 56))
|
171
202
|
subject.boolean.should eq(true)
|
172
203
|
subject.enum.should eq(TestEnum::Member1)
|
@@ -187,6 +218,9 @@ describe Riveter::Attributes do
|
|
187
218
|
'integer' => 1,
|
188
219
|
'decimal' => 9.998,
|
189
220
|
'date' => Date.new(2010, 1, 12),
|
221
|
+
'date_range' => Date.new(2010, 1, 12)..Date.new(2011, 1, 12),
|
222
|
+
'date_range_from' => Date.new(2010, 1, 12),
|
223
|
+
'date_range_to' => Date.new(2011, 1, 12),
|
190
224
|
'time' => Time.new(2010, 1, 12, 14, 56),
|
191
225
|
'boolean' => true,
|
192
226
|
'enum' => TestEnum::Member1,
|
@@ -16,68 +16,66 @@ describe Riveter::BooleanessValidator do
|
|
16
16
|
errors
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
}
|
80
|
-
end
|
19
|
+
describe "with defaults" do
|
20
|
+
it {
|
21
|
+
validator = subject.new(:attributes => [:test])
|
22
|
+
record = record_for('not_a_boolean')
|
23
|
+
expect(record).to receive(:errors).and_return(errors)
|
24
|
+
|
25
|
+
validator.validate(record)
|
26
|
+
}
|
27
|
+
|
28
|
+
it {
|
29
|
+
validator = subject.new(:attributes => [:test])
|
30
|
+
record = record_for(true)
|
31
|
+
expect(record).to_not receive(:errors)
|
32
|
+
|
33
|
+
validator.validate(record)
|
34
|
+
}
|
35
|
+
|
36
|
+
it {
|
37
|
+
validator = subject.new(:attributes => [:test])
|
38
|
+
record = record_for(false)
|
39
|
+
expect(record).to_not receive(:errors)
|
40
|
+
|
41
|
+
validator.validate(record)
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "with allow_nil" do
|
46
|
+
it {
|
47
|
+
validator = subject.new(:attributes => [:test], :allow_nil => false)
|
48
|
+
record = record_for(nil)
|
49
|
+
expect(record).to receive(:errors).and_return(errors)
|
50
|
+
|
51
|
+
validator.validate(record)
|
52
|
+
}
|
53
|
+
|
54
|
+
it {
|
55
|
+
validator = subject.new(:attributes => [:test], :allow_nil => true)
|
56
|
+
record = record_for(nil)
|
57
|
+
expect(record).to_not receive(:errors)
|
58
|
+
|
59
|
+
validator.validate(record)
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "with allow_blank" do
|
64
|
+
it {
|
65
|
+
validator = subject.new(:attributes => [:test], :allow_blank => false)
|
66
|
+
record = record_for('')
|
67
|
+
expect(record).to receive(:errors).and_return(errors)
|
68
|
+
|
69
|
+
validator.validate(record)
|
70
|
+
}
|
71
|
+
|
72
|
+
it {
|
73
|
+
validator = subject.new(:attributes => [:test], :allow_blank => true)
|
74
|
+
record = record_for('')
|
75
|
+
expect(record).to_not receive(:errors)
|
76
|
+
|
77
|
+
validator.validate(record)
|
78
|
+
}
|
81
79
|
end
|
82
80
|
end
|
83
81
|
end
|
@@ -0,0 +1,97 @@
|
|
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
|