From 716b819de29cac7ba269173bfc204527c019d669 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 11 Aug 2024 16:19:54 +0200 Subject: [PATCH] Use jsonapi-rails to serialize API responses --- Gemfile | 4 +++- Gemfile.lock | 15 +++++++++++++++ app/controllers/guests_controller.rb | 5 +++++ app/serializers/serializable_guest.rb | 9 +++++++++ config/initializers/cors.rb | 8 ++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 app/serializers/serializable_guest.rb create mode 100644 config/initializers/cors.rb diff --git a/Gemfile b/Gemfile index 2f2abee..6bcb96a 100644 --- a/Gemfile +++ b/Gemfile @@ -68,4 +68,6 @@ gem "money" gem 'acts-as-taggable-on' gem "rubytree" -gem 'react-rails' \ No newline at end of file +gem 'react-rails' +gem 'rack-cors' +gem 'jsonapi-rails' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index d23deb5..90dbe8c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -122,6 +122,17 @@ GEM actionview (>= 5.0.0) activesupport (>= 5.0.0) json (2.7.2) + jsonapi-deserializable (0.2.0) + jsonapi-parser (0.1.1) + jsonapi-rails (0.4.1) + jsonapi-parser (~> 0.1.0) + jsonapi-rb (~> 0.5.0) + jsonapi-rb (0.5.0) + jsonapi-deserializable (~> 0.2.0) + jsonapi-serializable (~> 0.3.0) + jsonapi-renderer (0.2.2) + jsonapi-serializable (0.3.1) + jsonapi-renderer (~> 0.2.0) loofah (2.22.0) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -170,6 +181,8 @@ GEM nio4r (~> 2.0) racc (1.8.1) rack (3.1.7) + rack-cors (2.0.2) + rack (>= 2.0.0) rack-session (2.0.0) rack (>= 3.0.0) rack-test (2.1.0) @@ -286,10 +299,12 @@ DEPENDENCIES faker importmap-rails jbuilder + jsonapi-rails money pg (~> 1.1) pry puma (>= 5.0) + rack-cors rails (~> 7.1.3, >= 7.1.3.2) react-rails redis (>= 4.0.1) diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index 72baf50..77298d6 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -9,6 +9,11 @@ class GuestsController < ApplicationController .left_outer_joins(:affinity_groups) .order('tags.name' => :asc) .includes(:affinity_groups, :unbreakable_bonds) + + respond_to do |format| + format.html + format.json { render jsonapi: @guests } + end end # GET /guests/1 or /guests/1.json diff --git a/app/serializers/serializable_guest.rb b/app/serializers/serializable_guest.rb new file mode 100644 index 0000000..c1e4bcc --- /dev/null +++ b/app/serializers/serializable_guest.rb @@ -0,0 +1,9 @@ +class SerializableGuest < JSONAPI::Serializable::Resource + type 'guest' + + attributes :id, :email + + attribute :name do + "#{@object.first_name} #{@object.last_name}" + end +end diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb new file mode 100644 index 0000000..a908920 --- /dev/null +++ b/config/initializers/cors.rb @@ -0,0 +1,8 @@ +# config/initializers/cors.rb + +Rails.application.config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', headers: :any, methods: [:get, :post, :patch, :put, :delete] + end + end \ No newline at end of file