-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathhelper.rb
More file actions
445 lines (384 loc) · 16.4 KB
/
helper.rb
File metadata and controls
445 lines (384 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
require 'digest/md5'
require 'cloudinary/video_helper'
require 'cloudinary/responsive'
module CloudinaryHelper
include ActionView::Helpers::CaptureHelper
include Responsive
CL_BLANK = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
# Stand-in for Rails image_tag helper that accepts various options for transformations.
#
# source:: the public ID, possibly with a file type extension. If there is no extension, the
# :format option is expected to indicate what the extension is. This value can contain
# the version, or not.
# options:: Options you would normally pass to image_tag as well as Cloudinary-specific options
# to control the transformation. Depending on what options are provided, the
# generated URL may or may not have Cloudinary-specific details in it. For example, if
# you only specify :width and :height, these values will not be sent to Cloudinary, however
# if you also specify :crop, they will be.
#
# Examples
# # Image tag sized by the browser, not Cloudinary
# cl_image_tag "sample.png", :width=>100, :height=>100, :alt=>"hello" # W/H are not sent to Cloudinary
#
# # Image tag sized by Cloudinary using the :fit crop strategy
# cl_image_tag "sample.png", :width=>100, :height=>100, :alt=>"hello", :crop=>:fit # W/H are sent to Cloudinary
#
# Get a url for the image with the public id "sample", in :png format.
# cl_image_tag "sample", format: :png
#
# See documentation for more details and options: http://cloudinary.com/documentation/rails_image_manipulation
def cl_image_tag(source, options = {})
cloudinary_tag source, options do |source, options|
if source
image_tag_without_cloudinary(source, options)
else
tag 'img', options
end
end
end
def cl_picture_tag(source, options = {}, sources =[])
options = options.clone
content_tag 'picture' do
sources.map do |source_def|
source_options = options.clone
source_options = Cloudinary::Utils.chain_transformation(source_options, source_def[:transformation])
source_options[:media] = source_def
cl_source_tag(source, source_options)
end.push(cl_image_tag(source, options))
.join('')
.html_safe
end
end
def cl_source_tag(source, options)
srcset_param = options.fetch(:srcset, {}).merge(Cloudinary.config.srcset || {})
attributes = options.fetch(:attributes, {}).clone
responsive_attributes = generate_image_responsive_attributes(source, attributes, srcset_param, options)
attributes = attributes.merge(responsive_attributes)
unless attributes.has_key? :srcset
attributes[:srcset] = Cloudinary::Utils.cloudinary_url(source, options)
end
media_attr = generate_media_attribute(options[:media])
attributes[:media] = media_attr unless media_attr.empty?
tag "source", attributes, true
end
def cloudinary_tag(source, options = {})
tag_options = options.clone
tag_options[:width] = tag_options.delete(:html_width) if tag_options.include?(:html_width)
tag_options[:height] = tag_options.delete(:html_height) if tag_options.include?(:html_height)
tag_options[:size] = tag_options.delete(:html_size) if tag_options.include?(:html_size)
tag_options[:border] = tag_options.delete(:html_border) if tag_options.include?(:html_border)
srcset_param = Cloudinary::Utils.config_option_consume(tag_options, :srcset, {})
src = cloudinary_url_internal(source, tag_options)
attributes = tag_options.delete(:attributes) || {}
responsive_placeholder = Cloudinary::Utils.config_option_consume(tag_options, :responsive_placeholder)
client_hints = Cloudinary::Utils.config_option_consume(tag_options, :client_hints)
hidpi = tag_options.delete(:hidpi)
responsive = tag_options.delete(:responsive)
if !client_hints && (hidpi || responsive)
tag_options["data-src"] = src
src = nil
extra_class = responsive ? "cld-responsive" : "cld-hidpi"
tag_options[:class] = [tag_options[:class], extra_class].compact.join(" ")
responsive_placeholder = CL_BLANK if responsive_placeholder == "blank"
tag_options[:src] = responsive_placeholder
end
responsive_attrs = generate_image_responsive_attributes(source, attributes, srcset_param, options)
unless responsive_attrs.empty?
tag_options.delete(:width)
tag_options.delete(:height)
tag_options.merge! responsive_attrs
end
if block_given?
yield(src,tag_options)
else
tag('div', tag_options)
end
end
def cl_blank
CL_BLANK
end
# Works similarly to cl_image_tag, however just generates the URL of the image
def cl_image_path(source, options = {})
options = options.clone
url = cloudinary_url_internal(source, options)
image_path_without_cloudinary(url)
end
alias_method :cl_path, :cl_image_path
def image_tag_with_cloudinary(*args)
source, options = args
cl_image_tag(source, {:type=>:asset}.merge(options || {}))
end
def image_path_with_cloudinary(*args)
source, options = args
cl_image_path(source, {:type=>:asset}.merge(options || {}))
end
def fetch_image_tag(profile, options = {})
cl_image_tag(profile, {:type=>:fetch}.merge(options))
end
def facebook_profile_image_tag(profile, options = {})
cl_image_tag(profile, {:type=>:facebook}.merge(options))
end
def facebook_profile_image_path(profile, options = {})
cl_image_path(profile, {:type=>:facebook}.merge(options))
end
def gravatar_profile_image_tag(email, options = {})
cl_image_tag(Digest::MD5.hexdigest(email.strip.downcase), {:type=>:gravatar, :format=>:jpg}.merge(options))
end
def gravatar_profile_image_path(email, options = {})
cl_image_path(Digest::MD5.hexdigest(email.strip.downcase), {:type=>:gravatar, :format=>:jpg}.merge(options))
end
def twitter_profile_image_tag(profile, options = {})
cl_image_tag(profile, {:type=>:twitter}.merge(options))
end
def twitter_profile_image_path(profile, options = {})
cl_image_path(profile, {:type=>:twitter}.merge(options))
end
def twitter_name_profile_image_tag(profile, options = {})
cl_image_tag(profile, {:type=>:twitter_name}.merge(options))
end
def twitter_name_profile_image_path(profile, options = {})
cl_image_path(profile, {:type=>:twitter_name}.merge(options))
end
def gplus_profile_image_tag(profile, options = {})
cl_image_tag(profile, {:type=>:gplus}.merge(options))
end
def gplus_profile_image_path(profile, options = {})
cl_image_path(profile, {:type=>:gplus}.merge(options))
end
def cl_sprite_url(source, options = {})
options = options.clone
version_store = options.delete(:version_store)
if options[:version].blank? && (version_store == :file) && defined?(Rails) && defined?(Rails.root)
file_name = "#{Rails.root}/tmp/cloudinary/cloudinary_sprite_#{source.sub(/\..*/, '')}.version"
if File.exist?(file_name)
options[:version] = File.read(file_name).chomp
end
end
options[:format] = "css" unless source.ends_with?(".css")
cloudinary_url_internal(source, options.merge(:type=>:sprite))
end
def cl_sprite_tag(source, options = {})
stylesheet_link_tag(cl_sprite_url(source, options))
end
# cl_form_tag was originally contributed by Milovan Zogovic
def cl_form_tag(callback_url, options={}, &block)
form_options = options.delete(:form) || {}
form_options[:method] = :post
form_options[:multipart] = true
params = Cloudinary::Uploader.build_upload_params(options.merge(:callback=>callback_url))
params[:signature] = Cloudinary::Utils.api_sign_request(params, Cloudinary.config.api_secret)
params[:api_key] = Cloudinary.config.api_key
api_url = Cloudinary::Utils.cloudinary_api_url("upload",
{:resource_type => options.delete(:resource_type), :upload_prefix => options.delete(:upload_prefix)})
form_tag(api_url, form_options) do
content = []
params.each do |name, value|
content << hidden_field_tag(name, value, :id => nil) if value.present?
end
content << capture(&block)
content.join("\n").html_safe
end
end
CLOUDINARY_JS_CONFIG_PARAMS = [:api_key, :cloud_name, :private_cdn, :secure_distribution, :cdn_subdomain]
def cloudinary_js_config(**tag_options)
params = {}
CLOUDINARY_JS_CONFIG_PARAMS.each do
|param|
value = Cloudinary.config.send(param)
params[param] = value if !value.nil?
end
content_tag(
"script",
"$.cloudinary.config(#{params.to_json});".html_safe,
:type=>"text/javascript",
**tag_options
)
end
def cl_client_hints_meta_tag
tag "meta", "http-equiv" => "Accept-CH", :content => "DPR, Viewport-Width, Width"
end
def cloudinary_url(source, options = {})
cloudinary_url_internal(source, options.clone)
end
def cl_image_upload(object_name, method, options={})
cl_image_upload_tag("#{object_name}[#{method}]", options)
end
alias_method :cl_upload, :cl_image_upload
def cl_unsigned_image_upload(object_name, method, upload_preset, options={})
cl_unsigned_image_upload_tag("#{object_name}[#{method}]", upload_preset, options)
end
alias_method :cl_unsigned_upload, :cl_unsigned_image_upload
def cl_upload_url(options={})
Cloudinary::Utils.cloudinary_api_url("upload", {:resource_type=>:auto}.merge(options))
end
def cl_upload_tag_params(options={})
cloudinary_params = Cloudinary::Uploader.build_upload_params(options)
cloudinary_params[:callback] = build_callback_url(options)
if options[:unsigned]
return cloudinary_params.reject{|k, v| Cloudinary::Utils.safe_blank?(v)}.to_json
else
return Cloudinary::Utils.sign_request(cloudinary_params, options).to_json
end
end
def cl_image_upload_tag(field, options={})
html_options = options.delete(:html) || {}
if options.delete(:multiple)
html_options[:multiple] = true
field = "#{ field }[]" unless field.to_s[-2..-1] == "[]"
end
tag_options = html_options.merge(:type=>"file", :name=>"file",
:"data-url"=>cl_upload_url(options),
:"data-form-data"=>cl_upload_tag_params(options),
:"data-cloudinary-field"=>field,
:"data-max-chunk-size"=>options[:chunk_size],
:"class" => [html_options[:class], "cloudinary-fileupload"].flatten.compact
).reject{|k,v| v.blank?}
tag("input", tag_options)
end
alias_method :cl_upload_tag, :cl_image_upload_tag
def cl_unsigned_image_upload_tag(field, upload_preset, options={})
cl_image_upload_tag(field, options.merge(:unsigned => true, :upload_preset => upload_preset))
end
alias_method :cl_unsigned_upload_tag, :cl_unsigned_image_upload_tag
def cl_private_download_url(public_id, format, options = {})
Cloudinary::Utils.private_download_url(public_id, format, options)
end
# @see {Cloudinary::Utils.download_archive_url}
def cl_download_archive_url(options = {})
Cloudinary::Utils.download_archive_url(options)
end
# @see {Cloudinary::Utils.download_zip_url}
def cl_download_zip_url(options = {})
Cloudinary::Utils.download_zip_url(options)
end
def cl_signed_download_url(public_id, options = {})
Cloudinary::Utils.cloudinary_url(public_id, options)
end
def self.included(base)
ActionView::Helpers::FormBuilder.send(:include, Cloudinary::FormBuilder)
base.class_eval do
unless method_defined?(:image_tag)
include ActionView::Helpers::AssetTagHelper
end
alias_method :image_tag_without_cloudinary, :image_tag unless public_method_defined? :image_tag_without_cloudinary
alias_method :image_path_without_cloudinary, :image_path unless public_method_defined? :image_path_without_cloudinary
if Cloudinary.config.enhance_image_tag
alias_method :image_tag, :image_tag_with_cloudinary
alias_method :image_path, :image_path_with_cloudinary
end
end
end
private
def cloudinary_url_internal(source, options = {})
if defined?(CarrierWave::Uploader::Base) && source.is_a?(CarrierWave::Uploader::Base)
if source.version_name.present?
options[:transformation] = Cloudinary::Utils.build_array(source.transformation) + Cloudinary::Utils.build_array(options[:transformation])
end
options.reverse_merge!(
:resource_type => Cloudinary::Utils.resource_type_for_format(source.filename || source.format),
:type => source.storage_type,
:format => source.format)
source = source.full_public_id
end
Cloudinary::Utils.cloudinary_url(source, options)
end
def build_callback_url(options)
callback_path = options.delete(:callback_cors) || Cloudinary.config.callback_cors || "/cloudinary_cors.html"
if callback_path.match(/^https?:\/\//)
callback_path
else
callback_url = request.scheme + "://"
callback_url << request.host
if request.scheme == "https" && request.port != 443 ||
request.scheme == "http" && request.port != 80
callback_url << ":#{request.port}"
end
callback_url << callback_path
end
end
end
module Cloudinary::FormBuilder
def cl_image_upload(method, options={})
@template.cl_image_upload(@object_name, method, objectify_options(options))
end
alias_method :cl_upload, :cl_image_upload
def cl_unsigned_image_upload(method, upload_preset, options={})
@template.cl_unsigned_image_upload(@object_name, method, upload_preset, objectify_options(options))
end
alias_method :cl_unsigned_upload, :cl_unsigned_image_upload
end
if defined? ActionView::Helpers::AssetUrlHelper
module ActionView::Helpers::AssetUrlHelper
alias :original_path_to_asset :path_to_asset
def path_to_asset(source, options={})
options ||= {}
if Cloudinary.config.enhance_image_tag && options[:type] == :image
source = Cloudinary::Utils.cloudinary_url(source, options.merge(:type=>:asset))
end
original_path_to_asset(source, options)
end
end
end
if defined?(::Rails::VERSION::MAJOR) && ::Rails::VERSION::MAJOR == 2
ActionView::Base.send :include, ActionView::Helpers::AssetTagHelper
ActionView::Base.send :include, CloudinaryHelper
end
begin
require 'sass-rails'
if defined?(Sass::Rails::Resolver)
class Sass::Rails::Resolver
alias :original_image_path :image_path
def image_path(img)
if Cloudinary.config.enhance_image_tag
original_image_path(Cloudinary::Utils.cloudinary_url(img, :type=>:asset))
else
original_image_path(img)
end
end
end
end
rescue LoadError
# no sass rails support. Ignore.
end
begin
require 'sass'
require 'sass/script/functions'
module Sass::Script::Functions
def cloudinary_url(public_id, sass_options={})
options = {}
sass_options.each{|k, v| options[k.to_sym] = v.value}
url = Cloudinary::Utils.cloudinary_url(public_id.value, {:type=>:asset}.merge(options))
Sass::Script::String.new("url(#{url})")
end
declare :cloudinary_url, [:string], :var_kwargs => true
end
rescue LoadError
# no sass support. Ignore.
end
begin
require 'sassc'
require 'sassc/script/functions'
module SassC::Script::Functions
# Helper method for generating cloudinary_url in scss files.
#
# As opposed to sass(deprecated), optional named arguments are not supported, use hash map instead.
#
# Example:
# Sass: cloudinary-url("sample", $quality: "auto", $fetch_format: "auto");
# becomes
# SassC: cloudinary-url("sample", ("quality": "auto", "fetch_format": "auto"));
#
# @param [::SassC::Script::Value::String] public_id The public ID of the resource
# @param [::SassC::Script::Value::Map] sass_options Additional options
#
# @return [::SassC::Script::Value::String]
def cloudinary_url(public_id, sass_options = {})
options = {}
sass_options.to_h.each { |k, v| options[k.value.to_sym] = v.value }
url = Cloudinary::Utils.cloudinary_url(public_id.value, {:type => :asset}.merge(options))
::SassC::Script::Value::String.new("url(#{url})")
end
end
rescue LoadError
# no sassc support. Ignore.
end