Module: Tramway::Core::Concerns::AttributesDecoratorHelper

Included in:
ApplicationDecorator
Defined in:
app/decorators/tramway/core/concerns/attributes_decorator_helper.rb

Instance Method Summary collapse

Instance Method Details

#date_view(value) ⇒ Object



4
5
6
# File 'app/decorators/tramway/core/concerns/attributes_decorator_helper.rb', line 4

def date_view(value)
  I18n.l value.to_date if value
end

#datetime_view(value) ⇒ Object



8
9
10
# File 'app/decorators/tramway/core/concerns/attributes_decorator_helper.rb', line 8

def datetime_view(value)
  I18n.l value if value
end

#enumerize_view(value) ⇒ Object



46
47
48
# File 'app/decorators/tramway/core/concerns/attributes_decorator_helper.rb', line 46

def enumerize_view(value)
  value.text
end

#image_view(original, thumb: nil, filename: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/decorators/tramway/core/concerns/attributes_decorator_helper.rb', line 16

def image_view(original, thumb: nil, filename: nil)
  if original.present?
    thumb ||= original.is_a?(CarrierWave::Uploader::Base) ? original.small : nil
    filename ||= original.is_a?(CarrierWave::Uploader::Base) ? original.path&.split('/')&.last : nil
    src_thumb = if thumb&.is_a?(CarrierWave::Uploader::Base)
                  thumb.url
                elsif thumb&.match(%r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$})
                  "data:image/jpeg;base64,#{thumb}"
                else
                  thumb
                end
    src_original = if original.is_a?(CarrierWave::Uploader::Base)
                     original.url
                   elsif original.match(%r{^(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==)|(?:[a-zA-Z0-9+/]{1}===))$})
                     "data:image/jpeg;base64,#{original}"
                   else
                     original
                   end
    (:div) do
      begin
        concat image_tag src_thumb || src_original
      rescue NoMethodError => e
        error = Tramway::Error.new plugin: :core, method: :image_view, message: "You should mount PhotoUploader to your model. Just add `mount_uploader \#{attribute_name}, PhotoUploader` to your model. #{e.message}"
        raise error.message
      end
      concat link_to(fa_icon(:download), src_original, class: 'btn btn-success', download: filename) if filename
    end
  end
end

#state_machine_view(object, attribute_name) ⇒ Object



12
13
14
# File 'app/decorators/tramway/core/concerns/attributes_decorator_helper.rb', line 12

def state_machine_view(object, attribute_name)
  object.send "human_#{attribute_name}_name"
end