class Attachy::Viewer

Public Class Methods

new(method, object, options = {}, view = ActionController::Base.helpers) click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 7
def initialize(method, object, options = {}, view = ActionController::Base.helpers)
  @method  = method
  @object  = object
  @options = options
  @view    = view
end

Public Instance Methods

attachments() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 14
def attachments
  [criteria].flatten.compact
end
button_label(html: htm(:button)) click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 22
def button_label(html: htm(:button))
  html = button_label_options.merge(html)

  @view.content_tag :span, html.delete(:text), html
end
button_label_options() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 18
def button_label_options
  { text: '...' }
end
content(html: {}) { |html, attachments| ... } click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 28
def content(html: {})
  html = content_options.merge(html)

  return yield(html, attachments) if block_given?

  @view.content_tag :ul, nodes.join.html_safe, html
end
content_options() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 36
def content_options
  {
    class: :attachy__content,

    data: {
      crop:     metadata[:crop],
      height:   transform[:height],
      multiple: metadata[:multiple],
      width:    transform[:width]
    }
  }
end
field(html: {}) { |html| ... } click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 53
def field(html: {})
  html = field_options.merge(html)

  return yield(html) if block_given?

  @view.content_tag :div, content + file_button, html
end
field_options() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 49
def field_options
  { class: :attachy }
end
file_button(html: htm(:button)) { |html| ... } click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 65
def file_button(html: htm(:button))
  html = file_button_options.merge(html)

  return yield(html) if block_given?

  @view.content_tag :div, button_label + file_field + hidden_field, html
end
file_button_options() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 61
def file_button_options
  { class: :attachy__button }
end
file_field(html: file_field_options) click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 83
def file_field(html: file_field_options)
  options = { html: html, tags: [ENV_TAG, TMP_TAG] }

  options[:folder] = metadata[:folder] if metadata[:folder]

  @view.cl_image_upload_tag @method, options
end
file_field_options() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 73
def file_field_options
  options = { class: :attachy__fileupload }
  accept  = MIME::Types.type_for([metadata[:accept]].flatten.map(&:to_s))

  options[:accept]   = accept.join(',') if accept.present?
  options[:multiple] = true             if metadata[:multiple]

  options
end
hidden_field() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 91
def hidden_field
  @view.hidden_field @object.class.name.downcase, @method, value: value, id: nil
end
image(file = criteria, t: transform, html: htm) click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 95
def image(file = criteria, t: transform, html: htm)
  return if file.nil?

  url         = file.url(t)
  html        = html.reverse_merge(height: t[:height], width: t[:width])
  html[:data] = file.transform(t)

  @view.image_tag url, html
end
node(file = criteria, tl: { crop: :none }, t: transform, html: {}) { |html, attachments| ... } click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 121
def node(file = criteria, tl: { crop: :none }, t: transform, html: {})
  html = html.reverse_merge(node_options)

  return yield(html, attachments) if block_given?

  value = [link(file, t: t, tl: tl)]
  value << remove_button unless @options[:destroy] == false

  @view.content_tag :li, value.join.html_safe, html
end
node_options() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 117
def node_options
  { class: :attachy__node }
end
nodes() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 132
def nodes
  attachments.map { |file| node file }
end
remove_button(html: {}) { |html| ... } click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 140
def remove_button(html: {})
  html = html.reverse_merge(remove_button_options)

  return yield(html) if block_given?

  @view.content_tag :span, '&#215;'.html_safe, html
end
remove_button_options() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 136
def remove_button_options
  { class: :attachy__remove }
end
value() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 148
def value
  default? ? '[]' : attachments.to_json
end

Private Instance Methods

criteria() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 154
def criteria
  @criteria ||= @object.send(@method)
end
default?() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 158
def default?
  attachments.size == 1 && attachments.last.public_id == Attachy::File.default&.public_id
end
htm(path = []) click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 162
def htm(path = [])
  @options.dig(*[path, :html].flatten) || {}
end
metadata() click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 166
def metadata
  @metadata ||= @object.send("#{@method}_metadata")
end
transform(path = []) click to toggle source
# File lib/attachy/models/attachy/viewer.rb, line 170
def transform(path = [])
  @options.dig(*[path, :t].flatten) || {}
end