0

我正在實施「功能開關」,以便管理員可以使用管理Web界面打開和關閉新功能。如何在使用資產管道時爲導軌中的功能開關創建「條件資產」?

我能夠很容易地與這樣如

- if FeatureSwitch.where(name: 'display_demo_feature_switch_image', status: 'on').count > 0 
    # in reality would refactor that query to the controller/model. put here for clarity. 
    %br 
    %br 
    %div{class: "onoffswitch"} 
    %input{type: "checkbox", name: "onoffswitch", class: "onoffswitch-checkbox", id: "myonoffswitch"} 
    %label{class: "onoffswitch-label", for: "myonoffswitch"} 
     %span{class: "onoffswitch-inner"} 
     %span{class: "onoffswitch-switch"} 

但我怎麼能做到這一點的看法時,我使用的管道資產,使資產是「有條件」包括這樣做的看法?

所以,如果我有一個CSS資產清單(app/assets/stylesheets/application.css)有:

/* 
*= require_self 
*= require main 
*= require default 
*= require on_off_switch 
*= require jquery-ui-1.8.22.custom.css 
*/ 

我怎樣才能讓require on_off_switch 「有條件的」,這取決於從

FeatureSwitch 
    .where(name: 'display_demo_feature_switch_image', status: 'on').count > 0 

我真/假返回嘗試重命名爲application.cssapplication.css.erb並使用

<% if 'abc' == 'def' %> # While developing/testing 
*= require on_off_switch 
<% end %> 

但資產始終得到包括儘管'abc' == 'def'是假的......

回答

0

一個可能的解決方案可以創建兩個體現,例如說on.css和off.css

在控制器中添加before_filter與:

def my_before_filter_name 
    @count = FeatureSwitch.where(name: 'display_demo_feature_switch_image', status: 'on').count 
end 

順便說一句,避免在您的意見中使用活動記錄。

在您的佈局:

- if @count > 0 
    = stylesheet_link_tag 'on.css' 
- else 
    = stylesheet_link_tag 'off.css' 

請注意,您可能需要測試是否存在@count。

如果這種解決方案對您來說不是最佳選擇,您可以使用content_for。在您的佈局產量:ON_OFF並在視圖中添加content_for:ON_OFF和測試@count到stylesheet_link_tag「on_off_switch」(更多細節rails documentation

0

的解決方案似乎是剛剛上面放的條件在

app/views/layout/application.html.haml # (I use haml instead of erb) 

= stylesheet_link_tag 'application' 
- if FeatureSwitch.where(name: 'display_demo_feature_switch_image', status: 'on').count > 0 
    = stylesheet_link_tag 'on_off_switch' 

不過雖然在發展工作並沒有在生產wortk和樣式表將不包括在內。