Class: Authorization::ObligationScope

Inherits:
ActiveRecord::NamedScope::Scope
  • Object
show all
Defined in:
lib/declarative_authorization/obligation_scope.rb

Overview

The ObligationScope class parses any number of obligations into joins and conditions.

In ObligationScope parlance, “association paths” are one-dimensional arrays in which each element represents an attribute or association (or “step”), and “leads” to the next step in the association path.

Suppose we have this path defined in the context of model Foo: { :bar => { :baz => { :foo => { :attr => is { user } } } } }

To parse this path, ObligationScope evaluates each step in the context of the preceding step. The first step is evaluated in the context of the parent scope, the second step is evaluated in the context of the first, and so forth. Every time we encounter a step representing an association, we make note of the fact by storing the path (up to that point), assigning it a table alias intended to match the one that will eventually be chosen by ActiveRecord when executing the find method on the scope.

+@table_aliases =

[] => 'foos',
[:bar] => 'bars',
[:bar, :baz] => 'bazzes',
[:bar, :baz, :foo] => 'foos_bazzes' # Alias avoids collisions with 'foos' (already used)

+

At the “end” of each path, we expect to find a comparison operation of some kind, generally comparing an attribute of the most recent association with some other value (such as an ID, constant, or array of values). When we encounter a step representing a comparison, we make note of the fact by storing the path (up to that point) and the comparison operation together. (Note that individual obligations’ conditions are kept separate, to allow their conditions to be OR’ed together in the generated scope options.)

+@obligation_conditions[[:bar, :baz, :foo]] = [

[ :attr, :is, <user.id> ]

]+

After successfully parsing an obligation, all of the stored paths and conditions are converted into scope options (stored in proxy_options as :joins and :conditions). The resulting scope may then be used to find all scoped objects for which at least one of the parsed obligations is fully met.

@proxy_options = { :bar => { :baz => :foo } } @proxy_options = [ ‘foos_bazzes.attr = :foos_bazzes__id_0’, { :foos_bazzes__id_0 => 1 } ]

Instance Method Summary collapse

Instance Method Details

#parse!(obligation) ⇒ Object

Consumes the given obligation, converting it into scope join and condition options.



47
48
49
50
51
52
53
54
# File 'lib/declarative_authorization/obligation_scope.rb', line 47

def parse!( obligation )
  @current_obligation = obligation
  obligation_conditions[@current_obligation] ||= {}
  follow_path( obligation )

  rebuild_condition_options!
  rebuild_join_options!
end