繁体   English   中英

使用Rubocop重构Rails应用的最佳方法

[英]Best way to refactor a Rails app using Rubocop

我正在研究一项奖学金申请,人们可以在其中捐款,以支持人们想要参与的各种计划。我需要一些关于Rubocop重构轨道的帮助。

我有以下问题;

  1. 控制器操作仅调用一种模型方法,而不是初始查找或新方法。 必要时在模型中创建自定义.new或.update方法。
  2. 索引的分配分支条件大小太大
  3. 方法行太多

我试图重构代码,但是代码仍然面临着同样的问题。

我的密码是

仪表板控制器(初始*)

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

仪表板控制器(重构)

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

捐赠模式(初始)

class Donation < ApplicationRecord
  belongs_to :program
end

捐赠模型(重构)

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

我遵循瘦模型和瘦控制器rails原理,需要一些有关Rails最佳实践的帮助来解决这些问题。

我认为在这种情况下,您可以在模型Donation中创建方法以返回所有值以1个哈希值显示。

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 :deployed_sum, -> { where(deployment: true).sum(:amount) }
  scope :not_deployed_sum, -> { where(deployment: false).sum(:amount) }

  def self.deployed_donations_percentage
    (deployed_sum / size) * 100
  end

  def self.not_deployed_donations_percentage
    (not_deployed_sum / size) * 100
  end

  def self.info
    {}.tap do |info|
      info[:paid_donations] = paid_count
      info[:unpaid_donations] = unpaid_count
      info[:total_donations_sum] = paid_count
      info[:deployed_donations_percentage] = deployed_donations_percentage
      info[:not_deployed_donations_percentage] = not_deployed_donations_percentage
      #...anything you want to show
    end

  end
end

在您的控制器中

class DashboardController < ApplicationController    
  def index
    # donations info
    @donations_info = Donation.info
    # All Programs
    @programs = Program.all
  end
end

在您看来,您可以通过

<%= @donations_info[:paid_donations] %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM