Module: DcImageHelper

Defined in:
app/helpers/dc_image_helper.rb

Overview

Helpers needed by some form fields

Instance Method Summary collapse

Instance Method Details

#dc_image_choices_for_image_sizeObject

Will return choices for preset image sizes



52
53
54
55
56
57
# File 'app/helpers/dc_image_helper.rb', line 52

def dc_image_choices_for_image_size
  sizes = dc_get_site.params.dig('dc_image', 'sizes')
  return ['300x200'] if sizes.blank?

  sizes.split(",").map(&:strip)
end

#dc_image_get_by_size(file_name, size) ⇒ String

Will return image file for requested size.

Parameters:

  • file_name (String)

    : Image file name

  • size (String)

    : Preferred image size

Returns:

  • (String)

    : Image file name if requested size is found. Otherwise first available image.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/dc_image_helper.rb', line 112

def dc_image_get_by_size(file_name, size)
  id = file_name[file_name.rindex('/') + 1, 24]
  return 'error: ID not valid' unless BSON::ObjectId.legal?(id)

  image = DcImage.find(id)
  return 'error: ID not found' unless image

  what = %w[o s m l].inject('l') do |r, e|
    field_name = "size_#{e}".to_sym
    break e if doc.send(field_name) == size
    e
  end
  "/#{dc_get_site.params.dig('dc_image', 'location')}/#{doc.id}-#{what}.#{doc.img_type}"
end

#dc_image_invoke(field_name) ⇒ Object

Will return code for invoking dc_image_search form to select image select on a DRG Form.

Parameters:

  • field_name (String)

    : Field name to which selected image value will be saved.



64
65
66
67
68
69
# File 'app/helpers/dc_image_helper.rb', line 64

def dc_image_invoke(field_name)
  return '' unless dc_get_site.params.dig('dc_image', 'location')

  url = url_for(controller: :cmsedit, form_name: :dc_image_search, table: :dc_image, field_name: field_name)
  %(<span class="dc-window-open" data-url="#{url}" title="#{t('drgcms.dc_image.invoke')}">#{mi_icon('image-o')}</span>).html_safe
end

Will return HTML code for selecting image



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/dc_image_helper.rb', line 89

def dc_image_link_for_select(doc, what)
  field = "size_#{what}"
  value = doc.send(field)
  return '' if value.blank?

  value = value.split(/\+/).first
  src = "/#{dc_get_site.params.dig('dc_image', 'location')}/#{doc.id}-#{what}.#{doc.img_type}"
  %(
<div class="img-link"><div>
 #{value}<br>
  <i class="mi-o mi-preview" onclick="dc_image_preview('#{src}');" title="#{t('drgcms.dc_image.preview')}"></i>
  <i class="mi-o mi-check_circle" onclick="dc_image_select('#{src}');" title="#{t('drgcms.dc_image.select')}"></i>
</div></div>)
end

#dc_image_preview(document, *parms) ⇒ Object

Will return code for previewing image on top of dc_image entry form



33
34
35
36
# File 'app/helpers/dc_image_helper.rb', line 33

def dc_image_preview(document, *parms)
  src = "/#{dc_get_site.params.dig('dc_image', 'location')}/#{document.first_available_image}"
  %(<span class="dc-image-preview dc-image-preview-1"><img src="#{src}"></img></span>).html_safe
end

#dc_image_preview_resized(document, yaml, ignore) ⇒ Object

Will return code for previewing resized images on dc_image entry form



41
42
43
44
45
46
47
# File 'app/helpers/dc_image_helper.rb', line 41

def dc_image_preview_resized(document, yaml, ignore)
  size = yaml['name'].last
  return '' if document["size_#{size}"].blank?

  src = "/#{dc_get_site.params.dig('dc_image', 'location')}/#{document.id}-#{size}.#{document.img_type}?#{Time.now.to_i}"
  %(<span class="dc-image-preview"><img src="#{src}"></img></span><div id="dc-image-preview"></div>).html_safe
end

#first_dc_image(document, *parms) ⇒ Object

Will return code for previewing image on top of dc_image entry form



74
75
76
77
# File 'app/helpers/dc_image_helper.rb', line 74

def first_dc_image(document, *parms)
  src = "/#{dc_get_site.params.dig('dc_image', 'location')}/#{document.first_available_image}"
  %(<span class="dc-image-preview"><img src="#{src}"></img></span><span id="dc-image-preview">).html_safe
end

Will format qry result as html code for selecting image



82
83
84
# File 'app/helpers/dc_image_helper.rb', line 82

def select_links_for_dc_image(doc, *parms)
  %w[o s m l].inject('') { | r,size| r << dc_image_link_for_select(doc, size) }.html_safe
end