首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Rubocop重构Rails应用程序的最佳方法

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

Stack Overflow用户
提问于 2019-06-11 17:55:56
回答 1查看 250关注 0票数 1

我正在做一个奖学金申请,在那里人们可以捐款支持不同的项目,他们想参加。我需要一些在rails中进行Rubocop重构的帮助。

I有以下问题:

  1. 控制器操作只调用一个模型方法,而不是初始查找或新方法。在模型中使用所有必要的定制.new或.update方法。
  2. 索引的赋值分支条件大小太高
  3. 方法有太多的行。

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

我的密码是;

仪表板控制器(初始*)

代码语言:javascript
复制
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

仪表板控制器(重构)

代码语言:javascript
复制
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

捐赠模型(初始)

代码语言:javascript
复制
class Donation < ApplicationRecord
  belongs_to :program
end

捐赠模型(重构)

代码语言:javascript
复制
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原则。

EN

回答 1

Stack Overflow用户

发布于 2019-06-12 08:27:31

我认为在这种情况下,您可以在模型捐赠中创建方法来返回所有的值,以在1散列中显示。

代码语言:javascript
复制
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

在你的控制器里

代码语言:javascript
复制
class DashboardController < ApplicationController    
  def index
    # donations info
    @donations_info = Donation.info
    # All Programs
    @programs = Program.all
  end
end

在您的视图中,您可以使用

代码语言:javascript
复制
<%= @donations_info[:paid_donations] %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56549312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档