Class: RailsBestPractices::Reviews::UseQueryAttributeReview

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

Overview

Make sure to use query attribute instead of nil?, blank? and present?.

See the best practice details here rails-bestpractices.com/posts/56-use-query-attribute.

Implementation:

Review process:

check all method calls within conditional statements, like @user.login.nil?
if their subjects are one of the model names
and their messages of first call are not pluralize and not in any of the association names
and their messages of second call are one of nil?, blank?, present?, or they are == ""
then you can use query attribute instead.

Constant Summary collapse

QUERY_METHODS =
[:nil?, :blank?, :present?]

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, #interesting_files, #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_nodesObject



26
27
28
# File 'lib/rails_best_practices/reviews/use_query_attribute_review.rb', line 26

def interesting_nodes
  [:if]
end

#start_if(node) ⇒ Object

check if node to see whose conditional statement nodes contain nodes that can use query attribute instead.

it will check every call nodes in the if nodes. If the call node is

  1. two method calls, like @user.login.nil?

  2. the subject is one of the model names

  3. the message of first call is the model’s attribute, the message is not in any of associations name and is not pluralize

  4. the message of second call is one of nil?, blank? or present? or the message is == and the argument is “”

then the call node can use query attribute instead.



42
43
44
45
46
47
# File 'lib/rails_best_practices/reviews/use_query_attribute_review.rb', line 42

def start_if(node)
  if node = query_attribute_node(node.conditional_statement)
    subject_node = node.subject
    add_error "use query attribute (#{subject_node.subject}.#{subject_node.message}?)", node.file, node.line
  end
end

#urlObject



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

def url
  "http://rails-bestpractices.com/posts/56-use-query-attribute"
end