I feel I am repeating here too much the same and wish to DRY it but I don't know how to.
I have a Prize model and one of the attribute is prize_type which is a select (you can choose one of the 4 values inside a CONSTANT called PRIZE_TYPE). I have a table_for for each type of prizes.
For example, I'd like to use some sort of mapping (.map?) in case one day, if I add a fifth or sixth value in the array PRIZE_TYPE, I want my panels to automatically add them without me having to copy/paste another table_for here below:
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
Solução
Can't you do this?
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