class RailsBestPractices::Reviews::MoveFinderToNamedScopeReview

Review a controller file to make sure there are no complex finder.

See the best practice details here rails-bestpractices.com/posts/2010/07/14/move-finder-to-named_scope/

Implementation:

Review process:

check all method method_add_arg nodes in controller files.
if there is any call node with message find, all, first or last,
and it has a hash argument,
then it is a complex finder, and should be moved to model's named scope.

Constants

FINDERS

Private Instance Methods

finder?(node) click to toggle source

check if the method_add_arg node is a finder.

if the receiver of method_add_arg node is a constant, and the message of call method_add_arg is one of find, all, first or last, and any of its arguments is a hash, then it is a finder.

# File lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb, line 38
def finder?(node)
  FINDERS.include?(node[1].message.to_s) && node[1].sexp_type == :call &&
    node.arguments.grep_nodes_count(sexp_type: :bare_assoc_hash) > 0
end