Use jsonapi-rails to serialize API responses #35
2
Gemfile
2
Gemfile
@ -69,3 +69,5 @@ gem 'acts-as-taggable-on'
|
|||||||
|
|
||||||
gem "rubytree"
|
gem "rubytree"
|
||||||
gem 'react-rails'
|
gem 'react-rails'
|
||||||
|
gem 'rack-cors'
|
||||||
|
gem 'jsonapi-rails'
|
15
Gemfile.lock
15
Gemfile.lock
@ -122,6 +122,17 @@ GEM
|
|||||||
actionview (>= 5.0.0)
|
actionview (>= 5.0.0)
|
||||||
activesupport (>= 5.0.0)
|
activesupport (>= 5.0.0)
|
||||||
json (2.7.2)
|
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)
|
loofah (2.22.0)
|
||||||
crass (~> 1.0.2)
|
crass (~> 1.0.2)
|
||||||
nokogiri (>= 1.12.0)
|
nokogiri (>= 1.12.0)
|
||||||
@ -170,6 +181,8 @@ GEM
|
|||||||
nio4r (~> 2.0)
|
nio4r (~> 2.0)
|
||||||
racc (1.8.1)
|
racc (1.8.1)
|
||||||
rack (3.1.7)
|
rack (3.1.7)
|
||||||
|
rack-cors (2.0.2)
|
||||||
|
rack (>= 2.0.0)
|
||||||
rack-session (2.0.0)
|
rack-session (2.0.0)
|
||||||
rack (>= 3.0.0)
|
rack (>= 3.0.0)
|
||||||
rack-test (2.1.0)
|
rack-test (2.1.0)
|
||||||
@ -286,10 +299,12 @@ DEPENDENCIES
|
|||||||
faker
|
faker
|
||||||
importmap-rails
|
importmap-rails
|
||||||
jbuilder
|
jbuilder
|
||||||
|
jsonapi-rails
|
||||||
money
|
money
|
||||||
pg (~> 1.1)
|
pg (~> 1.1)
|
||||||
pry
|
pry
|
||||||
puma (>= 5.0)
|
puma (>= 5.0)
|
||||||
|
rack-cors
|
||||||
rails (~> 7.1.3, >= 7.1.3.2)
|
rails (~> 7.1.3, >= 7.1.3.2)
|
||||||
react-rails
|
react-rails
|
||||||
redis (>= 4.0.1)
|
redis (>= 4.0.1)
|
||||||
|
@ -9,6 +9,11 @@ class GuestsController < ApplicationController
|
|||||||
.left_outer_joins(:affinity_groups)
|
.left_outer_joins(:affinity_groups)
|
||||||
.order('tags.name' => :asc)
|
.order('tags.name' => :asc)
|
||||||
.includes(:affinity_groups, :unbreakable_bonds)
|
.includes(:affinity_groups, :unbreakable_bonds)
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html
|
||||||
|
format.json { render jsonapi: @guests }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET /guests/1 or /guests/1.json
|
# GET /guests/1 or /guests/1.json
|
||||||
|
9
app/serializers/serializable_guest.rb
Normal file
9
app/serializers/serializable_guest.rb
Normal file
@ -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
|
8
config/initializers/cors.rb
Normal file
8
config/initializers/cors.rb
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user