class AzaharaSchema::Presenter

Attributes

model[R]
template[R]

Public Class Methods

default_formatter() click to toggle source
# File lib/azahara_schema/presenter.rb, line 8
def self.default_formatter
  @default_formatter || AzaharaSchema::Presenter
end
default_formatter=(formatter_klass) click to toggle source
# File lib/azahara_schema/presenter.rb, line 4
def self.default_formatter=(formatter_klass)
  @default_formatter = formatter_klass
end
formatter_for(schema_or_entity) click to toggle source
# File lib/azahara_schema/presenter.rb, line 12
def self.formatter_for(schema_or_entity)
  klass = schema_or_entity.class if !schema_or_entity.is_a?(Class)
  klass = schema_or_entity.model if schema_or_entity.is_a?(::AzaharaSchema::Schema)
  klass ||= schema_or_entity
  klasses = [klass]
  while klass != klass.base_class
    klass = klass.superclass
    klasses << klass
  end
  klasses.each do |kls|
    schema_klass = "#{kls.name}Presenter".safe_constantize || "Presenters::#{kls.name}Presenter".safe_constantize
    return schema_klass if schema_klass
  end
  default_formatter
end
new(schema_or_entity, template, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 30
def initialize(schema_or_entity, template, **options)
  @schema = schema_or_entity if schema_or_entity.is_a?(::AzaharaSchema::Schema)
  @entity = schema_or_entity if schema_or_entity.is_a?(::ActiveRecord::Base)
  @model = @schema ? @schema.model : (@entity ? schema_or_entity.class : schema_or_entity)
  @options = options
  @template = template
end

Public Instance Methods

attribute_html_label(attribute, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 77
def attribute_html_label(attribute, **options)
  attribute.attribute_name.human(options)
end
attribute_human_value(attribute, entity, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 65
def attribute_human_value(attribute, entity, **options)
  human_value(attribute, attribute.value(entity))
end
format_value(attribute, unformated_value, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 91
def format_value(attribute, unformated_value, **options)
  unformated_value
end
format_value_html(attribute, unformated_value, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 95
def format_value_html(attribute, unformated_value, **options)
  real_formatter(attribute).format_value(attribute, unformated_value, options) || template.unfilled_attribute_message
end
formatted_value(attribute, entity, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 69
def formatted_value(attribute, entity, **options)
  real_formatter(attribute).format_value(attribute, attribute_human_value(attribute, entity), formatting_options(attribute,entity).merge(options))
end
formatting_options(attribute, entity) click to toggle source
# File lib/azahara_schema/presenter.rb, line 99
def formatting_options(attribute, entity)
  {}
end
html_formatted_value(attribute, entity, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 73
def html_formatted_value(attribute, entity, **options)
  format_value_html(attribute, attribute_human_value(attribute, entity, options), formatting_options(attribute,entity).merge(options))
end
human_value(attribute, value, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 50
def human_value(attribute, value, **options)
  case attribute.type
  when 'love'
    attribute.available_values.detect{|l, v| v == value }.try(:[], 0)
  when 'list'
    attribute.attribute_name.human_list_value(value, options)
  when 'datetime'
    value ? l(value) : value
  when 'date'
    value ? l(value.to_date) : value
  else
    value
  end
end
icon_class_for_attribute(attribute) click to toggle source
# File lib/azahara_schema/presenter.rb, line 46
def icon_class_for_attribute(attribute)
  'fa'
end
labeled_html_attribute_value(attribute, entity, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 81
def labeled_html_attribute_value(attribute, entity, **options)
  template.content_tag('div', class: 'attribute') do
    s = ''.html_safe
    s << template.content_tag('div', attribute_html_label(attribute, options), class: 'label')
    s << template.content_tag('div', html_formatted_value(attribute, entity, options), class: 'value')
    s
  end
end
new_path(**options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 38
def new_path(**options)
  template.new_polymorphic_path(model, options)
end
real_formatter(attribute) click to toggle source
# File lib/azahara_schema/presenter.rb, line 103
def real_formatter(attribute)
  if attribute.respond_to?(:attribute)
    self.class.formatter_for(attribute.attribute.model).new(attribute.attribute.model, template, @options).real_formatter(attribute.attribute)
  else
    self
  end
end
real_formatter_and_attribute(attribute) click to toggle source
# File lib/azahara_schema/presenter.rb, line 111
def real_formatter_and_attribute(attribute)
  if attribute.respond_to?(:attribute)
    self.class.formatter_for(attribute.attribute.model).new(attribute.attribute.model, template, @options).real_formatter_and_attribute(attribute.attribute)
  else
    [self, attribute]
  end
end
show_path(entity, **options) click to toggle source
# File lib/azahara_schema/presenter.rb, line 42
def show_path(entity, **options)
  template.polymorphic_path(entity, options)
end
with_real_formatter_and_attribute(attribute) { |real_formatter_and_attribute(attribute)| ... } click to toggle source
# File lib/azahara_schema/presenter.rb, line 119
def with_real_formatter_and_attribute(attribute, &block)
  yield real_formatter_and_attribute(attribute)
end