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 local assignment and instance 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::CONTROLLER_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::NODE_TYPES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILE, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Attribute Summary

Attributes inherited from Core::Check

#errors

Instance Method Summary collapse

Methods inherited from Review

#equal?, #model_associations, #model_attributes, #models, #remember_variable_use_count, #reset_variable_use_count, #variable, #variable_use_count

Methods inherited from Core::Check

#add_error, #method_missing, #node_end, #node_start

Constructor Details

#initializeIsolateSeedDataReview

Returns a new instance of IsolateSeedDataReview.



36
37
38
39
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 36

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

#interesting_filesObject



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

def interesting_files
  MIGRATION_FILES
end

#interesting_nodesObject



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

def interesting_nodes
  [:call, :lasgn, :iasgn]
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.



65
66
67
68
69
70
71
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 65

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

#start_iasgn(node) ⇒ Object

check instance assignment node.

if the right value of the node is a call node with :new message, then remember it as new variables (@new_variables).



53
54
55
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 53

def start_iasgn(node)
  remember_new_variable(node)
end

#start_lasgn(node) ⇒ Object

check local assignment node.

if the right value of the node is a call node with :new message, then remember it as new variables (@new_variables).



45
46
47
# File 'lib/rails_best_practices/reviews/isolate_seed_data_review.rb', line 45

def start_lasgn(node)
  remember_new_variable(node)
end

#urlObject



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

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