Sha256: 9557c47f4f146e24d1bdd3d673b7cec830da2341258e6500c20531217cc39c1e

Contents?: true

Size: 1.62 KB

Versions: 34

Compression:

Stored size: 1.62 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a controller file to make sure that complex model creation should not exist in controller, move it to model factory method
    #
    # Implementation: check the count of variable attribute assignment calling before saving, 
    # if more than defined attribute assignment count, then it's a complex creation.
    class ReplaceComplexCreationWithFactoryMethodCheck < Check
      
      def interesting_nodes
        [:defn]
      end
      
      def interesting_files
        CONTROLLER_FILES
      end
      
      def initialize(options = {})
        super()
        @attrasgn_count = options['attribute_assignment_count'] || 2
      end
      
      def evaluate_start(node)
        @variables = {}
        node.recursive_children do |child|
          case child.node_type
          when :attrasgn
            attribute_assignment(child)
          when :call
            call_assignment(child)
          else
          end
        end
        @variables = nil
      end
      
      private
      
      def attribute_assignment(node)
        variable = node.subject
        return if variable.nil? or ![:lvar, :ivar].include? node.subject.node_type
        @variables[variable] ||= 0
        @variables[variable] += 1
      end
      
      def call_assignment(node)
        if node.message == :save
          variable = node.subject
          add_error "replace complex creation with factory method (#{variable.to_ruby} attribute_assignment_count > #{@attrasgn_count})" if @variables[variable] > @attrasgn_count
        end
      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/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.4.1 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.4.0 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.27 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.26 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.25 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.24 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.23 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.22 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.21 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.20 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.19 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.18 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.17 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.14 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.13 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.12 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
rails_best_practices-0.3.11 lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb