Sha256: 619155aa6e985130a17485810c4d1aff0920f79de6699eff9d5bf8ec8fef055c
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
require 'contexts/version' require 'mapper' module Contexts def self.resolve(name) "#{name}_context".classify.constantize.new end def self.included(base) base.class_eval do extend ClassMethods before_action :apply_contexts helper_method :contexts, :current_context, :locked_context, :context_locked? end end module ClassMethods 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(name = nil) if name (ctx = contexts[name]) && ctx.current else Hash[contexts.map{ |name, ctx| [ name, ctx.current ] }] end end def locked_context(name = nil) locked = (session[:locked_context] ||= {}) name ? locked[name.to_s] : locked end 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? } ctx end protected def apply_contexts 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[name] = option options end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
contexts-2.0.0 | lib/contexts.rb |