I am working on a scholarship application, where people can make donations to support persons for different programs that they want to participate in. I need some help with Rubocop refactoring in rails.
I have the following issues;
- Controller action only calls one model method other than an initial find or new. Make custom .new or .update methods in the model with all necessary.
- Assignment Branch Condition size for index is too high
- Method has too many lines
I have tried to refactor the code, but I am still facing the same issues with the code.
My codes are;
Dashboard Controller (Initial*)
class DashboardController < ApplicationController
def index
#Paid Donations in Chart
@paid_donations = Donation.where(payment: true).count
#Unpaid Donations in Chart
@unpaid_donations = Donation.where(payment: false).count
#Total Donations Sum
@total_donations_sum = Donation.where(payment: true).sum(:amount)
#Deployed Donations
@deployed_donations = Donation.where(deployment: true).sum(:amount)
#Not Deployed Donations
@not_deployed_donations = Donation.where(deployment: false, payment: true).sum(:amount)
#Deployed Donations Percentage
@deployed_donations_percentage = (@deployed_donations.to_f / @total_donations_sum.to_f) * 100
#Not Deployed Donations Percentage
@not_deployed_donations_percentage = (@not_deployed_donations.to_f / @total_donations_sum.to_f) * 100
#Total Donations
@total_donations = Donation.count
#Paid Donations
@paid_donations = Donation.where(payment: true).count
#Unpaid Donations
@unpaid_donations = Donation.where(payment: false).count
#All Programs
@programs = Program.all
end
end
Dashboard Controller (Refactored)
class DashboardController < ApplicationController
def index
# Paid Donations in Chart
@paid_donations = Donation.paid_count
# Unpaid Donations in Chart
@unpaid_donations = Donation.unpaid_count
# Total Donations Sum
@total_donations_sum = Donation.paid_sum
# Deployed Donations
@deployed_donations = Donation.deployed_sum
# Not Deployed Donations
@not_deployed_donations = Donation.not_deployed_sum
# Deployed Donations Percentage
@deployed_donations_percentage = percentage(@deployed_donations, @total_donations_sum)
# Not Deployed Donations Percentage
@not_deployed_donations_percentage = (@not_deployed_donations.to_f / @total_donations_sum.to_f) * 100
# Total Donations
@total_donations = Donation.count
# Paid Donations
@paid_donations = Donation.paid_count
# Unpaid Donations
@unpaid_donations = Donation.unpaid_count
# All Programs
@programs = Program.all
end
end
Donation Model (Initial)
class Donation < ApplicationRecord
belongs_to :program
end
Donations Model (Refactored)
Class Donation < ApplicationRecord
belongs_to :program
scope :paid_count, -> { where(payment: true).count }
scope :unpaid_count, -> { where(payment: false).count }
scope :paid_sum, -> { where(payment: true).sum(:amount) }
scope :paid_sum, -> { where(payment: true).sum(:amount) }
scope :deployed_sum, -> { where(deployment: true).sum(:amount) }
scope :not_deployed_sum, -> { where(deployment: false).sum(:amount) }
def percentage(donate, total)
(donate.to_f / total.to_f) * 100
end
end
I need some assistance on the rails best practices to resolve these issues, following the skinny models and skinny controllers rails principle.