Class: RailsBestPractices::Reviews::AddModelVirtualAttributeReview

Inherits:
Review
  • Object
show all
Defined in:
lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb

Overview

Make sure to add a model virual attribute to simplify model creation.

See the best practice details here rails-bestpractices.com/posts/4-add-model-virtual-attribute

Implementation:

Review process:

check method define nodes in all controller files,
if there are more than one [] method calls with the same subject and arguments,
but assigned to one model's different attribute.
and after these method calls, there is a save method call for that model,
then the model needs to add a virtual attribute.

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 virtual attribute instead in review process.

it will check every attribute assignment nodes and call node of message :save or :save!, if

  1. there are more than one arguments who contain array reference node in the right value of assignment nodes,

  2. the messages of attribute assignment nodes housld be different (:first_name= , :last_name=)

  3. the argument of call nodes with message :[] should be same (:full_name)

  4. there should be a call node with message :save or :save! after attribute assignment nodes

  5. and the subject of save or save! call node should be the same with the subject of attribute assignment nodes

then the attribute assignment nodes can add model virtual attribute instead.



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

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

#urlObject



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

def url
  "http://rails-bestpractices.com/posts/4-add-model-virtual-attribute"
end