Module: ImagesHelper

Defined in:
app/helpers/images_helper.rb

Instance Method Summary collapse

Instance Method Details

#image_autocomplete_tag(image) ⇒ Object



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

def image_autocomplete_tag(image)
  (:figure) do
    (
      image_tag(image.image_file.url(:thumb)) +
      (:caption, "id:#{image.id}", class: ['feedback', 'feedback-primary', 'feedback-thin'])
    ).html_safe
  end
end

#image_display_url(image) ⇒ Object



71
72
73
74
75
76
77
78
# File 'app/helpers/images_helper.rb', line 71

def image_display_url(image)
  case image.image_file_content_type
  when 'image/tiff'
    "/images/#{image.id}/extract/0/0/#{image.width}/#{image.height}"
  else
    root_url + image.image_file.url[1..-1]
  end
end

#image_inventory(depictions, api: true, sort_order: []) ⇒ Object

Integrate images and Depictions for concise, Otu-based responses that are sortable by depiction type context. Somewhat convoluted.

Parameters:

  • sort_order (defaults to: [])

    Array of base-class names



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/helpers/images_helper.rb', line 85

def image_inventory(depictions, api: true, sort_order: [])
  q = depictions
  sort_order = %w{Otu CollectionObject Observation} if sort_order.blank?

   = {}
  images = {}

  # find_each strips order, don't use that here
  q.all.each do |d|
    [d.depiction_object_type] ||= []
    [d.depiction_object_type].push(
      {
        id: d.id,
        label: label_for_depiction(d),
        depiction_type: d.depiction_object_type,
        depiction_object_id: d.depiction_object_id,
        image_id: d.image_id
      }
    )

    images[d.image_id] ||= d.image
  end

  r = {}

  images.values.each do |i|
    p = {
      original_png: original_as_png_via_api(i, api:),
      content_type: i.image_file_content_type,
      thumb: short_url(i.image_file.url(:thumb)),
      medium: short_url(i.image_file.url(:medium)),
      depictions: []
    }

    if i.source 
      p[:source] = {
        label: i.source.cached
      }
    end

    if i.attribution
      p[:attribution] = {
        label: label_for_attribution(i.attribution),
        license: CREATIVE_COMMONS_LICENSES[i.attribution.license]
      }
    end

    r[i.id] = p
  end

  # Recombine the data
  .each do |t, v|
    v.each do |d|
      id = d[:image_id]
      r[id][:depictions].push d.select{|m,n| m != :image_id} # trim out the cross-referencing image_id
    end
  end

  # Calculate a sort order
  image_ids = images.keys
  image_order = []

  sort_order.each do |o|
    if [o]
      [o].each do |d|
        image_order.push d[:image_id]
      end
    end
  end

  image_order.uniq!

  # Put the non-requested sort image ids in order at the end
  image_order += (image_ids - image_order)

  return {
    image_order:,
    images: r
  }
end

!! Rails already provides image_tag, i.e. it is not required here.



5
6
7
8
# File 'app/helpers/images_helper.rb', line 5

def image_link(image, size: :thumb)
  return nil if image.nil?
  link_to(image_tag(image.image_file.url(size)), image)
end

#image_short_url(image, api: true) ⇒ Object

Return a ShortenedUrl to the original file image



34
35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/images_helper.rb', line 34

def image_short_url(image, api: true)
  if !image.kind_of?(::Image) && (Integer(image) rescue false)
    image = ::Image.find(image)
  end

  if api
    short_url( api_v1_image_file_path( image) )
  else
    short_url( image.image_file.url(:original) )
  end
end

#image_type(image) ⇒ String

Returns like ‘jpeg`, `png`.

Returns:

  • (String)

    like ‘jpeg`, `png`



16
17
18
# File 'app/helpers/images_helper.rb', line 16

def image_type(image)
  image.image_file.content_type.split('/').last.downcase
end

#images_recent_objects_partialTrue

Returns indicates a custom partial should be used, see list_helper.rb.

Returns:

  • (True)

    indicates a custom partial should be used, see list_helper.rb



48
49
50
# File 'app/helpers/images_helper.rb', line 48

def images_recent_objects_partial
  true
end

#images_search_formObject



10
11
12
# File 'app/helpers/images_helper.rb', line 10

def images_search_form
  render('/images/quick_search_form')
end

#original_as_png_via_api(image, api: true) ⇒ Object

“/images/scale_to_box/:x/:y/:width/:height/:box_width/:box_height”



22
23
24
25
26
27
28
29
30
# File 'app/helpers/images_helper.rb', line 22

def original_as_png_via_api(image, api: true)
  h = image.height
  w = image.width

  s = api ? '/api/v1' : ''

  s << "/images/#{image.id}/scale_to_box/0/0/#{w}/#{h}/#{w}/#{h}"
  s
end

#thumb_list_tag(object) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'app/helpers/images_helper.rb', line 61

def thumb_list_tag(object)
  if object.depictions.any?
    object.depictions.collect{|a|
      (:div)  do
        link_to( depiction_tag(a, size: :medium), a.image.image_file.url())
      end
    }.join.html_safe
  end
end