decanter 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42bc613e6f1049e3332f4ed3bad67d6031be513c
4
- data.tar.gz: bc2fbd47a658c6dc924ea3de2667ab73c6c3b723
3
+ metadata.gz: db2d47fb35516ad171707804d74d599dfc9f4275
4
+ data.tar.gz: 9de4216e15cef03ab046d1dc68372ce3de6f834d
5
5
  SHA512:
6
- metadata.gz: 43919de7aab2fce821689d08b631ff3e18226cd6e54823d979521073df659fe14bdd2178bac3713f3dc6c0399c58867ed0eb2cbda4b94c14ab035e2489fa57ef
7
- data.tar.gz: ae021eb752244c37311232ec2b1213755c2f20efaa7b16bca9a3d7200ce8db052e8a9c74bc6fcb9e21775a487f04127216c9dc62dfa1e312c1fbe52365ff23d3
6
+ metadata.gz: f5a4887ef4cebe7358b2a57a25ef558647ebc4760369636c91dd65aa266649e0a85df014f02b746849d3fd301f603cb865154349aedba8a8232684d0ff1da127
7
+ data.tar.gz: 3efb3ee49fc50aaa223c2205fe4e2ff9bde46fc482529510d86735bc16d31db7243f7d1e58bc2433b852f46ec74358312fb748544f268e37176c3c9512f35eb8
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- decanter (0.6.0)
4
+ decanter (0.6.2)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -8,7 +8,9 @@ Decanter
8
8
  What is Decanter?
9
9
  ---
10
10
 
11
- Decanter is a Rails gem that makes it easy to manipulate form data before it hits the model. The basic idea is that form data entered by a user often needs to be processed before it is stored into the database. A typical example of this is a datepicker. A user selects January 15th, 2015 as the date, but this is going to come into our controller as a string like "01/15/2015", so we need to convert this string to a Ruby Date object before it is stored in our database. Many developers perform this conversion right in the controller, which results in errors and unnecessary complexity, especially as the application grows.
11
+ Decanter is a Rails gem that makes it easy to transform incoming data before it hits the model. The basic idea is that form data entered by a user often needs to be processed before it is stored into the database. A typical example of this is a datepicker. A user selects January 15th, 2015 as the date, but this is going to come into our controller as a string like "01/15/2015", so we need to convert this string to a Ruby Date object before it is stored in our database. Many developers perform this conversion right in the controller, which results in errors and unnecessary complexity, especially as the application grows.
12
+
13
+ You can think of Decanter as the opposite of Active Model Serializer. Whereas AMS transforms your outbound data into a format that your frontend consumes, Decanter transforms your incoming data into a format that your backend consumes.
12
14
 
13
15
  Installation
14
16
  ---
@@ -73,6 +75,17 @@ In your controller:
73
75
  end
74
76
  ```
75
77
 
78
+ Or, if you would prefer to get the parsed hash and then do your own logic, you can do the following:
79
+
80
+ ```ruby
81
+ def create
82
+ parsed_params = Trip.decant(params[:trip])
83
+ @trip = Trip.new(parsed_params)
84
+
85
+ # save logic here
86
+ end
87
+ ```
88
+
76
89
  Basic Example
77
90
  ---
78
91
 
@@ -134,7 +147,7 @@ class TripDecanter < Decanter::Base
134
147
  end
135
148
  ```
136
149
 
137
- You'll also notice that instead of ```@trip = Trip.new(params[:trip])``` we do ```@trip = Trip.decant_new(params[:trip])```. ```decant_new`` is where the magic happens. It is converting the params from this:
150
+ You'll also notice that instead of ```@trip = Trip.new(params[:trip])``` we do ```@trip = Trip.decant_new(params[:trip])```. ```decant_new``` is where the magic happens. It is converting the params from this:
138
151
 
139
152
  ```ruby
140
153
  {
@@ -271,7 +284,7 @@ Each of the destinations in our params[:trip] are automatically parsed according
271
284
  Non Database-Backed Objects
272
285
  ---
273
286
 
274
- Decanter will work for you non database-backed objects as well. We just need to call ```decant``` to parse our params according to our decanter logic.
287
+ Decanter will work for your non database-backed objects as well. We just need to call ```decant``` to parse our params according to our decanter logic.
275
288
 
276
289
  Let's say we have a search filtering object called ```SearchFilter```. We start by generating our decanter:
277
290
 
@@ -336,11 +349,34 @@ rails g parser SquashDate
336
349
  # app/decanter/squashers/date_squasher.rb
337
350
 
338
351
  class SquashDateParser < Decanter::Parser::Base
339
- parser do |name, values, options|
340
- day = values[0]
341
- month = values[1]
342
- year = values[2]
352
+ parser do |name, values, options|
353
+ day, month, year = values.map(&:to_i)
343
354
  Date.new(year, month, day)
344
355
  end
345
356
  end
346
357
  ```
358
+
359
+ No Need for Strong Params
360
+ ---
361
+
362
+ Since you are already defining your expected inputs in Decanter, you really don't need strong_params anymore.
363
+
364
+ In order to tell Decanter to ignore the params not defined in your Decanter, just add the ```strict``` flag to your Decanters:
365
+
366
+ ```ruby
367
+ class TripDecanter < Decanter::Base
368
+ strict true
369
+
370
+ input :name
371
+ end
372
+ ```
373
+
374
+ Or to raise exceptions when parameters arrive in your Decanter that you didn't expect:
375
+
376
+ ```ruby
377
+ class TripDecanter < Decanter::Base
378
+ strict :with_exception
379
+
380
+ input :name
381
+ end
382
+ ```
@@ -15,6 +15,10 @@ module Decanter
15
15
  self.save!(context: options[:context])
16
16
  end
17
17
 
18
+ def decant(args, **options)
19
+ self.class.decant(args, options)
20
+ end
21
+
18
22
  module ClassMethods
19
23
 
20
24
  def decant_create(args, **options)
@@ -31,7 +35,7 @@ module Decanter
31
35
  .save!(context: options[:context])
32
36
  end
33
37
 
34
- def decant(args, options)
38
+ def decant(args, options={})
35
39
  options.fetch(:decanter, Decanter.decanter_for(self))
36
40
  .decant(args)
37
41
  end
@@ -1,3 +1,3 @@
1
1
  module Decanter
2
- VERSION = '0.6.2'
2
+ VERSION = '0.6.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decanter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Francis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-02-18 00:00:00.000000000 Z
12
+ date: 2016-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport