Class: RailsBestPractices::Reviews::UseBeforeFilterReview
- Inherits:
-
Review
- Object
- Core::Check
- Review
- RailsBestPractices::Reviews::UseBeforeFilterReview
- Defined in:
- lib/rails_best_practices/reviews/use_before_filter_review.rb
Overview
Review a controller file to make sure to use before_filter to remove duplicated first code line in different action.
See the best practice detailed here rails-bestpractices.com/posts/22-use-before_filter.
Implementation:
Review process:
check all first code line in method definitions (actions),
if they are duplicated, then they should be moved to before_filter.
Constant Summary
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
-
#initialize(options = {}) ⇒ UseBeforeFilterReview
constructor
A new instance of UseBeforeFilterReview.
-
#start_class(node) ⇒ Object
check class define node to see if there are method define nodes whose first code line are duplicated.
- #url ⇒ Object
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!, #interesting_files, interesting_files, interesting_nodes, #interesting_nodes, #method_missing, #node_end, #node_start, #parse_file?, #result, #total_files_checked
Constructor Details
#initialize(options = {}) ⇒ UseBeforeFilterReview
Returns a new instance of UseBeforeFilterReview.
23 24 25 26 |
# File 'lib/rails_best_practices/reviews/use_before_filter_review.rb', line 23 def initialize( = {}) super() @customize_count = ['customize_count'] || 2 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check
Instance Method Details
#start_class(node) ⇒ Object
check class define node to see if there are method define nodes whose first code line are duplicated.
it will check every def nodes in the class node until protected or private identification, if there are defn nodes who have the same first code line, then these duplicated first code lines should be moved to before_filter.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rails_best_practices/reviews/use_before_filter_review.rb', line 33 def start_class(node) @first_sentences = {} node.body.statements.each do |statement_node| break if [:var_ref, :vcall].include?(statement_node.sexp_type) && ["protected", "private"].include?(statement_node.to_s) remember_first_sentence(statement_node) if :def == statement_node.sexp_type end @first_sentences.each do |first_sentence, def_nodes| if def_nodes.size > @customize_count add_error "use before_filter for #{def_nodes.map { |node| node.method_name.to_s }.join(',')}", node.file, def_nodes.map(&:line).join(',') end end end |
#url ⇒ Object
19 20 21 |
# File 'lib/rails_best_practices/reviews/use_before_filter_review.rb', line 19 def url "http://rails-bestpractices.com/posts/22-use-before_filter" end |