Sha256: 6143e83078583f0e2f77e66b8d805f7f19b11af39865b75c32f1d8c19efcb00c

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 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.rb$/
      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
        if @variables.values.any? { |count| count > @called_count }
          add_error "move model logic into model"
        end
        @variables = nil
      end

      private
      
      def call_node(node)
        variable = node.subject
        return if variable.nil?
        @variables[variable] ||= 0
        @variables[variable] += 1
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_best_practices-0.1.2 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
rails_best_practices-0.1.1 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb
rails_best_practices-0.1.0 lib/rails_best_practices/checks/move_model_logic_into_model_check.rb