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, like

    def create
      @user = User.new(params[:user])
      @user.first_name = params[:full_name].split(' ', 2).first
      @user.last_name = params[:full_name].split(' ', 2).last
      @user.save
    end

then the model needs to add a virtual attribute.

Constant Summary

Constants inherited from Core::Check

Core::Check::CONTROLLER_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::NODE_TYPES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILE, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Attribute Summary

Attributes inherited from Core::Check

#errors

Instance Method Summary collapse

Methods inherited from Review

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

Methods inherited from Core::Check

#add_error, #initialize, #method_missing, #node_end, #node_start

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

#interesting_filesObject



35
36
37
# File 'lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb', line 35

def interesting_files
  CONTROLLER_FILES
end

#interesting_nodesObject



31
32
33
# File 'lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb', line 31

def interesting_nodes
  [:defn]
end

#start_defn(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 call node with messages :[] in attribute assignment nodes, e.g.

    @user.first_name = params[:full_name].split(' ').first
    @user.last_name = params[:full_name].split(' ').last
    
  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

    @user.save
    
  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.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb', line 53

def start_defn(node)
  @attrasgns = {}
  node.recursive_children do |child|
    case child.node_type
    when :attrasgn
      attribute_assignment(child)
    when :call
      call_assignment(child)
    else
    end
  end
end

#urlObject



27
28
29
# File 'lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb', line 27

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