简体   繁体   中英

Ruby: How to pluralize/singularize a word in Active Admin panel according to variable quantity (Active Admin, rails 3.2, ruby)

I'ma newbie in Ruby on Rails and I don't manage to change the end of a word according to the quantity it has.

Near top of the code below, you'll see that I write

h3 "The deal has #{deal.prizes.where(:prize_type => u).count} #{u}

The "u" here takes one of the value inside an array called PRIZE_TYPE. How can I do so that

  • if count= 1, it displays "the deal has 1 jackpot prize"

  • if count=4, it displays "the deal has 4 jackpot prizeS

Note: The count can't be = 0 or < 0

Here is my code:

panel "Details of Prizes" do

      PRIZE_TYPES.each do |u|

        table_for deal.prizes.where(:prize_type => u) do |t|
          h3 "The deal has #{deal.prizes.where(:prize_type => u).count} #{u}s
          initially set in the campaign for a total value of #{custom_number_to_currency (deal.category_prizes_total_initial_value(u)) }", class: 'title-within-table'
          if deal.prizes.where(:prize_type => u).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 }

      end # end of Prize.each...

    end # end of panel "details of prizes"

And the PRIZE_TYPE constant:

PRIZE_TYPES = ["Jackpot prize","Consolation prize", "awesome prize"]

You could use ruby's pluralize:

prize_type.pluralize(count)
"Jackpot prize".pluralize(1)
#> Jackpot prize
"Jackpot prize".pluralize(2)
#> Jackpot prizes

Or ActiveSupport::Inflector.pluralize if you want to do the check yourself:

prize_type.pluralize
"Jackpot prize".pluralize
#> "Jackpot Prizes"

Or rails has pluralize that uses a combination Inflector and some base logic:

pluralize(count, prize_type)
pluralize(1, prize_type)
#> 1 Jackpot prize
pluralize(2, prize_type)
#> 2 Jackpot prizes

you can use the ruby instance method

http://apidock.com/rails/ActionView/Helpers/TextHelper/pluralize

"string".pluralize(count)

so for your case,

prizes_count = deal.prizes.where(prize_type: u).count
h3 "The deal has #{prizes_count} #{u.pluralize(prizes_count)}"

Use pluralize

count = deal.prizes.where(:prize_type => u).count
h3 "The deal has #{count} #{u.pluralize(count)}}"

According to pluralize

you can just do

pluralize(1, 'person')
# => 1 person

pluralize(2, 'person')
# => 2 people

pluralize(3, 'person', plural: 'users')
# => 3 users

pluralize(0, 'person')
# => 0 people

pluralize(2, 'Person', locale: :de)
# => 2 Personen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM