Module: LcpRuby::Permissions::Setup

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

Class Method Summary collapse

Class Method Details

.apply!(loader) ⇒ Object

Boot-time setup for DB-backed permission source. Called after models are built and roles 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
48
# File 'lib/lcp_ruby/permissions/setup.rb', line 8

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

  perm_model_name = LcpRuby.configuration.permission_model

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

    # 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::Permissions] #{message}")
      return
    end

    raise MetadataError, message
  end

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

  # Mark registry as available and install callbacks
  Registry.mark_available!

  perm_model_class = LcpRuby.registry.model_for(perm_model_name)
  ChangeHandler.install!(perm_model_class)
  DefinitionValidator.install!(perm_model_class)
end