Class: HasHelpers::BarcodeBasePresenter

Inherits:
BasePresenter show all
Defined in:
app/presenters/has_helpers/barcode_base_presenter.rb

Instance Attribute Summary

Attributes inherited from BasePresenter

#errors, #wrapped_object

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasePresenter

#==, #error_on, #initialize, wrap, wrap_with_options

Methods included from Attributes

included

Constructor Details

This class inherits a constructor from HasHelpers::BasePresenter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HasHelpers::BasePresenter

Class Method Details

.get_presenter_class(object) ⇒ Object

Returns a BarcodePresenter class that matches the object passed in. It does this by looking for a BarcodePresenter matching the object's base class. For example, if the object is a Policy (or any subclass of Policy), this method will see if there is a Policy::BarcodePresenter class. If it finds a matching class, it returns the class; if not, it returns nil.



20
21
22
23
# File 'app/presenters/has_helpers/barcode_base_presenter.rb', line 20

def self.get_presenter_class(object)
  base_class_name = object.class.base_class.name
  "::#{base_class_name}::BarcodePresenter".safe_constantize
end

.get_presenter_class!(object) ⇒ Object

Same as #get_presenter_class, but raises an error if it cannot find a matching BarcodePresenter class.



27
28
29
30
31
# File 'app/presenters/has_helpers/barcode_base_presenter.rb', line 27

def self.get_presenter_class!(object)
  presenter_class = get_presenter_class(object)
  raise "Cannot find a barcode presenter for #{object.class}." unless presenter_class
  presenter_class
end

Instance Method Details

#barcode_infosObject

Returns an array of hashes representing barcodes. Each hash has the following keys:

:text => String with the plain text version of the barcode
:image_src => String with the Base64-encoded barcode PNG, prefixed with
            the necessary inline image HTML prefix; intended to be
            the entire value needed for an IMG tag's SRC attribute
:last => Set to true for last barcode (to help rendering)

It gets the actual content for the hashes from the #get_texts method, and then adds one more barcode representing the wrapped object's base class name.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/presenters/has_helpers/barcode_base_presenter.rb', line 45

def barcode_infos
  @barcode_infos ||= begin
                       texts = get_texts
                       texts << ("#{base_class_name}/") if base_class_name

                       infos = texts.map do |text|
                         { text: text, image_src: barcode_inline_image_src(text) }
                       end
                       infos.last[:last] = true
                       infos
                     end
end