multiparameter_attributes_handler 0.0.1 → 0.0.2
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.
- data/README.rdoc +36 -27
- data/lib/multiparameter_attributes_handler.rb +2 -2
- data/lib/multiparameter_attributes_handler/version.rb +18 -1
- data/test/thing.rb +7 -7
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -18,27 +18,50 @@ when set to 24th June 2004, will generate
|
|
18
18
|
}
|
19
19
|
|
20
20
|
ActiveRecord understands this and converts these key/value pairs into a date
|
21
|
-
object that is passed to thing.
|
21
|
+
object that is passed to thing.last_read=
|
22
22
|
|
23
23
|
=== A solution without ActiveRecord
|
24
24
|
|
25
|
-
Multiparameter Attributes Handler allows other objects to do the same.
|
26
|
-
particular, ActiveResource models.
|
25
|
+
Multiparameter Attributes Handler allows other objects to do the same.
|
27
26
|
|
28
|
-
|
29
|
-
the values passed down the attributes= chain gets converted to:
|
27
|
+
thing_params = MultiparameterAttributesHandler.manipulate_all(params[:thing])
|
30
28
|
|
31
|
-
|
29
|
+
This creates a last_read key with a time object derived from the values
|
30
|
+
|
31
|
+
thing_params == {
|
32
32
|
"last_read(1i)" => "2004",
|
33
33
|
"last_read(2i)" => "6",
|
34
34
|
"last_read(3i)" => "24",
|
35
35
|
"last_read" => Time.local('2004', '6', '24')
|
36
|
-
|
36
|
+
}
|
37
37
|
|
38
|
-
So for a ActiveResource Thing, this functionality
|
38
|
+
So for a ActiveResource Thing, this functionality can be added by over-riding
|
39
|
+
the attributes setter:
|
39
40
|
|
40
41
|
class Thing < ActiveResource::Base
|
41
|
-
|
42
|
+
def attributes=(params)
|
43
|
+
super MultiparameterAttributesHandler.manipulate_all(params)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
However, as the params splitting and rebuilding is happening in the views and
|
48
|
+
controller space, it might well make more sense to do the manipulation in the
|
49
|
+
controller:
|
50
|
+
|
51
|
+
class ThingsController < ApplicationController
|
52
|
+
|
53
|
+
....
|
54
|
+
|
55
|
+
def update
|
56
|
+
@thing = Thing.find(params[:id])
|
57
|
+
@property.update_attributes thing_params
|
58
|
+
redirect_to @property
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def thing_params
|
63
|
+
MultiparameterAttributesHandler.manipulate_all(params[:thing])
|
64
|
+
end
|
42
65
|
end
|
43
66
|
|
44
67
|
=== Only setter affected
|
@@ -47,34 +70,20 @@ Note that this gem provides handling of the parameters received from the form
|
|
47
70
|
submission. For the form helpers to work, they need to passed a date or time
|
48
71
|
to the form.
|
49
72
|
|
50
|
-
One solution is to
|
51
|
-
above
|
73
|
+
One solution is to override the getter in the model. So for the Thing example
|
74
|
+
above:
|
52
75
|
|
53
76
|
require 'date'
|
54
77
|
class Thing < ActiveResource::Base
|
55
|
-
include MultiparameterAttributesHandler
|
56
78
|
|
57
79
|
def last_read
|
58
|
-
|
80
|
+
Time.parse(super)
|
59
81
|
end
|
60
82
|
end
|
61
83
|
|
62
84
|
Rails form date helpers will then work for last_read:
|
63
85
|
|
64
|
-
|
65
|
-
|
66
|
-
And in the controller's create and update methods:
|
67
|
-
|
68
|
-
thing.attributes = params[:thing]
|
69
|
-
|
70
|
-
Or
|
71
|
-
|
72
|
-
Thing.new(params[:thing])
|
73
|
-
|
74
|
-
Or
|
75
|
-
|
76
|
-
thing.update_attributes(params[:thing])
|
77
|
-
|
86
|
+
datetime_select(:thing, :last_read)
|
78
87
|
|
79
88
|
=== Installation
|
80
89
|
|
@@ -2,8 +2,8 @@ require_relative 'multiparameter_attributes_handler/manipulator'
|
|
2
2
|
require_relative 'multiparameter_attributes_handler/multiparameter_attributes_handler_error'
|
3
3
|
module MultiparameterAttributesHandler
|
4
4
|
|
5
|
-
def
|
6
|
-
|
5
|
+
def self.manipulate_all(hash)
|
6
|
+
Manipulator.new(hash).output
|
7
7
|
end
|
8
8
|
|
9
9
|
end
|
@@ -1,10 +1,27 @@
|
|
1
1
|
module MultiparameterAttributesHandler
|
2
|
-
VERSION = "0.0.
|
2
|
+
VERSION = "0.0.2"
|
3
3
|
end
|
4
4
|
|
5
5
|
# History
|
6
6
|
# =======
|
7
7
|
#
|
8
|
+
# 0.0.2: Modified usage so that developer decides where manipulation happens
|
9
|
+
# ---------------------------------------------------------------------------
|
10
|
+
#
|
11
|
+
# Previously, it was assumed that the functionality would be applied to a model's
|
12
|
+
# attributes= method, as this is where most params values are sent (eventually).
|
13
|
+
#
|
14
|
+
# After some experimentation and consideration, it was realised that this is
|
15
|
+
# too restrictive. It also became apparent that the manipulation may need to
|
16
|
+
# happen in the controller rather than the model. Especially in Rails 4 where
|
17
|
+
# strong_parameters will already be manipulating the params.
|
18
|
+
#
|
19
|
+
# Therefore, the functionality was repackaged so it could be used anywhere.
|
20
|
+
#
|
21
|
+
# This change in usage will also make it easier to create single method usage
|
22
|
+
# in the future: where rather than manipulating all multiparameter attributes,
|
23
|
+
# it will be possible to specify which attributes to manipulate.
|
24
|
+
#
|
8
25
|
# 0.0.1: First gem
|
9
26
|
# ----------------
|
10
27
|
#
|
data/test/thing.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
require_relative '../lib/multiparameter_attributes_handler'
|
2
2
|
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
class Thing < Parent
|
3
|
+
class Thing
|
4
|
+
|
5
|
+
attr_reader :attributes
|
9
6
|
|
10
|
-
include MultiparameterAttributesHandler
|
11
7
|
|
12
8
|
def initialize
|
13
9
|
|
14
10
|
end
|
11
|
+
|
12
|
+
def attributes=(params)
|
13
|
+
@attributes = MultiparameterAttributesHandler.manipulate_all(params)
|
14
|
+
end
|
15
15
|
end
|
16
16
|
|