class RailsBestPractices::Reviews::IsolateSeedDataReview
Make sure not to insert data in migration, move them to seed file.
See the best practice details here rails-bestpractices.com/posts/2010/07/24/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 receiver is included in new variables, then it should be isolated to db seed.
Public Class Methods
new(options = {})
click to toggle source
Calls superclass method
# File lib/rails_best_practices/reviews/isolate_seed_data_review.rb, line 27 def initialize(options = {}) super(options) @new_variables = [] end
Private Instance Methods
new_record?(node)
click to toggle source
see if the receiver of the call node is included in the @new_varaibles.
# File lib/rails_best_practices/reviews/isolate_seed_data_review.rb, line 69 def new_record?(node) @new_variables.include? node.receiver.to_s end
remember_new_variable(node)
click to toggle source
check assignment node, if the right vavlue is a method_add_arg node with message “new”, then remember the left value as new variable.
# File lib/rails_best_practices/reviews/isolate_seed_data_review.rb, line 61 def remember_new_variable(node) right_value = node.right_value if right_value.sexp_type == :method_add_arg && right_value.message.to_s == 'new' @new_variables << node.left_value.to_s end end