tradesman 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +72 -4
- data/lib/tradesman/configuration.rb +12 -2
- data/spec/tradesman_spec.rb +35 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db5c2d7076e95ba6d615b720841bd7060498fd41
|
4
|
+
data.tar.gz: 8e3c89647d347d22d91f8b6674b97365ce0615b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fda495879e02cc5199b8327879fae9fa7acbacd1aa2f6e3b94a8391482d3fb82b2a3a1e130006e3df9fcf0ddeeefdb423ad4f1b2e7b597d7c7fefeb473e3d9b
|
7
|
+
data.tar.gz: d6868892dc9f4441426dbe234e3bafe40c38f83788e592b34e5a7caba9410071f280156e7466babd790c8989fcd3cd6ae86dae11d19d467c988d8d92f676a8aa
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ outcome.success? #=> true
|
|
13
13
|
outcome.result #=> User Entity
|
14
14
|
|
15
15
|
# Passing a block - Well-suited for Controllers
|
16
|
-
Tradesman::UpdateUser.run(params[:id]
|
16
|
+
Tradesman::UpdateUser.run({ id: params[:id] }.merge(user_update_params)) do
|
17
17
|
success do |result|
|
18
18
|
render(text: 'true', status: 200)
|
19
19
|
end
|
@@ -28,10 +28,10 @@ Tradesman::UpdateUser.run(params[:id], user_update_params) do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
# Can also Delete
|
31
|
-
Tradesman::DeleteUser.run(params[:id])
|
31
|
+
Tradesman::DeleteUser.run(id: params[:id])
|
32
32
|
|
33
33
|
# Or Create as a child of an existing record
|
34
|
-
Tradesman::CreateUserForEmployer.run(employer_id
|
34
|
+
Tradesman::CreateUserForEmployer.run({ parent_id: employer_id }.merge(user_params))
|
35
35
|
```
|
36
36
|
|
37
37
|
## Why is this necessary?
|
@@ -68,7 +68,7 @@ Tradesman is designed to handle the above and a few other common use-cases to re
|
|
68
68
|
|
69
69
|
Tradesman version of the above:
|
70
70
|
```ruby
|
71
|
-
Tradesman::UpdateUser.run(
|
71
|
+
Tradesman::UpdateUser.run(user_params) do
|
72
72
|
success do |result|
|
73
73
|
@user = result
|
74
74
|
render 'user'
|
@@ -80,6 +80,74 @@ Tradesman::UpdateUser.run(params[:id], user_params) do
|
|
80
80
|
|
81
81
|
failure { |result| render(text: 'false', status: 400) } # If you prefer one-liners
|
82
82
|
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def user_params
|
87
|
+
params.permit(:id, :first_name, :last_name)
|
88
|
+
end
|
83
89
|
```
|
84
90
|
|
85
91
|
The Tradesman version says exactly what it does, is cruft free, and is much quicker to test (more on that later).
|
92
|
+
|
93
|
+
## Config
|
94
|
+
|
95
|
+
**Define your adapter**
|
96
|
+
|
97
|
+
_config/initializers/tradesman.rb_
|
98
|
+
```ruby
|
99
|
+
Tradesman.configure { |config| config.adapter = :active_record }
|
100
|
+
```
|
101
|
+
|
102
|
+
**Development Mode and Model Namespaces**
|
103
|
+
|
104
|
+
Rails' lazy-loading in the development environment makes a bit more configuration necessary, particularly if you have wrapped your models in namespaces.
|
105
|
+
|
106
|
+
Consider:
|
107
|
+
```ruby
|
108
|
+
module MyNamespace
|
109
|
+
class Employer < ActiveRecord::Base
|
110
|
+
has_many :users
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
module MyOtherNamespace
|
115
|
+
class User < ActiveRecord::Base
|
116
|
+
belongs_to :employer
|
117
|
+
end
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
In order to help Tradesman lazy load these models, you need to enable development mode and configure any namespaces:
|
122
|
+
|
123
|
+
_config/initializers/tradesman.rb_
|
124
|
+
```ruby
|
125
|
+
Tradesman.configure do |config|
|
126
|
+
config.adapter = :active_record
|
127
|
+
config.development_mode = Rails.env.development?
|
128
|
+
config.namespaces = [MyNamespace, MyOtherNamespace]
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
**Reset Tradesman** _(Can be done at runtime or in tests)_
|
133
|
+
```ruby
|
134
|
+
Tradesman.reset
|
135
|
+
```
|
136
|
+
|
137
|
+
## Adapters
|
138
|
+
|
139
|
+
Tradesman sits on top of [Horza](https://github.com/onfido/horza/), and can use any of its adapters.
|
140
|
+
|
141
|
+
## Edge Cases
|
142
|
+
|
143
|
+
**Models ending with double 's'**
|
144
|
+
|
145
|
+
Some models end with a double 's', ie `Address`, `Business`. Rails has a well documented inability to properly inflect this type of word.
|
146
|
+
There is a simple fix:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
# config/initializers/inflections.rb
|
150
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
151
|
+
inflect.singular(/ess$/i, 'ess')
|
152
|
+
end
|
153
|
+
```
|
@@ -20,11 +20,21 @@ module Tradesman
|
|
20
20
|
end
|
21
21
|
|
22
22
|
class Config
|
23
|
-
attr_accessor :adapter
|
23
|
+
attr_accessor :adapter, :namespaces
|
24
24
|
|
25
|
-
def
|
25
|
+
def adapter=(adapter)
|
26
26
|
Horza.configure { |config| config.adapter = adapter }
|
27
27
|
@adapter = Horza.adapter
|
28
28
|
end
|
29
|
+
|
30
|
+
def development_mode=(mode)
|
31
|
+
Horza.configure { |config| config.development_mode = mode }
|
32
|
+
end
|
33
|
+
|
34
|
+
def namespaces=(namespaces)
|
35
|
+
fail Tradesman::Errors::Base.new 'namespaces must be an array' unless namespaces.is_a? Array
|
36
|
+
Horza.configure { |config| config.namespaces = namespaces }
|
37
|
+
@namespaces = namespaces
|
38
|
+
end
|
29
39
|
end
|
30
40
|
end
|
data/spec/tradesman_spec.rb
CHANGED
@@ -32,7 +32,7 @@ end
|
|
32
32
|
|
33
33
|
describe Tradesman do
|
34
34
|
let(:adapter) { :active_record }
|
35
|
-
before { Tradesman.configure { |config| config.
|
35
|
+
before { Tradesman.configure { |config| config.adapter = adapter } }
|
36
36
|
after do
|
37
37
|
TradesmanSpec::User.delete_all
|
38
38
|
TradesmanSpec::StrictUser.delete_all
|
@@ -56,6 +56,40 @@ describe Tradesman do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
+
context 'namespaces' do
|
60
|
+
context 'when no namespaces are set' do
|
61
|
+
it 'does not forward namespaces to Horza' do
|
62
|
+
expect(Horza.configuration.namespaces.empty?).to be true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
context 'when namespaces are set' do
|
66
|
+
before do
|
67
|
+
Tradesman.configure { |config| config.namespaces = [TradesmanSpec] }
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'forwards namespaces to Horza' do
|
71
|
+
expect(Horza.configuration.namespaces).to eq [TradesmanSpec]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'development_mode' do
|
77
|
+
context 'when development_mode is not set' do
|
78
|
+
it 'does not forward namespaces to Horza' do
|
79
|
+
expect(Horza.configuration.development_mode).to be nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
context 'when namespaces are set' do
|
83
|
+
before do
|
84
|
+
Tradesman.configure { |config| config.development_mode = true }
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'forwards namespaces to Horza' do
|
88
|
+
expect(Horza.configuration.development_mode).to be true
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
59
93
|
context '#run' do
|
60
94
|
context 'Create' do
|
61
95
|
context 'when parameters are valid' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tradesman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Turner
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-06-
|
12
|
+
date: 2015-06-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: horza
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 0.3.
|
20
|
+
version: 0.3.3
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 0.3.
|
27
|
+
version: 0.3.3
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: tzu
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|