Sha256: 58ce6c1e0cb8bd4ffaf46b51dd1ebc70e12c8496fcd4f2d72662eb083321469f

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require_relative 'util/i18n'
require_relative 'util/record_mapping'
require_relative 'util/field_mapping'
require_relative 'util/form_field_mapping'
require_relative 'util/form_value_mapping'
require_relative 'util/display_value_mapping'
require_relative 'util/association_mapping'
require_relative 'util/decorator'

module ActiveElement
  module Components
    # Utility classes for components (data mapping from models, etc.)
    module Util
      def self.json_name(name)
        name.to_s.camelize(:lower)
      end

      def self.record_name(record)
        record&.try(:model_name)&.try(&:singular) || record.class.name.demodulize.underscore
      end

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

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

      def self.json_pretty_print(json)
        theme = Rouge::Themes::Base16.mode(:light)
        formatter = Rouge::Formatters::HTMLLinewise.new(Rouge::Formatters::HTMLInline.new(theme))
        lexer = Rouge::Lexers::JSON.new
        content = JSON.pretty_generate(json.is_a?(String) ? JSON.parse(json) : json)
        formatted = formatter.format(lexer.lex(content)).gsub('  ', '  ')
        # rubocop:disable Rails/OutputSafety
        # TODO: Move to a template.
        "<div style='font-family: monospace;'>#{formatted}</div>".html_safe
        # rubocop:enable Rails/OutputSafety
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_element-0.0.3 lib/active_element/components/util.rb
active_element-0.0.2 lib/active_element/components/util.rb