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 =
%w(nil? blank? present?)

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_if(node) ⇒ Object Also known as: start_unless, start_elsif

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.



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

def start_if(node)
  all_conditions = node.conditional_statement == node.conditional_statement.all_conditions ? [node.conditional_statement] : node.conditional_statement.all_conditions
  all_conditions.each do |condition_node|
    if query_attribute_node = query_attribute_node(condition_node)
      subject_node = query_attribute_node.subject
      add_error "use query attribute (#{subject_node.subject}.#{subject_node.message}?)", node.file, query_attribute_node.line
    end
  end
end

#urlObject



24
25
26
# File 'lib/rails_best_practices/reviews/use_query_attribute_review.rb', line 24

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