Class: JSONAPI::ResourceController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- JSONAPI::ResourceController
- Includes:
- ResourceFor
- Defined in:
- lib/jsonapi/resource_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #create_association ⇒ Object
-
#create_operations_processor ⇒ Object
Override this to use another operations processor.
- #destroy ⇒ Object
- #destroy_association ⇒ Object
- #get_related_resource ⇒ Object
- #get_related_resources ⇒ Object
- #index ⇒ Object
- #show ⇒ Object
- #show_association ⇒ Object
- #update ⇒ Object
- #update_association ⇒ Object
Methods included from ResourceFor
Instance Method Details
#create ⇒ Object
81 82 83 |
# File 'lib/jsonapi/resource_controller.rb', line 81 def create process_request_operations end |
#create_association ⇒ Object
85 86 87 |
# File 'lib/jsonapi/resource_controller.rb', line 85 def create_association process_request_operations end |
#create_operations_processor ⇒ Object
Override this to use another operations processor
141 142 143 |
# File 'lib/jsonapi/resource_controller.rb', line 141 def create_operations_processor JSONAPI::ActiveRecordOperationsProcessor.new end |
#destroy ⇒ Object
97 98 99 |
# File 'lib/jsonapi/resource_controller.rb', line 97 def destroy process_request_operations end |
#destroy_association ⇒ Object
101 102 103 |
# File 'lib/jsonapi/resource_controller.rb', line 101 def destroy_association process_request_operations end |
#get_related_resource ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/jsonapi/resource_controller.rb', line 105 def association_type = params[:association] source_resource = @request.source_klass.find_by_key(@request.source_id, context: context) serializer = JSONAPI::ResourceSerializer.new(@request.source_klass, include: @request.include, fields: @request.fields, base_url: base_url, key_formatter: key_formatter, route_formatter: route_formatter) render json: serializer.serialize_to_hash(source_resource.send(association_type)) end |
#get_related_resources ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/jsonapi/resource_controller.rb', line 119 def association_type = params[:association] source_resource = @request.source_klass.find_by_key(@request.source_id, context: context) = source_resource.send(association_type, { filters: @request.source_klass.verify_filters(@request.filters, context), sort_criteria: @request.sort_criteria, paginator: @request.paginator }) serializer = JSONAPI::ResourceSerializer.new(@request.source_klass, include: @request.include, fields: @request.fields, base_url: base_url, key_formatter: key_formatter, route_formatter: route_formatter) render json: serializer.serialize_to_hash() end |
#index ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jsonapi/resource_controller.rb', line 20 def index serializer = JSONAPI::ResourceSerializer.new(resource_klass, include: @request.include, fields: @request.fields, base_url: base_url, key_formatter: key_formatter, route_formatter: route_formatter) resource_records = resource_klass.find(resource_klass.verify_filters(@request.filters, context), context: context, sort_criteria: @request.sort_criteria, paginator: @request.paginator) render json: serializer.serialize_to_hash(resource_records) rescue => e handle_exceptions(e) end |
#show ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jsonapi/resource_controller.rb', line 38 def show serializer = JSONAPI::ResourceSerializer.new(resource_klass, include: @request.include, fields: @request.fields, base_url: base_url, key_formatter: key_formatter, route_formatter: route_formatter) keys = parse_key_array(params[resource_klass._primary_key]) resource_records = if keys.length > 1 resource_klass.find_by_keys(keys, context: context) else resource_klass.find_by_key(keys[0], context: context) end render json: serializer.serialize_to_hash(resource_records) rescue => e handle_exceptions(e) end |
#show_association ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jsonapi/resource_controller.rb', line 59 def show_association association_type = params[:association] parent_key = params[resource_klass._as_parent_key] parent_resource = resource_klass.find_by_key(parent_key, context: context) association = resource_klass._association(association_type) serializer = JSONAPI::ResourceSerializer.new(resource_klass, fields: @request.fields, base_url: base_url, key_formatter: key_formatter, route_formatter: route_formatter) render json: serializer.serialize_to_links_hash(parent_resource, association) rescue => e # :nocov: handle_exceptions(e) # :nocov: end |
#update ⇒ Object
93 94 95 |
# File 'lib/jsonapi/resource_controller.rb', line 93 def update process_request_operations end |
#update_association ⇒ Object
89 90 91 |
# File 'lib/jsonapi/resource_controller.rb', line 89 def update_association process_request_operations end |