railsful 0.2.0 → 0.3.0

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: 287dbb619eaa2542331bdfe1c4dc85b167fd5d278ccb29d9e8c6efc9f7018f25
4
- data.tar.gz: 76d078511d927554643d36f55b1180c145150d0ad57e66531b657a3f9b066954
3
+ metadata.gz: 5cd2f4395f88962e11a4335a4bf967c227211ecf1cd0ebc8a08f6a874e776789
4
+ data.tar.gz: fa6dd976d49839ac1ba66059338511e9531cdf07cedbba0d984024145b9b5d11
5
5
  SHA512:
6
- metadata.gz: f5dbe0c90a4f3104bcac9b2eb41c289b4c3a11adff09884f020d5eaf0f5321290e8970d71ea084cb7a4798cf6bfd7d6da429a2a5e61339e6bdd1c427fb50a174
7
- data.tar.gz: aec33b3ee51fcd7338a0e83b6306bdf2e42e9fd7bbd35ec4cefa9e0dc584506f7aaafc61c1dba37d61474786af67e9e7caf5c10dc1468f95167e2a45c04a4344
6
+ metadata.gz: 5b497801b60386efe403923d63ca2e77f0df7c899bec213dda0a332df117342d0dd92a1ad8e353bab72a478725afdb0249d674b646152a7b39b6ed3dd8c3aa17
7
+ data.tar.gz: 0ce44ea6f1a17ed26b9b3d7a77b1ac5e062365d2f13aa70190e1e4816cf00012c7c8594206b8ceb2fd70f2565e2603d6fa3a4c29eb91cb5f42fb20d78b5bce54
data/README.md CHANGED
@@ -77,6 +77,17 @@ GET /some_module/users
77
77
  }
78
78
  ```
79
79
 
80
+ If you want to use another serializer you can do this by specifying a
81
+ `serializer` key inside the options:
82
+
83
+ ``` ruby
84
+ render json: User.first, serializer: 'author'
85
+
86
+ # or
87
+
88
+ render json: User.first, serializer: AuthorSerializer
89
+ ```
90
+
80
91
  ### Deserialization
81
92
  For deserialization of jsonapi compliant request all controllers that
82
93
  inherit from `ActionController` can use the `#deserialized_params` method.
@@ -39,7 +39,7 @@ module Railsful
39
39
  return options unless renderable
40
40
 
41
41
  # Try to fetch the right serializer for given renderable.
42
- serializer = serializer_for(renderable)
42
+ serializer = serializer_for(renderable, options)
43
43
 
44
44
  # When no serializer is found just pass the original options hash.
45
45
  return options unless serializer
@@ -49,12 +49,37 @@ module Railsful
49
49
  end
50
50
 
51
51
  # Find the right serializer for given object.
52
+ # First we will look if the options hash includes a serializer. If not,
53
+ # we try to guess the right serializer from the model/class name.
52
54
  #
53
- # @param [ApplicationRecord, ActiveRecord::Relation]
55
+ # @param renderable [ApplicationRecord, ActiveRecord::Relation]
56
+ # @param options [Hash]
54
57
  # @return [Class] The serializer class.
55
58
  #
56
59
  # :reek:UtilityFunction
57
- def serializer_for(renderable)
60
+ def serializer_for(renderable, options = {})
61
+ serializer_by_options(options) || serializer_by_renderable(renderable)
62
+ end
63
+
64
+ # Check the options hash for a serializer key.
65
+ #
66
+ # @return [Class] The serializer class.
67
+ #
68
+ # :reek:UtilityFunction
69
+ def serializer_by_options(options)
70
+ serializer = options[:serializer]
71
+ return unless serializer
72
+
73
+ # If the given serializer is a class return it.
74
+ return serializer if serializer.is_a? Class
75
+
76
+ "#{serializer.to_s.classify}Serializer".safe_constantize
77
+ end
78
+
79
+ # @return [Class] The serializer class.
80
+ #
81
+ # :reek:UtilityFunction
82
+ def serializer_by_renderable(renderable)
58
83
  # Get the class in order to find the right serializer.
59
84
  klass = if renderable.is_a?(ActiveRecord::Relation)
60
85
  renderable.model.name
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Railsful
4
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.3.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railsful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Vogt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-05-27 00:00:00.000000000 Z
12
+ date: 2019-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: deep_merge