Class: RailsBestPractices::Reviews::UseModelAssociationReview

Inherits:
Review show all
Defined in:
lib/rails_best_practices/reviews/use_model_association_review.rb

Overview

review a controller file to make sure to use model association instead of foreign key id assignment.

See the best practice details here rails-bestpractices.com/posts/2-use-model-association.

Implementation:

Review process:

check model define nodes in all controller files,
if there is an attribute assignment node with message xxx_id=,
and after it, there is a call node with message "save" or "save!",
and the subjects of attribute assignment node and call node are the same,
then model association should be used instead of xxx_id assignment.

Constant Summary

Constants inherited from Core::Check

Core::Check::ALL_FILES, Core::Check::CONTROLLER_FILES, Core::Check::DEPLOY_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILES, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Method Summary collapse

Methods inherited from Review

#model_associations, #model_attributes, #models, #remember_variable_use_count, #reset_variable_use_count, #variable, #variable_use_count

Methods inherited from Core::Check

add_callback, #add_error, #after_prepare, #after_review, callbacks, #errors, #increment_total_files_checked!, #initialize, interesting_files, #interesting_files, #interesting_nodes, interesting_nodes, #method_missing, #node_end, #node_start, #parse_file?, #result, #total_files_checked

Constructor Details

This class inherits a constructor from RailsBestPractices::Core::Check

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check

Instance Method Details

#start_def(node) ⇒ Object

check method define nodes to see if there are some attribute assignments that can use model association instead.

it will check attribute assignment node with message xxx_id=, and call node with message “save” or “save!”

  1. if there is an attribute assignment node with message xxx_id=, then remember the subject of attribute assignment node.

  2. after assignment, if there is a call node with message “save” or “save!”, and the subject of call node is one of the subject of attribute assignment node, then the attribute assignment should be replaced by using model association.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rails_best_practices/reviews/use_model_association_review.rb', line 35

def start_def(node)
  @assignments = {}
  node.recursive_children do |child|
    case child.sexp_type
    when :assign
      attribute_assignment(child)
    when :call
      call_assignment(child)
    else
    end
  end
  @assignments = nil
end

#urlObject



22
23
24
# File 'lib/rails_best_practices/reviews/use_model_association_review.rb', line 22

def url
  "http://rails-bestpractices.com/posts/2-use-model-association"
end