-1

I have the following partial (_card_brand.html.erb), which looks like this:

<div class="payment-card">
    <i class="fa fa-cc-<%= brand.downcase %> payment-icon-big text-success"></i>
</div>

That renders HTML that looks like this:

<div class="payment-card">
    <i class="fa fa-cc-visa payment-icon-big text-success"></i>
</div>

The above is rendered with this:

<%= render partial: "subscriptions/card_brand", locals: { brand: current_user.card_brand } %>

What I want to do is change the class text-success, to be either: text-warning, text-primary, text-danger, etc. depending on if the card has brand: visa, amex, mastercard, discovery, etc.

So:

  • Visa = Success
  • AMEX = Warning
  • Mastercard = Primary
  • Discovery = Danger

Any other cards would be other classes.

How do I elegantly represent that in my view that renders the partial?

marcamillion
  • 32,933
  • 55
  • 189
  • 380

1 Answers1

1

You may create a helper and use it so that it will be easy to add new classes as well.

application_helper.rb

CARD_CLASS = {
  'visa' => 'success',
  'amex' => 'warning',
  'mastercard' => 'primary',
  'discovery' => 'danger'
}

def payment_class(type)
  CARD_CLASS[type.downcase]
end

_card_brand.html.erb

<div class="payment-card">
    <i class="fa fa-cc-<%= brand.downcase %> payment-icon-big text-text-<%= payment_class(brand.downcase) %>"></i>
</div>
marcamillion
  • 32,933
  • 55
  • 189
  • 380
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88