Skip to content

Commit

Permalink
Remove AMS and replace with simple json rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp committed Nov 11, 2024
1 parent 33312f9 commit 0a6e45b
Show file tree
Hide file tree
Showing 41 changed files with 260 additions and 601 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ gem 'rails', '~> 7.2'
gem 'pg', '>= 1.2.3', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 6.4'
# Use ActiveModelSerializer for serializing to JSON
gem 'active_model_serializers', '~> 0.10'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1'

Expand Down
9 changes: 0 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ GEM
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
active_model_serializers (0.10.14)
actionpack (>= 4.1)
activemodel (>= 4.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (7.2.2)
activesupport (= 7.2.2)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -88,8 +83,6 @@ GEM
brakeman (6.2.2)
racc
builder (3.3.0)
case_transform (0.2)
activesupport
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crass (1.0.6)
Expand Down Expand Up @@ -135,7 +128,6 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.5)
jsonapi-renderer (0.2.2)
language_server-protocol (3.17.0.3)
logger (1.6.1)
loofah (2.23.1)
Expand Down Expand Up @@ -290,7 +282,6 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers (~> 0.10)
annotaterb (~> 4.13)
bcrypt (~> 3.1)
bootsnap (~> 1.18.4)
Expand Down
29 changes: 25 additions & 4 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class AlbumsController < ApplicationController
include ImageRendering

before_action :set_album, only: %i[show update destroy merge]

has_scope :by_filter, as: 'filter'
Expand All @@ -12,27 +14,28 @@ def index
.includes(:album_artists, :album_labels, image: [{ image_attachment: :blob }, :image_type])
.paginate(page: params[:page], per_page: params[:per_page])
add_pagination_headers(@albums)
render json: @albums

render json: @albums.map { |it| transform_album_for_json(it) }
end

def show
render json: @album
render json: transform_album_for_json(@album)
end

def create
authorize Album
@album = Album.new(transformed_attributes)

if @album.save
render json: @album, status: :created
render json: transform_album_for_json(@album), status: :created
else
render json: @album.errors, status: :unprocessable_entity
end
end

def update
if @album.update(transformed_attributes)
render json: @album, status: :ok
render json: transform_album_for_json(@album), status: :ok
else
render json: @album.errors, status: :unprocessable_entity
end
Expand Down Expand Up @@ -90,4 +93,22 @@ def transformed_attributes

attributes
end

def transform_album_for_json(album)
result = %i[id title normalized_title release review_comment edition edition_description created_at updated_at].index_with { |it| album.send(it) }
%i[image image100 image250 image500 image_type].each do |attr|
result[attr] = send(attr, album)
end
result[:album_artists] = album.album_artists.map { |it| transform_album_artist_for_json(it) }
result[:album_labels] = album.album_labels.map { |it| transform_album_label_for_json(it) }
result
end

def transform_album_artist_for_json(album_artist)
%i[artist_id name normalized_name order separator].index_with { |attr| album_artist.send(attr) }
end

def transform_album_label_for_json(album_label)
%i[label_id catalogue_number].index_with { |attr| album_label.send(attr) }
end
end
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class ApplicationController < ActionController::API

attr_accessor :current_user

helper_method :current_user

before_action :authenticate_from_token
after_action :verify_authorized
# rubocop:disable Rails/LexicallyScopedActionFilter
Expand All @@ -16,8 +18,6 @@ class ApplicationController < ActionController::API

has_scope :sorted, default: nil, allow_blank: true, except: :stats

serialization_scope :url_options

protected

def add_pagination_headers(collection)
Expand Down
19 changes: 15 additions & 4 deletions app/controllers/artists_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ArtistsController < ApplicationController
include ImageRendering

before_action :set_artist, only: %i[show update destroy merge]

has_scope :by_filter, as: 'filter'
Expand All @@ -9,27 +11,28 @@ def index
.includes(image: [{ image_attachment: :blob }, :image_type])
.paginate(page: params[:page], per_page: params[:per_page])
add_pagination_headers(@artists)
render json: @artists

render json: @artists.map { |it| transform_artist_for_json(it) }
end

def show
render json: @artist
render json: transform_artist_for_json(@artist)
end

def create
authorize Artist
@artist = Artist.new(transformed_attributes)

if @artist.save
render json: @artist, status: :created
render json: transform_artist_for_json(@artist), status: :created
else
render json: @artist.errors, status: :unprocessable_entity
end
end

def update
if @artist.update(transformed_attributes)
render json: @artist, status: :ok
render json: transform_artist_for_json(@artist), status: :ok
else
render json: @artist.errors, status: :unprocessable_entity
end
Expand Down Expand Up @@ -76,4 +79,12 @@ def transformed_attributes
end
attributes
end

def transform_artist_for_json(artist)
result = %i[id name normalized_name review_comment created_at updated_at].index_with { |it| artist.send(it) }
%i[image image100 image250 image500 image_type].each do |attr|
result[attr] = send(attr, artist)
end
result
end
end
17 changes: 14 additions & 3 deletions app/controllers/auth_tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ def index
.order(id: :asc)
.paginate(page: params[:page], per_page: params[:per_page])
add_pagination_headers(@auth_tokens)
render json: @auth_tokens

render json: @auth_tokens.map { |it| transform_auth_token_for_json(it) }
end

def show
render json: @auth_token
render json: transform_auth_token_for_json(@auth_token)
end

def create
Expand All @@ -31,7 +32,7 @@ def create
)

