Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ def create
if @album.save
render json: transform_album_for_json(@album), status: :created
else
render json: @album.errors, status: :unprocessable_content
render json: transform_errors_for_json(@album), status: :unprocessable_content
end
end

def update
if @album.update(transformed_attributes)
render json: transform_album_for_json(@album), status: :ok
else
render json: @album.errors, status: :unprocessable_content
render json: transform_errors_for_json(@album), status: :unprocessable_content
end
end

def destroy
render json: @album.errors, status: :unprocessable_content unless @album.destroy
render json: transform_errors_for_json(@album), status: :unprocessable_content unless @album.destroy
end

def destroy_empty
Expand All @@ -51,7 +51,7 @@ def destroy_empty
end

def merge
render json: @album.errors, status: :unprocessable_content unless @album.merge(Album.find(params.expect(:source_id)))
render json: transform_errors_for_json(@album), status: :unprocessable_content unless @album.merge(Album.find(params.expect(:source_id)))
end

private
Expand Down
21 changes: 14 additions & 7 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class ApplicationController < ActionController::API
include Pundit::Authorization
include ActionController::HttpAuthentication::Token::ControllerMethods

# This map only includes the type of validation errors that we could have inside the app
ERROR_TYPE_MAP = { blank: :required, taken: :not_unique }.freeze

etag { params[:page] }
etag { params[:per_page] }

Expand Down Expand Up @@ -42,6 +45,15 @@ def stale?(scope:, **)
super(etag:, **)
end

def transform_error_for_json(error)
{ attribute: error.attribute, type: ERROR_TYPE_MAP[error.type] }
end

# This method expects an instance of a class that includes `ActiveModel::Errors`
def transform_errors_for_json(object)
{ errors: object.errors.errors.map { transform_error_for_json(it) } }
end

private

def authenticate_user
Expand All @@ -52,16 +64,11 @@ def authenticate_user
end

def user_not_authorized(exc)
policy_name = exc.policy.class.to_s.underscore

status = current_user.present? ? :forbidden : :unauthorized
render json: { status => [I18n.t("#{policy_name}.#{exc.query}",
scope: 'pundit',
default: :default)] },
status:
render json: { errors: [{ policy: exc.policy.class.to_s.underscore, type: status, action: exc.query }] }, status:
end

def model_not_found(exc)
render json: { not_found: ["#{exc.model.pluralize.downcase}.not-found"] }, status: :not_found
render json: { errors: [{ model: exc.model.downcase, type: :not_found }] }, status: :not_found
end
end
8 changes: 4 additions & 4 deletions app/controllers/artists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ def create
if @artist.save
render json: transform_artist_for_json(@artist), status: :created
else
render json: @artist.errors, status: :unprocessable_content
render json: transform_errors_for_json(@artist), status: :unprocessable_content
end
end

def update
if @artist.update(transformed_attributes)
render json: transform_artist_for_json(@artist), status: :ok
else
render json: @artist.errors, status: :unprocessable_content
render json: transform_errors_for_json(@artist), status: :unprocessable_content
end
end

def destroy
render json: @artist.errors, status: :unprocessable_content unless @artist.destroy
render json: transform_errors_for_json(@artist), status: :unprocessable_content unless @artist.destroy
end

def destroy_empty
Expand All @@ -51,7 +51,7 @@ def destroy_empty
end

def merge
render json: @artist.errors, status: :unprocessable_content unless @artist.merge(Artist.find(params.expect(:source_id)))
render json: transform_errors_for_json(@artist), status: :unprocessable_content unless @artist.merge(Artist.find(params.expect(:source_id)))
end

private
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/auth_tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def create

user = User.find_by(name: params[:name])
unless user.try(:authenticate, params[:password])
render json: { unauthorized: [I18n.t('auth_tokens.create.wrong_credentials')] },
status: :unauthorized
render json: { errors: [{ attribute: :base, type: :wrong_credentials }] }, status: :unauthorized
return
end

