tradesman 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +86 -22
- data/lib/tradesman/class_methods.rb +4 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ea231527a974f62567a09d96470e63b76b4d4dc
|
4
|
+
data.tar.gz: 69f01315549ff78600d26241d780ce82a05bb680
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d96de3e357daa072bc382cd35e20ef70c90c7cb685a96a5a5c5351b3e275e8e5b363984e460165eca368fe3247ef3fef0dcb68544508922948499c1a8e455524
|
7
|
+
data.tar.gz: d36f8d0d668865ac215bd49674132fd74bf40229fb61b12f77c16530ba00f8a0c736e613626d704c62750f1832e6b6146c3853487cb212828fac8bee31926b18
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Tradesman
|
2
2
|
|
3
|
-
Tradesman lets you invoke human-readble classes that handle the pass, fail, and invalid cases of common create, update, and delete actions
|
3
|
+
Tradesman lets you invoke human-readble classes that handle the pass, fail, and invalid cases of common create, update, and delete actions.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
@@ -19,7 +19,8 @@ outcome.result #=> Error Class
|
|
19
19
|
outcome.type #=> :validation
|
20
20
|
|
21
21
|
# With invalid parameters - fail loudly!
|
22
|
-
outcome = Tradesman::CreateUser.go!(invalid_user_params)
|
22
|
+
outcome = Tradesman::CreateUser.go!(invalid_user_params)
|
23
|
+
#=> raises Tradesman::Invalid or Tradesman::Failure
|
23
24
|
|
24
25
|
# Passing a block - Well-suited for Controllers
|
25
26
|
Tradesman::UpdateUser.go(params[:id], user_update_params) do
|
@@ -52,8 +53,6 @@ Tradesman::CreateUserForEmployer.go(employer, [user_params, user_params, user_pa
|
|
52
53
|
Tradesman::UpdateUser.go([user1, user2, user3], update_params)
|
53
54
|
|
54
55
|
# Update n records with n sets of parameters
|
55
|
-
# Whenever you pass an id, you can either pass the id itself,
|
56
|
-
# or an object that response to :id
|
57
56
|
update_params = {
|
58
57
|
user1.id => user1_params,
|
59
58
|
user2.id => user2_params,
|
@@ -94,10 +93,63 @@ CreateInvoiceForCustomer
|
|
94
93
|
|
95
94
|
Where 'For' is a string literal and **ParentRecord** is the parent model classname, CamelCased.
|
96
95
|
|
96
|
+
## Parameters
|
97
|
+
|
98
|
+
**Create**
|
99
|
+
|
100
|
+
`Create` classes take either a parameters hash or an array of parameters hashes.
|
101
|
+
|
102
|
+
Examples:
|
103
|
+
```ruby
|
104
|
+
Tradesman::CreateUser.go(params)
|
105
|
+
Tradesman::CreateUser.go([params1, params2, params3])
|
106
|
+
```
|
107
|
+
|
108
|
+
**CreateForParent**
|
109
|
+
|
110
|
+
`CreateForParent` classes take a parent and either a parameters hash or an array of parameters hashes.
|
111
|
+
The parent can either be an :id or an object that responds to #id.
|
112
|
+
|
113
|
+
Examples:
|
114
|
+
```ruby
|
115
|
+
Tradesman::CreateInvoiceForCustomer.go(customer, invoice_params)
|
116
|
+
Tradesman::CreateInvoiceForCustomer.go(123, [invoice1, invoice2, invoice3])
|
117
|
+
```
|
118
|
+
|
119
|
+
**Update**
|
120
|
+
|
121
|
+
`Update` classes take a record or array of records, and a paramter hash or array of parameter hashes.
|
122
|
+
|
123
|
+
Examples:
|
124
|
+
```ruby
|
125
|
+
# Update a single record
|
126
|
+
Tradesman::UpdateUser.go(user, update_params)
|
127
|
+
|
128
|
+
# Update multiple records with the same parameters
|
129
|
+
Tradesman::UpdateUser.go([111, 222, 333], update_params)
|
130
|
+
|
131
|
+
# Update n records with n sets of parameters
|
132
|
+
update_params = {
|
133
|
+
user1 => user1_params,
|
134
|
+
user2 => user2_params,
|
135
|
+
user3 => user3_params
|
136
|
+
}
|
137
|
+
Tradesman::UpdateUser.go(update_params.keys, update_params.values)
|
138
|
+
```
|
139
|
+
|
140
|
+
**Delete**
|
141
|
+
|
142
|
+
`Delete` classes take either a record of array of records.
|
143
|
+
|
144
|
+
Examples:
|
145
|
+
```ruby
|
146
|
+
Tradesman::DeleteUser.go(123)
|
147
|
+
Tradesman::DeleteUser.go([user1, user2, user3])
|
148
|
+
```
|
97
149
|
|
98
150
|
## Why is this necessary?
|
99
151
|
|
100
|
-
|
152
|
+
Many Create, Update and Delete actions we program are simple and often repeated (albeit with different records and parameter lists) in several locations. They can generally be broken in to the following steps:
|
101
153
|
|
102
154
|
- Query existing record by some group of parameters, but generally just by :id (Update and Delete only)
|
103
155
|
- Return 404 if record does not exist
|
@@ -129,17 +181,19 @@ Tradesman is designed to handle the above and a few other common use-cases to re
|
|
129
181
|
|
130
182
|
Tradesman version of the above:
|
131
183
|
```ruby
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
184
|
+
def update
|
185
|
+
Tradesman::UpdateUser.go(params[:id], user_params) do
|
186
|
+
success do |result|
|
187
|
+
@user = result
|
188
|
+
render 'user'
|
189
|
+
end
|
137
190
|
|
138
|
-
|
139
|
-
|
140
|
-
|
191
|
+
invalid do |error|
|
192
|
+
render(text: error.message, status: 422)
|
193
|
+
end
|
141
194
|
|
142
|
-
|
195
|
+
failure { |result| render(text: 'false', status: 400) } # If you prefer one-liners
|
196
|
+
end
|
143
197
|
end
|
144
198
|
|
145
199
|
private
|
@@ -149,20 +203,34 @@ def user_params
|
|
149
203
|
end
|
150
204
|
```
|
151
205
|
|
152
|
-
The Tradesman version
|
206
|
+
The Tradesman version is self-documenting, cruft-free, and designed for testing.
|
153
207
|
|
154
208
|
## Config
|
155
209
|
|
156
210
|
**Define your adapter**
|
157
211
|
|
212
|
+
Tradesman sits on top of [Horza](https://github.com/onfido/horza/), and can use any of its adapters.
|
213
|
+
|
158
214
|
```ruby
|
159
215
|
# config/initializers/tradesman.rb
|
160
216
|
Tradesman.configure { |config| config.adapter = :active_record }
|
161
217
|
```
|
162
218
|
|
163
|
-
**Development Mode
|
219
|
+
**Development Mode**
|
164
220
|
|
165
|
-
|
221
|
+
Turn on development mode to help Tradesman cope with Rails' lazy-loading of classes.
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
# config/initializers/tradesman.rb
|
225
|
+
Tradesman.configure do |config|
|
226
|
+
config.adapter = :active_record
|
227
|
+
config.development_mode = Rails.env.development?
|
228
|
+
end
|
229
|
+
```
|
230
|
+
|
231
|
+
**Model Namespaces**
|
232
|
+
|
233
|
+
Lazy loading becomes more complex if your models are namespaced.
|
166
234
|
|
167
235
|
Consider:
|
168
236
|
```ruby
|
@@ -179,7 +247,7 @@ module MyOtherNamespace
|
|
179
247
|
end
|
180
248
|
```
|
181
249
|
|
182
|
-
In order to help Tradesman lazy load these models, you need to
|
250
|
+
In order to help Tradesman lazy load these models, you need to explicitly configure any namespaces:
|
183
251
|
|
184
252
|
```ruby
|
185
253
|
# config/initializers/tradesman.rb
|
@@ -195,10 +263,6 @@ end
|
|
195
263
|
Tradesman.reset
|
196
264
|
```
|
197
265
|
|
198
|
-
## Adapters
|
199
|
-
|
200
|
-
Tradesman sits on top of [Horza](https://github.com/onfido/horza/), and can use any of its adapters.
|
201
|
-
|
202
266
|
## Edge Cases
|
203
267
|
|
204
268
|
**Models ending with double 's'**
|
@@ -24,9 +24,10 @@ module Tradesman
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def id_from_obj(obj)
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
id = obj.respond_to?(:id) ? obj.id : obj
|
28
|
+
Integer(id)
|
29
|
+
rescue ArgumentError
|
30
|
+
raise Tradesman::InvalidId.new('You must pass an object that responds to id or an integer')
|
30
31
|
end
|
31
32
|
|
32
33
|
def prepare_params(params)
|
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: 1.0.
|
4
|
+
version: 1.0.1
|
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-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: horza
|