Sha256: e0cae8066d953437b40ce7e0bf2b742d3257bba98d68b6f785c45561209793c5

Contents?: true

Size: 1.71 KB

Versions: 34

Compression:

Stored size: 1.71 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a controller to make sure adding a model virual attribute to simplify model creation.
    #
    # Implementation: check arguments of params#[]= before calling save, 
    # if they have duplicated arguments, then the model may need to add a model virtual attribute.
    class AddModelVirtualAttributeCheck < Check
      
      def interesting_nodes
        [:defn]
      end
      
      def interesting_files
        CONTROLLER_FILES
      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
        arguments_node = nil
        node.arguments.recursive_children do |child|
          if :[] == child.message
            arguments_node = child
            break
          end
        end
        return if variable.nil? or arguments_node.nil?
        @variables[variable] ||= []
        @variables[variable] << {:message => node.message, :arguments => arguments_node}
      end
      
      def call_assignment(node)
        if node.message == :save
          variable = node.subject
          add_error "add model virtual attribute (for #{node.subject.to_ruby})" if params_dup?(@variables[variable].collect {|h| h[:arguments]})
        end
      end
      
      def params_dup?(nodes)
        return false if nodes.nil?
        !nodes.dups.empty?
      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/add_model_virtual_attribute_check.rb
rails_best_practices-0.4.1 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.4.0 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.27 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.26 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.25 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.24 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.23 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.22 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.21 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.20 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.19 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.18 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.17 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.14 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.13 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.12 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb
rails_best_practices-0.3.11 lib/rails_best_practices/checks/add_model_virtual_attribute_check.rb