ruby-on-rails - 如何干掉这段Ruby代码(Active Admin/rails 3.2)

标签 ruby-on-rails ruby ruby-on-rails-3 activeadmin dry

<分区>

我觉得我在这里重复太多了,希望 DRY它,但我不知道该怎么做。

我有一个 Prize 模型,其中一个属性是 prize_type这是一个选择(您可以在名为 PRIZE_TYPE 的 CONSTANT 中选择 4 个值之一)。我有一个 table_for对于每种类型的奖品。

例如,如果有一天我在数组 .map 中添加第五个或第六个值,我想使用某种映射(PRIZE_TYPE ?) , 我希望我的面板自动添加它们而无需复制/粘贴另一个 table_for在下面:

show do 

    def custom_number_to_currency(u)
      number_to_currency u,
        separator: ".",
        delimiter: ",",
        precision: 0,
        raise: true  
    end    



 panel "Details of Prizes" do

  table_for deal.prizes.where(:prize_type => PRIZE_TYPES[0]) do |t|
    h3 "The deal has #{deal.prizes.where(:prize_type => PRIZE_TYPES[0]).count} #{PRIZE_TYPES[0]}s
    initially set in the campaign for a total value of #{custom_number_to_currency (deal.prizes.where(:prize_type => PRIZE_TYPES[0]).sum(:prize_unit_certified_market_value)) }", class: 'title-within-table'
    if deal.prizes.where(:prize_type => PRIZE_TYPES[0]).count > 0 # if there is at least one record of 'jackpot prize'
      t.column("Prize")             { |prize| link_to( image_tag( prize.prize_image_url, class: 'main_img_in_admin_interface' ), admin_prize_path(prize), target: :blank ) }
      t.column("Name")              { |prize| link_to prize.prize_name, admin_prize_path(prize), target: :blank }
      t.column("Category")          { |prize| prize.prize_category }
      t.column("Initial quantity")  { |prize| prize.prize_initial_stock_quantity }
      t.column("Remaining quantity"){ |prize| if prize.prize_remaining_stock_quantity == 0
                                                status_tag('no prizes left (all won)', :class => 'green')
                                              else
                                                prize.prize_remaining_stock_quantity
                                              end } 

  end

  table_for deal.prizes.where(:prize_type => PRIZE_TYPES[1]) do |t|
    h3 "The deal has #{deal.prizes.where(:prize_type => PRIZE_TYPES[1]).count} #{PRIZE_TYPES[1]}s
    initially set in the campaign for a total value of #{custom_number_to_currency (deal.prizes.where(:prize_type => PRIZE_TYPES[1]).sum(:prize_unit_certified_market_value)) }", class: 'title-within-table'
    if deal.prizes.where(:prize_type => PRIZE_TYPES[1]).count > 0 # if there is at least one record of 'in-modal prize'
      t.column("Prize")             { |prize| link_to( image_tag( prize.prize_image_url, class: 'main_img_in_admin_interface' ), admin_prize_path(prize), target: :blank ) }
      t.column("Name")              { |prize| link_to prize.prize_name, admin_prize_path(prize), target: :blank }
      t.column("Category")          { |prize| prize.prize_category }
      t.column("Initial quantity")  { |prize| prize.prize_initial_stock_quantity }
      t.column("Remaining quantity"){ |prize| if prize.prize_remaining_stock_quantity == 0
                                                status_tag('no prizes left (all won)', :class => 'green')
                                              else
                                                prize.prize_remaining_stock_quantity
                                              end } 

  end

  table_for deal.prizes.where(:prize_type => PRIZE_TYPES[2]) do |t|
    h3 "The deal has #{deal.prizes.where(:prize_type => PRIZE_TYPES[2]).count} #{PRIZE_TYPES[2]}s
    initially set in the campaign for a total value of #{custom_number_to_currency (deal.prizes.where(:prize_type => PRIZE_TYPES[2]).sum(:prize_unit_certified_market_value)) }", class: 'title-within-table'
    if deal.prizes.where(:prize_type => PRIZE_TYPES[2]).count > 0 # if there is at least one record of 'Consolation prize'
      t.column("Prize")             { |prize| link_to( image_tag( prize.prize_image_url, class: 'main_img_in_admin_interface' ), admin_prize_path(prize), target: :blank ) }
      t.column("Name")              { |prize| link_to prize.prize_name, admin_prize_path(prize), target: :blank }
      t.column("Category")          { |prize| prize.prize_category }
      t.column("Initial quantity")  { |prize| prize.prize_initial_stock_quantity }
      t.column("Remaining quantity"){ |prize| if prize.prize_remaining_stock_quantity == 0
                                                status_tag('no prizes left (all won)', :class => 'green')
                                              else
                                                prize.prize_remaining_stock_quantity
                                              end } 

  end

  table_for deal.prizes.where(:prize_type => PRIZE_TYPES[3]) do |t|
    h3 "The deal has #{deal.prizes.where(:prize_type => PRIZE_TYPES[3]).count} #{PRIZE_TYPES[3]}s
    initially set in the campaign for a total value of #{custom_number_to_currency (deal.prizes.where(:prize_type => PRIZE_TYPES[3]).sum(:prize_unit_certified_market_value)) }", class: 'title-within-table'
    if deal.prizes.where(:prize_type => PRIZE_TYPES[3]).count > 0 # if there is at least one record of 'Activation prize'
      t.column("Prize")             { |prize| link_to( image_tag( prize.prize_image_url, class: 'main_img_in_admin_interface' ), admin_prize_path(prize), target: :blank ) }
      t.column("Name")              { |prize| link_to prize.prize_name, admin_prize_path(prize), target: :blank }
      t.column("Category")          { |prize| prize.prize_category }
      t.column("Initial quantity")  { |prize| prize.prize_initial_stock_quantity }
      t.column("Remaining quantity"){ |prize| if prize.prize_remaining_stock_quantity == 0
                                                status_tag('no prizes left (all won)', :class => 'green')
                                              else
                                                prize.prize_remaining_stock_quantity
                                              end } 

  end

