Module: Workarea::Seeds

Defined in:
lib/workarea/seeds.rb

Class Method Summary collapse

Class Method Details

.delete_mongoid_dataObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/workarea/seeds.rb', line 38

def self.delete_mongoid_data
  puts 'Cleaning MongoDB collections...'
  Mongoid::AuditLog::Entry
    .where(audited_type: /^Workarea::/)
    .delete_all

  Mongoid::Clients
    .with_name('default')
    .collections
    .select { |c| c.name =~ /workarea_/ }
    .each(&:drop)
end

.delete_redis_dataObject



51
52
53
54
# File 'lib/workarea/seeds.rb', line 51

def self.delete_redis_data
  puts 'Flushing Redis database...'
  Workarea.redis.flushdb
end

.delete_search_indexesObject



33
34
35
36
# File 'lib/workarea/seeds.rb', line 33

def self.delete_search_indexes
  puts 'Deleting Elasticsearch indexes...'
  Elasticsearch::Document.all.each(&:delete_indexes!)
end

.installObject



56
57
58
59
60
61
62
63
# File 'lib/workarea/seeds.rb', line 56

def self.install
  puts 'Ensuring MongoDB indexes...'
  Rake::Task['db:mongoid:create_indexes'].invoke

  puts 'Ensuring Elasticsearch indexes...'
  Elasticsearch::Document.all.each(&:create_indexes!)
  Search::Storefront.ensure_dynamic_mappings
end

.puts_with_color(string, color) ⇒ Object



65
66
67
# File 'lib/workarea/seeds.rb', line 65

def self.puts_with_color(string, color)
  puts "\e[#{Workarea::COLOR_CODES[color]}m#{string}\e[0m"
end

.resetObject



24
25
26
27
28
29
30
31
# File 'lib/workarea/seeds.rb', line 24

def self.reset
  delete_search_indexes
  delete_mongoid_data
  delete_redis_data
  install

  Rails.cache.clear
end

.runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/workarea/seeds.rb', line 6

def self.run
  Workarea.with_config do |config|
    config.send_email = false

    puts_with_color "== Setting up...", :yellow
    reset

    puts_with_color "\n== Loading MongoDB data", :yellow
    Workarea.config.seeds.each { |c| c.constantize.new.perform }

    puts_with_color "\n== Loading Elasticsearch data", :yellow
    Rake::Task['workarea:search_index:all'].invoke
    Catalog::Category.all.each { |c| IndexCategorization.perform(c) }

    puts_with_color "\nSuccess!", :green
  end
end