Module: CloudinaryHelper

Includes:
ActionView::Helpers::AssetTagHelper
Defined in:
lib/cloudinary/helper.rb

Constant Summary collapse

CLOUDINARY_JS_CONFIG_PARAMS =
[:api_key, :cloud_name, :private_cdn, :secure_distribution, :cdn_subdomain]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



187
188
189
# File 'lib/cloudinary/helper.rb', line 187

def self.included(base)
  ActionView::Helpers::FormBuilder.send(:include, Cloudinary::FormBuilder)
end

Instance Method Details

#cl_form_tag(callback_url, options = {}, &block) ⇒ Object

cl_form_tag was originally contributed by Milovan Zogovic



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cloudinary/helper.rb', line 109

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

#cl_image_path(source, options = {}) ⇒ Object



21
22
23
24
25
# File 'lib/cloudinary/helper.rb', line 21

def cl_image_path(source, options = {})
  options = options.clone
  url = cloudinary_url(source, options)
  original_image_path(url)    
end

#cl_image_tag(source, options = {}) ⇒ Object

Examples cl_image_tag “israel.png”, :width=>100, :height=>100, :alt=>“hello” # W/H are not sent to cloudinary cl_image_tag “israel.png”, :width=>100, :height=>100, :alt=>“hello”, :crop=>:fit # W/H are sent to cloudinary



10
11
12
13
14
15
16
17
18
19
# File 'lib/cloudinary/helper.rb', line 10

def cl_image_tag(source, options = {})
  options = options.clone
  source = cloudinary_url(source, options)
  options[:width] = options.delete(:html_width) if options.include?(:html_width)
  options[:height] = options.delete(:html_height) if options.include?(:html_height)
  options[:size] = options.delete(:html_size) if options.include?(:html_size)
  options[:border] = options.delete(:html_border) if options.include?(:html_border)

  original_image_tag(source, options)
end

#cl_image_upload(object_name, method, options = {}) ⇒ Object



150
151
152
# File 'lib/cloudinary/helper.rb', line 150

def cl_image_upload(object_name, method, options={})
  cl_image_upload_tag("#{object_name}[#{method}]", options)
end

#cl_image_upload_tag(field, options = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cloudinary/helper.rb', line 154

def cl_image_upload_tag(field, options={})
  html_options = options.delete(:html) || {}
  cloudinary_upload_url = Cloudinary::Utils.cloudinary_api_url("upload", {:resource_type=>:auto}.merge(options))
  
  api_key = options[:api_key] || Cloudinary.config.api_key || raise(CloudinaryException, "Must supply api_key")
  api_secret = options[:api_secret] || Cloudinary.config.api_secret || raise(CloudinaryException, "Must supply api_secret")

  cloudinary_params = Cloudinary::Uploader.build_upload_params(options)
  cloudinary_params[:callback] = build_callback_url(options)
  cloudinary_params[:signature] = Cloudinary::Utils.api_sign_request(cloudinary_params, api_secret)
  cloudinary_params[:api_key] = api_key

  tag_options = html_options.merge(:type=>"file", :name=>"file", 
    :"data-url"=>cloudinary_upload_url,
    :"data-form-data"=>cloudinary_params.reject{|k, v| v.blank?}.to_json,
    :"data-cloudinary-field"=>field,
    :"class" => [html_options[:class], "cloudinary-fileupload"].flatten.compact 
  ).reject{|k,v| v.blank?}
  ("input", nil, tag_options)
end

#cl_private_download_url(public_id, format, options = {}) ⇒ Object



175
176
177
# File 'lib/cloudinary/helper.rb', line 175

def cl_private_download_url(public_id, format, options = {})
  Cloudinary::Utils.private_download_url(public_id, format, options)
end

#cl_signed_download_url(public_id, options = {}) ⇒ Object



183
184
185
# File 'lib/cloudinary/helper.rb', line 183

def cl_signed_download_url(public_id, options = {})
  Cloudinary::Utils.signed_download_url(public_id, options)
end

#cl_sprite_tag(source, options = {}) ⇒ Object



104
105
106
# File 'lib/cloudinary/helper.rb', line 104

def cl_sprite_tag(source, options = {})
  stylesheet_link_tag(cl_sprite_url(source, options))
end

#cl_sprite_url(source, options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cloudinary/helper.rb', line 89

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.exists?(file_name)
      options[:version] = File.read(file_name).chomp        
    end
  end  
  
  options[:format] = "css" unless source.ends_with?(".css")
  cloudinary_url(source, options.merge(:type=>:sprite))
end

#cl_zip_download_url(tag, options = {}) ⇒ Object



179
180
181
# File 'lib/cloudinary/helper.rb', line 179

def cl_zip_download_url(tag, options = {})
  Cloudinary::Utils.zip_download_url(tag, options)
end

