Skip to content

Commit 8ddea5d

Browse files
committed
Make code compatible with Rails 3
1 parent 5d6d6c8 commit 8ddea5d

File tree

13 files changed

+235
-27
lines changed

13 files changed

+235
-27
lines changed

.gitignore

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
/.config
44
capybara-*.html
55
.rspec
6-
/log
7-
/tmp
8-
/db/*.sqlite3
9-
/db/*.sqlite3-journal
6+
log/
7+
db/*.sqlite3
8+
db/*.sqlite3-journal
109
/public/system
1110
/coverage/
1211
/InstalledFiles
@@ -15,7 +14,7 @@ capybara-*.html
1514
/test/tmp/
1615
/spec/tmp
1716
/test/version_tmp/
18-
/tmp/
17+
tmp/
1918
**.orig
2019
rerun.txt
2120
pickle-email-*.html
@@ -42,14 +41,15 @@ build/
4241
# for a library or gem, you might want to ignore these files since the code is
4342
# intended to run in multiple environments; otherwise, check them in:
4443
Gemfile.lock
45-
.ruby-version
46-
.ruby-gemset
44+
.ruby-version*
45+
.ruby-gemset*
46+
.rvmrc*
4747

4848
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
4949
.rvmrc
5050

5151
# if using bower-rails ignore default bower_components path bower.json files
52-
/vendor/assets/bower_components
52+
vendor/assets/bower_components
5353
*.bowerrc
5454
bower.json
5555

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source "http://rubygems.org"
2-
2+
ruby RUBY_VERSION
33
# Specify your gem's dependencies in cloudinary.gemspec
44
gemspec

cloudinary.gemspec

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,18 @@ Gem::Specification.new do |s|
2020
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
2121
s.require_paths = ["lib"]
2222

23-
s.add_dependency "rest-client"
2423
s.add_dependency "aws_cf_signer"
2524
s.add_development_dependency "rspec", '>=2.11'
26-
s.add_development_dependency "actionpack"
25+
s.add_development_dependency "rspec-rails"
26+
27+
if RUBY_VERSION > "1.9"
28+
s.add_dependency "rest-client"
29+
s.add_development_dependency "actionpack"
30+
s.add_development_dependency "simplecov"
31+
else
32+
s.add_dependency "i18n", "<0.7.0"
33+
s.add_dependency "rest-client", "<=1.6.8"
34+
s.add_development_dependency "actionpack", "~>3.2.0"
35+
end
36+
2737
end
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
class Hash
2+
# Returns a new hash with all keys converted using the block operation.
3+
#
4+
# hash = { name: 'Rob', age: '28' }
5+
#
6+
# hash.transform_keys{ |key| key.to_s.upcase }
7+
# # => {"NAME"=>"Rob", "AGE"=>"28"}
8+
def transform_keys
9+
return enum_for(:transform_keys) unless block_given?
10+
result = self.class.new
11+
each_key do |key|
12+
result[yield(key)] = self[key]
13+
end
14+
result
15+
end
16+
17+
# Destructively convert all keys using the block operations.
18+
# Same as transform_keys but modifies +self+.
19+
def transform_keys!
20+
return enum_for(:transform_keys!) unless block_given?
21+
keys.each do |key|
22+
self[yield(key)] = delete(key)
23+
end
24+
self
25+
end
26+
27+
# Returns a new hash with all keys converted to strings.
28+
#
29+
# hash = { name: 'Rob', age: '28' }
30+
#
31+
# hash.stringify_keys
32+
# # => {"name"=>"Rob", "age"=>"28"}
33+
def stringify_keys
34+
transform_keys{ |key| key.to_s }
35+
end
36+
37+
# Destructively convert all keys to strings. Same as
38+
# +stringify_keys+, but modifies +self+.
39+
def stringify_keys!
40+
transform_keys!{ |key| key.to_s }
41+
end
42+
43+
# Returns a new hash with all keys converted to symbols, as long as
44+
# they respond to +to_sym+.
45+
#
46+
# hash = { 'name' => 'Rob', 'age' => '28' }
47+
#
48+
# hash.symbolize_keys
49+
# # => {:name=>"Rob", :age=>"28"}
50+
def symbolize_keys
51+
transform_keys{ |key| key.to_sym rescue key }
52+
end
53+
alias_method :to_options, :symbolize_keys
54+
55+
# Destructively convert all keys to symbols, as long as they respond
56+
# to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
57+
def symbolize_keys!
58+
transform_keys!{ |key| key.to_sym rescue key }
59+
end
60+
alias_method :to_options!, :symbolize_keys!
61+
62+
# Validate all keys in a hash match <tt>*valid_keys</tt>, raising
63+
# ArgumentError on a mismatch.
64+
#
65+
# Note that keys are treated differently than HashWithIndifferentAccess,
66+
# meaning that string and symbol keys will not match.
67+
#
68+
# { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
69+
# { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
70+
# { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
71+
def assert_valid_keys(*valid_keys)
72+
valid_keys.flatten!
73+
each_key do |k|
74+
unless valid_keys.include?(k)
75+
raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
76+
end
77+
end
78+
end
79+
80+
# Returns a new hash with all keys converted by the block operation.
81+
# This includes the keys from the root hash and from all
82+
# nested hashes and arrays.
83+
#
84+
# hash = { person: { name: 'Rob', age: '28' } }
85+
#
86+
# hash.deep_transform_keys{ |key| key.to_s.upcase }
87+
# # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
88+
def deep_transform_keys(&block)
89+
_deep_transform_keys_in_object(self, &block)
90+
end
91+
92+
# Destructively convert all keys by using the block operation.
93+
# This includes the keys from the root hash and from all
94+
# nested hashes and arrays.
95+
def deep_transform_keys!(&block)
96+
_deep_transform_keys_in_object!(self, &block)
97+
end
98+
99+
# Returns a new hash with all keys converted to strings.
100+
# This includes the keys from the root hash and from all
101+
# nested hashes and arrays.
102+
#
103+
# hash = { person: { name: 'Rob', age: '28' } }
104+
#
105+
# hash.deep_stringify_keys
106+
# # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
107+
def deep_stringify_keys
108+
deep_transform_keys{ |key| key.to_s }
109+
end
110+
111+
# Destructively convert all keys to strings.
112+
# This includes the keys from the root hash and from all
113+
# nested hashes and arrays.
114+
def deep_stringify_keys!
115+
deep_transform_keys!{ |key| key.to_s }
116+
end
117+
118+
# Returns a new hash with all keys converted to symbols, as long as
119+
# they respond to +to_sym+. This includes the keys from the root hash
120+
# and from all nested hashes and arrays.
121+
#
122+
# hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
123+
#
124+
# hash.deep_symbolize_keys
125+
# # => {:person=>{:name=>"Rob", :age=>"28"}}
126+
def deep_symbolize_keys
127+
deep_transform_keys{ |key| key.to_sym rescue key }
128+
end
129+
130+
# Destructively convert all keys to symbols, as long as they respond
131+
# to +to_sym+. This includes the keys from the root hash and from all
132+
# nested hashes and arrays.
133+
def deep_symbolize_keys!
134+
deep_transform_keys!{ |key| key.to_sym rescue key }
135+
end
136+
137+
private
138+
# support methods for deep transforming nested hashes and arrays
139+
def _deep_transform_keys_in_object(object, &block)
140+
case object
141+
when Hash
142+
object.each_with_object({}) do |(key, value), result|
143+
result[yield(key)] = _deep_transform_keys_in_object(value, &block)
144+
end
145+
when Array
146+
object.map {|e| _deep_transform_keys_in_object(e, &block) }
147+
else
148+
object
149+
end
150+
end
151+
152+
def _deep_transform_keys_in_object!(object, &block)
153+
case object
154+
when Hash
155+
object.keys.each do |key|
156+
value = object.delete(key)
157+
object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
158+
end
159+
object
160+
when Array
161+
object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
162+
else
163+
object
164+
end
165+
end
166+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Files in this folder are taken from Rails 4.

lib/cloudinary/video_helper.rb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require 'active_support/core_ext/hash/keys'
2-
1+
if !Hash.respond_to?(:deep_symbolize_keys)
2+
require 'cloudinary/active_support/core_ext/hash/keys'
3+
end
34
module CloudinaryHelper
45
include ActionView::Context
56
DEFAULT_POSTER_OPTIONS = { :format => 'jpg', :resource_type => 'video' }
@@ -28,7 +29,7 @@ def cl_video_tag(source, options = {}, &block)
2829
options = Hash[options].deep_symbolize_keys
2930

3031
options[:source_types] ||= DEFAULT_SOURCE_TYPES
31-
32+
video_attributes.keep_if{ |key, _| options.has_key?(key)} # required prior to Rails 4.x
3233
video_options = options.extract!(*video_attributes)
3334
if video_options.has_key? :poster
3435
poster = video_options.delete(:poster)
@@ -58,7 +59,7 @@ def cl_video_tag(source, options = {}, &block)
5859
cloudinary_tag(source, options) do |_, tag_options|
5960
content_tag('video', tag_options.merge(video_options)) do
6061
source_tags = source_types.map do |type|
61-
transformation = (source_transformation[type.to_sym] || {}).symbolize_keys
62+
transformation = source_transformation[type.to_sym] || {}
6263
cloudinary_tag("#{source}.#{type}", tag_options.merge(transformation)) do |url, _|
6364
mime_type = "video/#{(type == 'ogv' ? 'ogg' : type)}"
6465
tag("source", :src => url, :type => mime_type)
@@ -97,6 +98,26 @@ def cl_video_thumbnail_path(source, options={})
9798
def strip_known_ext(name)
9899
name.sub(/\.(#{DEFAULT_SOURCE_TYPES.join("|")})$/, '')
99100
end
101+
102+
def safe_join(array, sep=$,)
103+
sep = ERB::Util.unwrapped_html_escape(sep)
104+
array.flatten.map! { |i| ERB::Util.unwrapped_html_escape(i) }.join(sep).html_safe
105+
end unless method_defined?( :safe_join)
106+
107+
# HTML escapes strings but doesn't wrap them with an ActiveSupport::SafeBuffer.
108+
# This method is not for public consumption! Seriously!
109+
def unwrapped_html_escape(s) # :nodoc:
110+
s = s.to_s
111+
if s.html_safe?
112+
s
113+
else
114+
s.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE)
115+
end
116+
end unless method_defined?( :unwrapped_html_escape)
117+
118+
HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;', "'" => '&#39;' } unless defined?( HTML_ESCAPE)
119+
HTML_ESCAPE_REGEXP = /[&"'><]/ unless defined?(HTML_ESCAPE_REGEXP)
120+
100121
end
101122

102123

samples/basic-rails/Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source 'https://rubygems.org'
22

3-
gem 'rails', '3.2.13'
4-
gem 'cloudinary'
3+
gem 'rails', '3.2.21'
4+
gem 'cloudinary', :path => "../.."

samples/basic/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
source 'https://rubygems.org'
22

3-
gem 'cloudinary'
3+
gem 'cloudinary', :path => "../.."

samples/photo_album/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ gem 'jquery-rails'
66

77
gem 'carrierwave'
88

9-
gem 'cloudinary', '~> 1.0.79'
9+
gem 'cloudinary', :path => "../.."
1010

1111
group :assets do
1212
gem 'uglifier', '>= 1.0.3'

spec/cloudinary_helper_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
let(:test_tag) { TestTag.new( helper.cl_image_tag('sample.jpg', options)) }
3737

3838
context ":responsive_width" do
39-
let(:options) { {responsive_width: true, cloud_name: "test"} }
39+
let(:options) { {:responsive_width => true, :cloud_name => "test"} }
4040
it "should use data-src for responsive_width" do
4141
expect(test_tag.name).to match( 'img')
4242
expect(test_tag['class']).to eq("cld-responsive")
@@ -45,7 +45,7 @@
4545
end
4646

4747
context ":dpr_auto" do
48-
let(:options) { {dpr: :auto, cloud_name: "test"} }
48+
let(:options) { {:dpr => :auto, :cloud_name => "test"} }
4949
it "should use data-src for dpr auto" do
5050
expect(test_tag.name).to match( 'img')
5151
expect(test_tag['class']).to eq( 'cld-hidpi')

spec/spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def [](symbol_or_string)
4949
end
5050

5151
def method_missing(symbol, *args)
52-
if m = /children_by_(?<name>\w+)/.match( symbol.to_s) and !args.empty?
53-
@children.select{ |c| c[m[:name]] == args[0]}
52+
if (m = /children_by_(\w+)/.match(symbol.to_s)) and !args.empty?
53+
@children.select{ |c| c[m[1]] == args[0]}
5454
else
5555
super
5656
end

spec/utils_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@
367367
end
368368

369369
it "should support responsive width" do
370-
test_cloudinary_url("test", { :width => 100, :height => 100, :crop => :crop, :responsive_width => true }, "#{upload_path}/c_crop,h_100,w_100/c_limit,w_auto/test", { responsive: true })
370+
test_cloudinary_url("test", { :width => 100, :height => 100, :crop => :crop, :responsive_width => true }, "#{upload_path}/c_crop,h_100,w_100/c_limit,w_auto/test", { :responsive => true })
371371
Cloudinary.config.responsive_width_transformation = {:width => :auto, :crop => :pad}
372-
test_cloudinary_url("test", { :width => 100, :height => 100, :crop => :crop, :responsive_width => true }, "#{upload_path}/c_crop,h_100,w_100/c_pad,w_auto/test", { responsive: true })
372+
test_cloudinary_url("test", { :width => 100, :height => 100, :crop => :crop, :responsive_width => true }, "#{upload_path}/c_crop,h_100,w_100/c_pad,w_auto/test", { :responsive => true })
373373
end
374374

375375
it "should correctly encode double arrays" do

spec/video_tag_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
require 'cloudinary'
44
require 'action_view'
55
require 'cloudinary/helper'
6-
6+
require 'rails/version'
77
include CloudinaryHelper
8+
include ActionView::Helpers::AssetTagHelper
9+
10+
if Rails::VERSION::MAJOR == 3
11+
def config
12+
@config ||= {}
13+
end
14+
def controller
15+
@controller ||={}
16+
end
17+
end
818

919
describe CloudinaryHelper do
1020
before(:each) do
@@ -33,7 +43,7 @@
3343
:muted => true,
3444
:preload => true }) }
3545
it "should suport video tag parameters" do
36-
expect(test_tag.attributes.keys).to include("autoplay", "controls", "loop", "muted", "poster", "preload")
46+
expect(test_tag.attributes.keys).to include("autoplay", "controls", "loop", "muted", "preload")
3747
end
3848
end
3949
{ :autoplay => true, :controls => false, :loop => false, :muted => true, :preload => true }

0 commit comments

Comments
 (0)