Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Rubocop重构Rails应用程序的最佳方法_Ruby On Rails_Rubocop - Fatal编程技术网

Ruby on rails 使用Rubocop重构Rails应用程序的最佳方法

Ruby on rails 使用Rubocop重构Rails应用程序的最佳方法,ruby-on-rails,rubocop,Ruby On Rails,Rubocop,我正在申请奖学金,人们可以通过捐款来支持他们想参加的不同项目。我需要一些关于rails中Rubocop重构的帮助 我有以下问题 控制器操作只调用一个模型方法,而不是初始方法 发现或新的。使用在模型中创建自定义.new或.update方法 一切都是必要的 索引的分配分支条件大小太大 高 方法有太多行 我已经尝试过重构代码,但我仍然面临着与代码相同的问题 我的代码是 仪表板控制器(首字母*) class仪表板控制器{where(payment:true).sum(:amount)} 作用域:depl

我正在申请奖学金,人们可以通过捐款来支持他们想参加的不同项目。我需要一些关于rails中Rubocop重构的帮助

我有以下问题

  • 控制器操作只调用一个模型方法,而不是初始方法 发现或新的。使用在模型中创建自定义.new或.update方法 一切都是必要的
  • 索引的分配分支条件大小太大 高
  • 方法有太多行
  • 我已经尝试过重构代码,但我仍然面临着与代码相同的问题

    我的代码是

    仪表板控制器(首字母*)

    class仪表板控制器
    仪表板控制器(重构)

    class仪表板控制器
    捐赠模式(初始)

    类捐赠
    捐赠模型(重构)

    类捐赠{where(payment:true).count}
    范围:未付款计数,->{where(payment:false).count}
    范围:已付金额,->{where(payment:true).sum(:amount)}
    范围:已付金额,->{where(payment:true).sum(:amount)}
    作用域:deployed_sum,->{where(deployment:true).sum(:amount)}
    作用域:not_deployed_sum,->{where(deployment:false).sum(:amount)}
    def百分比(捐赠,总计)
    (捐赠给/总计捐赠给)*100
    结束
    结束
    

    我需要一些rails最佳实践方面的帮助来解决这些问题,遵循skinny models and skinny controllers rails原则。

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

    类捐赠{where(payment:true).count}
    范围:未付款计数,->{where(payment:false).count}
    范围:已付金额,->{where(payment:true).sum(:amount)}
    作用域:deployed_sum,->{where(deployment:true).sum(:amount)}
    作用域:not_deployed_sum,->{where(deployment:false).sum(:amount)}
    def自部署_捐款_百分比
    (总数/规模)*100
    结束
    def self.not_deployed_捐赠_百分比
    (未部署金额/大小)*100
    结束
    def self.info
    {}。点击do| info|
    信息[:已付捐款]=已付金额
    信息[:未付捐款]=未付捐款计数
    信息[:捐款总额]=已付金额
    信息[:已部署的捐款百分比]=已部署的捐款百分比
    信息[:未部署的\u捐款\u百分比]=未部署的\u捐款\u百分比
    #…你想展示什么
    结束
    结束
    结束
    
    在控制器中

    class仪表板控制器
    在您看来,您可以使用


    我认为在本例中,您可以在模型捐赠中创建方法,以返回所有值,并在1哈希中显示

    类捐赠{where(payment:true).count}
    范围:未付款计数,->{where(payment:false).count}
    范围:已付金额,->{where(payment:true).sum(:amount)}
    作用域:deployed_sum,->{where(deployment:true).sum(:amount)}
    作用域:not_deployed_sum,->{where(deployment:false).sum(:amount)}
    def自部署_捐款_百分比
    (总数/规模)*100
    结束
    def self.not_deployed_捐赠_百分比
    (未部署金额/大小)*100
    结束
    def self.info
    {}。点击do| info|
    信息[:已付捐款]=已付金额
    信息[:未付捐款]=未付捐款计数
    信息[:捐款总额]=已付金额
    信息[:已部署的捐款百分比]=已部署的捐款百分比
    信息[:未部署\u捐款\u pe
    
    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