Expand All @@ -33,12 +32,12 @@ def create
if @auth_token.save
render json: transform_auth_token_for_json_with_token(@auth_token), status: :created
else
render json: @auth_token.errors, status: :unprocessable_content
render json: transform_errors_for_json(@auth_token), status: :unprocessable_content
end
end

def destroy
render json: @auth_token.errors, status: :unprocessable_content unless @auth_token.destroy
render json: transform_errors_for_json(@auth_token), status: :unprocessable_content unless @auth_token.destroy
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/codec_conversions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ def create
if @codec_conversion.save
render json: transform_codec_conversion_for_json(@codec_conversion), status: :created
else
render json: @codec_conversion.errors, status: :unprocessable_content
render json: transform_errors_for_json(@codec_conversion), status: :unprocessable_content
end
end

def update
if @codec_conversion.update(permitted_attributes(CodecConversion))
render json: transform_codec_conversion_for_json(@codec_conversion), status: :ok
else
render json: @codec_conversion.errors, status: :unprocessable_content
render json: transform_errors_for_json(@codec_conversion), status: :unprocessable_content
end
end

def destroy
render json: @codec_conversion.errors, status: :unprocessable_content unless @codec_conversion.destroy
render json: transform_errors_for_json(@codec_conversion), status: :unprocessable_content unless @codec_conversion.destroy
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/codecs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ def create
if @codec.save
render json: transform_codec_for_json(@codec), status: :created
else
render json: @codec.errors, status: :unprocessable_content
render json: transform_errors_for_json(@codec), status: :unprocessable_content
end
end

def update
if @codec.update(permitted_attributes(@codec))
render json: transform_codec_for_json(@codec), status: :ok
else
render json: @codec.errors, status: :unprocessable_content
render json: transform_errors_for_json(@codec), status: :unprocessable_content
end
end

def destroy
render json: @codec.errors, status: :unprocessable_content unless @codec.destroy
render json: transform_errors_for_json(@codec), status: :unprocessable_content unless @codec.destroy
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/cover_filenames_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def create
if @cover_filename.save
render json: transform_cover_filename_for_json(@cover_filename), status: :created
else
render json: @cover_filename.errors, status: :unprocessable_content
render json: transform_errors_for_json(@cover_filename), status: :unprocessable_content
end
end

def destroy
render json: @cover_filename.errors, status: :unprocessable_content unless @cover_filename.destroy
render json: transform_errors_for_json(@cover_filename), status: :unprocessable_content unless @cover_filename.destroy
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/genres_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ def create
if @genre.save
render json: transform_genre_for_json(@genre), status: :created
else
render json: @genre.errors, status: :unprocessable_content
render json: transform_errors_for_json(@genre), status: :unprocessable_content
end
end

def update
if @genre.update(permitted_attributes(@genre))
render json: transform_genre_for_json(@genre), status: :ok
else
render json: @genre.errors, status: :unprocessable_content
render json: transform_errors_for_json(@genre), status: :unprocessable_content
end
end

def destroy
render json: @genre.errors, status: :unprocessable_content unless @genre.destroy
render json: transform_errors_for_json(@genre), status: :unprocessable_content unless @genre.destroy
end

def destroy_empty
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/image_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ def create
if @image_type.save
render json: transform_image_type_for_json(@image_type), status: :created
else
render json: @image_type.errors, status: :unprocessable_content
render json: transform_errors_for_json(@image_type), status: :unprocessable_content
end
end

def update
if @image_type.update(permitted_attributes(@image_type))
render json: transform_image_type_for_json(@image_type), status: :ok
else
render json: @image_type.errors, status: :unprocessable_content
render json: transform_errors_for_json(@image_type), status: :unprocessable_content
end
end

