class RailsBestPractices::Reviews::IsolateSeedDataReview
then it should be isolated to db seed.
and the receiver is included in new variables,
if the message is “save” or “save!”,
then it should be isolated to db seed.
if the message is “create” or “create!”,
2. check all call nodes,
then remember their left value as new variables.
if the right value is a call node with message “new”,
1. check all assignment nodes,
Review process:
Implementation:
See the best practice details here rails-bestpractices.com/posts/2010/07/24/isolating-seed-data/
Make sure not to insert data in migration, move them to seed file.
def initialize(options = {})
def initialize(options = {}) super(options) @new_variables = [] end
def new_record?(node)
def new_record?(node) @new_variables.include? node.receiver.to_s end
def remember_new_variable(node)
if the right vavlue is a method_add_arg node with message "new",
check assignment node,
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