簡體   English   中英

Rails通過非常復雜的條件渲染按鈕

[英]Rails render buttons by really complex condition

我的rails應用程序成員可能具有兩種不同的角色之一:vip_lite和vip。 他們可以訂閱續訂授權的計划。

我們還有四種類型的計划:vip_lite /每月,vip_lite /每年,vip /每月,vip /每年。

VIP用戶可能正在訂購四個計划之一,vip_lite也是如此。 具有不同角色和不同計划的會員可以在計划頁面中看到不同的按鈕(例如,vip_lite和訂閱vip_lite /每月會員可以在VIP計划頁面中看到“升級”按鈕)。

請參見下面的示例代碼:(當前寫在幫助器中)

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'
  elsif user.vip_lite? && user.subscribing_plan?(:vip, :monthly)
    'button_e'
  elsif user.vip_lite? && user.subscribing_plan?(:vip, :yearly)
    'button_e'      

    ... and so on...
  end
end

因此,對於這些頁面中的每個按鈕,我必須處理2 * 4 = 8個條件。 Rails中是否有處理這種情況的良好模式? 謝謝

您可以在此處使用一些有趣的模式。 讓我們看看最常重復的內容: user.vip?

讓我們從Guard子句開始

def plan_action_button(user)
  return 'whatever you need to return' unless user.vip?

  if user.subscribing_plan?(:vip_lite, :monthly)
   'button_a'
[...]

然后,由於存在很多組合,因此很難說出這些按鈕的變化和保持不變。 是I18n鑰匙嗎? 按鈕標簽的鏈接是否不同? 您是否可以分別訪問user.plan_nameuser.plan_frequency等這些部分?

回答這些問題將有助於您發現一種模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM