class RailsBestPractices::Reviews::Review

A Review class that takes charge of reviewing one rails best practice.

Public Instance Methods

model_associations() click to toggle source

get the model associations from Prepares.

@return [Hash]

# File lib/rails_best_practices/reviews/review.rb, line 53
def model_associations
  @model_associations ||= Prepares.model_associations
end
model_attributes() click to toggle source

get the model attributes from Prepares.

@return [Hash]

# File lib/rails_best_practices/reviews/review.rb, line 60
def model_attributes
  @model_attributes ||= Prepares.model_attributes
end
models() click to toggle source

get the models from Prepares.

@return [Array]

# File lib/rails_best_practices/reviews/review.rb, line 46
def models
  @models ||= Prepares.models
end
remember_variable_use_count(node) click to toggle source

remember use count for the variable in the call or assign node.

find the variable in the call or assign node, then save it to as key in @variable_use_count hash, and add the call count (hash value).

# File lib/rails_best_practices/reviews/review.rb, line 14
def remember_variable_use_count(node)
  variable_node = variable(node)
  if variable_node && variable_node.to_s != 'self' && @last_variable_node != variable_node
    @last_variable_node = variable_node
    variable_use_count[variable_node.to_s] ||= 0
    variable_use_count[variable_node.to_s] += 1
  end
end
reset_variable_use_count() click to toggle source

reset @variable_use_count hash.

# File lib/rails_best_practices/reviews/review.rb, line 29
def reset_variable_use_count
  @variable_use_count = nil
end
variable(node) click to toggle source

find variable in the call or field node.

# File lib/rails_best_practices/reviews/review.rb, line 34
def variable(node)
  while %i[call field method_add_arg method_add_block].include?(node.receiver.sexp_type)
    node = node.receiver
  end
  return if %i[fcall hash].include?(node.receiver.sexp_type)

  node.receiver
end
variable_use_count() click to toggle source

return @variable_use_count hash.

# File lib/rails_best_practices/reviews/review.rb, line 24
def variable_use_count
  @variable_use_count ||= {}
end