Skip to content

Instantly share code, notes, and snippets.

@adelevie
Created May 19, 2010 21:32
Show Gist options
  • Save adelevie/406865 to your computer and use it in GitHub Desktop.
Save adelevie/406865 to your computer and use it in GitHub Desktop.
module ApplicationHelper
#...
def feature(name)
if Feature.exists?(:name => name)
Feature.find_by_name(name).deployed?
else
Feature.create(:name => name, :deployed => false)
false
end
end
#...
end
class FeaturesController < ApplicationController
before_filter :require_admin #this could be anything, just make sure only an admin can access this controller
def index
@features = Feature.find(:all)
#use whatever layout you want. I decided to stick all of this in the admin section of my application
render :layout => 'admin'
end
def update
if params[:feature]
Feature.update(params[:id], params[:feature])
end
#I modified routes.rb to make this url work
redirect_to '/admin/featues/index' #or redirect to an error page
end
end
<h1>Features</h1>
<table border="1">
<tr>
<td>name</td>
<td></td>
</tr>
<% @features.each do |feature| %>
<tr>
<% form_for feature, :url => '/admin/features/update' do |f| %>
<input type="hidden" name="id" value=<%= feature.id %> />
<td><%= feature.name %></td>
<% if feature.deployed %>
<input type="hidden" name="feature[deployed]" value="false" />
<td><input type="submit" value="rollback" /></td>
<% else %>
<input type="hidden" name="feature[deployed]" value="true" />
<td><input type="submit" value="deploy" /></td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
def index
@products = Product.find(:all)
if feature("Everything Must Go")
#take 90% off everything!
@products.each {|product| product.price = product.price*(0.10)}
end
end
<% if feature("Time Machine") %>
<%= render :partial => 'time_machine' %>
<% else %>
<p>Our time machine is still in development. Check back soon!</p>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment