mercredi 14 novembre 2018

Rails render buttons by really complex condition

My rails application members may have one of the two different roles: vip_lite and vip. They can subscribe plan to renew their authorizing.

We also have four types plan: vip_lite/monthly, vip_lite/yearly, vip/monthly, vip/yearly.

A vip user might be subscribing one of the four plans, so does vip_lite. Member with different roles and different plans can see different buttons in plan page(e.g. vip_lite and subscribing vip_lite/monthly member can see 'upgrade' button in vip plan page).

see the following sample code: (currently write in helper)

def plan_action_button(user)
  if user.vip? && user.subscribing_plan?(:vip_lite, :monthly)
    'button_a'
  elsif user.vip? && user.subscribing_plan?(:vip_lite, :yearly)
    'button_b'
  elsif user.vip? && user.subscribing_plan?(:vip, :monthly)
    'button_c'
  elsif user.vip? && user.subscribing_plan?(:vip, :monthly)
    'button_d'

    ... and so on...
  end
end

So I have to deal with 2 * 4 = 8 conditions for each button in these pages. Is there a good pattern in rails for dealing with such case? Thanks

Aucun commentaire:

Enregistrer un commentaire