Ask A Question

Notifications

You’re not receiving notifications from this thread.

User Badges Features

Menj Villalobos asked in Rails

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

Thanks :)

Reply

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
  end

  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"

    end
  end

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?

Reply

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

Reply

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

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 71,728+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.