def destroy
render json: @image_type.errors, status: :unprocessable_content unless @image_type.destroy
render json: transform_errors_for_json(@image_type), status: :unprocessable_content unless @image_type.destroy
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/labels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ def create
if @label.save
render json: transform_label_for_json(@label), status: :created
else
render json: @label.errors, status: :unprocessable_content
render json: transform_errors_for_json(@label), status: :unprocessable_content
end
end

def update
if @label.update(permitted_attributes(@label))
render json: transform_label_for_json(@label), status: :ok
else
render json: @label.errors, status: :unprocessable_content
render json: transform_errors_for_json(@label), status: :unprocessable_content
end
end

def destroy
render json: @label.errors, status: :unprocessable_content unless @label.destroy
render json: transform_errors_for_json(@label), status: :unprocessable_content unless @label.destroy
end

def destroy_empty
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def create
if @location.save
render json: transform_location_for_json(@location), status: :created
else
render json: @location.errors, status: :unprocessable_content
render json: transform_errors_for_json(@location), status: :unprocessable_content
end
end

def destroy
render json: @location.errors, status: :unprocessable_content unless @location.destroy
render json: transform_errors_for_json(@location), status: :unprocessable_content unless @location.destroy
end

private
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/playlists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ def create
if @playlist.save
render json: transform_playlist_for_json(@playlist), status: :created
else
render json: @playlist.errors, status: :unprocessable_content
render json: transform_errors_for_json(@playlist), status: :unprocessable_content
end
end

def update
if @playlist.update(permitted_attributes(@playlist))
render json: transform_playlist_for_json(@playlist), status: :ok
else
render json: @playlist.errors, status: :unprocessable_content
render json: transform_errors_for_json(@playlist), status: :unprocessable_content
end
end

def destroy
render json: @playlist.errors, status: :unprocessable_content unless @playlist.destroy
render json: transform_errors_for_json(@playlist), status: :unprocessable_content unless @playlist.destroy
end

def add_item
@item = @playlist.items.create(permitted_attributes(@playlist))

render json: @item.errors, status: :unprocessable_content unless @item.save
render json: transform_errors_for_json(@item), status: :unprocessable_content unless @item.save
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/plays_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create
if @play.save
render json: transform_play_for_json(@play), status: :created
else
render json: @play.errors, status: :unprocessable_content
render json: transform_errors_for_json(@play), status: :unprocessable_content
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/controllers/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ def create
if @track.save
render json: transform_track_for_json(@track), status: :created
else
render json: @track.errors, status: :unprocessable_content
render json: transform_errors_for_json(@track), status: :unprocessable_content
end
end

def update
if @track.update(transformed_attributes)
render json: transform_track_for_json(@track), status: :ok
else
render json: @track.errors, status: :unprocessable_content
render json: transform_errors_for_json(@track), status: :unprocessable_content
end
end

def destroy
render json: @track.errors, status: :unprocessable_content unless @track.destroy
render json: transform_errors_for_json(@track), status: :unprocessable_content unless @track.destroy
end

def destroy_empty
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,27 @@ def create
if @user.save
render json: transform_user_for_json(@user), status: :created
else
render json: @user.errors, status: :unprocessable_content
render json: transform_errors_for_json(@user), status: :unprocessable_content
end
end

def update
if @user == current_user &&
params[:user][:password].present? &&
!@user.try(:authenticate, params[:user][:current_password])
render json: { unauthorized: [I18n.t('users.current_password_is_incorrect')] },
status: :unauthorized
render json: { errors: [{ attribute: :base, type: :incorrect_password }] }, status: :unauthorized
return
end

if @user.update(permitted_attributes(@user))
render json: transform_user_for_json(@user), status: :ok
else
render json: @user.errors, status: :unprocessable_content
render json: transform_errors_for_json(@user), status: :unprocessable_content
end
end

def destroy
render json: @user.errors, status: :unprocessable_content unless @user.destroy
render json: transform_errors_for_json(@user), status: :unprocessable_content unless @user.destroy
end

private
Expand Down
Loading