rom-rails 0.9.0 → 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +14 -0
- data/Gemfile +8 -5
- data/lib/generators/rom/commands/templates/create.rb.erb +5 -7
- data/lib/generators/rom/commands/templates/update.rb.erb +5 -7
- data/lib/generators/rom/relation/templates/relation.rb.erb +6 -4
- data/lib/rom-rails.rb +0 -1
- data/lib/rom/rails/configuration.rb +0 -2
- data/lib/rom/rails/version.rb +1 -1
- data/rom-rails.gemspec +2 -4
- data/spec/dummy/app/commands/create_user.rb +10 -0
- data/spec/dummy/app/commands/update_user.rb +9 -0
- data/spec/dummy/app/controllers/users_controller.rb +12 -8
- data/spec/dummy/app/forms/user_form.rb +29 -9
- data/spec/dummy/app/mappers/task_mapper.rb +1 -1
- data/spec/dummy/app/mappers/user_mapper.rb +1 -1
- data/spec/dummy/app/models/user.rb +1 -1
- data/spec/dummy/app/relations/dummy_relation.rb +2 -1
- data/spec/dummy/app/relations/tasks.rb +2 -0
- data/spec/dummy/app/relations/users.rb +2 -0
- data/spec/dummy/lib/rom/test_adapter.rb +1 -1
- data/spec/features/users_spec.rb +1 -3
- data/spec/integration/activerecord_setup.rb +1 -1
- data/spec/integration/initializer_spec.rb +1 -3
- data/spec/integration/logger_spec.rb +1 -3
- data/spec/integration/user_commands_spec.rb +7 -7
- data/spec/integration/user_model_mapping_spec.rb +2 -4
- data/spec/lib/active_record/configuration_spec.rb +1 -2
- data/spec/lib/generators/commands_generator_spec.rb +7 -17
- data/spec/lib/generators/mapper_generator_spec.rb +1 -3
- data/spec/lib/generators/relation_generator_spec.rb +4 -8
- data/spec/lib/generators/repository_generator_spec.rb +1 -3
- data/spec/spec_helper.rb +1 -1
- metadata +12 -61
- data/lib/generators/rom/form/templates/base_form.rb.erb +0 -17
- data/lib/generators/rom/form/templates/edit_form.rb.erb +0 -20
- data/lib/generators/rom/form/templates/new_form.rb.erb +0 -20
- data/lib/generators/rom/form_generator.rb +0 -47
- data/lib/rom/rails/model/form.rb +0 -173
- data/lib/rom/rails/model/form/class_interface.rb +0 -457
- data/lib/rom/rails/model/form/error_proxy.rb +0 -53
- data/spec/dummy/app/forms/new_user_form.rb +0 -13
- data/spec/dummy/app/forms/update_user_form.rb +0 -11
- data/spec/integration/form_with_injected_commands_spec.rb +0 -37
- data/spec/integration/new_user_form_spec.rb +0 -19
- data/spec/lib/generators/form_generator_spec.rb +0 -140
- data/spec/unit/form_spec.rb +0 -366
@@ -1,8 +1,6 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
1
|
require 'generators/rom/relation_generator'
|
4
2
|
|
5
|
-
describe ROM::Generators::RelationGenerator, type: :generator do
|
3
|
+
RSpec.describe ROM::Generators::RelationGenerator, type: :generator do
|
6
4
|
destination File.expand_path('../../../../tmp', __FILE__)
|
7
5
|
|
8
6
|
before(:each) do
|
@@ -19,11 +17,9 @@ describe ROM::Generators::RelationGenerator, type: :generator do
|
|
19
17
|
file 'users_relation.rb' do
|
20
18
|
contains <<-CONTENT.strip_heredoc
|
21
19
|
class UsersRelation < ROM::Relation[:#{default_adapter}]
|
22
|
-
|
23
|
-
|
24
|
-
dataset :users
|
20
|
+
gateway :default
|
25
21
|
|
26
|
-
|
22
|
+
schema(:users, infer: true)
|
27
23
|
|
28
24
|
# define your methods here ie:
|
29
25
|
#
|
@@ -56,6 +52,6 @@ describe ROM::Generators::RelationGenerator, type: :generator do
|
|
56
52
|
run_generator ['users', '--register=profiles']
|
57
53
|
|
58
54
|
relation = File.read(File.join(destination_root, 'app', 'relations', 'users_relation.rb'))
|
59
|
-
expect(relation).to
|
55
|
+
expect(relation).to match(/schema.*as: :profiles/)
|
60
56
|
end
|
61
57
|
end
|
@@ -1,9 +1,7 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
1
|
require 'rom-repository'
|
4
2
|
require 'generators/rom/repository_generator'
|
5
3
|
|
6
|
-
describe ROM::Generators::RepositoryGenerator, type: :generator do
|
4
|
+
RSpec.describe ROM::Generators::RepositoryGenerator, type: :generator do
|
7
5
|
destination File.expand_path('../../../../tmp', __FILE__)
|
8
6
|
|
9
7
|
before(:each) do
|
data/spec/spec_helper.rb
CHANGED
@@ -23,7 +23,7 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
|
|
23
23
|
Dry::Core::Deprecations.set_logger!(Rails.root.join('log/deprecations.log'))
|
24
24
|
|
25
25
|
RSpec.configure do |config|
|
26
|
-
config.
|
26
|
+
config.disable_monkey_patching!
|
27
27
|
config.example_status_persistence_file_path = "tmp/examples.txt"
|
28
28
|
|
29
29
|
config.before(:suite) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rom
|
@@ -16,28 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '3.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rom-model
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0.3'
|
19
|
+
version: 4.0.0.beta
|
34
20
|
type: :runtime
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
24
|
- - "~>"
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
26
|
+
version: 4.0.0.beta
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: dry-equalizer
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,20 +44,14 @@ dependencies:
|
|
58
44
|
requirements:
|
59
45
|
- - "~>"
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
62
|
-
- - ">="
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
version: 0.2.4
|
47
|
+
version: '0.3'
|
65
48
|
type: :runtime
|
66
49
|
prerelease: false
|
67
50
|
version_requirements: !ruby/object:Gem::Requirement
|
68
51
|
requirements:
|
69
52
|
- - "~>"
|
70
53
|
- !ruby/object:Gem::Version
|
71
|
-
version: '0.
|
72
|
-
- - ">="
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
version: 0.2.4
|
54
|
+
version: '0.3'
|
75
55
|
- !ruby/object:Gem::Dependency
|
76
56
|
name: addressable
|
77
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,20 +66,6 @@ dependencies:
|
|
86
66
|
- - "~>"
|
87
67
|
- !ruby/object:Gem::Version
|
88
68
|
version: '2.3'
|
89
|
-
- !ruby/object:Gem::Dependency
|
90
|
-
name: charlatan
|
91
|
-
requirement: !ruby/object:Gem::Requirement
|
92
|
-
requirements:
|
93
|
-
- - "~>"
|
94
|
-
- !ruby/object:Gem::Version
|
95
|
-
version: '0.1'
|
96
|
-
type: :runtime
|
97
|
-
prerelease: false
|
98
|
-
version_requirements: !ruby/object:Gem::Requirement
|
99
|
-
requirements:
|
100
|
-
- - "~>"
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
version: '0.1'
|
103
69
|
- !ruby/object:Gem::Dependency
|
104
70
|
name: railties
|
105
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -197,10 +163,6 @@ files:
|
|
197
163
|
- lib/generators/rom/commands/templates/delete.rb.erb
|
198
164
|
- lib/generators/rom/commands/templates/update.rb.erb
|
199
165
|
- lib/generators/rom/commands_generator.rb
|
200
|
-
- lib/generators/rom/form/templates/base_form.rb.erb
|
201
|
-
- lib/generators/rom/form/templates/edit_form.rb.erb
|
202
|
-
- lib/generators/rom/form/templates/new_form.rb.erb
|
203
|
-
- lib/generators/rom/form_generator.rb
|
204
166
|
- lib/generators/rom/mapper/templates/mapper.rb.erb
|
205
167
|
- lib/generators/rom/mapper_generator.rb
|
206
168
|
- lib/generators/rom/relation/templates/relation.rb.erb
|
@@ -212,9 +174,6 @@ files:
|
|
212
174
|
- lib/rom/rails/configuration.rb
|
213
175
|
- lib/rom/rails/controller_extension.rb
|
214
176
|
- lib/rom/rails/inflections.rb
|
215
|
-
- lib/rom/rails/model/form.rb
|
216
|
-
- lib/rom/rails/model/form/class_interface.rb
|
217
|
-
- lib/rom/rails/model/form/error_proxy.rb
|
218
177
|
- lib/rom/rails/railtie.rb
|
219
178
|
- lib/rom/rails/tasks/db.rake
|
220
179
|
- lib/rom/rails/version.rb
|
@@ -223,11 +182,11 @@ files:
|
|
223
182
|
- spec/dummy/README.rdoc
|
224
183
|
- spec/dummy/Rakefile
|
225
184
|
- spec/dummy/app/commands/create_task.rb
|
185
|
+
- spec/dummy/app/commands/create_user.rb
|
226
186
|
- spec/dummy/app/commands/delete_user.rb
|
187
|
+
- spec/dummy/app/commands/update_user.rb
|
227
188
|
- spec/dummy/app/controllers/application_controller.rb
|
228
189
|
- spec/dummy/app/controllers/users_controller.rb
|
229
|
-
- spec/dummy/app/forms/new_user_form.rb
|
230
|
-
- spec/dummy/app/forms/update_user_form.rb
|
231
190
|
- spec/dummy/app/forms/user_form.rb
|
232
191
|
- spec/dummy/app/helpers/application_helper.rb
|
233
192
|
- spec/dummy/app/mailers/.keep
|
@@ -280,20 +239,16 @@ files:
|
|
280
239
|
- spec/dummy/vendor/assets/stylesheets/.keep
|
281
240
|
- spec/features/users_spec.rb
|
282
241
|
- spec/integration/activerecord_setup.rb
|
283
|
-
- spec/integration/form_with_injected_commands_spec.rb
|
284
242
|
- spec/integration/initializer_spec.rb
|
285
243
|
- spec/integration/logger_spec.rb
|
286
|
-
- spec/integration/new_user_form_spec.rb
|
287
244
|
- spec/integration/user_commands_spec.rb
|
288
245
|
- spec/integration/user_model_mapping_spec.rb
|
289
246
|
- spec/lib/active_record/configuration_spec.rb
|
290
247
|
- spec/lib/generators/commands_generator_spec.rb
|
291
|
-
- spec/lib/generators/form_generator_spec.rb
|
292
248
|
- spec/lib/generators/mapper_generator_spec.rb
|
293
249
|
- spec/lib/generators/relation_generator_spec.rb
|
294
250
|
- spec/lib/generators/repository_generator_spec.rb
|
295
251
|
- spec/spec_helper.rb
|
296
|
-
- spec/unit/form_spec.rb
|
297
252
|
homepage: http://rom-rb.org
|
298
253
|
licenses:
|
299
254
|
- MIT
|
@@ -309,9 +264,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
309
264
|
version: '0'
|
310
265
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
311
266
|
requirements:
|
312
|
-
- - "
|
267
|
+
- - ">"
|
313
268
|
- !ruby/object:Gem::Version
|
314
|
-
version:
|
269
|
+
version: 1.3.1
|
315
270
|
requirements: []
|
316
271
|
rubyforge_project:
|
317
272
|
rubygems_version: 2.4.5.1
|
@@ -323,11 +278,11 @@ test_files:
|
|
323
278
|
- spec/dummy/README.rdoc
|
324
279
|
- spec/dummy/Rakefile
|
325
280
|
- spec/dummy/app/commands/create_task.rb
|
281
|
+
- spec/dummy/app/commands/create_user.rb
|
326
282
|
- spec/dummy/app/commands/delete_user.rb
|
283
|
+
- spec/dummy/app/commands/update_user.rb
|
327
284
|
- spec/dummy/app/controllers/application_controller.rb
|
328
285
|
- spec/dummy/app/controllers/users_controller.rb
|
329
|
-
- spec/dummy/app/forms/new_user_form.rb
|
330
|
-
- spec/dummy/app/forms/update_user_form.rb
|
331
286
|
- spec/dummy/app/forms/user_form.rb
|
332
287
|
- spec/dummy/app/helpers/application_helper.rb
|
333
288
|
- spec/dummy/app/mailers/.keep
|
@@ -380,17 +335,13 @@ test_files:
|
|
380
335
|
- spec/dummy/vendor/assets/stylesheets/.keep
|
381
336
|
- spec/features/users_spec.rb
|
382
337
|
- spec/integration/activerecord_setup.rb
|
383
|
-
- spec/integration/form_with_injected_commands_spec.rb
|
384
338
|
- spec/integration/initializer_spec.rb
|
385
339
|
- spec/integration/logger_spec.rb
|
386
|
-
- spec/integration/new_user_form_spec.rb
|
387
340
|
- spec/integration/user_commands_spec.rb
|
388
341
|
- spec/integration/user_model_mapping_spec.rb
|
389
342
|
- spec/lib/active_record/configuration_spec.rb
|
390
343
|
- spec/lib/generators/commands_generator_spec.rb
|
391
|
-
- spec/lib/generators/form_generator_spec.rb
|
392
344
|
- spec/lib/generators/mapper_generator_spec.rb
|
393
345
|
- spec/lib/generators/relation_generator_spec.rb
|
394
346
|
- spec/lib/generators/repository_generator_spec.rb
|
395
347
|
- spec/spec_helper.rb
|
396
|
-
- spec/unit/form_spec.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
class <%= model_name %>Form < ROM::Model::Form
|
2
|
-
|
3
|
-
input do
|
4
|
-
set_model_name '<%= model_name %>'
|
5
|
-
|
6
|
-
# define always-present form input attributes
|
7
|
-
# attribute :name, String
|
8
|
-
end
|
9
|
-
|
10
|
-
validations do
|
11
|
-
relation :<%= relation %>
|
12
|
-
|
13
|
-
# Add invariant form validations
|
14
|
-
# validates :name, presence: true
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
class Edit<%= model_name %>Form < <%= model_name %>Form
|
2
|
-
commands <%= relation %>: :update
|
3
|
-
|
4
|
-
input do
|
5
|
-
# define form input attributes
|
6
|
-
# attribute :name, String
|
7
|
-
|
8
|
-
timestamps :updated_at
|
9
|
-
end
|
10
|
-
|
11
|
-
validations do
|
12
|
-
# Add form validations
|
13
|
-
# validates :name, presence: true
|
14
|
-
end
|
15
|
-
|
16
|
-
def commit!
|
17
|
-
<%= relation %>.try { <%= relation %>.update.by_id(id).set(attributes) }
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
class New<%= model_name %>Form < <%= model_name %>Form
|
2
|
-
commands <%= relation %>: :create
|
3
|
-
|
4
|
-
input do
|
5
|
-
# define form input attributes
|
6
|
-
# attribute :name, String
|
7
|
-
|
8
|
-
timestamps
|
9
|
-
end
|
10
|
-
|
11
|
-
validations do
|
12
|
-
# Add form validations
|
13
|
-
# validates :name, presence: true
|
14
|
-
end
|
15
|
-
|
16
|
-
def commit!
|
17
|
-
<%= relation %>.try { <%= relation %>.create.call(attributes) }
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'generators/rom'
|
2
|
-
|
3
|
-
module ROM
|
4
|
-
module Generators
|
5
|
-
class FormGenerator < Base
|
6
|
-
class_option :command,
|
7
|
-
banner: "--command=command",
|
8
|
-
desc: "specify command to use"
|
9
|
-
|
10
|
-
def create_base_form
|
11
|
-
template "base_form.rb.erb",
|
12
|
-
File.join("app", "forms", "#{file_name.singularize}_form.rb")
|
13
|
-
end
|
14
|
-
|
15
|
-
def create_new
|
16
|
-
create(:new) if create_new_form?
|
17
|
-
end
|
18
|
-
|
19
|
-
def create_edit
|
20
|
-
create(:edit) if create_edit_form?
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def create(type)
|
26
|
-
template "#{type}_form.rb.erb",
|
27
|
-
File.join("app", "forms", "#{type}_#{file_name.singularize}_form.rb")
|
28
|
-
end
|
29
|
-
|
30
|
-
def create_new_form?
|
31
|
-
options[:command].blank? || %w(new create).include?(options[:command].to_s.downcase)
|
32
|
-
end
|
33
|
-
|
34
|
-
def create_edit_form?
|
35
|
-
options[:command].blank? || %w(edit update).include?(options[:command].to_s.downcase)
|
36
|
-
end
|
37
|
-
|
38
|
-
def model_name
|
39
|
-
class_name.singularize.camelcase
|
40
|
-
end
|
41
|
-
|
42
|
-
def relation
|
43
|
-
class_name.pluralize.underscore
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
data/lib/rom/rails/model/form.rb
DELETED
@@ -1,173 +0,0 @@
|
|
1
|
-
require 'dry/equalizer'
|
2
|
-
require 'dry/core/class_attributes'
|
3
|
-
|
4
|
-
require 'rom-model'
|
5
|
-
|
6
|
-
require 'rom/rails/model/form/class_interface'
|
7
|
-
require 'rom/rails/model/form/error_proxy'
|
8
|
-
|
9
|
-
module ROM
|
10
|
-
module Model
|
11
|
-
# Abstract form class
|
12
|
-
#
|
13
|
-
# Form objects in ROM are your top-level interface to persist data in the
|
14
|
-
# database. They combine many features that you know from ActiveRecord:
|
15
|
-
#
|
16
|
-
# * params processing with sanitization and coercion
|
17
|
-
# * attribute validations
|
18
|
-
# * persisting data in the database
|
19
|
-
#
|
20
|
-
# The major difference is that a ROM form object separates those
|
21
|
-
# responsibilities - a ROM form class has its own Attributes, Validator and
|
22
|
-
# ROM commands that are accessible within its instance.
|
23
|
-
#
|
24
|
-
# @example
|
25
|
-
# class UserForm < ROM::Model::Form
|
26
|
-
# commands users: :create
|
27
|
-
#
|
28
|
-
# input do
|
29
|
-
# set_model_name 'User'
|
30
|
-
#
|
31
|
-
# attribute :name, String
|
32
|
-
# end
|
33
|
-
#
|
34
|
-
# validations do
|
35
|
-
# validates :name, presence: true
|
36
|
-
# end
|
37
|
-
# end
|
38
|
-
#
|
39
|
-
# class CreateUserForm < UserForm
|
40
|
-
# attributes.timestamps :created_at
|
41
|
-
#
|
42
|
-
# def commit!
|
43
|
-
# users.try { users.create.call(attributes) }
|
44
|
-
# end
|
45
|
-
# end
|
46
|
-
#
|
47
|
-
# # then in your controller
|
48
|
-
# CreateUserForm.build(params[:user]).save
|
49
|
-
#
|
50
|
-
# @api public
|
51
|
-
class Form
|
52
|
-
include Dry::Equalizer(:params, :model, :result)
|
53
|
-
|
54
|
-
extend Dry::Core::ClassAttributes
|
55
|
-
extend Form::ClassInterface
|
56
|
-
|
57
|
-
defines :relation, :mappings
|
58
|
-
|
59
|
-
# Return raw params received from the request
|
60
|
-
#
|
61
|
-
# @return [Object]
|
62
|
-
#
|
63
|
-
# @api public
|
64
|
-
attr_reader :params
|
65
|
-
|
66
|
-
# Return model instance representing an ActiveModel object that will be
|
67
|
-
# persisted or updated
|
68
|
-
#
|
69
|
-
# @return [Object]
|
70
|
-
#
|
71
|
-
# @api public
|
72
|
-
attr_reader :model
|
73
|
-
|
74
|
-
# Return the result of commit!
|
75
|
-
#
|
76
|
-
# @return [Object]
|
77
|
-
#
|
78
|
-
# @api public
|
79
|
-
attr_reader :result
|
80
|
-
|
81
|
-
# Return any errors with the form
|
82
|
-
#
|
83
|
-
# @return [ErrorProxy]
|
84
|
-
#
|
85
|
-
# @api public
|
86
|
-
attr_reader :errors
|
87
|
-
|
88
|
-
delegate :model_name, :persisted?, :to_key, to: :model
|
89
|
-
alias to_model model
|
90
|
-
|
91
|
-
class << self
|
92
|
-
delegate :model_name, to: :attributes
|
93
|
-
end
|
94
|
-
|
95
|
-
# Raise deprecation warning when class is used:
|
96
|
-
#
|
97
|
-
# @api private
|
98
|
-
def self.inherited(klass)
|
99
|
-
Dry::Core::Deprecations.announce "ROM::Model::Form", <<-MSG, tag: :rom
|
100
|
-
We now recommend using Reform or another dedicated form library,
|
101
|
-
in conjunction with ROM::Repository or explicit command invocations.
|
102
|
-
MSG
|
103
|
-
|
104
|
-
super
|
105
|
-
end
|
106
|
-
|
107
|
-
# @api private
|
108
|
-
def initialize(params = {}, options = {})
|
109
|
-
@params = params.respond_to?(:to_unsafe_hash) ?
|
110
|
-
params.to_unsafe_hash : params
|
111
|
-
@model = self.class.model.new(params.to_h.merge(options.slice(*self.class.key)))
|
112
|
-
@result = nil
|
113
|
-
@errors = ErrorProxy.new
|
114
|
-
options.each { |key, value| instance_variable_set("@#{key}", value) }
|
115
|
-
end
|
116
|
-
|
117
|
-
# A specialized form object must implement this method
|
118
|
-
#
|
119
|
-
# @abstract
|
120
|
-
#
|
121
|
-
# @api public
|
122
|
-
def commit!
|
123
|
-
raise NotImplementedError, "#{self.class}#commit! must be implemented"
|
124
|
-
end
|
125
|
-
|
126
|
-
# Save a form by calling commit! and memoizing result
|
127
|
-
#
|
128
|
-
# @return [self]
|
129
|
-
#
|
130
|
-
# @api public
|
131
|
-
def save(*args)
|
132
|
-
validate!
|
133
|
-
|
134
|
-
@result = commit!(*args) unless @errors.present?
|
135
|
-
@errors.set @result.error if result.respond_to? :error
|
136
|
-
|
137
|
-
self
|
138
|
-
end
|
139
|
-
|
140
|
-
# Return whether commit was successful
|
141
|
-
#
|
142
|
-
# @return [TrueClass,FalseClass]
|
143
|
-
#
|
144
|
-
# @api public
|
145
|
-
def success?
|
146
|
-
errors.success?
|
147
|
-
end
|
148
|
-
|
149
|
-
# Trigger validation and store errors (if any)
|
150
|
-
#
|
151
|
-
# @api public
|
152
|
-
def validate!
|
153
|
-
@errors.clear
|
154
|
-
return unless defined? self.class::Validator
|
155
|
-
validator = self.class::Validator.new(attributes)
|
156
|
-
validator.validate
|
157
|
-
|
158
|
-
@errors.set validator.errors
|
159
|
-
end
|
160
|
-
|
161
|
-
# Sanitize and coerce input params
|
162
|
-
#
|
163
|
-
# This can also set default values
|
164
|
-
#
|
165
|
-
# @return [Model::Attributes]
|
166
|
-
#
|
167
|
-
# @api public
|
168
|
-
def attributes
|
169
|
-
self.class.attributes[params]
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|