Sha256: 8b6e860fb5a183ecbc56e2c416c67570078c02bb29d1a677a6a6be219ca08a12

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

# encoding: utf-8
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})" if @variables[variable]
        end
      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/use_model_association_check.rb
rails_best_practices-0.5.5 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.5.3 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.5.2 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.5.1 lib/rails_best_practices/checks/use_model_association_check.rb
rails_best_practices-0.5.0 lib/rails_best_practices/checks/use_model_association_check.rb