Module: ActiveElement::Components::Util

Defined in:
lib/active_element/components/util.rb,
lib/active_element/components/util/i18n.rb,
lib/active_element/components/util/decorator.rb,
lib/active_element/components/util/record_path.rb,
lib/active_element/components/util/field_mapping.rb,
lib/active_element/components/util/numeric_field.rb,
lib/active_element/components/util/record_mapping.rb,
lib/active_element/components/util/form_field_mapping.rb,
lib/active_element/components/util/form_value_mapping.rb,
lib/active_element/components/util/association_mapping.rb,
lib/active_element/components/util/default_display_value.rb,
lib/active_element/components/util/display_value_mapping.rb

Overview

Utility classes for components (data mapping from models, etc.)

Defined Under Namespace

Modules: RecordMapping Classes: AssociationMapping, Decorator, DefaultDisplayValue, DisplayValueMapping, FieldMapping, FormFieldMapping, FormValueMapping, I18n, NumericField, RecordPath

Class Method Summary collapse

Class Method Details

.default_record_name(record) ⇒ Object



45
46
47
# File 'lib/active_element/components/util.rb', line 45

def self.default_record_name(record)
  (record.is_a?(Class) ? record.name : record.class.name).demodulize.underscore
end

.json_name(name) ⇒ Object



19
20
21
# File 'lib/active_element/components/util.rb', line 19

def self.json_name(name)
  name.to_s.camelize(:lower)
end

.json_pretty_print(json) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/active_element/components/util.rb', line 64

def self.json_pretty_print(json)
  formatter = Rouge::Formatters::HTML.new
  lexer = Rouge::Lexers::JSON.new
  content = JSON.pretty_generate(json.is_a?(String) ? JSON.parse(json) : json)
  formatted = formatter.format(lexer.lex(content)).gsub('  ', '&nbsp;&nbsp;').gsub("\n", '<br/>')
  # rubocop:disable Rails/OutputSafety
  # TODO: Move to a template.
  "<div class='json-highlight' style='font-family: monospace;'>#{formatted}</div>".html_safe
  # rubocop:enable Rails/OutputSafety
end

.json_schema(model:, field:) ⇒ Object



57
58
59
60
61
62
# File 'lib/active_element/components/util.rb', line 57

def self.json_schema(model:, field:)
  YAML.safe_load(
    Rails.root.join("config/forms/#{model.name.underscore}/#{field}.yml").read,
    symbolize_names: true
  )
end

.record_name(record) ⇒ Object



23
24
25
# File 'lib/active_element/components/util.rb', line 23

def self.record_name(record)
  record&.try(:model_name)&.try(&:singular) || default_record_name(record)
end

.relation_controller(model, controller, relation) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/active_element/components/util.rb', line 49

def self.relation_controller(model, controller, relation)
  namespace = controller.controller_path.rpartition('/').first.presence
  base = "#{model.reflect_on_association(relation).klass.name.pluralize}Controller"
  return base.safe_constantize if namespace.nil?

  "#{namespace.classify}::#{base}".safe_constantize || base.safe_constantize
end

.sti_record_name(record) ⇒ Object

TODO: Remove and use .sti_record_names everywhere instead.



28
29
30
31
32
# File 'lib/active_element/components/util.rb', line 28

def self.sti_record_name(record)
  return default_record_name(record) unless record.class.respond_to?(:inheritance_column)

  record&.class&.superclass&.model_name&.singular if record&.try(record.class.inheritance_column).present?
end

.sti_record_names(record) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



34
35
36
37
38
39
40
41
42
43
# File 'lib/active_element/components/util.rb', line 34

def self.sti_record_names(record) # rubocop:disable Metrics/CyclomaticComplexity
  record.class.ancestors.select do |ancestor|
    next false if ancestor == record.class
    next false if ancestor.try(:inheritance_column).blank?
    next false unless ancestor < ActiveRecord::Base
    next false if ancestor.abstract_class?

    true
  end.map(&:model_name).map(&:singular)
end