Sha256: 5ae218f931ec307169cfedd7bd37a9bb166bf50a3112d9fd2af5c6003cab1c1a

Contents?: true

Size: 1.56 KB

Versions: 28

Compression:

Stored size: 1.56 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
        [:defs, :call, :lasgn]
      end

      def interesting_files
        MIGRATION_FILES
      end

      def initialize
        super
        @new_variables = []
        @files = []
        @parse = false
      end

      def evaluate_start(node)
        # check duplicate migration because of always_add_db_index_check.
        if :defs == node.node_type
          if @files.include? node.file
            @parse = true if :up == node.message
          else
            @files << node.file
          end
        end
        
        if @parse
          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
      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

28 entries across 28 versions & 1 rubygems

Version Path
rails_best_practices-0.3.24 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.23 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.22 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.21 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.20 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.19 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.18 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.17 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.14 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.13 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.12 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.11 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.10 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.9 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.8 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.7 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.6 lib/rails_best_practices/checks/isolate_seed_data_check.rb
rails_best_practices-0.3.5 lib/rails_best_practices/checks/isolate_seed_data_check.rb