if @auth_token.save
render json: @auth_token, serializer: AuthTokenWithSecretSerializer, status: :created
render json: transform_auth_token_for_json_with_secret(@auth_token), status: :created
else
render json: @auth_token.errors, status: :unprocessable_entity
end
Expand All @@ -47,4 +48,14 @@ def set_auth_token
@auth_token = AuthToken.find(params[:id])
authorize @auth_token
end

def transform_auth_token_for_json(auth_token)
%i[id device_id user_id user_agent application].index_with { |it| auth_token.send(it) }
end

def transform_auth_token_for_json_with_secret(auth_token)
result = transform_auth_token_for_json(auth_token)
result[:secret] = auth_token.secret
result
end
end
13 changes: 9 additions & 4 deletions app/controllers/codec_conversions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@ def index
.order(id: :asc)
.paginate(page: params[:page], per_page: params[:per_page])
add_pagination_headers(@codec_conversions)
render json: @codec_conversions

render json: @codec_conversions.map { |it| transform_codec_conversion_for_json(it) }
end

def show
render json: @codec_conversion
render json: transform_codec_conversion_for_json(@codec_conversion)
end

def create
authorize CodecConversion
@codec_conversion = CodecConversion.new(permitted_attributes(CodecConversion))

if @codec_conversion.save
render json: @codec_conversion, status: :created
render json: transform_codec_conversion_for_json(@codec_conversion), status: :created
else
render json: @codec_conversion.errors, status: :unprocessable_entity
end
end

def update
if @codec_conversion.update(permitted_attributes(CodecConversion))
render json: @codec_conversion, status: :ok
render json: transform_codec_conversion_for_json(@codec_conversion), status: :ok
else
render json: @codec_conversion.errors, status: :unprocessable_entity
end
Expand All @@ -45,4 +46,8 @@ def set_codec_conversion
@codec_conversion = CodecConversion.find(params[:id])
authorize @codec_conversion
end

def transform_codec_conversion_for_json(codec_conversion)
%i[id name ffmpeg_params resulting_codec_id].index_with { |it| codec_conversion.send(it) }
end
end
13 changes: 9 additions & 4 deletions app/controllers/codecs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ def index
.order(id: :asc)
.paginate(page: params[:page], per_page: params[:per_page])
add_pagination_headers(@codecs)
render json: @codecs

render json: @codecs.map { |it| transform_codec_for_json(it) }
end

def show
render json: @codec
render json: transform_codec_for_json(@codec)
end

def create
authorize Codec
@codec = Codec.new(permitted_attributes(Codec))

if @codec.save
render json: @codec, status: :created
render json: transform_codec_for_json(@codec), status: :created
else
render json: @codec.errors, status: :unprocessable_entity
end
end

def update
if @codec.update(permitted_attributes(@codec))
render json: @codec, status: :ok
render json: transform_codec_for_json(@codec), status: :ok
else
render json: @codec.errors, status: :unprocessable_entity
end
Expand All @@ -43,4 +44,8 @@ def set_codec
@codec = Codec.find(params[:id])
authorize @codec
end

def transform_codec_for_json(codec)
%i[id mimetype extension].index_with { |it| codec.send(it) }
end
end
40 changes: 40 additions & 0 deletions app/controllers/concerns/image_rendering.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module ImageRendering
extend ActiveSupport::Concern

included do
private def image(object)
return nil if object.image.blank?
return nil unless object.image.image.variable?

rails_blob_url(object.image.image)
end

private def image100(object)
return nil if object.image.blank?
return nil unless object.image.image.variable?

rails_representation_url(object.image.image.variant(resize_to_cover: [100, 100]))
end

private def image250(object)
return nil if object.image.blank?
return nil unless object.image.image.variable?

rails_representation_url(object.image.image.variant(resize_to_cover: [250, 250]))
end

private def image500(object)
return nil if object.image.blank?
return nil unless object.image.image.variable?

rails_representation_url(object.image.image.variant(resize_to_cover: [500, 500]))
end

private def image_type(object)

Check failure on line 33 in app/controllers/concerns/image_rendering.rb

View workflow job for this annotation

GitHub Actions / lint

Style/AccessModifierDeclarations: `private` should not be inlined in method definitions.
return nil if object.image.blank?
return nil unless object.image.image.variable?

object.image.image_type.mimetype
end
end
end
11 changes: 8 additions & 3 deletions app/controllers/cover_filenames_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ def index
.order(id: :asc)
.paginate(page: params[:page], per_page: params[:per_page])
add_pagination_headers(@cover_filenames)
render json: @cover_filenames

render json: @cover_filenames.map { |it| transform_cover_filename_for_json(it) }
end

def show
render json: @cover_filename
render json: transform_cover_filename_for_json(@cover_filename)
end

def create
authorize CoverFilename
@cover_filename = CoverFilename.new(permitted_attributes(CoverFilename))

if @cover_filename.save
render json: @cover_filename, status: :created
render json: transform_cover_filename_for_json(@cover_filename), status: :created
else
render json: @cover_filename.errors, status: :unprocessable_entity
end
Expand All @@ -35,4 +36,8 @@ def set_cover_filename
@cover_filename = CoverFilename.find(params[:id])
authorize @cover_filename
end

def transform_cover_filename_for_json(cover_filename)
%i[id filename].index_with { |it| cover_filename.send(it) }
end
end
Loading

0 comments on commit 0a6e45b

Please sign in to comment.