-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
188 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'rails/all' | ||
require 'rspec/rails' | ||
|
||
require 'oj_serializers/sugar' | ||
require 'support/controllers/albums_controller' | ||
|
||
RSpec.describe AlbumsController, type: :controller do | ||
it 'should work as expected' do | ||
get :index | ||
get :show, params: { id: 'example' } | ||
binding.pry | ||
before(:all) do | ||
Rails.application.quick_setup | ||
end | ||
|
||
it 'should be able to use serializer options and legacy serializers' do | ||
get :list | ||
albums = parse_json[:albums] | ||
expect(albums.size).to eq 3 | ||
|
||
get :show | ||
album = parse_json | ||
songs = album[:songs] | ||
expect(album).to eq albums.first | ||
expect(album).to include( | ||
name: 'Abraxas', | ||
release: 'September 23, 1970', | ||
genres: ['Pyschodelic Rock', 'Blues Rock', 'Jazz Fusion', 'Latin Rock'], | ||
) | ||
expect(songs.size).to eq 9 | ||
expect(songs.second).to eq( | ||
track: 2, | ||
name: 'Black Magic Woman / Gypsy Queen', | ||
composers: ['Peter Green', 'Gábor Szabó'], | ||
) | ||
|
||
get :legacy_list | ||
legacy_albums = parse_json[:albums] | ||
expect(legacy_albums).to eq albums | ||
|
||
get :legacy_show | ||
legacy_album = parse_json | ||
expect(legacy_album).to eq album | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'oj_serializers/sugar' | ||
require 'actionpack' | ||
require_relative 'application' | ||
|
||
class AlbumsController < ActionController::Base | ||
require 'support/models/album' | ||
require 'support/serializers/album_serializer' | ||
require 'support/serializers/legacy_serializers' | ||
|
||
class AlbumsController < ApplicationController | ||
def show | ||
album = Album.abraxas | ||
render json: album, serializer: AlbumSerializer | ||
end | ||
|
||
def index | ||
albums = [Album.abraxas] | ||
def list | ||
albums = [Album.abraxas] * 3 | ||
render json: albums, each_serializer: AlbumSerializer, root: :albums | ||
end | ||
|
||
def legacy_show | ||
album = Album.abraxas | ||
render json: album, serializer: LegacyAlbumSerializer | ||
end | ||
|
||
def legacy_list | ||
albums = [Album.abraxas] * 3 | ||
render json: { albums: albums.map { |album| LegacyAlbumSerializer.new(album) } } | ||
end | ||
end | ||
|
||
Rails.application.routes.draw do | ||
resource :albums, only: [] do | ||
get :show, on: :collection | ||
get :list, on: :collection | ||
get :legacy_show, on: :collection | ||
get :legacy_list, on: :collection | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails' | ||
require 'action_pack' | ||
require 'action_controller/railtie' | ||
require 'support/models/sql' | ||
|
||
class MusicApplication < Rails::Application | ||
def quick_setup | ||
initializers.find { |i| i.name == 'active_model_serializers.action_controller' }.run | ||
initializers.find { |i| i.name == 'oj_serializers.action_controller' }.run | ||
end | ||
end | ||
|
||
class ApplicationController < ActionController::Base | ||
end | ||
|
||
require 'oj_serializers/sugar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_model_serializers' | ||
|
||
# https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md#1-disable-root-globally-for-all-or-per-class | ||
ActiveSupport.on_load(:active_model_serializers) do | ||
# Disable for all serializers (except ArraySerializer) | ||
ActiveModel::Serializer.root = false | ||
|
||
# Disable for ArraySerializer | ||
ActiveModel::ArraySerializer.root = false | ||
end | ||
|
||
class LegacySongSerializer < ActiveModel::Serializer | ||
attributes( | ||
:track, | ||
:name, | ||
:composers, | ||
) | ||
|
||
def composers | ||
object.composer&.split(', ') | ||
end | ||
end | ||
|
||
class LegacyAlbumSerializer < ActiveModel::Serializer | ||
attributes( | ||
:name, | ||
:genres, | ||
:release, | ||
) | ||
|
||
has_many :songs, serializer: LegacySongSerializer | ||
|
||
def release | ||
object.release_date.strftime('%B %d, %Y') | ||
end | ||
|
||
def include_release? | ||
object.released? | ||
end | ||
end |