rest_framework 0.0.9 → 0.0.10

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
  SHA256:
3
- metadata.gz: ce317c05e27ab6421b18f37595dbb846a747c8616d19fc9a1927a9ecc30fefc7
4
- data.tar.gz: b8f88631bfca835f395677af0e0c0079b40c079aa8d9a6cc8c8ee46dc5d589c2
3
+ metadata.gz: a3ed68df38881608ec5cc6c66c286624a600398a745ab730e6cef89513f0138f
4
+ data.tar.gz: 7f2654b29027aa24357b14082e0e0236d9024521fe47e3ae90be7fdeda535b5f
5
5
  SHA512:
6
- metadata.gz: abd9fc1e83d61c60af77d834d3a3e03a4a904863f3ad4bb851d8cf35a5a563a322eef508a273764ce727303e3e25f526cca03a643127957a7d9952e4988de743
7
- data.tar.gz: 8aadf4ba5467c645da2404df61880bd741a491eea0c14a09b20e7056726022f7fc7bd4b8a3f90e9ac068b578e63870c85cfaa813296245cf8c4fddfb7d002a66
6
+ metadata.gz: 460fd09cc0b375e5509a83ff64bf5ffb87f864cb53e5cb51aa1027bce3a39bbd12a8f692a3c621ad93ac200f98bb095b369610b2953eedb9a47a181568fea948
7
+ data.tar.gz: 03c6ec7c3d910dac783e2ddb927ea53212e066246dcf1b2673d95e703754a9295a5e792d9daeb3c40fd5d334491e489e076b4740f766ab7880eaf3d731344bda
@@ -19,7 +19,7 @@
19
19
  <div class="container py-3">
20
20
  <div class="container">
21
21
  <div class="row">
22
- <h1><%= @title %></h1>
22
+ <h1><%= (@header_text if defined? @header_text) || @title %></h1>
23
23
  </div>
24
24
  <hr/>
25
25
  <% if @json_payload || @xml_payload %>
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.0.10
@@ -75,7 +75,7 @@ module RESTFramework
75
75
  end
76
76
 
77
77
  # Helper alias for `respond_to`/`render`, and replace nil responses with blank ones. `payload`
78
- # must be already serialized to Ruby primitives.
78
+ # should be already serialized to Ruby primitives.
79
79
  def api_response(payload, html_kwargs: nil, json_kwargs: nil, xml_kwargs: nil, **kwargs)
80
80
  html_kwargs ||= {}
81
81
  json_kwargs ||= {}
@@ -31,43 +31,37 @@ module RESTFramework
31
31
  return self.class.serializer_class || NativeModelSerializer
32
32
  end
33
33
 
34
- # Get a list of fields for an action (or the current action if none given).
35
- def get_fields(action: nil)
34
+ # Get a list of fields for the current action.
35
+ def get_fields
36
36
  action_fields = self.class.action_fields || {}
37
-
38
- # action will, by default, be the current action name
39
- action = action_name.to_sym unless action
37
+ action = self.action_name.to_sym
40
38
 
41
39
  # index action should use :list fields if :index is not provided
42
40
  action = :list if action == :index && !action_fields.key?(:index)
43
41
 
44
- return action_fields[action] || self.class.fields || []
42
+ return (action_fields[action] if action) || self.class.fields || []
45
43
  end
46
44
 
47
- # Get a native serializer config for an action (or the current action if none given).
48
- def get_native_serializer_config(action: nil)
49
- native_serializer_action_config = self.class.native_serializer_action_config || {}
50
-
51
- # action will, by default, be the current action name
52
- action = action_name.to_sym unless action
45
+ # Get a native serializer config for the current action.
46
+ def get_native_serializer_config
47
+ action_serializer_config = self.class.native_serializer_action_config || {}
48
+ action = self.action_name.to_sym
53
49
 
54
50
  # index action should use :list serializer config if :index is not provided
55
- action = :list if action == :index && !native_serializer_action_config.key?(:index)
51
+ action = :list if action == :index && !action_serializer_config.key?(:index)
56
52
 
57
- return native_serializer_action_config[action] || self.class.native_serializer_config
53
+ return (action_serializer_config[action] if action) || self.class.native_serializer_config
58
54
  end
59
55
 
60
- # Get a list of parameters allowed for an action (or the current action if none given).
61
- def get_allowed_parameters(action: nil)
56
+ # Get a list of parameters allowed for the current action.
57
+ def get_allowed_parameters
62
58
  allowed_action_parameters = self.class.allowed_action_parameters || {}
63
-
64
- # action will, by default, be the current action name
65
- action = action_name.to_sym unless action
59
+ action = self.action_name.to_sym
66
60
 
67
61
  # index action should use :list allowed parameters if :index is not provided
68
62
  action = :list if action == :index && !allowed_action_parameters.key?(:index)
