diff --git a/app/controllers/affinities_controller.rb b/app/controllers/affinities_controller.rb index fab690a..f8b7052 100644 --- a/app/controllers/affinities_controller.rb +++ b/app/controllers/affinities_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7cb39a8..e1bebb9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/expenses_controller.rb b/app/controllers/expenses_controller.rb index 2a125d9..0b86498 100644 --- a/app/controllers/expenses_controller.rb +++ b/app/controllers/expenses_controller.rb @@ -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 diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index f53ed35..6ba61ca 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -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 diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index 54975b6..e8b1cd0 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -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] } }) diff --git a/app/controllers/summary_controller.rb b/app/controllers/summary_controller.rb index 55983dc..0959d4d 100644 --- a/app/controllers/summary_controller.rb +++ b/app/controllers/summary_controller.rb @@ -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 diff --git a/app/controllers/tables_arrangements_controller.rb b/app/controllers/tables_arrangements_controller.rb index 00ba8b1..c042b6c 100644 --- a/app/controllers/tables_arrangements_controller.rb +++ b/app/controllers/tables_arrangements_controller.rb @@ -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 diff --git a/app/controllers/tokens_controller.rb b/app/controllers/tokens_controller.rb index c3dabba..58cf243 100644 --- a/app/controllers/tokens_controller.rb +++ b/app/controllers/tokens_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright (C) 2024 Manuel Bustillo class TokensController < ApplicationController diff --git a/app/controllers/users/confirmations_controller.rb b/app/controllers/users/confirmations_controller.rb index 660d263..d073fef 100644 --- a/app/controllers/users/confirmations_controller.rb +++ b/app/controllers/users/confirmations_controller.rb @@ -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 \ No newline at end of file +end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 0aae89d..7a9560b 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -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 \ No newline at end of file +end diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 0de9af5..4b7c7d6 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -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 \ No newline at end of file +module Users + class SessionsController < Devise::SessionsController + clear_respond_to + respond_to :json + end +end