Class: AzaharaSchema::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/azahara_schema/presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_or_entity, template, **options) ⇒ Presenter

Returns a new instance of Presenter.



30
31
32
33
34
35
36
# 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

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



28
29
30
# File 'lib/azahara_schema/presenter.rb', line 28

def model
  @model
end

#templateObject (readonly)

Returns the value of attribute template.



28
29
30
# File 'lib/azahara_schema/presenter.rb', line 28

def template
  @template
end

Class Method Details

.default_formatterObject



8
9
10
# File 'lib/azahara_schema/presenter.rb', line 8

def self.default_formatter
  @default_formatter || AzaharaSchema::Presenter
end

.default_formatter=(formatter_klass) ⇒ Object



4
5
6
# File 'lib/azahara_schema/presenter.rb', line 4

def self.default_formatter=(formatter_klass)
  @default_formatter = formatter_klass
end

.formatter_for(schema_or_entity) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Method Details

#attribute_html_label(attribute, **options) ⇒ Object



77
78
79
# 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) ⇒ Object



65
66
67
# 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) ⇒ Object



91
92
93
# 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) ⇒ Object



95
96
97
# 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) ⇒ Object



69
70
71
# 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) ⇒ Object



99
100
101
# File 'lib/azahara_schema/presenter.rb', line 99

def formatting_options(attribute, entity)
  {}
end

#html_formatted_value(attribute, entity, **options) ⇒ Object



73
74
75
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



46
47
48
# File 'lib/azahara_schema/presenter.rb', line 46

def icon_class_for_attribute(attribute)
  'fa'
end

#labeled_html_attribute_value(attribute, entity, **options) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/azahara_schema/presenter.rb', line 81

def labeled_html_attribute_value(attribute, entity, **options)
  template.('div', class: 'attribute') do
    s = ''.html_safe
    s << template.('div', attribute_html_label(attribute, **options), class: 'label')
    s << template.('div', html_formatted_value(attribute, entity, **options), class: 'value')
    s
  end
end

#new_path(**options) ⇒ Object



38
39
40
# File 'lib/azahara_schema/presenter.rb', line 38

def new_path(**options)
  template.new_polymorphic_path(model, options)
end

#real_formatter(attribute) ⇒ Object



103
104
105
106
107
108
109
# 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) ⇒ Object



111
112
113
114
115
116
117
# 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) ⇒ Object



42
43
44
# 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)| ... } ⇒ Object

Yields:



119
120
121
# File 'lib/azahara_schema/presenter.rb', line 119

def with_real_formatter_and_attribute(attribute, &block)
  yield real_formatter_and_attribute(attribute)
end