Class: AzaharaSchema::Presenter
- Inherits:
-
Object
- Object
- AzaharaSchema::Presenter
- Defined in:
- lib/azahara_schema/presenter.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
- .default_formatter ⇒ Object
- .default_formatter=(formatter_klass) ⇒ Object
- .formatter_for(schema_or_entity) ⇒ Object
Instance Method Summary collapse
- #attribute_html_label(attribute, **options) ⇒ Object
- #attribute_human_value(attribute, entity, **_options) ⇒ Object
- #format_value(attribute, unformated_value, **_options) ⇒ Object
- #format_value_html(attribute, unformated_value, **options) ⇒ Object
- #formatted_value(attribute, entity, **options) ⇒ Object
- #formatting_options(attribute, entity) ⇒ Object
- #html_formatted_value(attribute, entity, **options) ⇒ Object
- #human_value(attribute, value, **options) ⇒ Object
- #icon_class_for_attribute(attribute) ⇒ Object
-
#initialize(schema_or_entity, template, **options) ⇒ Presenter
constructor
A new instance of Presenter.
- #labeled_html_attribute_value(attribute, entity, **options) ⇒ Object
- #new_path(**options) ⇒ Object
- #real_formatter(attribute) ⇒ Object
- #real_formatter_and_attribute(attribute) ⇒ Object
- #show_path(entity, **options) ⇒ Object
- #with_real_formatter_and_attribute(attribute) {|real_formatter_and_attribute(attribute)| ... } ⇒ Object
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, **) @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 = @template = template end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
28 29 30 |
# File 'lib/azahara_schema/presenter.rb', line 28 def model @model end |
#template ⇒ Object (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_formatter ⇒ Object
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, **) attribute.attribute_name.human(**) 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, **) 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, **) 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, **) real_formatter(attribute).format_value(attribute, unformated_value, **) || template. end |
#formatted_value(attribute, entity, **options) ⇒ Object
69 70 71 |
# File 'lib/azahara_schema/presenter.rb', line 69 def formatted_value(attribute, entity, **) real_formatter(attribute).format_value(attribute, attribute_human_value(attribute, entity), **(attribute,entity).merge()) end |
#formatting_options(attribute, entity) ⇒ Object
99 100 101 |
# File 'lib/azahara_schema/presenter.rb', line 99 def (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, **) format_value_html(attribute, attribute_human_value(attribute, entity, **), **(attribute,entity).merge()) 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, **) case attribute.type when 'love' attribute.available_values.detect{|l, v| v == value }.try(:[], 0) when 'list' attribute.attribute_name.human_list_value(value, **) 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, **) template.content_tag('div', class: 'attribute') do s = ''.html_safe s << template.content_tag('div', attribute_html_label(attribute, **), class: 'label') s << template.content_tag('div', html_formatted_value(attribute, entity, **), class: 'value') s end end |
#new_path(**options) ⇒ Object
38 39 40 |
# File 'lib/azahara_schema/presenter.rb', line 38 def new_path(**) template.new_polymorphic_path(model, ) 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, **) template.polymorphic_path(entity, ) end |
#with_real_formatter_and_attribute(attribute) {|real_formatter_and_attribute(attribute)| ... } ⇒ Object
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 |