Sha256: 3f946251c231a57f984e84622d1a1ad01e466e518ff88333ecae2b68453d363e

Contents?: true

Size: 1.2 KB

Versions: 29

Compression:

Stored size: 1.2 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_FILES
      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.node_type == :call and (call_node.message == :current_user or (call_node.subject.message == :current_user and call_node.message == :id))
      end
      
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
rails_best_practices-0.4.2 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.4.1 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.4.0 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.27 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.26 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.25 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.24 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.23 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.22 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.21 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.20 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.19 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.18 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.17 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.14 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.13 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.12 lib/rails_best_practices/checks/use_scope_access_check.rb
rails_best_practices-0.3.11 lib/rails_best_practices/checks/use_scope_access_check.rb