Module: LcpRuby::Roles::Setup

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

Class Method Summary collapse

Class Method Details

.apply!(loader) ⇒ Object

Boot-time setup for DB-backed role source. Called after models are built and custom fields are set up.

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
44
45
46
47
# File 'lib/lcp_ruby/roles/setup.rb', line 8

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

  role_model_name = LcpRuby.configuration.role_model

  # Verify the role model exists
  model_def = loader.model_definitions[role_model_name]
  unless model_def
    message = "role_source is :model but model '#{role_model_name}' is not defined. " \
              "Define it in your models YAML or run: rails generate lcp_ruby:role_model"

    # When running inside a generator, skip the hard error so the generator
    # can boot the app and create the missing files (chicken-and-egg).
    if LcpRuby.generator_context?
      Rails.logger.warn("[LcpRuby::Roles] #{message}")
      return
    end

    raise MetadataError, message
  end

  # Validate the model meets the contract
  result = ContractValidator.validate(model_def)
  unless result.valid?
    raise MetadataError, "Role model '#{role_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::Roles] #{warning}")
    end
  end

  # Mark registry as available and install cache invalidation
  Registry.mark_available!

  role_model_class = LcpRuby.registry.model_for(role_model_name)
  ChangeHandler.install!(role_model_class)
end