Module: RivetCms::Seeds

Defined in:
lib/rivet_cms/seeds.rb

Defined Under Namespace

Classes: Builder, FieldSet

Constant Summary collapse

TEMPLATES_PATH =
"db/seeds/templates/*.rb".freeze

Class Method Summary collapse

Class Method Details

.availableObject



19
20
21
22
# File 'lib/rivet_cms/seeds.rb', line 19

def available
  load_definitions
  registry.keys.sort
end

.load!(organization:, only: nil) ⇒ Object

Apply one, several, or all templates to an organization. Returns the builders that ran.



26
27
28
29
30
31
32
33
34
# File 'lib/rivet_cms/seeds.rb', line 26

def load!(organization:, only: nil)
  load_definitions
  names = Array(only).map(&:to_s).reject(&:empty?)
  names = registry.keys if names.empty?

  RivetCms::Current.set(organization: organization) do
    names.map { |name| apply(name, organization) }
  end
end

.registryObject



10
11
12
# File 'lib/rivet_cms/seeds.rb', line 10

def registry
  @registry ||= {}
end

.resolve_organization(domain = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/rivet_cms/seeds.rb', line 36

def resolve_organization(domain = nil)
  org = if domain.present?
    RivetCms::Organization.find_by(domain: domain)
  else
    RivetCms::Organization.find_by(default: true) || RivetCms::Organization.first
  end
  return org if org

  raise "No organization found#{domain.present? ? " for domain #{domain}" : ''}. Create one first."
end

.template(name, &block) ⇒ Object

Called by each template file to register its definition block.



15
16
17
# File 'lib/rivet_cms/seeds.rb', line 15

def template(name, &block)
  registry[name.to_s] = block
end