Module: JSONAPI::ActsAsResourceController
- Included in:
- ResourceController, YourModelsController
- Defined in:
- lib/jsonapi/acts_as_resource_controller.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- MEDIA_TYPE_MATCHER =
/.+".+"[^,]*|[^,]+/
- ALL_MEDIA_TYPES =
'*/*'
Instance Attribute Summary collapse
-
#response_document ⇒ Object
readonly
Returns the value of attribute response_document.
Class Method Summary collapse
Instance Method Summary collapse
- #create ⇒ Object
- #create_relationship ⇒ Object
- #destroy ⇒ Object
- #destroy_relationship ⇒ Object
- #get_related_resource ⇒ Object
- #get_related_resources ⇒ Object
- #index ⇒ Object
- #index_related_resources ⇒ Object
- #process_operation(operation) ⇒ Object
- #process_operations(transactional) ⇒ Object
- #process_request ⇒ Object
- #show ⇒ Object
- #show_related_resource ⇒ Object
- #show_relationship ⇒ Object
- #update ⇒ Object
- #update_relationship ⇒ Object
Instance Attribute Details
#response_document ⇒ Object (readonly)
Returns the value of attribute response_document.
16 17 18 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 16 def response_document @response_document end |
Class Method Details
.included(base) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 8 def self.included(base) base.extend ClassMethods base.include Callbacks base.cattr_reader :server_error_callbacks base.define_jsonapi_resources_callbacks :process_operations, :transaction end |
Instance Method Details
#create ⇒ Object
30 31 32 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 30 def create process_request end |
#create_relationship ⇒ Object
34 35 36 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 34 def create_relationship process_request end |
#destroy ⇒ Object
46 47 48 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 46 def destroy process_request end |
#destroy_relationship ⇒ Object
50 51 52 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 50 def destroy_relationship process_request end |
#get_related_resource ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 62 def # :nocov: ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resource`"\ " action. Please use `show_related_resource` instead." # :nocov: end |
#get_related_resources ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 70 def # :nocov: ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resources`"\ " action. Please use `index_related_resources` instead." # :nocov: end |
#index ⇒ Object
18 19 20 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 18 def index process_request end |
#index_related_resources ⇒ Object
58 59 60 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 58 def process_request end |
#process_operation(operation) ⇒ Object
139 140 141 142 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 139 def process_operation(operation) result = operation.process response_document.add_result(result, operation) end |
#process_operations(transactional) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 123 def process_operations(transactional) if transactional run_callbacks :transaction do ActiveRecord::Base.transaction do yield end end else begin yield rescue ActiveRecord::Rollback # Can't rollback without transaction, so just ignore it end end end |
#process_request ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 78 def process_request @response_document = create_response_document unless verify_content_type_header && verify_accept_header render_response_document return end request_parser = JSONAPI::RequestParser.new( params, context: context, key_formatter: key_formatter, server_error_callbacks: (self.class.server_error_callbacks || [])) transactional = request_parser.transactional? begin process_operations(transactional) do run_callbacks :process_operations do request_parser.each(response_document) do |op| op.[:serializer] = resource_serializer_klass.new( op.resource_klass, include_directives: op.[:include_directives], fields: op.[:fields], base_url: base_url, key_formatter: key_formatter, route_formatter: route_formatter, serialization_options: , controller: self ) op.[:cache_serializer_output] = !JSONAPI.configuration.resource_cache.nil? process_operation(op) end end if response_document.has_errors? raise ActiveRecord::Rollback end end rescue => e handle_exceptions(e) end render_response_document end |
#show ⇒ Object
22 23 24 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 22 def show process_request end |
#show_related_resource ⇒ Object
54 55 56 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 54 def process_request end |
#show_relationship ⇒ Object
26 27 28 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 26 def show_relationship process_request end |
#update ⇒ Object
42 43 44 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 42 def update process_request end |
#update_relationship ⇒ Object
38 39 40 |
# File 'lib/jsonapi/acts_as_resource_controller.rb', line 38 def update_relationship process_request end |