#cloudinary_js_configObject



135
136
137
138
139
140
141
142
143
# File 'lib/cloudinary/helper.rb', line 135

def cloudinary_js_config
  params = {}
  CLOUDINARY_JS_CONFIG_PARAMS.each do
    |param| 
    value = Cloudinary.config.send(param)
    params[param] = value if !value.nil?
  end    
  ("script", "$.cloudinary.config(#{params.to_json});".html_safe, :type=>"text/javascript")      
end

#cloudinary_url(source, options = {}) ⇒ Object



145
146
147
148
# File 'lib/cloudinary/helper.rb', line 145

def cloudinary_url(source, options = {})
  options[:ssl_detected] = request.ssl? if defined?(request) && request && request.respond_to?(:ssl?)
  Cloudinary::Utils.cloudinary_url(source, options)
end

#facebook_profile_image_path(profile, options = {}) ⇒ Object



53
54
55
# File 'lib/cloudinary/helper.rb', line 53

def facebook_profile_image_path(profile, options = {})    
  cl_image_path(profile, {:type=>:facebook}.merge(options))
end

#facebook_profile_image_tag(profile, options = {}) ⇒ Object



49
50
51
# File 'lib/cloudinary/helper.rb', line 49

def facebook_profile_image_tag(profile, options = {})    
  cl_image_tag(profile, {:type=>:facebook}.merge(options))
end

#fetch_image_tag(profile, options = {}) ⇒ Object



45
46
47
# File 'lib/cloudinary/helper.rb', line 45

def fetch_image_tag(profile, options = {})    
  cl_image_tag(profile, {:type=>:fetch}.merge(options))
end

#gplus_profile_image_path(profile, options = {}) ⇒ Object



85
86
87
# File 'lib/cloudinary/helper.rb', line 85

def gplus_profile_image_path(profile, options = {})    
  cl_image_path(profile, {:type=>:gplus}.merge(options))
end

#gplus_profile_image_tag(profile, options = {}) ⇒ Object



81
82
83
# File 'lib/cloudinary/helper.rb', line 81

def gplus_profile_image_tag(profile, options = {})    
  cl_image_tag(profile, {:type=>:gplus}.merge(options))
end

#gravatar_profile_image_path(email, options = {}) ⇒ Object



61
62
63
# File 'lib/cloudinary/helper.rb', line 61

def gravatar_profile_image_path(email, options = {})    
  cl_image_path(Digest::MD5.hexdigest(email.strip.downcase), {:type=>:gravatar, :format=>:jpg}.merge(options))
end

#gravatar_profile_image_tag(email, options = {}) ⇒ Object



57
58
59
# File 'lib/cloudinary/helper.rb', line 57

def gravatar_profile_image_tag(email, options = {})    
  cl_image_tag(Digest::MD5.hexdigest(email.strip.downcase), {:type=>:gravatar, :format=>:jpg}.merge(options))
end

#image_path(*args) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/cloudinary/helper.rb', line 36

def image_path(*args)
  if Cloudinary.config.enhance_image_tag
    source, options = args
    cl_image_path(source, {:type=>:asset}.merge(options || {}))
  else
    original_image_path(*args)
  end
end

#image_tag(*args) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/cloudinary/helper.rb', line 27

def image_tag(*args)
  if Cloudinary.config.enhance_image_tag
    source, options = args
    cl_image_tag(source, {:type=>:asset}.merge(options || {}))
  else
    original_image_tag(*args)
  end
end

#original_image_pathObject



5
# File 'lib/cloudinary/helper.rb', line 5

alias :original_image_path :image_path

#original_image_tagObject



4
# File 'lib/cloudinary/helper.rb', line 4

alias :original_image_tag :image_tag

#twitter_name_profile_image_path(profile, options = {}) ⇒ Object



77
78
79
# File 'lib/cloudinary/helper.rb', line 77

def twitter_name_profile_image_path(profile, options = {})    
  cl_image_path(profile, {:type=>:twitter_name}.merge(options))
end

#twitter_name_profile_image_tag(profile, options = {}) ⇒ Object



73
74
75
# File 'lib/cloudinary/helper.rb', line 73

def twitter_name_profile_image_tag(profile, options = {})    
  cl_image_tag(profile, {:type=>:twitter_name}.merge(options))
end

#twitter_profile_image_path(profile, options = {}) ⇒ Object



69
70
71
# File 'lib/cloudinary/helper.rb', line 69

def twitter_profile_image_path(profile, options = {})    
  cl_image_path(profile, {:type=>:twitter}.merge(options))
end

#twitter_profile_image_tag(profile, options = {}) ⇒ Object



65
66
67
# File 'lib/cloudinary/helper.rb', line 65

def twitter_profile_image_tag(profile, options = {})    
  cl_image_tag(profile, {:type=>:twitter}.merge(options))
end