Sha256: 6d5b7b9bd54583382e8695ee8d03c9a6a4fc7cbb260536fb8c55a991ae2dbe3f

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a controller to make sure using scope access
    #
    # Implementation: simply check if or unless compare with current_user or current_user.id and there is a redirect_to message in if or unless block
    class UseScopeAccessCheck < Check
      
      def interesting_nodes
        [:if, :unless]
      end
      
      def interesting_files
        /_controller.rb$/
      end
      
      def evaluate_start(node)
        add_error "use scope access" if current_user_redirect?(node)
      end
      
      private
      
      def current_user_redirect?(node)
        condition_node = node.call
        
        condition_node.message == :== and 
        (current_user?(condition_node.arguments.call) or current_user?(condition_node.subject)) and 
        (node.false_node.body.any? {|n| n.message == :redirect_to} or node.true_node.method_body.any? {|n| n.message == :redirect_to})
      end
      
      def current_user?(call_node)
        call_node.message == :current_user or (call_node.subject.message == :current_user and call_node.message == :id)
      end
      
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rails_best_practices-0.2.13 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.2.12 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.2.11 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.2.10 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.2.9 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.2.8 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.2.6 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.2.5 lib/rails_best_practices/checks/use_scope_access_check.rb