lib/acl9/controller_extensions/dsl_base.rb in acl9-0.12.0 vs lib/acl9/controller_extensions/dsl_base.rb in acl9-0.12.1

- old
+ new

@@ -3,38 +3,27 @@ class Base attr_reader :allows, :denys def initialize(*args) @default_action = nil - @allows = [] - @denys = [] - + @denys = [] @original_args = args end def acl_block!(&acl_block) instance_eval(&acl_block) end def default_action - if @default_action.nil? then :deny else @default_action end + @default_action.nil? ? :deny : @default_action end def allowance_expression - allowed_expr = if @allows.size > 0 - @allows.map { |clause| "(#{clause})" }.join(' || ') - else - "false" - end + allowed_expr = @allows.any? ? @allows.map { |clause| "(#{clause})" }.join(' || ') : 'false' + not_denied_expr = @denys.any? ? @denys.map { |clause| "!(#{clause})" }.join(' && ') : 'true' - not_denied_expr = if @denys.size > 0 - @denys.map { |clause| "!(#{clause})" }.join(' && ') - else - "true" - end - [allowed_expr, not_denied_expr]. map { |expr| "(#{expr})" }. join(default_action == :deny ? ' && ' : ' || ') end @@ -80,17 +69,13 @@ raise ArgumentError, "You cannot use :to/:except inside actions block" if to || except end end subsidiary.acl_block!(&block) - action_check = _action_check_expression(args) + squash = lambda { |rules| action_check + ' && ' + _either_of(rules) } - squash = lambda do |rules| - _either_of(rules) + ' && ' + action_check - end - @allows << squash.call(subsidiary.allows) if subsidiary.allows.size > 0 @denys << squash.call(subsidiary.denys) if subsidiary.denys.size > 0 end alias action actions @@ -110,13 +95,13 @@ object = _role_object(options) role_checks = args.map do |who| case who - when anonymous() then "#{_subject_ref}.nil?" - when logged_in() then "!#{_subject_ref}.nil?" - when all() then "true" + when anonymous then "#{_subject_ref}.nil?" + when logged_in then "!#{_subject_ref}.nil?" + when all then "true" else "!#{_subject_ref}.nil? && #{_subject_ref}.has_role?('#{who.to_s.singularize}', #{object})" end end @@ -154,22 +139,16 @@ end def _set_action_clause(to, except) raise ArgumentError, "both :to and :except cannot be specified in the rule" if to && except - @action_clause = nil - - action_list = to || except + @action_clause = nil + action_list = to || except return unless action_list expr = _action_check_expression(action_list) - - @action_clause = if to - "#{expr}" - else - "!#{expr}" - end + @action_clause = to ? "#{expr}" : "!#{expr}" end def _action_check_expression(action_list) unless action_list.is_a?(Array) action_list = [ action_list.to_s ] @@ -197,15 +176,12 @@ object = options[prep.to_sym] end end case object - when Class - object.to_s - when Symbol - _object_ref object - when nil - "nil" + when Class then object.to_s + when Symbol then _object_ref object + when nil then "nil" else raise ArgumentError, "object specified by preposition can only be a Class or a Symbol" end end