如何从视图中提取逻辑

时间:2018-11-12 16:44:11

标签: ruby-on-rails ruby ruby-on-rails-5

在我的应用中,我可以按日期搜索consumptions

我有一个belongs_to :users的活动记录模型 Consumption 和一个模型 ConsumptionSearch

consumption.rb

class Consumption < ApplicationRecord
    belongs_to :user
end

consumption_search.rb

class ConsumptionSearch 
    attr_reader :date_from, :date_to

    def initialize(params)
        params ||= {}
        @date_from = parsed_date(params[:date_from],Time.now.beginning_of_month.to_date.to_s)
        @date_to   = parsed_date(params[:date_to], (Date.today + 1).to_s)
    end

    def date_range
        Consumption.where('created_at BETWEEN ? AND ?', @date_from, @date_to)
    end

    private
        def parsed_date(date_string, default)
            Date.parse(date_string)
            rescue ArgumentError, TypeError
            default
        end
end

consumptions_controller操作的index中,我可以按日期检索所需的消费量

class ConsumptionsController < ApplicationController
    def index
        @search = ConsumptionSearch.new(params[:search])
        @consumptions = @search.date_range
        @consumptions = @consumptions.order('created_at ASC').where(user_id: current_user.id)
    end
end

消费模式可能会有所帮助:

  create_table "consumptions", force: :cascade do |t|
    t.float "total_price"
    t.float "kilometers"
    t.string "shop"
    t.float "liter_price"
    t.float "total_liters"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.float "difference", default: 0.0
  end

因此,在我的consumptions/view/index.html.erb中,我只需要遍历@consumptions即可显示所需信息。

问题是:

如何使它更“红润”,我认为视图中的逻辑太多...应该在哪里以及如何提取逻辑?

<%= price = (@consumptions.map { |c| c.total_price }.sum - @consumptions.last.total_price).round(2) %> 
<%= total_km = (@consumptions.map { |c| c.difference }.sum).round.abs %>
<%= (price / total_km).round(4) %> 
<%= (price / total_km * 100).round(2) %>

2 个答案:

答案 0 :(得分:2)

您可以创建助手:app / helpers / consumptions_helper.rb

module ConsumptionsHelper
 def some_logic
   ...
 end
end

并在视图中使用它

<%= some_logic %>

请在此处查看RoR文档 https://api.rubyonrails.org/classes/ActionController/Helpers.html

答案 1 :(得分:1)

为什么不将逻辑添加到您的ConsumptionSearch类中?您已经在抽象逻辑了。并且,您已经在视图中访问它。因此,也许像这样:

class ConsumptionSearch 
  attr_reader :date_from, :date_to

    def initialize(params)
      params ||= {}
      @date_from = parsed_date(params[:date_from],Time.now.beginning_of_month.to_date.to_s)
      @date_to   = parsed_date(params[:date_to], (Date.today + 1).to_s)
    end

    def date_range
      Consumption.where('created_at BETWEEN ? AND ?', @date_from, @date_to)
    end

    def price
      ...
    end

    def total_km
      ...
    end

    def price_per_km(round_to)
      (price/total_km).round(round_to)
    end

  private

    def parsed_date(date_string, default)
      Date.parse(date_string)
      rescue ArgumentError, TypeError
      default
    end
end

然后在视图中:

<%= @consumptions.price %> 
<%= @consumptions.total_km %>
<%= @consumptions.price_per_km(4) %> 
<%= @consumptions.price_per_km(2) %>

就个人而言,helpers不是我最喜欢的东西。但是,我意识到有些人经常使用它们并取得了巨大的成功。我只是那样奇怪。

相关问题