rom-rails 0.2.1 → 0.3.0.beta1
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/.rubocop.yml +26 -0
- data/CHANGELOG.md +17 -0
- data/README.md +4 -0
- data/lib/generators/rom/commands/templates/create.rb.erb +10 -0
- data/lib/generators/rom/commands/templates/delete.rb.erb +7 -0
- data/lib/generators/rom/commands/templates/update.rb.erb +10 -0
- data/lib/generators/rom/commands_generator.rb +33 -5
- data/lib/generators/rom/form/templates/edit_form.rb.erb +24 -0
- data/lib/generators/rom/form/templates/new_form.rb.erb +24 -0
- data/lib/generators/rom/form_generator.rb +39 -0
- data/lib/generators/rom/mapper/templates/mapper.rb.erb +9 -11
- data/lib/generators/rom/mapper_generator.rb +11 -1
- data/lib/generators/rom/relation/templates/relation.rb.erb +3 -2
- data/lib/generators/rom/relation_generator.rb +16 -1
- data/lib/rom-rails.rb +0 -5
- data/lib/rom/model.rb +7 -90
- data/lib/rom/rails/active_record/configuration.rb +105 -0
- data/lib/rom/rails/configuration.rb +4 -27
- data/lib/rom/rails/model/form.rb +59 -0
- data/lib/rom/rails/model/form/dsl.rb +173 -0
- data/lib/rom/rails/model/params.rb +72 -0
- data/lib/rom/rails/model/validator.rb +74 -0
- data/lib/rom/rails/model/validator/uniqueness_validator.rb +39 -0
- data/lib/rom/rails/railtie.rb +65 -29
- data/lib/rom/rails/version.rb +1 -1
- data/rom-rails.gemspec +3 -1
- data/spec/dummy/app/commands/tasks.rb +5 -0
- data/spec/dummy/app/commands/users.rb +4 -10
- data/spec/dummy/app/controllers/users_controller.rb +30 -0
- data/spec/dummy/app/forms/new_user_form.rb +9 -0
- data/spec/dummy/app/forms/update_user_form.rb +9 -0
- data/spec/dummy/app/forms/user_form.rb +15 -0
- data/spec/dummy/app/mappers/users.rb +6 -6
- data/spec/dummy/app/relations/tasks.rb +9 -0
- data/spec/dummy/app/relations/users.rb +5 -1
- data/spec/dummy/app/views/users/edit.html.erb +6 -0
- data/spec/dummy/app/views/users/new.html.erb +6 -0
- data/spec/dummy/config/routes.rb +5 -1
- data/spec/dummy/db/migrate/20141110205016_add_users.rb +2 -0
- data/spec/dummy/db/migrate/20150202194440_create_tasks.rb +7 -0
- data/spec/dummy/db/schema.rb +8 -8
- data/spec/dummy/spec/features/users_spec.rb +30 -0
- data/spec/dummy/spec/integration/logger_spec.rb +1 -1
- data/spec/dummy/spec/integration/user_commands_spec.rb +2 -18
- data/spec/dummy/spec/integration/user_model_mapping_spec.rb +2 -2
- data/spec/dummy/spec/integration/user_params_spec.rb +30 -15
- data/spec/lib/active_record/configuration_spec.rb +98 -0
- data/spec/lib/generators/commands_generator_spec.rb +54 -14
- data/spec/lib/generators/form_generator_spec.rb +89 -0
- data/spec/lib/generators/mapper_generator_spec.rb +10 -12
- data/spec/lib/generators/relation_generator_spec.rb +16 -6
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/form_spec.rb +297 -0
- data/spec/unit/{model_spec.rb → params_spec.rb} +0 -0
- data/spec/unit/validator_spec.rb +75 -0
- metadata +72 -20
- data/lib/generators/rom/commands/templates/commands.rb.erb +0 -15
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/params/user_params.rb +0 -7
- data/spec/dummy/app/validators/user_validator.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dca5938e41a98dc3af6fe2aa5d301c13a6984d04
|
4
|
+
data.tar.gz: 372f0581874750544b013ff63048ff60ee69085b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0b26883fb509ab4ff4a39294b84d90436bd73c736e653f16ad90b4fb89c8fe6949590ac3f5f1513f0da752291ed96e6d69fb26fd164e47ba22b6a6cff400e94
|
7
|
+
data.tar.gz: d5fe5b5a9c13523bd2d53c5c193402de75e963edce3a79c6a815eff1c8a2f03010af8520cce4f037850a100dcccd96ba7cc1a692d6fd71bbd80fcc531f3c5ba0
|
data/.rubocop.yml
CHANGED
@@ -8,6 +8,7 @@ Metrics/CyclomaticComplexity:
|
|
8
8
|
|
9
9
|
# FIXME: No clue how to fix that one line…
|
10
10
|
Metrics/LineLength:
|
11
|
+
Max: 100
|
11
12
|
Exclude:
|
12
13
|
- lib/rom/rails/configuration.rb
|
13
14
|
|
@@ -27,6 +28,23 @@ AllCops:
|
|
27
28
|
- spec/dummy/db/schema.rb
|
28
29
|
- spec/dummy/db/seeds.rb
|
29
30
|
- tmp/**/*
|
31
|
+
- vendor/**/*
|
32
|
+
|
33
|
+
# The enforced style doesn’t match Vim’s defaults
|
34
|
+
Style/AlignParameters:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
# UTF-8 is perfectly fine in comments
|
38
|
+
Style/AsciiComments:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
# Allow using braces for value-returning blocks
|
42
|
+
Style/Blocks:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Style/BracesAroundHashParameters:
|
46
|
+
Enabled: true
|
47
|
+
EnforcedStyle: context_dependent
|
30
48
|
|
31
49
|
# Documentation checked by Inch CI
|
32
50
|
Style/Documentation:
|
@@ -41,6 +59,10 @@ Style/FileName:
|
|
41
59
|
Style/GuardClause:
|
42
60
|
Enabled: false
|
43
61
|
|
62
|
+
# Multiline block chains are ok
|
63
|
+
Style/MultilineBlockChain:
|
64
|
+
Enabled: false
|
65
|
+
|
44
66
|
# Model::Validator uses Model::ValidationError for this
|
45
67
|
Style/RaiseArgs:
|
46
68
|
Exclude:
|
@@ -57,3 +79,7 @@ Style/SignalException:
|
|
57
79
|
# Accept both single and double quotes
|
58
80
|
Style/StringLiterals:
|
59
81
|
Enabled: false
|
82
|
+
|
83
|
+
Style/AccessorMethodName:
|
84
|
+
Exclude:
|
85
|
+
- lib/rom/rails/model/params.rb
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
## v0.3.0 to-be-released
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* `ROM::Model::Form` for modeling and setting up web-forms (solnic + cflipse)
|
6
|
+
* Support for timestamps attributes in Form objects (kchien)
|
7
|
+
|
8
|
+
|
9
|
+
### Changed
|
10
|
+
|
11
|
+
* Improved initialization process which works with AR-style configurations (aflatter)
|
12
|
+
* Allow setup using a configuration block from railtie (aflatter)
|
13
|
+
|
14
|
+
[Compare v0.2.1...master](https://github.com/rom-rb/rom-rails/compare/v0.2.1...master)
|
15
|
+
|
1
16
|
## v0.2.1 2015-01-07
|
2
17
|
|
3
18
|
### Changed
|
@@ -8,6 +23,8 @@
|
|
8
23
|
|
9
24
|
* `rom` extension is now mixed into ActionController::Base which addresses #12 (solnic)
|
10
25
|
|
26
|
+
[Compare v0.2.1...v0.2.0](https://github.com/rom-rb/rom-rails/compare/v0.2.0...v0.2.1)
|
27
|
+
|
11
28
|
## v0.2.0 2014-12-31
|
12
29
|
|
13
30
|
### Added
|
data/README.md
CHANGED
@@ -23,6 +23,10 @@ ships with:
|
|
23
23
|
* Mapper generators
|
24
24
|
* Command generators
|
25
25
|
|
26
|
+
## Issues
|
27
|
+
|
28
|
+
Please report any issues in the main [rom-rb/rom](https://github.com/rom-rb/rom/issues) issue tracker.
|
29
|
+
|
26
30
|
## Resources
|
27
31
|
|
28
32
|
You can read more about ROM and Rails on the official website:
|
@@ -3,11 +3,39 @@ require 'generators/rom'
|
|
3
3
|
module ROM
|
4
4
|
module Generators
|
5
5
|
class CommandsGenerator < Base
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
class_option :adapter,
|
7
|
+
banner: "--adapter=adapter",
|
8
|
+
desc: "specify an adapter to use", required: true,
|
9
|
+
default: ROM.adapters.keys.first
|
10
|
+
|
11
|
+
def create_create_command
|
12
|
+
template 'create.rb.erb', command_file(:create)
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_update_command
|
16
|
+
template 'update.rb.erb', command_file(:update)
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_delete_command
|
20
|
+
template 'delete.rb.erb', command_file(:delete)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def command_file(command)
|
26
|
+
File.join('app', 'commands', file_name, "#{command}.rb")
|
27
|
+
end
|
28
|
+
|
29
|
+
def relation
|
30
|
+
class_name.pluralize.underscore
|
31
|
+
end
|
32
|
+
|
33
|
+
def model_name
|
34
|
+
class_name.singularize
|
35
|
+
end
|
36
|
+
|
37
|
+
def adapter
|
38
|
+
options[:adapter]
|
11
39
|
end
|
12
40
|
end
|
13
41
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Edit<%= model_name %>Form < ROM::Model::Form
|
2
|
+
commands <%= relation %>: :update
|
3
|
+
|
4
|
+
input do
|
5
|
+
set_model_name '<%= model_name %>'
|
6
|
+
|
7
|
+
# define form input attributes
|
8
|
+
# attribute :name, String
|
9
|
+
|
10
|
+
timestamps :updated_at
|
11
|
+
end
|
12
|
+
|
13
|
+
validations do
|
14
|
+
relation :<%= relation %>
|
15
|
+
|
16
|
+
# Add form validations
|
17
|
+
# validates :name, presence: true
|
18
|
+
end
|
19
|
+
|
20
|
+
def commit!
|
21
|
+
<%= relation %>.try { <%= relation %>.update.by_id(id).set(params) }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class New<%= model_name %>Form < ROM::Model::Form
|
2
|
+
commands <%= relation %>: :create
|
3
|
+
|
4
|
+
input do
|
5
|
+
set_model_name '<%= model_name %>'
|
6
|
+
|
7
|
+
# define form input attributes
|
8
|
+
# attribute :name, String
|
9
|
+
|
10
|
+
timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
validations do
|
14
|
+
relation :<%= relation %>
|
15
|
+
|
16
|
+
# Add form validations
|
17
|
+
# validates :name, presence: true
|
18
|
+
end
|
19
|
+
|
20
|
+
def commit!
|
21
|
+
<%= relation %>.try { <%= relation %>.create.call(params) }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
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", required: true
|
9
|
+
|
10
|
+
def create_command
|
11
|
+
type = edit_or_new
|
12
|
+
|
13
|
+
template "#{type}_form.rb.erb",
|
14
|
+
File.join("app", "forms", "#{type}_#{file_name.singularize}_form.rb")
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def model_name
|
20
|
+
class_name.singularize.camelcase
|
21
|
+
end
|
22
|
+
|
23
|
+
def relation
|
24
|
+
class_name.pluralize.underscore
|
25
|
+
end
|
26
|
+
|
27
|
+
def edit_or_new
|
28
|
+
case options[:command].downcase
|
29
|
+
when 'edit', 'update'
|
30
|
+
:edit
|
31
|
+
when 'new', 'create'
|
32
|
+
:new
|
33
|
+
else
|
34
|
+
raise "Unknown command"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,12 +1,10 @@
|
|
1
|
-
ROM
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
|
1
|
+
class <%= model_name %>Mapper < ROM::Mapper
|
2
|
+
# relation :<%= relation %>
|
3
|
+
#
|
4
|
+
# specify model and attributes ie
|
5
|
+
#
|
6
|
+
# model <%= model_name %>
|
7
|
+
#
|
8
|
+
# attribute :name
|
9
|
+
# attribute :email
|
12
10
|
end
|
@@ -6,9 +6,19 @@ module ROM
|
|
6
6
|
def create_mapper_file
|
7
7
|
template(
|
8
8
|
'mapper.rb.erb',
|
9
|
-
File.join('app', 'mappers', "#{file_name}.rb")
|
9
|
+
File.join('app', 'mappers', "#{file_name.singularize}_mapper.rb")
|
10
10
|
)
|
11
11
|
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def model_name
|
16
|
+
class_name.singularize
|
17
|
+
end
|
18
|
+
|
19
|
+
def relation
|
20
|
+
class_name.pluralize.underscore
|
21
|
+
end
|
12
22
|
end
|
13
23
|
end
|
14
24
|
end
|
@@ -3,12 +3,27 @@ require 'generators/rom'
|
|
3
3
|
module ROM
|
4
4
|
module Generators
|
5
5
|
class RelationGenerator < Base
|
6
|
+
class_option :adapter,
|
7
|
+
banner: "--adapter=adapter",
|
8
|
+
desc: "specify an adapter to use", required: true,
|
9
|
+
default: ROM.adapters.keys.first
|
10
|
+
|
6
11
|
def create_relation_file
|
7
12
|
template(
|
8
13
|
'relation.rb.erb',
|
9
|
-
File.join('app', 'relations', "#{file_name}.rb")
|
14
|
+
File.join('app', 'relations', "#{file_name}_relation.rb")
|
10
15
|
)
|
11
16
|
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def dataset
|
21
|
+
class_name.underscore.pluralize
|
22
|
+
end
|
23
|
+
|
24
|
+
def adapter
|
25
|
+
options[:adapter]
|
26
|
+
end
|
12
27
|
end
|
13
28
|
end
|
14
29
|
end
|
data/lib/rom-rails.rb
CHANGED
data/lib/rom/model.rb
CHANGED
@@ -1,97 +1,14 @@
|
|
1
|
-
require '
|
2
|
-
require 'active_model'
|
1
|
+
require 'charlatan'
|
3
2
|
|
4
3
|
module ROM
|
5
4
|
module Model
|
6
5
|
class ValidationError < CommandError
|
7
|
-
|
8
|
-
|
9
|
-
def initialize(params)
|
10
|
-
@params = params
|
11
|
-
@messages = params.errors
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
# Mixin for validatable and coercible parameters
|
16
|
-
#
|
17
|
-
# @example
|
18
|
-
#
|
19
|
-
# class UserParams
|
20
|
-
# include ROM::Model::Params
|
21
|
-
#
|
22
|
-
# attribute :email, String
|
23
|
-
# attribute :age, Integer
|
24
|
-
#
|
25
|
-
# validates :email, :age, presence: true
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
# user_params = UserParams.new(email: '', age: '18')
|
29
|
-
#
|
30
|
-
# user_params.email # => ''
|
31
|
-
# user_params.age # => 18
|
32
|
-
#
|
33
|
-
# user_params.valid? # => false
|
34
|
-
# user_params.errors # => #<ActiveModel::Errors:0x007fd2423fadb0 ...>
|
35
|
-
#
|
36
|
-
# @api public
|
37
|
-
module Params
|
38
|
-
VirtusModel = Virtus.model(strict: true, required: false)
|
39
|
-
|
40
|
-
def self.included(base)
|
41
|
-
base.class_eval do
|
42
|
-
include VirtusModel
|
43
|
-
include ActiveModel::Validations
|
44
|
-
include ActiveModel::Conversion
|
45
|
-
end
|
46
|
-
base.extend(ClassMethods)
|
47
|
-
end
|
48
|
-
|
49
|
-
module ClassMethods
|
50
|
-
def param_key(name)
|
51
|
-
class_eval <<-RUBY
|
52
|
-
def self.model_name
|
53
|
-
@model_name ||= ActiveModel::Name.new(self, nil, #{name.to_s.inspect})
|
54
|
-
end
|
55
|
-
RUBY
|
56
|
-
end
|
57
|
-
|
58
|
-
def [](input)
|
59
|
-
new(input)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# Mixin for ROM-compliant validator objects
|
65
|
-
#
|
66
|
-
# @example
|
67
|
-
#
|
68
|
-
#
|
69
|
-
# class UserParams
|
70
|
-
# include ROM::Model::Params
|
71
|
-
#
|
72
|
-
# attribute :name
|
73
|
-
#
|
74
|
-
# validates :name, presence: true
|
75
|
-
# end
|
76
|
-
#
|
77
|
-
# class UserValidator
|
78
|
-
# include ROM::Model::Validator
|
79
|
-
# end
|
80
|
-
#
|
81
|
-
# params = UserParams.new(name: '')
|
82
|
-
# UserValidator.call(params) # raises ValidationError
|
83
|
-
#
|
84
|
-
# @api public
|
85
|
-
module Validator
|
86
|
-
def self.included(base)
|
87
|
-
base.extend(ClassMethods)
|
88
|
-
end
|
89
|
-
|
90
|
-
module ClassMethods
|
91
|
-
def call(params)
|
92
|
-
raise ValidationError.new(params) unless params.valid?
|
93
|
-
end
|
94
|
-
end
|
6
|
+
include Charlatan.new(:errors)
|
7
|
+
include Equalizer.new(:errors)
|
95
8
|
end
|
96
9
|
end
|
97
10
|
end
|
11
|
+
|
12
|
+
require 'rom/rails/model/params'
|
13
|
+
require 'rom/rails/model/validator'
|
14
|
+
require 'rom/rails/model/form'
|