Run Rubocop autocorrect on app/controllers

This commit is contained in:
Manuel Bustillo 2024-12-28 18:28:28 +01:00
parent 4fc95185fb
commit 0c7c69ee5e
11 changed files with 115 additions and 72 deletions

View File

@ -6,22 +6,27 @@ class AffinitiesController < ApplicationController
before_action :set_group
def index
overridden_affinities = @group.affinities
.each_with_object({}) { |affinity, acc| acc[affinity.another_group(@group).id] = affinity.discomfort }
Group.where.not(id: @group.id).pluck(:id).index_with { |group_id| GroupAffinity::MAX_DISCOMFORT - (overridden_affinities[group_id] || GroupAffinity::NEUTRAL) }
.then { |affinities| render json: affinities }
overridden = @group.affinities.each_with_object({}) do |affinity, acc|
acc[affinity.another_group(@group).id] = affinity.discomfort
end
Group.where.not(id: @group.id)
.pluck(:id)
.index_with { |group_id| GroupAffinity::MAX_DISCOMFORT - (overridden[group_id] || GroupAffinity::NEUTRAL) }
.then { |affinities| render json: affinities }
end
def bulk_update
params.expect(affinities: [[:group_id, :affinity]]).map(&:to_h).map do |affinity|
affinities = params.expect(affinities: [%i[group_id affinity]]).map(&:to_h).map do |affinity|
{
group_a_id: @group.id,
group_b_id: affinity[:group_id],
discomfort: GroupAffinity::MAX_DISCOMFORT - affinity[:affinity]
}
end.then { |affinities| GroupAffinity.upsert_all(affinities) }
render json: {}, status: :ok
end
GroupAffinity.upsert_all(affinities)
render json: {}, status: :ok
rescue ActiveRecord::InvalidForeignKey
render json: { error: 'At least one of the group IDs provided does not exist.' }, status: :bad_request
rescue ActiveRecord::StatementInvalid

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class ApplicationController < ActionController::Base
@ -40,9 +42,9 @@ class ApplicationController < ActionController::Base
end
def captcha_params
params.expect(captcha: [:id, :answer])
params.expect(captcha: %i[id answer])
end
def default_url_options(options = {})
options.merge(path_params: { slug: ActsAsTenant.current_tenant&.slug })
end
@ -53,7 +55,7 @@ class ApplicationController < ActionController::Base
def development_swagger?
Rails.env.test? ||
Rails.env.development? && request.headers['referer']&.include?('/api-docs/index.html')
(Rails.env.development? && request.headers['referer']&.include?('/api-docs/index.html'))
end
def set_csrf_cookie

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class ExpensesController < ApplicationController
@ -6,7 +8,7 @@ class ExpensesController < ApplicationController
end
def index
render json: Expense.all.order(pricing_type: :asc, amount: :desc).as_json(only: %i[id name amount pricing_type])
render json: Expense.order(pricing_type: :asc, amount: :desc).as_json(only: %i[id name amount pricing_type])
end
def create

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class GroupsController < ApplicationController
@ -39,6 +41,6 @@ class GroupsController < ApplicationController
end
def group_params
params.expect(group: [:name, :icon, :color])
params.expect(group: %i[name icon color])
end
end

View File

@ -1,10 +1,12 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
require 'csv'
class GuestsController < ApplicationController
def index
render json: Guest.all.includes(:group)
render json: Guest.includes(:group)
.left_joins(:group)
.order('groups.name' => :asc, name: :asc)
.as_json(only: %i[id name status], include: { group: { only: %i[id name] } })

View File

@ -1,29 +1,43 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class SummaryController < ApplicationController
def index
expense_summary = Expenses::TotalQuery.new(wedding: ActsAsTenant.current_tenant).call
guest_summary = Guest.group(:status).count
render json: {
expenses: {
projected: {
total: expense_summary['total_projected'],
guests: expense_summary['projected_guests']
},
confirmed: {
total: expense_summary['total_confirmed'],
guests: expense_summary['confirmed_guests']
},
status: {
paid: 0
}
expenses:,
guests:
}
end
private
def guests
guest_summary = Guest.group(:status).count
{
total: guest_summary.except('considered').values.sum,
confirmed: guest_summary['confirmed'].to_i,
declined: guest_summary['declined'].to_i,
tentative: guest_summary['tentative'].to_i,
invited: guest_summary['invited'].to_i
}
end
def expenses
expense_summary = Expenses::TotalQuery.new(wedding: ActsAsTenant.current_tenant).call
{
projected: {
total: expense_summary['total_projected'],
guests: expense_summary['projected_guests']
},
guests: {
total: guest_summary.except('considered').values.sum,
confirmed: guest_summary['confirmed'].to_i,
declined: guest_summary['declined'].to_i,
tentative: guest_summary['tentative'].to_i,
invited: guest_summary['invited'].to_i
confirmed: {
total: expense_summary['total_confirmed'],
guests: expense_summary['confirmed_guests']
},
status: {
paid: 0
}
}
end

View File

@ -1,8 +1,10 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class TablesArrangementsController < ApplicationController
def index
render json: TablesArrangement.all.order(discomfort: :asc).limit(3).as_json(only: %i[id name discomfort])
render json: TablesArrangement.order(discomfort: :asc).limit(3).as_json(only: %i[id name discomfort])
end
def show

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class TokensController < ApplicationController

View File

@ -1,23 +1,27 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class Users::ConfirmationsController < Devise::ConfirmationsController
clear_respond_to
respond_to :json
module Users
class ConfirmationsController < Devise::ConfirmationsController
clear_respond_to
respond_to :json
def show
super do |resource|
if resource.errors.empty?
respond_to do |format|
format.json { render json: resource, status: :ok }
format.any { redirect_to root_path }
def show
super do |resource|
if resource.errors.empty?
respond_to do |format|
format.json { render json: resource, status: :ok }
format.any { redirect_to root_path }
end
else
render json: {
message: 'Record invalid',
errors: resource.errors.full_messages
}, status: :unprocessable_entity
end
else
render json: {
message: 'Record invalid',
errors: resource.errors.full_messages
}, status: :unprocessable_entity
return
end
return
end
end
end
end

View File

@ -1,28 +1,32 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class Users::RegistrationsController < Devise::RegistrationsController
clear_respond_to
respond_to :json
module Users
class RegistrationsController < Devise::RegistrationsController
clear_respond_to
respond_to :json
before_action :validate_captcha!, only: :create
before_action :validate_captcha!, only: :create
def create
wedding = Wedding.create(slug: params[:slug])
unless wedding.persisted?
render json: { errors: wedding.errors.full_messages }, status: :unprocessable_entity
return
end
def create
wedding = Wedding.create(slug: params[:slug])
unless wedding.persisted?
render json: { errors: wedding.errors.full_messages }, status: :unprocessable_entity
return
end
ActsAsTenant.with_tenant(wedding) do
super do |user|
wedding.destroy unless user.persisted?
ActsAsTenant.with_tenant(wedding) do
super do |user|
wedding.destroy unless user.persisted?
end
end
end
end
private
private
def set_tenant
set_current_tenant(nil)
def set_tenant
set_current_tenant(nil)
end
end
end
end

View File

@ -1,6 +1,10 @@
# frozen_string_literal: true
# Copyright (C) 2024 Manuel Bustillo
class Users::SessionsController < Devise::SessionsController
clear_respond_to
respond_to :json
end
module Users
class SessionsController < Devise::SessionsController
clear_respond_to
respond_to :json
end
end