Ruby on rails方法部分?

时间:2016-03-15 13:54:13

标签: ruby-on-rails ruby

我想知道是否可以在rails上创建ruby方法,例如我有这个代码; -

@cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
@slots = @cart.slots.first
@slot_list = [@slots.slot_one, @slots.slot_two, @slots.slot_three, @slots.slot_four, @slots.slot_five,
              @slots.slot_six, @slots.slot_seven, @slots.slot_eight, @slots.slot_nine, @slots.slot_ten]
@user_products = []
@product = []
@slot_list.each do |item|
  if item.nil?
    p 'Item empty'
  else
    @product << item
  end
end
@product.each do |item|
  items = Product.where(:product_id => item).first
  @user_products << items
end

用多种方法编写来获取@user_products,我想知道是否有办法,所以我不必一直写这个并且可能运行方法或使用部分方法?

是否值得创建一个帮助程序来执行此操作并返回@user_products变量?

2 个答案:

答案 0 :(得分:0)

我接受了自己的建议并创建了两个帮助器,一个用于返回@user_products,另一个用于返回@total。

我在helper_method

中添加了方法的名称
helper_method :user_is_admin?, :authenticate_admin!, :product_available?, :get_user_products!, :get_user_total!

然后在文件底部添加了这两个方法; -

<强> get_user_products!

def get_user_products!
      @cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
    @slots = @cart.slots.first
    @slot_list = [@slots.slot_one, @slots.slot_two, @slots.slot_three, @slots.slot_four, @slots.slot_five,
                  @slots.slot_six, @slots.slot_seven, @slots.slot_eight, @slots.slot_nine, @slots.slot_ten]
    @user_products = []
    @product = []
    @slot_list.each do |item|
    if item.nil?
        p 'Item empty'
      else        
        @product << item
    end
    end
      @product.each do |item|
      items = Product.where(:product_id => item).first
      @user_products << items
    end
    return @user_products
end

<强> get_user_total!

def get_user_total!
@total = 0
    @cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
    @slots = @cart.slots.first
    @slot_list = [@slots.slot_one, @slots.slot_two, @slots.slot_three, @slots.slot_four, @slots.slot_five,
                  @slots.slot_six, @slots.slot_seven, @slots.slot_eight, @slots.slot_nine, @slots.slot_ten]

    @user_products = []
    @product = []
    @slot_list.each do |item|
    if item.nil?
        p 'Item empty'
      else        
        @product << item
    end
    end
      @product.each do |item|
      items = Product.where(:product_id => item).first
      @user_products << items
    end
    @user_products.each do |p|
      @total += p.product_price
    end
    return @total
end

要在任何控制器中使用这些方法,请执行以下操作: -

@user_products = get_user_products!
@total = get_user_total!

答案 1 :(得分:0)

我认为这是在控制器中?

你想要的是使用普通的旧Ruby对象(PO​​RO)。所以,你可能会有这样的事情:

  class UserProducts
    class << self

      def get(options={})
        @cart = Cart.where(:user_id => current_user.id).first if user_signed_in?
        @slots = @cart.slots.first
        @slot_list = [
          @slots.slot_one, 
          @slots.slot_two, 
          @slots.slot_three, 
          @slots.slot_four, 
          @slots.slot_five,
          @slots.slot_six, 
          @slots.slot_seven, 
          @slots.slot_eight, 
          @slots.slot_nine, 
          @slots.slot_ten
        ]

      @user_products = []
      @product = []
      @slot_list.each do |item|
        if item.nil?
          p 'Item empty'
        else                
          @product << item
        end
      end
      @product.each do |item|
        items = Product.where(:product_id => item).first
        @user_products << items
      end
    end
  end

然后,在您的控制器中,您可以执行以下操作:

class FooController < ApplicationController
  def index
    UserProducts.get(user_id: current_user.id)
  end
end

因此,UserProducts本质上是一个服务对象。我想有些人称他们为用例。我倾向于称他们为“经理人”。我将它们作为app/managers/user_products.rb放在自己的目录中。