Class: RailsBestPractices::Reviews::IsolateSeedDataReview

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

Overview

Make sure not to insert data in migration, move them to seed file.

See the best practice details here rails-bestpractices.com/posts/20-isolating-seed-data.

Implementation:

Review process:

1. check all assignment nodes,
if the right value is a call node with message "new",
then remember their left value as new variables.

2. check all call nodes,
if the message is "create" or "create!",
then it should be isolated to db seed.
if the message is "save" or "save!",
and the subject is included in new variables,
then it should be isolated to db seed.

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

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

#initializeIsolateSeedDataReview

Returns a new instance of IsolateSeedDataReview.



31
32
33
34
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 31

def initialize
  super
  @new_variables = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check

Instance Method Details

#start_assign(node) ⇒ Object

check assignment node.

if the right value of the node is a call node with “new” message, then remember it as new variables.



40
41
42
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 40

def start_assign(node)
  remember_new_variable(node)
end

#start_call(node) ⇒ Object

check the call node.

if the message of the call node is “create” or “create!”, then you should isolate it to seed data.

if the message of the call node is “save” or “save!”, and the subject of the call node is included in @new_variables, then you should isolate it to seed data.



52
53
54
55
56
57
58
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 52

def start_call(node)
  if ["create", "create!"].include? node.message.to_s
    add_error("isolate seed data")
  elsif ["save", "save!"].include? node.message.to_s
    add_error("isolate seed data") if new_record?(node)
  end
end

#urlObject



27
28
29
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 27

def url
  "http://rails-bestpractices.com/posts/20-isolating-seed-data"
end