最佳答案

你不能这样做吗?

panel "Details of Prizes" do   
    PRIZE_TYPES.each do |prize_type|
      table_for deal.prizes.where(:prize_type => prize_type) do |t|
        h3 "The deal has #{deal.prizes.where(:prize_type => prize_type).count} #{prize_type}s
        initially set in the campaign for a total value of #{custom_number_to_currency (deal.prizes.where(:prize_type => prize_type).sum(:prize_unit_certified_market_value)) }", class: 'title-within-table'
        if deal.prizes.where(:prize_type => prize_type).count > 0 # if there is at least one record of 'jackpot prize'
          t.column("Prize")             { |prize| link_to( image_tag( prize.prize_image_url, class: 'main_img_in_admin_interface' ), admin_prize_path(prize), target: :blank ) }
          t.column("Name")              { |prize| link_to prize.prize_name, admin_prize_path(prize), target: :blank }
          t.column("Category")          { |prize| prize.prize_category }
          t.column("Initial quantity")  { |prize| prize.prize_initial_stock_quantity }
          t.column("Remaining quantity"){ |prize| if prize.prize_remaining_stock_quantity == 0
                                                    status_tag('no prizes left (all won)', :class => 'green')
                                                  else
                                                    prize.prize_remaining_stock_quantity
                                                  end }  
   end
end

关于ruby-on-rails - 如何干掉这段Ruby代码(Active Admin/rails 3.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21439438/

相关文章:

ruby-on-rails - rails : display @cars as a comma-separated list

ruby-on-rails - "No verification key available"尝试访问由 Devise JWT 保护的 API 时

python - Ruby 正则表达式中的符号组名称(类似于 Python)

ruby - 无法重新索引太阳黑子。 "Solr Response: Bad Request"

ruby-on-rails - mongoid/事件模型的日期验证助手?

ruby-on-rails - rails 单个文件中的多个模型

基于预定义列表的 Ruby 排序

ruby - 在 Ruby On Rails View 中替代 <%= ?

javascript - jquery 切换无法正常工作

ruby-on-rails - Rails 3 和 Twilio 进行电话验证