Sha256: c304b442cc05158ce7a1a331ea8d16e2e75202c6c330ed1c73966637b659033d

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

# encoding: utf-8
module Railstar
  module ActiveRecordExt
    module ClassMethods
      def truncation
        return if self.count > 0
        self.truncation!
      end
      
      def truncation!
        table_name = self.to_s.underscore.pluralize
        file_name = "#{table_name}.yml"
        file_path = File.join(Rails.root, "resources", "db", file_name)
        raise "#{file_path} file not found."  unless File.exist?(file_path)
        self.transaction do
          case self.connection.adapter_name
          when "SQLite"
            self.connection.execute("DELETE FROM `#{self.table_name}`")
          else
            self.connection.execute("TRUNCATE TABLE `#{self.table_name}`")
          end
          YAML.load_file(file_path).each do |key, value|
            self.create value
          end
        end
      end
    end

    module InstanceMethods
    end

    def self.included(base)
      base.extend ClassMethods
      base.class_eval do
        include InstanceMethods
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
railstar-0.0.10 lib/railstar/active_record_ext.rb