Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

User Badges Features

Rails • Asked by Menj Villalobos

Hi @chris!
Is it possible for you to create a screencast with this features?

Thanks :)

I might be able to squeeze it in sometime. It's a lot simpler than you might think. :)

# app/helpers/user_helper.rb
  def user_badges(user)
    badges = []
    badges << sub_badge(user)
    badges << content_tag(:span, "Mod", class: "label label-primary") if user.moderator?
    badges << content_tag(:span, "Staff", class: "label label-success") if user.staff?
    badges.join(" ").html_safe

  def sub_badge(user)
    case user.charges_count
    when 1..2
      content_tag :span, fa_icon("diamond"), class: "label label-default", data: {toggle: :tooltip}, title: "1-Month Subscriber"

    when 3..5
      content_tag :span, fa_icon("diamond"), class: "label label-warning", data: {toggle: :tooltip}, title: "3-Month Subscriber"

    when 6..11
      content_tag :span, fa_icon("diamond"), class: "label label-primary", data: {toggle: :tooltip}, title: "6-Month Subscriber"

    when 12..23
      content_tag :span, fa_icon("diamond"), class: "label label-danger", data: {toggle: :tooltip}, title: "1-Year Subscriber"

    when 24..1000
      content_tag :span, fa_icon("diamond"), class: "label label-purple", data: {toggle: :tooltip}, title: "2-Year Subscriber"


At some point I might refactor this into a badge model and add rules and things into that if I expand the badging system past the basics. The sub_badge method needs some refactoring anyways. I also have a little bug that if you paid annually, it doesn't calculate your badge right, so the number of charges isn't a perfect count of a user's subscription length.

Maybe the episode would be good talking about refactoring this?

Thank you so much for this @chris I will try to implement this code. :)

It would be great to see an episode. And the Draper gem makes that easy

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.