4
4
require 'active_model'
5
5
6
6
require 'jsonapi/rails/configuration'
7
+ require 'jsonapi/rails/controller'
7
8
require 'jsonapi/rails/parser'
8
9
require 'jsonapi/rails/renderer'
9
10
@@ -16,51 +17,38 @@ class Railtie < ::Rails::Railtie
16
17
jsonapi_error : ErrorsRenderer . new
17
18
} . freeze
18
19
19
- initializer 'jsonapi-rails.action_controller' do
20
- ActiveSupport . on_load ( :action_controller ) do
21
- require 'jsonapi/rails/action_controller'
22
- include ::JSONAPI ::Rails ::ActionController
23
-
24
- if JSONAPI ::Rails . config . register_mime_type
25
- Mime ::Type . register MEDIA_TYPE , :jsonapi
26
- end
20
+ initializer 'jsonapi.init' , after : :load_config_initializers do
21
+ if JSONAPI ::Rails . config . register_mime_type
22
+ Mime ::Type . register MEDIA_TYPE , :jsonapi
23
+ end
27
24
28
- if JSONAPI ::Rails . config . register_parameter_parser
29
- if ::Rails ::VERSION ::MAJOR >= 5
30
- ::ActionDispatch ::Request . parameter_parsers [ :jsonapi ] = PARSER
31
- else
32
- ::ActionDispatch ::ParamsParser ::DEFAULT_PARSERS [ Mime [ :jsonapi ] ] = PARSER
33
- end
25
+ if JSONAPI ::Rails . config . register_parameter_parser
26
+ if ::Rails ::VERSION ::MAJOR >= 5
27
+ ::ActionDispatch ::Request . parameter_parsers [ :jsonapi ] = PARSER
28
+ else
29
+ ::ActionDispatch ::ParamsParser ::DEFAULT_PARSERS [ Mime [ :jsonapi ] ] = PARSER
34
30
end
31
+ end
35
32
36
- if JSONAPI ::Rails . config . register_renderers
37
- ::ActionController ::Renderers . add ( :jsonapi ) do |resources , options |
38
- self . content_type ||= Mime [ :jsonapi ]
39
-
40
- RENDERERS [ :jsonapi ] . render ( resources , options ) . to_json
41
- end
42
-
43
- ::ActionController ::Renderers . add ( :jsonapi_error ) do |errors , options |
44
- # Renderer proc is evaluated in the controller context, so it
45
- # has access to the jsonapi_pointers method.
46
- options = options . merge ( _jsonapi_pointers : jsonapi_pointers )
47
- self . content_type ||= Mime [ :jsonapi ]
48
-
49
- RENDERERS [ :jsonapi_error ] . render ( errors , options ) . to_json
50
- end
33
+ if JSONAPI ::Rails . config . extend_action_controller
34
+ ActiveSupport . on_load ( :action_controller ) do
35
+ include ::JSONAPI ::Rails ::Controller
51
36
end
37
+ end
52
38
53
- JSONAPI ::Deserializable ::Resource . configure do |config |
54
- config . default_has_one do |key , _rel , id , type |
55
- key = key . to_s . singularize
56
- type = type . to_s . singularize . camelize
57
- { "#{ key } _id" . to_sym => id , "#{ key } _type" . to_sym => type }
58
- end
59
-
60
- config . default_has_many do |key , _rel , ids , types |
61
- key = key . to_s . singularize
62
- types = types . map { |t | t . to_s . singularize . camelize }
63
- { "#{ key } _ids" . to_sym => ids , "#{ key } _types" . to_sym => types }
39
+ if JSONAPI ::Rails . config . register_renderers
40
+ ActiveSupport . on_load ( :action_controller ) do
41
+ RENDERERS . each do |name , renderer |
42
+ ::ActionController ::Renderers . add ( name ) do |resources , options |
43
+ # Renderer proc is evaluated in the controller context.
44
+ self . content_type ||= Mime [ :jsonapi ]
45
+
46
+ ActiveSupport ::Notifications . instrument ( 'render.jsonapi' ,
47
+ resources : resources ,
48
+ options : options ) do
49
+ renderer . render ( resources , options , self ) . to_json
50
+ end
51
+ end
64
52
end
65
53
end
66
54
end
0 commit comments