Module: LcpRuby::RecordAliases::MetadataChecker

Defined in:
lib/lcp_ruby/record_aliases/metadata_checker.rb

Overview

Stateless, metadata-only checks for record aliases and slug fields. Both ConfigurationValidator (rake-time) and RecordAliases::Setup (boot-time) consume the same rules so error/warning wording cannot drift.

Runtime-only checks (scope existence on built AR classes, DB scans) live in RecordAliases::Setup — they require loaded classes and cannot run from a rake context without booting the full engine.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.check_alias(presenter, model_def, alias_name, config, result) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 112

def check_alias(presenter, model_def, alias_name, config, result)
  label = "Presenter '#{presenter.name}': record alias '#{alias_name}'"

  case config["resolve"]
  when "current_user"
    check_current_user_alias(label, model_def, config, result)
  when "field_on_user"
    check_field_on_user_alias(label, config, result)
  when "scope"
    check_scope_alias(label, model_def, config, result)
  end
end

.check_all(loader) ⇒ Object



19
20
21
22
23
24
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 19

def check_all(loader)
  result = Result.empty
  check_slug_fields(loader, result)
  check_record_aliases(loader, result)
  result
end

.check_current_user_alias(label, model_def, config, result) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 125

def check_current_user_alias(label, model_def, config, result)
  field_name = config["field"]
  # field: is optional — when absent, Setup verifies at boot that the
  # presenter's model shares a table with the configured user class.
  return unless field_name

  assoc = model_def.associations.find { |a| a.name == field_name }
  unless assoc
    # bind_to models expose associations from the host class that may not
    # be mirrored in YAML. Defer the check to Setup's runtime AR reflection.
    return if model_def.bind_to?

    result.errors << "#{label}: field '#{field_name}' does not reference a belongs_to association on model '#{model_def.name}'"
    return
  end

  unless assoc.type == "belongs_to"
    result.errors << "#{label}: field '#{field_name}' must be a belongs_to association, got #{assoc.type}"
    return
  end

  if assoc.polymorphic
    result.errors << "#{label}: field '#{field_name}' is a polymorphic belongs_to — not supported for current_user strategy"
    return
  end

  fk_col = assoc.foreign_key || "#{field_name}_id"
  unless model_def.has_uniqueness_validation?(fk_col)
    result.warnings << "#{label}: FK column '#{fk_col}' has no uniqueness validation; " \
                       "find_by! may return non-deterministic results on 1:many relationships"
  end
end

.check_field_on_user_alias(label, config, result) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 158

def check_field_on_user_alias(label, config, result)
  user_field = config["user_field"]
  unless user_field
    result.errors << "#{label}: field_on_user strategy requires 'user_field'"
    return
  end

  user_class = resolve_user_class
  return unless user_class

  field_exists = user_class.public_method_defined?(user_field.to_sym) ||
                 (user_class.respond_to?(:column_names) && user_class.column_names.include?(user_field.to_s))

  unless field_exists
    result.errors << "#{label}: user_field '#{user_field}' is not a public method or column on #{user_class.name}"
  end

  if user_class.respond_to?(:reflect_on_association) && user_class.reflect_on_association(user_field.to_sym)
    result.warnings << "#{label}: user_field '#{user_field}' matches an association on #{user_class.name}; " \
                       "field_on_user expects a scalar PK value, not an association object"
  end
end

.check_record_aliases(loader, result) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 99

def check_record_aliases(loader, result)
  loader.presenter_definitions.each_value do |presenter|
    next if presenter.record_aliases.empty?

    model_def = loader.model_definitions[presenter.model]
    next unless model_def

    presenter.record_aliases.each do |alias_name, config|
      check_alias(presenter, model_def, alias_name, config, result)
    end
  end
end

.check_scope_alias(label, model_def, config, result) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 181

