forked from tannerburson/sinatra-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1f1b8c7
Showing
6 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'sinatra/base' | ||
|
||
module Sinatra | ||
module MethodAbsorber | ||
class Absorber | ||
instance_methods.each { |m| undef_method m unless m =~ /^__/ } | ||
def method_missing(*args) | ||
'' | ||
end | ||
end | ||
|
||
def e(obj) | ||
obj || Absorber.new | ||
end | ||
end | ||
helpers MethodAbsorber | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'sinatra/base' | ||
|
||
module Sinatra | ||
module NestedLayouts | ||
module Helpers | ||
|
||
def erb_nested_layout(page,options={}) | ||
layout_path = begin | ||
(class<<self;self;end).layout_name | ||
rescue NoMethodError | ||
options[:layout] | ||
end | ||
unless layout_path | ||
layout_path = '../layout' | ||
end | ||
if options[:layout] != false | ||
options.merge!(:layout => layout_path.to_sym) | ||
end | ||
old_erb page, options | ||
end | ||
|
||
end | ||
|
||
def self.registered(app) | ||
# alias the new erb helper | ||
app.helpers NestedLayouts::Helpers | ||
app.send(:alias_method,:old_erb, :erb) | ||
app.send(:alias_method,:erb, :erb_nested_layout) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
module Rack | ||
class Flash | ||
# Raised when the session passed to FlashHash initialize is nil. This | ||
# is usually an indicator that session middleware is not in use. | ||
class SessionUnavailable < StandardError; end | ||
|
||
# Implements bracket accessors for storing and retrieving flash entries. | ||
class FlashHash | ||
attr_reader :flagged | ||
|
||
def initialize(store, opts={}) | ||
raise Rack::Flash::SessionUnavailable \ | ||
.new('Rack::Flash depends on session middleware.') unless store | ||
|
||
@opts = opts | ||
@store = store | ||
@store[:__FLASH__] ||= {} | ||
|
||
if accessors = @opts[:accessorize] | ||
accessors.each { |opt| def_accessor(opt) } | ||
end | ||
end | ||
|
||
# Remove an entry from the session and return its value. Cache result in | ||
# the instance cache. | ||
def [](key) | ||
key = key.to_sym | ||
cache[key] ||= values.delete(key) | ||
end | ||
|
||
# Store the entry in the session, updating the instance cache as well. | ||
def []=(key,val) | ||
key = key.to_sym | ||
cache[key] = values[key] = val | ||
end | ||
|
||
# Store a flash entry for only the current request, swept regardless of | ||
# whether or not it was actually accessed. Useful for AJAX requests, where | ||
# you want a flash message, even though you're response isn't redirecting. | ||
def now | ||
cache | ||
end | ||
|
||
# Checks for the presence of a flash entry without retrieving or removing | ||
# it from the cache or store. | ||
def has?(key) | ||
[cache, values].any? { |store| store.keys.include?(key.to_sym) } | ||
end | ||
alias_method :include?, :has? | ||
|
||
# Mark existing entries to allow for sweeping. | ||
def flag! | ||
@flagged = values.keys | ||
end | ||
|
||
# Remove flagged entries from flash session, clear flagged list. | ||
def sweep! | ||
Array(flagged).each { |key| values.delete(key) } | ||
flagged.clear | ||
end | ||
|
||
# Hide the underlying :__FLASH__ session key and only expose values stored | ||
# in the flash. | ||
def inspect | ||
'#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect] | ||
end | ||
|
||
# Human readable for logging. | ||
def to_s | ||
values.inspect | ||
end | ||
|
||
private | ||
|
||
# Maintain an instance-level cache of retrieved flash entries. These | ||
# entries will have been removed from the session, but are still available | ||
# through the cache. | ||
def cache | ||
@cache ||= {} | ||
end | ||
|
||
# Helper to access flash entries from :__FLASH__ session value. This key | ||
# is used to prevent collisions with other user-defined session values. | ||
def values | ||
@store[:__FLASH__] | ||
end | ||
|
||
# Generate accessor methods for the given entry key if :accessorize is true. | ||
def def_accessor(key) | ||
raise ArgumentError.new('Invalid entry type: %s' % key) if respond_to?(key) | ||
|
||
class << self; self end.class_eval do | ||
define_method(key) { |*args| val = args.first; val ? (self[key]=val) : self[key] } | ||
define_method("#{key}=") { |val| self[key] = val } | ||
define_method("#{key}!") { |val| cache[key] = val } | ||
end | ||
end | ||
end | ||
|
||
# ------------------------------------------------------------------------- | ||
# - Rack Middleware implementation | ||
|
||
def initialize(app, opts={}) | ||
if defined?(Sinatra::Base) | ||
Sinatra::Base.class_eval do | ||
def flash; env['rack-flash'] end | ||
end | ||
end | ||
|
||
@app, @opts = app, opts | ||
end | ||
|
||
def call(env) | ||
env['rack-flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts) | ||
|
||
if @opts[:sweep] | ||
env['rack-flash'].flag! | ||
end | ||
|
||
res = @app.call(env) | ||
|
||
if @opts[:sweep] | ||
env['rack-flash'].sweep! | ||
end | ||
|
||
res | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'rack/request' | ||
|
||
module Rack | ||
class Request | ||
def subdomains | ||
@env['rack.env.subdomains'] ||= lambda { | ||
return [] if (host.nil? || | ||
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) | ||
host.split('.')[0...-2] | ||
}.call | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'sinatra/base' | ||
|
||
module Sinatra | ||
module RenderPartial | ||
def partial(page, options={}) | ||
erb page, options.merge!(:layout => false) | ||
end | ||
end | ||
|
||
helpers RenderPartial | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Sinatra | ||
module SessionAuth | ||
|
||
def current_user | ||
self.options.user_model.get! session[:user] | ||
end | ||
|
||
def login_required | ||
if session[:user] | ||
return true | ||
else | ||
session[:return_to] = request.fullpath | ||
redirect '/login' | ||
return false | ||
end | ||
end | ||
|
||
def redirect_to_stored | ||
if return_to = session[:return_to] | ||
session[:return_to] = nil | ||
redirect return_to | ||
else | ||
redirect '/' | ||
end | ||
end | ||
|
||
def logout_user | ||
session[:user] = nil | ||
end | ||
|
||
end | ||
end |