Module: StoneWall::Helpers

Defined in:
lib/stonewall/helpers.rb

Class Method Summary collapse

Class Method Details

.build_method_names(original_method_name) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/stonewall/helpers.rb', line 29

def self.build_method_names(original_method_name)
  aliased_target, punctuation = original_method_name.to_s.sub(/([?!=])$/, ''), $1
  checked_method = "#{aliased_target}_with_stonewall#{punctuation}"
  unchecked_method = "#{aliased_target}_without_stonewall#{punctuation}"
  
  return checked_method, unchecked_method
end

.fix_alias_for(guarded_class, m) ⇒ Object


This is 1/3rd of the magic in this gem. We earlier built a 'schpoo_with_stonewall' method on your class, and now we use alias_method_chain to wrap your original 'schpoo' method. You will have no hope of understanding this if you don't understand alias_method_chain, so go memorize that documentation. We have to do this after ActoveRecord synthesizes the attribute methods with a call to 'define_attribute_methods'; you'll see some magic in the base.instance_eval in the other file to make that magic happen.



24
25
26
# File 'lib/stonewall/helpers.rb', line 24

def self.fix_alias_for(guarded_class, m)
  guarded_class.send(:alias_method_chain, m, :stonewall)
end

.fix_aliases_for(guarded_class) ⇒ Object



9
10
11
12
13
# File 'lib/stonewall/helpers.rb', line 9

def self.fix_aliases_for(guarded_class)
  guarded_class.stonewall.guarded_methods.each do |m|
    fix_alias_for(guarded_class, m)
  end
end

.guard_attribute(guarded_class, a) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/stonewall/helpers.rb', line 63

def self.guard_attribute(guarded_class, a)
  guarded_class.stonewall.guarded_attributes << a
  guard_method(guarded_class, a)
  guarded_class.stonewall.method_groups[:readers] << a
  
  setter = (a.to_s + "=").to_sym
  guard_method(guarded_class, setter)
  guarded_class.stonewall.method_groups[:writers] << setter
end

.guard_method(guarded_class, m) ⇒ Object

neither of these methods belong here - they seem like access controller things. but I won't resolve that until we can do a proper refactoring with tests.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/stonewall/helpers.rb', line 40

def self.guard_method(guarded_class, m)
   guarded_class.stonewall.guarded_methods << m  #put this line where it belongs, and
                                                #this method truly becomes a helper, doing nothing but
                                                # defining the guard.
                                                
  checked_method, unchecked_method = build_method_names(m)    
  
  # --------------
  # This method is defined on the guarded class, so it is callable on
  # objects of that class.  This is 1/3rd of the magic of this gem-
  # if you declare 'schpoo' a guarded method, we generate this
  # 'schpoo_with_stonewall' method. Elsewhere, we use alias_method_chain
  # to wrap your original 'schpoo' method.
  guarded_class.send(:define_method, checked_method) do |*args|
    if stonewall.allowed?(self, User.current, m)
      self.send(unchecked_method, *args)
    else
      raise Stonewall::AccessViolationException.new " \n User id: #{User.current.id}\n User Role Info: #{User.current.stonewall_role_info}\n Number of Roles for User: #{User.current.stonewall_role_info.length}\n Class: #{self.class.name}\n Object id: #{self.id}\n Method: #{checked_method}"
    end
  end
  # -------------- end of bizzaro meta-juju
end

.symbolize_role(role) ⇒ Object



4
5
6
# File 'lib/stonewall/helpers.rb', line 4

def self.symbolize_role(role)
  [String, Symbol].include?(role.class) ? role.to_sym : role.name.parameterize("_").downcase.to_sym
end