module Contexts

Constants

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/contexts.rb, line 9
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
resolve(name) click to toggle source
# File lib/contexts.rb, line 5
def self.resolve(name)
  "#{name}_context".classify.constantize.new
end

Public Instance Methods

context_locked?(name = nil) click to toggle source
# File lib/contexts.rb, line 51
def context_locked?(name = nil)
  locked_context(name).present?
end
contexts() click to toggle source
# File lib/contexts.rb, line 33
def contexts
  @contexts ||= (request.env['contexts'] || {})
end
current_context(name = nil) click to toggle source
# File lib/contexts.rb, line 37
def current_context(name = nil)
  if name
    (ctx = contexts[name]) && ctx.current
  else
    Hash[contexts.map{ |name, ctx| [ name, ctx.current ] }]
  end
end
lock_context(data) click to toggle source
# File lib/contexts.rb, line 55
def lock_context(data)
  ctx = locked_context
  ctx.merge!(data.stringify_keys).reject!{ |k, v| v.blank? }
  ctx
end
locked_context(name = nil) click to toggle source
# File lib/contexts.rb, line 45
def locked_context(name = nil)
  locked = (session[:locked_context] ||= {})

  name ? locked[name.to_s] : locked
end

Protected Instance Methods

apply_contexts() click to toggle source
# File lib/contexts.rb, line 63
def apply_contexts
  contexts.each{ |name, ctx| ctx.apply(self, locked_context(name)) }
end
default_url_options() click to toggle source
# File lib/contexts.rb, line 67
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