Module: LcpRuby::Pages::Setup

Defined in:
lib/lcp_ruby/pages/setup.rb

Class Method Summary collapse

Class Method Details

.apply!(loader) ⇒ Object

Boot-time setup for DB-backed page source. Called after models are built and before DB pages are merged.

Parameters:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lcp_ruby/pages/setup.rb', line 8

def self.apply!(loader)
  return unless LcpRuby.configuration.page_source == :model

  page_model_name = LcpRuby.configuration.page_model

  model_def = loader.model_definitions[page_model_name]
  unless model_def
    message = "page_source is :model but model '#{page_model_name}' is not defined. " \
              "Define it in your models YAML or run: rails generate lcp_ruby:pages"

    if LcpRuby.generator_context?
      Rails.logger.warn("[LcpRuby::Pages] #{message}")
      return
    end

    raise MetadataError, message
  end

  result = ContractValidator.validate(model_def)
  unless result.valid?
    raise MetadataError, "Page config model '#{page_model_name}' does not satisfy the contract:\n" \
                         "#{result.errors.map { |e| "  - #{e}" }.join("\n")}"
  end

  result.warnings.each do |warning|
    if defined?(Rails) && Rails.respond_to?(:logger)
      Rails.logger.warn("[LcpRuby::Pages] #{warning}")
    end
  end

  Registry.mark_available!

  page_model_class = LcpRuby.registry.model_for(page_model_name)
  ChangeHandler.install!(page_model_class)
  DefinitionValidator.install!(page_model_class)
end