Sha256: 81145504701ce4764f1f34e042700ddac1a5e53ac392cfbcf4b3672525d92062

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a migration file to make sure not to insert data in migration, move them to seed file.
    #
    # Implementation: check if there are :create, :create!, and :new with :save or :save! exist, the migration file needs isolate seed data.
    class IsolateSeedDataCheck < Check

      def interesting_nodes
        [:call, :lasgn]
      end

      def interesting_files
        MIGRATION_FILES
      end

      def initialize
        super
        @new_variables = []
      end

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

      private

      def remember_new_variable(node)
        unless node.grep_nodes({:node_type => :call, :message => :new}).empty?
          @new_variables << node.subject.to_s
        end
      end

      def new_record?(node)
        @new_variables.include? node.subject.to_ruby
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_best_practices-0.4.2 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.4.1 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.4.0 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.27 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.26 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.25 lib/rails_best_practices/checks/isolate_seed_data_check.rb