Sha256: 9e65d72eeeff6f52cd9f03760982d633547c97d109bb9fdb4f0f801a8cd3cf61

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8
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} 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

6 entries across 6 versions & 1 rubygems

Version Path
rails_best_practices-0.5.6 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
rails_best_practices-0.5.5 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
rails_best_practices-0.5.3 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
rails_best_practices-0.5.2 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
rails_best_practices-0.5.1 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
rails_best_practices-0.5.0 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb