riveter 0.0.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 +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +17 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +147 -0
- data/LICENSE.txt +23 -0
- data/README.md +77 -0
- data/Rakefile +7 -0
- data/app/helpers/riveter/command_form_helper.rb +15 -0
- data/app/helpers/riveter/enquiry_form_helper.rb +16 -0
- data/app/helpers/riveter/query_filter_form_helper.rb +16 -0
- data/config/locales/forms.en.yml +25 -0
- data/config/locales/validators.en.yml +26 -0
- data/init.rb +25 -0
- data/lib/generators/erb/TODO +1 -0
- data/lib/generators/haml/command_controller/USAGE +0 -0
- data/lib/generators/haml/command_controller/command_controller_generator.rb +56 -0
- data/lib/generators/haml/command_controller/templates/new.html.haml +26 -0
- data/lib/generators/haml/enquiry_controller/USAGE +0 -0
- data/lib/generators/haml/enquiry_controller/enquiry_controller_generator.rb +56 -0
- data/lib/generators/haml/enquiry_controller/templates/index.html.haml +57 -0
- data/lib/generators/riveter/command/USAGE +0 -0
- data/lib/generators/riveter/command/command_generator.rb +55 -0
- data/lib/generators/riveter/command/templates/command.rb +40 -0
- data/lib/generators/riveter/command/templates/commands.en.yml +57 -0
- data/lib/generators/riveter/command/templates/module.rb +4 -0
- data/lib/generators/riveter/command_controller/USAGE +0 -0
- data/lib/generators/riveter/command_controller/command_controller_generator.rb +40 -0
- data/lib/generators/riveter/command_controller/templates/command_controller.rb +18 -0
- data/lib/generators/riveter/command_controller/templates/module.rb +4 -0
- data/lib/generators/riveter/enquiry/USAGE +0 -0
- data/lib/generators/riveter/enquiry/enquiry_generator.rb +29 -0
- data/lib/generators/riveter/enquiry/templates/enquiry.rb +11 -0
- data/lib/generators/riveter/enquiry/templates/module.rb +4 -0
- data/lib/generators/riveter/enquiry_controller/USAGE +0 -0
- data/lib/generators/riveter/enquiry_controller/enquiry_controller_generator.rb +34 -0
- data/lib/generators/riveter/enquiry_controller/templates/enquiry_controller.rb +9 -0
- data/lib/generators/riveter/enquiry_controller/templates/module.rb +4 -0
- data/lib/generators/riveter/enum/USAGE +41 -0
- data/lib/generators/riveter/enum/enum_generator.rb +51 -0
- data/lib/generators/riveter/enum/templates/enum.rb +17 -0
- data/lib/generators/riveter/enum/templates/enums.en.yml +35 -0
- data/lib/generators/riveter/enum/templates/module.rb +4 -0
- data/lib/generators/riveter/install/USAGE +0 -0
- data/lib/generators/riveter/install/install_generator.rb +14 -0
- data/lib/generators/riveter/presenter/USAGE +0 -0
- data/lib/generators/riveter/presenter/presenter_generator.rb +20 -0
- data/lib/generators/riveter/presenter/templates/module.rb +4 -0
- data/lib/generators/riveter/presenter/templates/presenter.rb +7 -0
- data/lib/generators/riveter/query/USAGE +22 -0
- data/lib/generators/riveter/query/query_generator.rb +20 -0
- data/lib/generators/riveter/query/templates/module.rb +4 -0
- data/lib/generators/riveter/query/templates/query.rb +31 -0
- data/lib/generators/riveter/query_filter/USAGE +0 -0
- data/lib/generators/riveter/query_filter/query_filter_generator.rb +52 -0
- data/lib/generators/riveter/query_filter/templates/module.rb +4 -0
- data/lib/generators/riveter/query_filter/templates/query_filter.rb +40 -0
- data/lib/generators/riveter/query_filter/templates/query_filters.en.yml +49 -0
- data/lib/generators/riveter/service/USAGE +0 -0
- data/lib/generators/riveter/service/service_generator.rb +20 -0
- data/lib/generators/riveter/service/templates/module.rb +4 -0
- data/lib/generators/riveter/service/templates/service.rb +12 -0
- data/lib/generators/riveter/worker/USAGE +0 -0
- data/lib/generators/riveter/worker/templates/module.rb +4 -0
- data/lib/generators/riveter/worker/templates/worker.rb +7 -0
- data/lib/generators/riveter/worker/worker_generator.rb +20 -0
- data/lib/generators/rspec/command/USAGE +0 -0
- data/lib/generators/rspec/command/command_generator.rb +11 -0
- data/lib/generators/rspec/command/templates/command_spec.rb +8 -0
- data/lib/generators/rspec/command_controller/USAGE +0 -0
- data/lib/generators/rspec/command_controller/command_controller_generator.rb +21 -0
- data/lib/generators/rspec/command_controller/templates/command_controller_spec.rb +70 -0
- data/lib/generators/rspec/enquiry/USAGE +0 -0
- data/lib/generators/rspec/enquiry/enquiry_generator.rb +11 -0
- data/lib/generators/rspec/enquiry/templates/enquiry_spec.rb +69 -0
- data/lib/generators/rspec/enquiry_controller/USAGE +0 -0
- data/lib/generators/rspec/enquiry_controller/enquiry_controller_generator.rb +16 -0
- data/lib/generators/rspec/enquiry_controller/templates/enquiry_controller_spec.rb +32 -0
- data/lib/generators/rspec/enum/USAGE +0 -0
- data/lib/generators/rspec/enum/enum_generator.rb +11 -0
- data/lib/generators/rspec/enum/templates/enum_spec.rb +8 -0
- data/lib/generators/rspec/presenter/USAGE +0 -0
- data/lib/generators/rspec/presenter/presenter_generator.rb +11 -0
- data/lib/generators/rspec/presenter/templates/presenter_spec.rb +8 -0
- data/lib/generators/rspec/query/USAGE +0 -0
- data/lib/generators/rspec/query/query_generator.rb +11 -0
- data/lib/generators/rspec/query/templates/query_spec.rb +41 -0
- data/lib/generators/rspec/query_filter/USAGE +0 -0
- data/lib/generators/rspec/query_filter/query_filter_generator.rb +11 -0
- data/lib/generators/rspec/query_filter/templates/query_filter_spec.rb +8 -0
- data/lib/generators/rspec/service/USAGE +0 -0
- data/lib/generators/rspec/service/service_generator.rb +11 -0
- data/lib/generators/rspec/service/templates/service_spec.rb +8 -0
- data/lib/generators/rspec/worker/USAGE +0 -0
- data/lib/generators/rspec/worker/templates/worker_spec.rb +8 -0
- data/lib/generators/rspec/worker/worker_generator.rb +11 -0
- data/lib/generators/test_unit/TODO +1 -0
- data/lib/riveter/associated_type_registry.rb +63 -0
- data/lib/riveter/attribute_default_values.rb +67 -0
- data/lib/riveter/attributes.rb +443 -0
- data/lib/riveter/booleaness_validator.rb +20 -0
- data/lib/riveter/command.rb +59 -0
- data/lib/riveter/command_controller.rb +93 -0
- data/lib/riveter/command_routes.rb +73 -0
- data/lib/riveter/core_extensions.rb +246 -0
- data/lib/riveter/email_validator.rb +20 -0
- data/lib/riveter/enquiry.rb +137 -0
- data/lib/riveter/enquiry_controller.rb +80 -0
- data/lib/riveter/enquiry_routes.rb +69 -0
- data/lib/riveter/enumerated.rb +107 -0
- data/lib/riveter/errors.rb +11 -0
- data/lib/riveter/form_builder_extensions.rb +21 -0
- data/lib/riveter/hash_with_dependency.rb +12 -0
- data/lib/riveter/presenter.rb +73 -0
- data/lib/riveter/query.rb +45 -0
- data/lib/riveter/query_filter.rb +22 -0
- data/lib/riveter/rails/engine.rb +6 -0
- data/lib/riveter/rails/railtie.rb +50 -0
- data/lib/riveter/service.rb +45 -0
- data/lib/riveter/spec_helper.rb +55 -0
- data/lib/riveter/tasks/.keep +0 -0
- data/lib/riveter/version.rb +3 -0
- data/lib/riveter/worker.rb +20 -0
- data/lib/riveter.rb +47 -0
- data/riveter.gemspec +40 -0
- data/spec/examples/attribute_examples.rb +57 -0
- data/spec/generators/haml/command_controller/command_controller_generator_spec.rb +34 -0
- data/spec/generators/haml/enquiry_controller/enquiry_controller_generator_spec.rb +34 -0
- data/spec/generators/riveter/command/command_generator_spec.rb +58 -0
- data/spec/generators/riveter/command_controller/command_controller_generator_spec.rb +0 -0
- data/spec/generators/riveter/enquiry/enquiry_generator_spec.rb +0 -0
- data/spec/generators/riveter/query_filter/query_filter_generator_spec.rb +58 -0
- data/spec/riveter/associated_type_registry_spec.rb +158 -0
- data/spec/riveter/attribute_default_value_spec.rb +69 -0
- data/spec/riveter/attributes_spec.rb +228 -0
- data/spec/riveter/command_controller_spec.rb +116 -0
- data/spec/riveter/command_routes_spec.rb +116 -0
- data/spec/riveter/command_spec.rb +66 -0
- data/spec/riveter/core_extensions_spec.rb +209 -0
- data/spec/riveter/enquiry_controller_spec.rb +128 -0
- data/spec/riveter/enquiry_routes_spec.rb +101 -0
- data/spec/riveter/enquiry_spec.rb +299 -0
- data/spec/riveter/enumerated_spec.rb +47 -0
- data/spec/riveter/form_builder_extensions_spec.rb +28 -0
- data/spec/riveter/presenter_spec.rb +131 -0
- data/spec/riveter/query_filter_spec.rb +19 -0
- data/spec/riveter/query_spec.rb +72 -0
- data/spec/riveter/service_spec.rb +49 -0
- data/spec/riveter/spec_helper_spec.rb +21 -0
- data/spec/riveter/worker_spec.rb +11 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/support/test_associated_class.rb +2 -0
- data/spec/support/test_class_with_attributes.rb +17 -0
- data/spec/support/test_command.rb +4 -0
- data/spec/support/test_command_controller.rb +12 -0
- data/spec/support/test_command_routes.rb +3 -0
- data/spec/support/test_enquiry.rb +7 -0
- data/spec/support/test_enquiry_controller.rb +12 -0
- data/spec/support/test_enquiry_routes.rb +3 -0
- data/spec/support/test_enum.rb +8 -0
- data/spec/support/test_form_builder.rb +3 -0
- data/spec/support/test_model.rb +2 -0
- data/spec/support/test_model_with_attribute_default_values.rb +29 -0
- data/spec/support/test_presenter.rb +2 -0
- data/spec/support/test_query.rb +5 -0
- data/spec/support/test_query_filter.rb +4 -0
- data/spec/support/test_service.rb +7 -0
- data/spec/support/validate_booleaness_of_matcher.rb +17 -0
- data/spec/support/validate_timeliness_of_matcher.rb +17 -0
- data/spec/support/validator_detector.rb +48 -0
- metadata +487 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Riveter::Attributes do
|
|
4
|
+
describe "class" do
|
|
5
|
+
subject do
|
|
6
|
+
# create an anonymous class
|
|
7
|
+
Class.new().class_eval {
|
|
8
|
+
include Riveter::Attributes
|
|
9
|
+
|
|
10
|
+
def self.name
|
|
11
|
+
'TestClass'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
self
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe ".attributes" do
|
|
19
|
+
it { subject.attributes.should_not be_nil }
|
|
20
|
+
it { subject.attributes.should be_a(Array) }
|
|
21
|
+
|
|
22
|
+
it "each class has it's own set" do
|
|
23
|
+
subject.attributes.should_not eq(TestClassWithAttributes.attributes)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it_should_behave_like "an attribute", :string, 'A', 'B'
|
|
28
|
+
|
|
29
|
+
it_should_behave_like "an attribute", :text, 'B', 'C'
|
|
30
|
+
|
|
31
|
+
it_should_behave_like "an attribute", :integer, 1, 2 do
|
|
32
|
+
describe "additional" do
|
|
33
|
+
before do
|
|
34
|
+
subject.attr_integer :an_attribute
|
|
35
|
+
end
|
|
36
|
+
let(:instance) { subject.new() }
|
|
37
|
+
|
|
38
|
+
it { instance.should validate_numericality_of(:an_attribute) }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it_should_behave_like "an attribute", :date, Date.new(2010, 1, 12), Date.new(2010, 1, 13) do
|
|
43
|
+
describe "additional" do
|
|
44
|
+
before do
|
|
45
|
+
subject.attr_date :an_attribute
|
|
46
|
+
end
|
|
47
|
+
let(:instance) { subject.new() }
|
|
48
|
+
|
|
49
|
+
it { instance.should validate_timeliness_of(:an_attribute) }
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it_should_behave_like "an attribute", :time, Time.new(2010, 1, 12, 8, 4, 45), Time.new(2010, 1, 12, 8, 4, 12) do
|
|
54
|
+
describe "additional" do
|
|
55
|
+
before do
|
|
56
|
+
subject.attr_time :an_attribute
|
|
57
|
+
end
|
|
58
|
+
let(:instance) { subject.new() }
|
|
59
|
+
|
|
60
|
+
it { instance.should validate_timeliness_of(:an_attribute) }
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it_should_behave_like "an attribute", :boolean, true, false do
|
|
65
|
+
describe "additional" do
|
|
66
|
+
before do
|
|
67
|
+
subject.attr_boolean :an_attribute
|
|
68
|
+
end
|
|
69
|
+
let(:instance) { subject.new() }
|
|
70
|
+
|
|
71
|
+
it { instance.should validate_booleaness_of(:an_attribute) }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it_should_behave_like "an attribute", :enum, TestEnum::Member1, TestEnum::Member2, TestEnum do
|
|
76
|
+
describe "additional" do
|
|
77
|
+
before do
|
|
78
|
+
subject.attr_enum :product_type, TestEnum
|
|
79
|
+
end
|
|
80
|
+
let(:instance) { subject.new() }
|
|
81
|
+
|
|
82
|
+
it { instance.should ensure_inclusion_of(:product_type).in_array(TestEnum.all) }
|
|
83
|
+
|
|
84
|
+
it { should respond_to(:product_type_enum)}
|
|
85
|
+
it { subject.product_type_enum.should eq(TestEnum) }
|
|
86
|
+
it { should respond_to(:product_types)}
|
|
87
|
+
it { subject.product_types.should eq(TestEnum.collection)}
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it_should_behave_like "an attribute", :array, [1, 2, 3], [5, 6]
|
|
92
|
+
|
|
93
|
+
it_should_behave_like "an attribute", :hash, {:a => :b}, {:c => :d}
|
|
94
|
+
|
|
95
|
+
it_should_behave_like "an attribute", :model, TestModel.new(), TestModel.new(), TestModel do
|
|
96
|
+
describe "additional" do
|
|
97
|
+
before do
|
|
98
|
+
subject.attr_model :product, TestModel
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it { should respond_to(:product_model)}
|
|
102
|
+
it { subject.product_model.should eq(TestModel) }
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it_should_behave_like "an attribute", :object, Object.new(), Object.new()
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
describe "instance" do
|
|
110
|
+
subject { TestClassWithAttributes.new() }
|
|
111
|
+
|
|
112
|
+
describe "#initialize" do
|
|
113
|
+
it "assigns attribute default values" do
|
|
114
|
+
subject.string.should eq('A')
|
|
115
|
+
subject.text.should eq('b')
|
|
116
|
+
subject.integer.should eq(1)
|
|
117
|
+
subject.decimal.should eq(9.998)
|
|
118
|
+
subject.date.should eq(Date.new(2010, 1, 12))
|
|
119
|
+
subject.time.should eq(Time.new(2010, 1, 12, 14, 56))
|
|
120
|
+
subject.boolean.should eq(true)
|
|
121
|
+
subject.enum.should eq(TestEnum::Member1)
|
|
122
|
+
subject.array.should eq([1, 2, 3])
|
|
123
|
+
subject.hash.should eq({:a => :b})
|
|
124
|
+
subject.model.should eq(TestModel)
|
|
125
|
+
subject.object.should eq('whatever')
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
describe "#attributes" do
|
|
130
|
+
it { subject.attributes.should_not be_nil }
|
|
131
|
+
it { subject.attributes.should be_a(Hash) }
|
|
132
|
+
it {
|
|
133
|
+
subject.attributes.should eq({
|
|
134
|
+
'string' => 'A',
|
|
135
|
+
'text' => 'b',
|
|
136
|
+
'integer' => 1,
|
|
137
|
+
'decimal' => 9.998,
|
|
138
|
+
'date' => Date.new(2010, 1, 12),
|
|
139
|
+
'time' => Time.new(2010, 1, 12, 14, 56),
|
|
140
|
+
'boolean' => true,
|
|
141
|
+
'enum' => TestEnum::Member1,
|
|
142
|
+
'array' => [1, 2, 3],
|
|
143
|
+
'hash' => {:a => :b},
|
|
144
|
+
'model' => TestModel,
|
|
145
|
+
'object' => 'whatever'
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
describe "#persisted?" do
|
|
151
|
+
it { subject.persisted?.should be_false }
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
describe "#column_for_attribute" do
|
|
155
|
+
it { subject.column_for_attribute(:string).should_not be_nil }
|
|
156
|
+
it { subject.column_for_attribute(:string).name.should eq(:string) }
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
describe "#filter_params" do
|
|
160
|
+
it "filters out unknown attributes" do
|
|
161
|
+
params = subject.send(:filter_params, :string => 'AA', :unknown => :value)
|
|
162
|
+
params.should include(:string)
|
|
163
|
+
params.should_not include(:unknown)
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
describe "#clean_params" do
|
|
168
|
+
it "removes blank attributes" do
|
|
169
|
+
params = subject.send(:clean_params, :string => '')
|
|
170
|
+
params.should_not include(:string)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
it "removes nil attributes" do
|
|
174
|
+
params = subject.send(:clean_params, :string => nil)
|
|
175
|
+
params.should_not include(:string)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
describe "nested attribute arrays" do
|
|
179
|
+
it "removes blank attributes" do
|
|
180
|
+
params = subject.send(:clean_params, :string => [1, 2, ''])
|
|
181
|
+
params.should include(:string)
|
|
182
|
+
params[:string].length.should eq(2)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
it "removes nil attributes" do
|
|
186
|
+
params = subject.send(:clean_params, :string => [1, 2, nil])
|
|
187
|
+
params.should include(:string)
|
|
188
|
+
params[:string].length.should eq(2)
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
describe "nested attribute hashes" do
|
|
193
|
+
it "removes blank attributes" do
|
|
194
|
+
params = subject.send(:clean_params, :string => { :a => 'A', :b => '' })
|
|
195
|
+
params.should include(:string)
|
|
196
|
+
params[:string].should_not include(:b)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
it "removes nil attributes" do
|
|
200
|
+
params = subject.send(:clean_params, :string => { :a => 'A', :b => nil })
|
|
201
|
+
params.should include(:string)
|
|
202
|
+
params[:string].should_not include(:b)
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
describe "#apply_params" do
|
|
208
|
+
it "assigns attributes" do
|
|
209
|
+
subject.string.should eq('A')
|
|
210
|
+
subject.send(:apply_params, :string => 'test')
|
|
211
|
+
subject.string.should eq('test')
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
it "raises UnknownAttributeError for unknown attribute" do
|
|
215
|
+
expect {
|
|
216
|
+
subject.send(:apply_params, :unknown => 'test')
|
|
217
|
+
}.to raise_error(Riveter::UnknownAttributeError)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
it "re-raises error attribute" do
|
|
221
|
+
allow(subject).to receive(:string=).and_raise(ArgumentError)
|
|
222
|
+
expect {
|
|
223
|
+
subject.send(:apply_params, :string => 'test')
|
|
224
|
+
}.to raise_error(ArgumentError)
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'action_controller'
|
|
3
|
+
|
|
4
|
+
describe Riveter::CommandController do
|
|
5
|
+
context "when not configured" do
|
|
6
|
+
describe "class" do
|
|
7
|
+
subject { TestCommandController }
|
|
8
|
+
|
|
9
|
+
it { should respond_to(:command_controller_for) }
|
|
10
|
+
it { should_not respond_to(:command_class) }
|
|
11
|
+
it { should_not respond_to(:command_options) }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "instance" do
|
|
15
|
+
subject { TestCommandController.new }
|
|
16
|
+
|
|
17
|
+
it { should_not respond_to(:new) }
|
|
18
|
+
it { should_not respond_to(:create) }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
context "when configured" do
|
|
23
|
+
describe "class" do
|
|
24
|
+
subject { TestTestCommandController }
|
|
25
|
+
|
|
26
|
+
it { should respond_to(:command_controller_for) }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe "instance" do
|
|
30
|
+
subject { TestTestCommandController.new }
|
|
31
|
+
|
|
32
|
+
it { should respond_to(:command_class) }
|
|
33
|
+
it { should respond_to(:command_options) }
|
|
34
|
+
it { should respond_to(:new) }
|
|
35
|
+
it { should respond_to(:create) }
|
|
36
|
+
|
|
37
|
+
describe "#command_class" do
|
|
38
|
+
it { subject.command_class.should eq(TestCommand) }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
describe "#command_options" do
|
|
42
|
+
it { subject.command_options.should have_key(:as) }
|
|
43
|
+
it { subject.command_options.should have_key(:attributes) }
|
|
44
|
+
|
|
45
|
+
it { subject.command_options[:as].should eq('test') }
|
|
46
|
+
it { subject.command_options[:attributes].should eq(TestCommand.attributes) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe "controller action" do
|
|
50
|
+
describe "#new" do
|
|
51
|
+
it "assigns @command" do
|
|
52
|
+
subject.new
|
|
53
|
+
subject.send(:eval, '@command').should_not be_nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe "#create" do
|
|
58
|
+
before do
|
|
59
|
+
allow(subject).to receive(:params) { ActionController::Parameters.new({:test => {:name => 'test'}}) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "assigns @command" do
|
|
63
|
+
allow_any_instance_of(TestCommand).to receive(:submit) { true }
|
|
64
|
+
allow(subject).to receive(:on_success) { true }
|
|
65
|
+
|
|
66
|
+
subject.create
|
|
67
|
+
subject.send(:eval, '@command').should_not be_nil
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "assigns attributes" do
|
|
71
|
+
allow_any_instance_of(TestCommand).to receive(:perform) { true }
|
|
72
|
+
allow(subject).to receive(:on_success) { true }
|
|
73
|
+
|
|
74
|
+
subject.create
|
|
75
|
+
command = subject.send(:eval, '@command')
|
|
76
|
+
command.name.should eq('test')
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
context "for a valid command" do
|
|
80
|
+
before do
|
|
81
|
+
allow_any_instance_of(TestCommand).to receive(:submit) { true }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "invokes 'on_success'" do
|
|
85
|
+
expect(subject).to receive(:on_success)
|
|
86
|
+
subject.create
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it "throws exception when 'on_success' not implemented" do
|
|
90
|
+
expect {
|
|
91
|
+
subject.create
|
|
92
|
+
}.to raise_error(NotImplementedError)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
context "for an invalid command" do
|
|
97
|
+
before do
|
|
98
|
+
allow_any_instance_of(TestCommand).to receive(:submit) { false }
|
|
99
|
+
allow(subject).to receive(:render) { true }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it "invokes 'on_failure' if defined" do
|
|
103
|
+
expect(subject).to receive(:on_failure) {}
|
|
104
|
+
subject.create
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "re-renders the 'new' template" do
|
|
108
|
+
expect(subject).to receive(:render).with(:new) { false }
|
|
109
|
+
subject.create
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'riveter/command_routes'
|
|
3
|
+
|
|
4
|
+
describe Riveter::CommandRoutes do
|
|
5
|
+
describe "instance" do
|
|
6
|
+
subject { TestCommandRoutes.new() }
|
|
7
|
+
|
|
8
|
+
describe "#command" do
|
|
9
|
+
it { should respond_to(:command) }
|
|
10
|
+
|
|
11
|
+
it "should define a GET and POST route" do
|
|
12
|
+
expect(subject).to receive(:get)
|
|
13
|
+
expect(subject).to receive(:post)
|
|
14
|
+
|
|
15
|
+
subject.command :test_command
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should define a GET route for the :new action" do
|
|
19
|
+
expect(subject).to receive(:get).with('test', {
|
|
20
|
+
:controller => :test_command,
|
|
21
|
+
:as => :test_command,
|
|
22
|
+
:action => :new
|
|
23
|
+
})
|
|
24
|
+
allow(subject).to receive(:post)
|
|
25
|
+
|
|
26
|
+
subject.command :test
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "should define a POST route for the :create action" do
|
|
30
|
+
allow(subject).to receive(:get)
|
|
31
|
+
expect(subject).to receive(:post).with('test', {
|
|
32
|
+
:controller => :test_command,
|
|
33
|
+
:as => nil,
|
|
34
|
+
:action => :create
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
subject.command :test
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should allow overriding the controller" do
|
|
41
|
+
expect(subject).to receive(:get).with('test', {
|
|
42
|
+
:controller => :my_controller,
|
|
43
|
+
:as => :test_command,
|
|
44
|
+
:action => :new
|
|
45
|
+
})
|
|
46
|
+
expect(subject).to receive(:post).with('test', {
|
|
47
|
+
:controller => :my_controller,
|
|
48
|
+
:as => nil,
|
|
49
|
+
:action => :create
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
subject.command :test, :controller => :my_controller
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "should allow overriding the path" do
|
|
56
|
+
expect(subject).to receive(:get).with('do_some_thing', {
|
|
57
|
+
:controller => :test_command,
|
|
58
|
+
:as => :test_command,
|
|
59
|
+
:action => :new
|
|
60
|
+
})
|
|
61
|
+
expect(subject).to receive(:post).with('do_some_thing', {
|
|
62
|
+
:controller => :test_command,
|
|
63
|
+
:as => nil,
|
|
64
|
+
:action => :create
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
subject.command :test, :path => 'do_some_thing'
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "should allow overriding the path helper name" do
|
|
71
|
+
expect(subject).to receive(:get).with('test', {
|
|
72
|
+
:controller => :test_command,
|
|
73
|
+
:as => :path_name,
|
|
74
|
+
:action => :new
|
|
75
|
+
})
|
|
76
|
+
expect(subject).to receive(:post).with('test', {
|
|
77
|
+
:controller => :test_command,
|
|
78
|
+
:as => nil,
|
|
79
|
+
:action => :create
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
subject.command :test, :as => :path_name
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "should allow overriding the new action" do
|
|
86
|
+
expect(subject).to receive(:get).with('test', {
|
|
87
|
+
:controller => :test_command,
|
|
88
|
+
:as => :test_command,
|
|
89
|
+
:action => :my_new_action
|
|
90
|
+
})
|
|
91
|
+
expect(subject).to receive(:post).with('test', {
|
|
92
|
+
:controller => :test_command,
|
|
93
|
+
:as => nil,
|
|
94
|
+
:action => :create
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
subject.command :test, :new_action => :my_new_action
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it "should allow overriding the create action" do
|
|
101
|
+
expect(subject).to receive(:get).with('test', {
|
|
102
|
+
:controller => :test_command,
|
|
103
|
+
:as => :test_command,
|
|
104
|
+
:action => :new
|
|
105
|
+
})
|
|
106
|
+
expect(subject).to receive(:post).with('test', {
|
|
107
|
+
:controller => :test_command,
|
|
108
|
+
:as => nil,
|
|
109
|
+
:action => :my_create_action
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
subject.command :test, :create_action => :my_create_action
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Riveter::Command do
|
|
4
|
+
it_should_behave_like "a class with attributes", TestCommand
|
|
5
|
+
|
|
6
|
+
describe "class" do
|
|
7
|
+
subject { TestCommand }
|
|
8
|
+
|
|
9
|
+
describe ".command_name" do
|
|
10
|
+
it { should respond_to(:command_name) }
|
|
11
|
+
it { subject.command_name.should be_a(ActiveModel::Name) }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe ".i18n_scope" do
|
|
15
|
+
it { should respond_to(:i18n_scope) }
|
|
16
|
+
it { subject.i18n_scope.should eq(:commands) }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe "instance" do
|
|
21
|
+
subject { TestCommand.new() }
|
|
22
|
+
|
|
23
|
+
describe "#can_perform?" do
|
|
24
|
+
it "should yield false when invalid" do
|
|
25
|
+
subject.can_perform?.should be_false
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should yield true when valid" do
|
|
29
|
+
subject.name = 'test'
|
|
30
|
+
subject.can_perform?.should be_true
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "#submit" do
|
|
35
|
+
it "should yield false when invalid" do
|
|
36
|
+
subject.submit().should be_false
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "should yield true when valid" do
|
|
40
|
+
allow(subject).to receive(:perform) { true }
|
|
41
|
+
|
|
42
|
+
subject.name = 'test'
|
|
43
|
+
subject.submit().should be_true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "should assign attributes" do
|
|
47
|
+
allow(subject).to receive(:perform) { true }
|
|
48
|
+
|
|
49
|
+
subject.submit(:name => 'test')
|
|
50
|
+
subject.name.should eq('test')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "should not assign unknown attributes" do
|
|
54
|
+
subject.submit(:unknown => 'test')
|
|
55
|
+
subject.name.should be_blank
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should invoke associated service" do
|
|
59
|
+
allow(subject).to receive(:valid?) { true }
|
|
60
|
+
expect_any_instance_of(TestService).to receive(:perform) { true }
|
|
61
|
+
|
|
62
|
+
subject.submit(:name => 'test')
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|