Class: Bootstrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/bootstrapper/railtie.rb,
lib/bootstrapper/bootstrapper.rb

Defined Under Namespace

Classes: Railtie

Class Method Summary collapse

Class Method Details

.fixtures(*fixtures) ⇒ Object



56
57
58
59
# File 'lib/bootstrapper/bootstrapper.rb', line 56

def self.fixtures(*fixtures)
  Fixtures.create_fixtures(File.join(Rails.root, 'db', 'populate'), fixtures)

end

.for(key, &block) ⇒ Object



5
6
7
# File 'lib/bootstrapper/bootstrapper.rb', line 5

def self.for(key, &block)
  tasks[key] = block
end

.run(key) ⇒ Object



23
24
25
26
27
# File 'lib/bootstrapper/bootstrapper.rb', line 23

def self.run(key)
  self.run_start(key)
  tasks[key].call(self)
  self.run_end(key)
end

.run_end(key) ⇒ Object



17
18
19
20
21
# File 'lib/bootstrapper/bootstrapper.rb', line 17

def self.run_end(key)
  @depth -= 1
  print "]"
  puts "\r\n\033[1;31;40mBootstrapping\e[0m >> \033[1;32;40mDone\e[0m" if @depth == 0
end

.run_start(key) ⇒ Object



11
12
13
14
15
# File 'lib/bootstrapper/bootstrapper.rb', line 11

def self.run_start(key)
  print "\033[1;31;40mBootstrapping\e[0m >>" if @depth == 0
  print " [\033[1;33;40m:#{key}\e[0m"
  @depth += 1
end

.sql(sql) ⇒ Object



61
62
63
# File 'lib/bootstrapper/bootstrapper.rb', line 61

def self.sql(sql)
  ActiveRecord::Base.connection.execute(sql)
end

.truncate_table(table_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/bootstrapper/bootstrapper.rb', line 45

def self.truncate_table(table_name)
  conn = ActiveRecord::Base.connection
  sql = case conn.adapter_name.downcase
        when /mysql/
          "TRUNCATE TABLE #{conn.quote_table_name(table_name)};"
        else
          "DELETE FROM #{table_name}"
        end
  conn.execute(sql)
end

.truncate_tables(*tables) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bootstrapper/bootstrapper.rb', line 29

def self.truncate_tables(*tables)
  options = tables.last.is_a?(Hash) ? tables.pop : {}
  if tables == [:all]
    except = options[:except] || []
    except = except.is_a?(Array) ? except.collect { |x| x.to_s } : [except.to_s]

    tables = ActiveRecord::Base.connection.tables.select do |table|
      table !~ /schema_(info|migrations)/ && !except.include?(table)
    end
  end

  tables.each do |table|
    self.truncate_table(table)
  end
end