Module: Kantox::Strategies

Defined in:
lib/kantox/roles/strategies/aspect.rb,
lib/kantox/roles/strategies/pundit.rb,
lib/kantox/roles/strategies/wrapper.rb,
lib/kantox/roles/strategies/cancancan.rb,
lib/kantox/roles/strategies/strategy_error.rb

Defined Under Namespace

Modules: AbilityFactory, PolicyFactory, Wrapper Classes: AspectError, CanCanCanError, PunditError, StrategyError

Class Method Summary collapse

Class Method Details

.aspect(context, im, *args) ⇒ Object



6
7
8
9
10
# File 'lib/kantox/roles/strategies/aspect.rb', line 6

def aspect context, im, *args
  # fail AspectError.new(context, im)
  Kantox::Helpers.info "Aspect strategy applyed to #{context}"
  Kantox::Helpers.warn 'Aspect strategy does not expect block passed.' if block_given?
end

.cancancan(context, im, *args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kantox/roles/strategies/cancancan.rb', line 29

def cancancan context, im, *args
  Kantox::Helpers.debug "CanCanCaning object: [#{context}], «#{im}»"
  begin # Whoever does not reply on classify would be punished :)
    model = const_defined?('Rails') ?
      context.instance_eval('controller_path.classify.singularize') :
      context.class.name
    ability = AbilityFactory.lookup model.split('::').last, im.split('#').last
    user = Kernel.const_defined?('User') && User.respond_to?(:current) ?
      User.current :
      (context.respond_to?(:current_user) ? context.current_user : '@fixme')
    fail CanCanCanError.new(context, im, ability) unless ability.new(user, '@todo').can *im.split('#')
  rescue NameError => e
    Kantox::Helpers.err "Error cancancaning «#{context}». Will reject request.\nOriginal error: #{e}"
    throw CanCanCanError.new(context, im)
  end
  Kantox::Helpers.warn "CanCanCan strategy does not expect block passed.\nFailed: «#{ability}»" if block_given?
end

.pundit(context, im, *args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kantox/roles/strategies/pundit.rb', line 48

def pundit context, im, *args
  context_as_string = "#{context}"
  context_as_string = "#{context_as_string[0..58]} [...]" if context_as_string.length > 65
  Kantox::Helpers.debug "Punditing object: [#{context_as_string}], «#{im}»"
  begin # Whoever does not reply on classify would be punished :)
    model = const_defined?('Rails') && context.respond_to?(:controller_path) ?
      context.instance_eval('controller_path.classify.singularize') :
      context.class.name
    model << 's' if model[-5..-1] == 'Statu' # Fucking Rails
    policy = PolicyFactory.lookup model.split('::')[2..-1].join('::')
    user = Kernel.const_defined?('User') && User.respond_to?(:current) ?
      User.current :
      (context.respond_to?(:current_user) ? context.current_user : nil)
    fail PunditError.new(context, im, policy, user) unless policy.new(user, context).send "#{im.split('#').last}?"
  rescue NameError => e
    Kantox::Helpers.err "Error punditing «#{context.class}». Will reject request.\nOriginal error: #{e}"
    throw PunditError.new(context, im)
  end
  Kantox::Helpers.warn "Pundit strategy does not expect block passed.\nFailed: «#{policy}»" if block_given?
end