attribeauty 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +42 -1
- data/lib/attribeauty/params.rb +13 -1
- data/lib/attribeauty/version.rb +1 -1
- data/lib/attribeauty.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16789f41e6c0cfea21c44f2f71fc3fd0c9664791ed8d1c97648834be86db6e92
|
4
|
+
data.tar.gz: e8c5ac4397043090870ffeacc5f4b1ae0565f0b7e85237a47254a41e31258032
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06c7ca8c0f104cc00ac9854fd4d5c31c043daf65c4d993c32da3dc0472ce4be296ebc2640e2b8b759fb6d99ba07d54e4a8b3c251c183da2048b22eb7da23af78
|
7
|
+
data.tar.gz: 13eb48b958c5cfd1da319a33326449e86683b90f9c89631964c4397d9447334a025b7b3647c87f40ad8c11465c51af7f73e32319ad0c9e344757d823f58e19a8
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -84,6 +84,8 @@ end
|
|
84
84
|
|
85
85
|
Experimental params sanitizer is now available. This will cast your params, and remove elements you want to exclude if `nil` or `empty`
|
86
86
|
|
87
|
+
Why is this needed? Params arrive into the controller in a messy state. Booleans are not ready for caparison, integers are often strings, empty strings, and nils abound. Rails does the casting of params at the model, which is simple and elegant, but in many cases, these params are used for a multitude of things before hitting the database. I truly believe we need to cast them before they do anything.
|
88
|
+
|
87
89
|
```
|
88
90
|
# app/controllers/my_controller.rb
|
89
91
|
class MyController
|
@@ -149,7 +151,46 @@ end
|
|
149
151
|
|
150
152
|
```
|
151
153
|
|
152
|
-
|
154
|
+
If you want to raise an error, rather than just return the errors in an array, use the `accept!` method. Will raise `Attribeauty::MissingAttributeError` with the required elements:
|
155
|
+
|
156
|
+
|
157
|
+
```
|
158
|
+
class MyController
|
159
|
+
def update
|
160
|
+
MyRecord.update(update_params)
|
161
|
+
|
162
|
+
redirect_to index_path
|
163
|
+
end
|
164
|
+
|
165
|
+
private
|
166
|
+
|
167
|
+
# your params look like this:
|
168
|
+
# { user: { profile: [{ address: { street_name: "Main St" } }] } }
|
169
|
+
#
|
170
|
+
def params_filter
|
171
|
+
Attribeauty::Params.with(request.params)
|
172
|
+
end
|
173
|
+
|
174
|
+
# This following with the accept! method
|
175
|
+
# will raise: Attribeauty::MissingAttributeError, "title required, email required"
|
176
|
+
#
|
177
|
+
def update_params
|
178
|
+
params_filter.accept! do
|
179
|
+
container :user do
|
180
|
+
attribute :title, :string, allow_nil: false, required: true
|
181
|
+
attribute :email do
|
182
|
+
attribute :address, :string, allow_empty: false
|
183
|
+
attribute :valid, :boolean, allow_nil: false
|
184
|
+
attribute :ip_address, :string, allow_blank: true
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
See `test/test_params.rb` for more examples
|
153
194
|
|
154
195
|
|
155
196
|
## Development
|
data/lib/attribeauty/params.rb
CHANGED
@@ -10,7 +10,7 @@ module Attribeauty
|
|
10
10
|
new(request_params)
|
11
11
|
end
|
12
12
|
|
13
|
-
attr_reader :allow_nil, :prefix, :request_params, :acceptables, :to_h, :errors
|
13
|
+
attr_reader :allow_nil, :prefix, :request_params, :acceptables, :to_h, :errors, :strict
|
14
14
|
|
15
15
|
def initialize(request_params)
|
16
16
|
@request_params = request_params.transform_keys(&:to_sym)
|
@@ -21,9 +21,17 @@ module Attribeauty
|
|
21
21
|
def accept(&)
|
22
22
|
instance_eval(&)
|
23
23
|
|
24
|
+
raise MissingAttributeError, errors.join(", ") if errors.any? && strict?
|
25
|
+
|
24
26
|
self
|
25
27
|
end
|
26
28
|
|
29
|
+
def accept!(&)
|
30
|
+
@strict = true
|
31
|
+
|
32
|
+
accept(&)
|
33
|
+
end
|
34
|
+
|
27
35
|
def to_hash = to_h
|
28
36
|
|
29
37
|
def [](key)
|
@@ -69,5 +77,9 @@ module Attribeauty
|
|
69
77
|
def valid?
|
70
78
|
errors.empty?
|
71
79
|
end
|
80
|
+
|
81
|
+
def strict?
|
82
|
+
strict
|
83
|
+
end
|
72
84
|
end
|
73
85
|
end
|
data/lib/attribeauty/version.rb
CHANGED
data/lib/attribeauty.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribeauty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toby
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: There are so many of these, I just needed this one.
|
14
14
|
email:
|