Sha256: e46f132b9ea5e3bd2a15647fa3cfb87b08ad4ed03f896e9be37ef2136a76122c

Contents?: true

Size: 1.32 KB

Versions: 34

Compression:

Stored size: 1.32 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a controller file to make sure that model logic should not exist in controller, move it into a model.
    #
    # Implementation: check the count of method calling of a model, 
    # if it is more than defined called count, then it contains model logic.
    class MoveModelLogicIntoModelCheck < Check
      
      def interesting_nodes
        [:defn]
      end
      
      def interesting_files
        CONTROLLER_FILES
      end

      def initialize(options = {})
        super()
        @called_count = options['called_count'] || 4
      end

      def evaluate_start(node)
        @variables = {}
        node.recursive_children do |child|
          case child.node_type
          when :attrasgn, :call
            call_node(child)
          end
        end
        
        @variables.each do |variable, count|
          add_error "move model logic into model (#{variable.to_ruby} called_count > #{@called_count})" if count > @called_count
        end
        @variables = nil
      end

      private
      
      def call_node(node)
        variable = node.subject
        return if variable.nil? or ![:lvar, :ivar].include? node.subject.node_type
        @variables[variable] ||= 0
        @variables[variable] += 1
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

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