### Install JSONAPI.rb Gem Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Instructions for adding the JSONAPI.rb gem to your application's Gemfile and installing it using Bundler, or installing it directly via RubyGems. ```ruby gem 'jsonapi.rb' ``` -------------------------------- ### Install JSONAPI.rb via Shell Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Command to install the JSONAPI.rb gem directly using the gem command-line tool. ```shell $ gem install jsonapi.rb ``` -------------------------------- ### JSONAPI.rb Includes and Sparse Fields Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Illustrates how to use JSONAPI::Fetching to include related resources and serialize only specific fields. It shows how to whitelist includes and provides an example of a curl command for fetching sparse fields. ```ruby class MyController < ActionController::Base include JSONAPI::Fetching def index render jsonapi: Model.all end private # Overwrite/whitelist the includes def jsonapi_include super & ['wanted_attribute'] end end ``` ```bash $ curl -X GET /api/resources?fields[model]=model_attr,relationship ``` -------------------------------- ### JSONAPI.rb Pagination Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Shows how to implement pagination for model record sets using JSONAPI::Pagination. Includes examples for rendering paginated results, adding pagination info to meta, and customizing the page size. ```ruby class MyController < ActionController::Base include JSONAPI::Pagination def index jsonapi_paginate(Model.all) do |paginated| render jsonapi: paginated end end end ``` ```ruby def jsonapi_meta(resources) pagination = jsonapi_pagination_meta(resources) { pagination: pagination } if pagination.present? end ``` ```ruby def jsonapi_page_size(pagination_params) per_page = pagination_params[:size].to_f.to_i per_page = 30 if per_page > 30 || per_page < 1 per_page end ``` -------------------------------- ### Collection Meta Information in Rails Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Example of providing meta information for a JSON:API collection response in a Rails controller by implementing the `jsonapi_meta` method, often used for pagination details like total counts. ```ruby class MyController < ActionController::Base def index render jsonapi: Model.all end private def jsonapi_meta(resources) { total: resources.count } if resources.respond_to?(:count) end end ``` -------------------------------- ### Custom Serializer Class Naming in Rails Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Example of how to implement `jsonapi_serializer_class` in a Rails controller to customize the naming convention for JSON:API serializers when the default naming scheme does not apply. ```ruby class CustomNamingController < ActionController::Base # ... private def jsonapi_serializer_class(resource, is_collection) JSONAPI::Rails.serializer_class(resource, is_collection) rescue NameError # your serializer class naming implementation end end ``` -------------------------------- ### Enable JSONAPI.rb for Rails Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Code snippet to include in a Rails initializer to enable JSONAPI.rb support. This registers the necessary mime types and renderers (`jsonapi` and `jsonapi_errors`) for Rails integration. ```ruby # config/initializers/jsonapi.rb require 'jsonapi' JSONAPI::Rails.install! ``` -------------------------------- ### JSONAPI.rb Error Handling Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Demonstrates how to use JSONAPI::Errors for handling exceptions, strong parameters errors, generic errors, and record not found scenarios. It shows how to render validation errors and override the internal server error method for exception notification. ```ruby class MyController < ActionController::Base include JSONAPI::Errors def update record = Model.find(params[:id]) if record.update(params.require(:data).require(:attributes).permit!) render jsonapi: record else render jsonapi_errors: record.errors, status: :unprocessable_entity end end private def render_jsonapi_internal_server_error(exception) # Call your exception notifier here. Example: # Raven.capture_exception(exception) super(exception) end end ``` -------------------------------- ### JSONAPI.rb Filtering and Sorting with Ransack Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Explains how to use JSONAPI::Filtering with Ransack for advanced filtering and sorting of record collections, including support for relationships and composite matchers. Requires adding `ransack` to the Gemfile. ```ruby class MyController < ActionController::Base include JSONAPI::Filtering def index allowed = [:model_attr, :relationship_attr] jsonapi_filter(Model.all, allowed) do |filtered| render jsonapi: filtered.result end end end ``` ```bash $ curl -X GET \ /api/resources?filter[model_attr_or_relationship_attr_cont_any]=value,name\ &sort=-model_attr,relationship_attr ``` -------------------------------- ### JSONAPI.rb Sorting with Expressions Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Details how to enable sorting using SQL aggregations like min, max, avg, sum, and count within JSONAPI.rb filtering. This requires enabling the `sort_with_expressions` option and potentially grouping results. ```ruby options = { sort_with_expressions: true } jsonapi_filter(User.all, allowed_fields, options) do |filtered| render jsonapi: filtered.result.group('id').to_a end ``` ```bash $ curl -X GET /api/resources?sort=-model_attr_sum ``` -------------------------------- ### Custom Serializer Parameters in Rails Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Demonstrates implementing `jsonapi_serializer_params` in a Rails controller to pass custom parameters to the JSON:API serializer, such as controlling field formatting based on request parameters. ```ruby class CustomSerializerParamsController < ActionController::Base # ... private def jsonapi_serializer_params { first_name_upcase: params[:upcase].present? } end end ``` -------------------------------- ### Deserialize JSONAPI Payload in Rails Controller Source: https://github.com/stas/jsonapi.rb/blob/master/README.md Demonstrates how to use the `jsonapi_deserialize` helper within a Rails controller action to update an ActiveRecord model with data from a JSONAPI payload. It shows how to handle successful updates and validation errors. ```ruby class MyController < ActionController::Base include JSONAPI::Deserialization def update model = MyModel.find(params[:id]) if model.update(jsonapi_deserialize(params, only: [:attr1, :rel_one])) render jsonapi: model else render jsonapi_errors: model.errors, status: :unprocessable_entity end end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.