Browse files

first commit

  • Loading branch information...
0 parents commit 160da0e549cce9441af3a448e3a75083473f02a0 @kbrock committed Sep 18, 2009
34 app/controllers/iron_gate/gate_controller.rb
@@ -0,0 +1,34 @@
+class IronGate::GateController < ApplicationController
+
+ unloadable
+
+ skip_before_filter :deny_not_nice
+
+ def index
+ nice=params[:nice]=='true'
+ make_nice(nice)
+ flash[:notice]="you should #{nice ? 'now' : 'not'} see the site"
+ redirect_to '/'
+ end
+
+ def open
+ params[:nice]='true'
+ index
+ end
+
+ def close
+ params[:nice]='false'
+ index
+ end
+
+ private
+
+ def make_nice(val='true')
+ val='false' if val==false
+ val='true' if val==true
+ #TODO: make the cookie persistant
+ #TODO: could add ip address, or something to make more secure
+ cookies['nice']=val
+ logger.info("set cookie to #{val}")
+ end
+end
1 init.rb
@@ -0,0 +1 @@
+require 'iron_gate'
10 lib/iron_gate.rb
@@ -0,0 +1,10 @@
+module IronGate
+end
+
+#require 'iron_gate/extensions/routes'
+require 'iron_gate/acts_as_gated'
+require 'iron_gate/closed'
+
+ActionController::Base.send :include, IronGate::ActsAsGated
+
+
45 lib/iron_gate/acts_as_gated.rb
@@ -0,0 +1,45 @@
+module IronGate
+ module ActsAsGated
+ def self.included(base) # :nodoc:
+ base.extend ClassMethods
+ base.send :include,SingletonMethods
+ end
+ end
+
+ module ClassMethods
+ def acts_as_gated(options = {})
+ #metaclass = (class << self; self; end)
+
+ template = options.delete(:template)||'shared/gate_closed'
+ gate_name = options.delete(:gate_name)
+ cattr_accessor :iron_gate_closed_template
+ self.iron_gate_closed_template=template
+
+ self.send :before_filter, :deny_not_nice, options
+ self.send :rescue_from, IronGate::Closed, :with => :render_iron_gate_closed
+
+ #gate_name specifies the route name
+ #e.g. :gate_name => 'green' means green/open starts things and green/closed stops
+
+ #adding directly to the config files would be better (would show up in rake routes)
+ #but want the gate name to be custom per application. (security by obscurity)
+ if gate_name
+ ActionController::Routing::Routes.draw do |map|
+ map.resource gate_name,
+ :controller => 'iron_gate/gate',
+ :only => nil, :collection => %w(open close)
+ end
+ end
+ end
+ end
+
+ module SingletonMethods
+ def render_iron_gate_closed
+ render :template => self.iron_gate_closed_template, :layout => false
+ end
+
+ def deny_not_nice
+ raise IronGate::Closed if cookies['nice']!='true'
+ end
+ end
+end
4 lib/iron_gate/closed.rb
@@ -0,0 +1,4 @@
+module IronGate
+ class Closed < Exception
+ end
+end
14 lib/iron_gate/extensions/routes.rb
@@ -0,0 +1,14 @@
+if defined?(ActionController::Routing::RouteSet) && defined?(Rails) #&& !Rails.env.production?
+ class ActionController::Routing::RouteSet
+ def load_routes_with_iron_gate!
+ lib_path = File.dirname(__FILE__)
+ iron_gate_routes = File.join(lib_path, *%w[.. .. .. config iron_gate_routes.rb])
+ unless configuration_files.include?(iron_gate_routes)
+ add_configuration_file(iron_gate_routes)
+ end
+ load_routes_without_iron_gate!
+ end
+
+ alias_method_chain :load_routes!, :iron_gate
+ end
+end

0 comments on commit 160da0e

Please sign in to comment.