69
63
 
70
- return allowed_action_parameters[action] || self.class.allowed_parameters
64
+ return (allowed_action_parameters[action] if action) || self.class.allowed_parameters
71
65
  end
72
66
 
73
67
  # Filter the request body for keys in current action's allowed_parameters/fields config.
@@ -7,28 +7,58 @@ module RESTFramework
7
7
  @data = data
8
8
  @controller = controller
9
9
  end
10
-
11
- def is_valid
12
- return true
13
- end
14
10
  end
15
11
 
16
12
  # This serializer uses `.as_json` to serialize objects. Despite the name, `.as_json` is a Rails
17
13
  # method which converts objects to Ruby primitives (with the top-level being either an array or a
18
14
  # hash).
19
15
  class NativeModelSerializer < BaseSerializer
20
- def initialize(model: nil, **kwargs)
16
+ class_attribute :config
17
+ class_attribute :singular_config
18
+ class_attribute :plural_config
19
+ class_attribute :action_config
20
+
21
+ def initialize(model: nil, many: nil, **kwargs)
21
22
  super(**kwargs)
22
- @model = model || @controller.send(:get_model)
23
+ @many = many
24
+ @model = model || (@controller ? @controller.send(:get_model) : nil)
25
+ end
26
+
27
+ # Get controller action, if possible.
28
+ def get_action
29
+ return @controller&.action_name&.to_sym
30
+ end
31
+
32
+ # Get a locally defined configuration, if one is defined.
33
+ def get_local_serializer_config
34
+ action = self.get_action
35
+
36
+ if action && self.action_config
37
+ # index action should use :list serializer config if :index is not provided
38
+ action = :list if action == :index && !self.action_config.key?(:index)
39
+
40
+ return self.action_config[action] if self.action_config[action]
41
+ end
42
+
43
+ # no action_config, so try singular/plural config
44
+ return self.plural_config if @many && self.plural_config
45
+ return self.singular_config if !@many && self.singular_config
46
+
47
+ # lastly, try the default config
48
+ return self.config
23
49
  end
24
50
 
25
51
  # Get a configuration passable to `as_json` for the model.
26
- def get_native_serializer_config
52
+ def get_serializer_config
53
+ # return a locally defined serializer config if one is defined
54
+ local_config = self.get_local_serializer_config
55
+ return local_config if local_config
56
+
27
57
  # return a serializer config if one is defined
28
58
  serializer_config = @controller.send(:get_native_serializer_config)
29
59
  return serializer_config if serializer_config
30
60
 
31
- # build serializer config from fields
61
+ # otherwise, build a serializer config from fields
32
62
  fields = @controller.send(:get_fields)
33
63
  unless fields.blank?
34
64
  columns, methods = fields.partition { |f| f.to_s.in?(@model.column_names) }
@@ -38,10 +68,52 @@ module RESTFramework
38
68
  return {}
39
69
  end
40
70
 
71
+ # Recursive method for traversing a config and evaluating nested serializers.
72
+ def _resolve_serializer_config(node: nil)
73
+ # First base case: found a serializer, so evaluate it and return it.
74
+ if node.is_a?(Class) && (node < BaseSerializer)
75
+ return node.new(controller: @controller).get_resolved_serializer_config
76
+ end
77
+
78
+ # Second base case: found a serializer instance, so evaluate it and return it.
79
+ if node.is_a?(BaseSerializer)
80
+ return node.get_resolved_serializer_config
81
+ end
82
+
83
+ # Third base case: node is not iterable, so return it.
84
+ unless node.respond_to?(:each)
85
+ return node
86
+ end
87
+
88
+ # Recursive case: node is iterable, so iterate and recursively resolve serializers.
89
+ if node.is_a? Hash
90
+ node.each do |k,v|
91
+ node[k] = self._resolve_serializer_config(node: v)
92
+ end
93
+ else
94
+ node.map! do |v|
95
+ self._resolve_serializer_config(node: v)
96
+ end
97
+ end
98
+
99
+ return node
100
+ end
101
+
102
+ # Get a serializer config and resolve any nested serializers into configs.
103
+ def get_resolved_serializer_config
104
+ config = self.get_serializer_config
105
+
106
+ # traverse the config, resolving nested serializers
107
+ return _resolve_serializer_config(node: config)
108
+ end
109
+
41
110
  # Convert the object(s) to Ruby primitives.
42
111
  def serialize
43
112
  if @object
44
- return @object.as_json(self.get_native_serializer_config)
113
+ @many = @object.respond_to?(:each) if @many.nil?
114
+ return @object.as_json(
115
+ self.get_resolved_serializer_config
116
+ )
45
117
  end
46
118
  return nil
47
119
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory N. Schmit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-06 00:00:00.000000000 Z
11
+ date: 2020-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails