lib/contexts.rb in contexts-1.1.0 vs lib/contexts.rb in contexts-2.0.0
- old
+ new
@@ -1,15 +1,11 @@
require 'contexts/version'
require 'mapper'
module Contexts
def self.resolve(name)
- if name.is_a?(Class)
- name.new
- else
- "#{name}_context".classify.constantize.new
- end
+ "#{name}_context".classify.constantize.new
end
def self.included(base)
base.class_eval do
extend ClassMethods
@@ -27,32 +23,35 @@
def preload_contexts(*names)
prepend_before_action do
names.each{ |name| contexts[name] ||= Contexts.resolve(name) }
end
end
+
+ alias_method :context, :preload_contexts
+ alias_method :contexts, :preload_contexts
end
def contexts
@contexts ||= (request.env['contexts'] || {})
end
- def current_context(key = nil)
- if key
- (ctx = contexts[key]) && ctx.current
+ def current_context(name = nil)
+ if name
+ (ctx = contexts[name]) && ctx.current
else
Hash[contexts.map{ |name, ctx| [ name, ctx.current ] }]
end
end
- def locked_context(key = nil)
- ctx = (session[:locked_context] ||= {})
+ def locked_context(name = nil)
+ locked = (session[:locked_context] ||= {})
- key ? ctx[key.to_s] : ctx
+ name ? locked[name.to_s] : locked
end
- def context_locked?(key = nil)
- locked_context(key).present?
+ def context_locked?(name = nil)
+ locked_context(name).present?
end
def lock_context(data)
ctx = locked_context
ctx.merge!(data.stringify_keys).reject!{ |k, v| v.blank? }
@@ -60,16 +59,16 @@
end
protected
def apply_contexts
- contexts.each{ |name, ctx| ctx.apply(self, locked_context(ctx.key)) }
+ contexts.each{ |name, ctx| ctx.apply(self, locked_context(name)) }
end
def default_url_options
contexts.inject({}) do |options, (name, ctx)|
option = ctx.respond_to?(:url_option) ? ctx.url_option : ctx.current
- options[ctx.key] = option
+ options[name] = option
options
end
end
end