Module: Seeds

Defined in:
lib/boothby/seeds.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.departmentsObject

rubocop:disable Style/OpenStructUse



99
100
101
102
103
104
105
106
# File 'lib/boothby/seeds.rb', line 99

def self.departments
  [
    Department.master_department,
    Department.executive_health,
    Department.signature_care,
    OpenStruct.new(name: 'General', key: 'general')
  ]
end

.display(text, indent: 0, header: false) ⇒ Object



160
161
162
163
164
165
# File 'lib/boothby/seeds.rb', line 160

def self.display(text, indent: 0, header: false)
  text = "\n======  #{text}" if header
  text = "#{'  ' * indent}- #{text}" if indent > 0

  puts(text)
end

.require_seed_modules!Object



140
141
142
# File 'lib/boothby/seeds.rb', line 140

def self.require_seed_modules!
  Dir[Rails.root.join('db/seeds/**/*.rb')].sort.each { |s| require(s) }
end

.seed(environment:) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/boothby/seeds.rb', line 118

def self.seed(environment:)
  label = environment == :base ? 'ALL ENVIRONMENTS' : environment.to_s.upcase
  env_class = environment.to_s.classify
  multi_spinner = TTY::Spinner::Multi.new(
    "[:spinner] SEEDING MODELS FOR #{label}",
    hide_cursor: true,
    success_mark: pastel.green(''),
    error_mark: pastel.red('×'),
    interval: 5,
    format: :arc
  )

  departments.each do |department|
    next unless seeds_available?(environment, department)

    seed_class = "Seeds::#{env_class}::#{department.key.classify}".constantize

    display(department.name.upcase, indent: 1)
    seed_from(seed_class, department: department)
  end
end

.seed_from(seed_class, department:) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/boothby/seeds.rb', line 144

def self.seed_from(seed_class, department:)
  seed_methods(seed_class).each do |seed_method|
    title = seed_method[4..-1].titleize
    display("Seeding #{title}...\n", indent: 2)
    seed_class.public_send(seed_method, department)
    display("Seeded: #{title}\n", indent: 2)
  end
end

.seed_methods(seed_class) ⇒ Object



153
154
155
156
157
158
# File 'lib/boothby/seeds.rb', line 153

def self.seed_methods(seed_class)
  methods = seed_class.methods.map(&:to_s).select do |method|
    method.starts_with?('seed_')
  end
  methods.sort_by { |method| seed_class.method(method).source_location.last }
end

.seeds_available?(environment, department) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
# File 'lib/boothby/seeds.rb', line 167

def self.seeds_available?(environment, department)
  seed_path = "db/seeds/#{environment}/#{department.key}/*.rb"
  Dir[Rails.root.join(seed_path)].count > 0
end

.sproutObject

rubocop:enable Style/OpenStructUse



109
110
111
112
113
114
115
116
# File 'lib/boothby/seeds.rb', line 109

def self.sprout
  return if Rails.env.test?

  require_seed_modules!

  seed_by_departments(environment: :base)
  seed_by_departments(environment: Rails.env.to_sym)
end