Sha256: 105861014b4a302f201689c535047f227b9f04286f8c195959c43b7d63cb869b

Contents?: true

Size: 1.24 KB

Versions: 47

Compression:

Stored size: 1.24 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a model creation to make sure using model association.
    #
    # Implementation: 
    # 1. check :attrasgn, if xxx_id is assigned to a variable, set the value of the assigned variable to true.
    # 2. check :call, if call message :save and caller is included in variables, add error.
    class UseModelAssociationCheck < Check
      
      def interesting_nodes
        [:defn]
      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)
        if node.message.to_s =~ /_id=$/
          variable = node.subject[1]
          @variables[variable] = true
        end
      end
      
      def call_assignment(node)
        if node.message == :save
          variable = node.subject[1]
          add_error "use model association (for #{node.subject.to_ruby})" if @variables[variable]
        end
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
rails_best_practices-0.3.10 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.9 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.8 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.7 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.6 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.5 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.4 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.3 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.2 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.1 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.3.0 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.16 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.15 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.14 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.13 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.12 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.11 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.10 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.9 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.2.8 lib/rails_best_practices/checks/use_model_association_check.rb