def check_scope_alias(label, model_def, config, result)
  scope_name = config["scope"]
  unless scope_name
    result.errors << "#{label}: scope strategy requires 'scope'"
    return
  end

  yaml_scope = model_def.scopes.find { |s| s["name"] == scope_name }
  return unless yaml_scope

  scope_type = yaml_scope["type"]
  if %w[parameterized custom].include?(scope_type)
    result.errors << "#{label}: scope '#{scope_name}' is #{scope_type} and requires arguments; " \
                     "record aliases only support zero-arity scopes"
  end

  where_clause = yaml_scope["where"]
  if where_clause.is_a?(Hash) && where_clause.values.any? { |v| v.to_s == "param" || v.to_s.start_with?(":") }
    result.errors << "#{label}: scope '#{scope_name}' requires parameters; " \
                     "record aliases only support zero-arity scopes"
  end
end

.check_slug_field_model(loader, model, result) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 33

def check_slug_field_model(loader, model, result)
  if model.bind_to?
    result.errors << "Model '#{model.name}': slug_field is not supported on bind_to models in v1"
    return
  end

  slug_f = model.field(model.slug_field)
  return unless slug_f

  unless model.has_uniqueness_validation?(model.slug_field)
    result.warnings << "Model '#{model.name}': slug_field '#{model.slug_field}' has no uniqueness validation — " \
                       "duplicate slugs may cause non-deterministic URL resolution"
  end

  check_slug_format_issues(model, slug_f, result)

  unless model.has_index_for?(model.slug_field)
    result.warnings << "Model '#{model.name}': slug_field '#{model.slug_field}' has no database index — " \
                       "find_by! queries may degrade on large tables"
  end

  check_sti_children_slug(loader, model, result)
end

.check_slug_fields(loader, result) ⇒ Object



26
27
28
29
30
31
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 26

def check_slug_fields(loader, result)
  loader.model_definitions.each_value do |model|
    next unless model.slug_field
    check_slug_field_model(loader, model, result)
  end
end

.check_slug_format_issues(model, slug_f, result) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 57

def check_slug_format_issues(model, slug_f, result)
  format_val = slug_f.validations.find { |v| v.type == "format" }
  return unless format_val

  pattern = (format_val.options[:with] || format_val.options[:pattern])&.to_s
  return if pattern.blank?

  begin
    regex = Regexp.new(pattern)
  rescue RegexpError
    return
  end

  if "123".match?(regex)
    result.warnings << "Model '#{model.name}': slug_field format pattern allows purely numeric values — " \
                       "numeric slugs are unreachable via URL (would be interpreted as record IDs)"
  end
  if "ABC".match?(regex)
    result.warnings << "Model '#{model.name}': slug_field format pattern allows uppercase characters — " \
                       "slug lookup is case-sensitive on PostgreSQL, recommend lowercase-only patterns"
  end
  if "a.b".match?(regex)
    result.warnings << "Model '#{model.name}': slug_field format pattern allows dots — " \
                       "Rails routing splits params[:id] on '.' into id and format, silently truncating slugs"
  end
end

.check_sti_children_slug(loader, parent_model, result) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 84

def check_sti_children_slug(loader, parent_model, result)
  loader.model_definitions.each_value do |child|
    next unless child.sti_child?
    next if child.slug_field
    next unless child.sti_parent_name == parent_model.name

    matching_field = child.field(parent_model.slug_field)
    next unless matching_field

    result.warnings << "Model '#{child.name}': STI child has string field '#{parent_model.slug_field}' " \
                       "matching parent '#{parent_model.name}' slug_field but no own slug_field declaration — " \
                       "inbound slug resolution will not work for this model's presenters"
  end
end

.resolve_user_classObject



204
205
206
207
208
209
210
# File 'lib/lcp_ruby/record_aliases/metadata_checker.rb', line 204

def resolve_user_class
  class_name = LcpRuby.configuration.user_class
  return nil if class_name.blank?
  class_name.constantize
rescue NameError
  nil
end