uploadcare-rails 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/config/initializers/uploadcare.rb +22 -23
  3. data/lib/uploadcare-rails.rb +6 -5
  4. data/lib/uploadcare/rails/action_view/include_tags.rb +35 -26
  5. data/lib/uploadcare/rails/action_view/uploader_tags.rb +31 -29
  6. data/lib/uploadcare/rails/active_record/has_file.rb +14 -26
  7. data/lib/uploadcare/rails/active_record/has_group.rb +23 -22
  8. data/lib/uploadcare/rails/active_record/has_object.rb +3 -4
  9. data/lib/uploadcare/rails/engine.rb +3 -3
  10. data/lib/uploadcare/rails/formtastic/formtastic.rb +19 -15
  11. data/lib/uploadcare/rails/objects/file.rb +13 -25
  12. data/lib/uploadcare/rails/objects/group.rb +45 -22
  13. data/lib/uploadcare/rails/operations.rb +57 -0
  14. data/lib/uploadcare/rails/settings.rb +26 -30
  15. data/lib/uploadcare/rails/simple_form/simple_form.rb +22 -20
  16. data/lib/uploadcare/rails/version.rb +1 -1
  17. data/spec/caching/file_caching_spec.rb +16 -24
  18. data/spec/caching/group_caching_spec.rb +23 -26
  19. data/{app → spec/dummy/app}/assets/javascripts/uploadcare-1.5.5.min.js +0 -0
  20. data/spec/dummy/app/views/layouts/application.html.erb +2 -2
  21. data/spec/dummy/config/routes.rb +2 -2
  22. data/spec/helpers/include_tags_spec.rb +41 -29
  23. data/spec/models/has_both_file_and_group_spec.rb +19 -17
  24. data/spec/models/has_file_spec.rb +46 -47
  25. data/spec/models/has_group_spec.rb +46 -32
  26. data/spec/objects/file_spec.rb +9 -11
  27. data/spec/objects/group_spec.rb +40 -29
  28. data/spec/operations_spec.rb +28 -0
  29. data/spec/spec_helper.rb +17 -5
  30. metadata +235 -199
  31. data/MIT-LICENSE +0 -20
  32. data/app/assets/javascripts/uploadcare-0.17.0.js +0 -8248
  33. data/app/assets/javascripts/uploadcare-0.17.0.min.js +0 -17
  34. data/app/assets/javascripts/uploadcare-0.18.3.js +0 -8484
  35. data/app/assets/javascripts/uploadcare-0.18.3.min.js +0 -17
  36. data/app/assets/javascripts/uploadcare-1.0.1.js +0 -8430
  37. data/app/assets/javascripts/uploadcare-1.0.1.min.js +0 -17
  38. data/app/assets/javascripts/uploadcare-1.3.1.js +0 -8563
  39. data/app/assets/javascripts/uploadcare-1.3.1.min.js +0 -17
  40. data/app/assets/javascripts/uploadcare-1.5.5.js +0 -9278
  41. data/app/assets/javascripts/uploadcare.js +0 -1
  42. data/spec/dummy/db/test.sqlite3 +0 -0
  43. data/spec/dummy/log/development.log +0 -0
  44. data/spec/dummy/log/test.log +0 -2166
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df036376b117b7bda8813588aa8d53d61218c6ae
4
- data.tar.gz: 7157e8cd38f96ed1b565416e9fa80c4e4834c26f
3
+ metadata.gz: 0113e45365a82f1af9474db0a516cae93dc19513
4
+ data.tar.gz: 4fe03ac56616d9adcff45499832871d206f1d23d
5
5
  SHA512:
6
- metadata.gz: 614bddb524b00e2c66d8894e8a7e6542c6bd8b00459c904ec59ffb5f9d6d1cca7336c0ed3796afd2bfc4a68b493bf99c08d2c2ee8b6298f29fb1babe8acec8be
7
- data.tar.gz: 4c3fbe5876596477b91a9c36cc9972686304e4bf854ed0180e614633e88167892659df41d23bea6064a826969a527f03745594456aae922b66df99a7fb4d17a7
6
+ metadata.gz: 9a112c863d5016ae1d7b191e64cb2a8b0a0c3d86cbac74a18a04abf95e8dbfe37afd30fec8676a39c4afc84622a6aa2d98ff66178ce423d1616c9e473523716f
7
+ data.tar.gz: de5e0f0f18f2e5ee4f8325bd8e80b368dcce7e7ac899d99fbd08708476005a93618892448bda5aa0dfddbf8c55e9d83506826c5f643d5793766720d589d02eaa
@@ -1,27 +1,26 @@
1
- config_file = "#{Rails.root}/config/uploadcare.yml"
1
+ config_file = Rails.root.join('config', 'uploadcare.yml')
2
2
 
3
- if File.exists?(config_file)
4
- # defaults = YAML.load_file(defaults_file)
5
- config = YAML.load(ERB.new(File.new(config_file).read).result)
6
- UPLOADCARE_SETTINGS = Uploadcare::Rails::Settings.new config
7
- else
8
- # output warning in red color
9
- # use default settings just to boot up
10
- puts <<-eos
11
- \e[31mWarning: no uploadcare.yml config file found.\e[0m
12
- \e[31mwhile we will use default settings please run\e[0m
13
- \e[36mrails g uploadcare_config\e[0m
14
- \e[31mto set up config file.\e[0m
15
- eos
3
+ config =
4
+ if File.exist?(config_file)
5
+ YAML.load(ERB.new(File.new(config_file).read).result)
6
+ else
7
+ # output warning in red color
8
+ # use default settings just to boot up
9
+ puts <<-eos
10
+ \e[31mWarning: no uploadcare.yml config file found.\e[0m
11
+ \e[31mwhile we will use default settings please run\e[0m
12
+ \e[36mrails g uploadcare_config\e[0m
13
+ \e[31mto set up config file.\e[0m
14
+ eos
16
15
 
17
- config = {
18
- "defaults" => {"public_key"=>"demopublickey", "private_key"=>"demoprivatekey"},
19
- "development" => {"public_key"=>"demopublickey", "private_key"=>"demoprivatekey"},
20
- "test" => {"public_key"=>"demopublickey", "private_key"=>"demoprivatekey"},
21
- "production" => {"public_key"=>"demopublickey", "private_key"=>"demoprivatekey"}
22
- }
23
- UPLOADCARE_SETTINGS = Uploadcare::Rails::Settings.new config
24
- end
16
+ {
17
+ defaults: { public_key: :demopublickey, private_key: :demoprivatekey },
18
+ development: { public_key: :demopublickey, private_key: :demoprivatekey },
19
+ test: { public_key: :demopublickey, private_key: :demoprivatekey },
20
+ production: { public_key: :demopublickey, private_key: :demoprivatekey }
21
+ }
22
+ end
25
23
 
24
+ UPLOADCARE_SETTINGS = Uploadcare::Rails::Settings.new(config)
26
25
  # shortcut for later usage
27
- Rails.application.config.uploadcare = UPLOADCARE_SETTINGS
26
+ Rails.application.config.uploadcare = UPLOADCARE_SETTINGS
@@ -1,11 +1,12 @@
1
- require "uploadcare/rails/engine"
2
- require "uploadcare/rails/settings"
3
- require "uploadcare"
1
+ require 'uploadcare/rails/engine'
2
+ require 'uploadcare/rails/settings'
3
+ require 'uploadcare/rails/operations'
4
+ require 'uploadcare'
4
5
 
5
6
  module Uploadcare
6
7
  module Rails
7
8
  DEFAULT_SETTINGS = {
8
- widget_version: "1.5.5"
9
+ widget_version: '2.9.0'
9
10
  }
10
11
  end
11
- end
12
+ end
@@ -1,38 +1,47 @@
1
1
  module Uploadcare::Rails::ActionView
2
2
  module IncludeTags
3
- def include_uploadcare_widget_from_cdn options={}
4
- settings = {
5
- min: true,
6
- version: UPLOADCARE_SETTINGS.widget_version
7
- }
8
-
9
- settings.merge!(options)
10
-
11
- v = settings[:version]
12
- m = settings[:min] ? ".min" : ""
13
- url = "https://ucarecdn.com/widget/#{v}/uploadcare/uploadcare-#{v}#{m}.js"
14
-
15
- javascript_include_tag(url)
3
+ def include_uploadcare_widget_from_cdn(options = {})
4
+ settings =
5
+ {
6
+ min: true,
7
+ version: UPLOADCARE_SETTINGS.widget_version
8
+ }.merge!(options)
9
+
10
+ minified = settings[:min] ? 'min' : nil
11
+
12
+ path =
13
+ [
14
+ 'widget',
15
+ settings[:version],
16
+ 'uploadcare',
17
+ ['uploadcare', minified, 'js'].compact.join('.')
18
+ ].join('/')
19
+
20
+ url = URI::HTTPS.
21
+ build(host: 'ucarecdn.com', path: '/' + path, scheme: :https)
22
+
23
+ javascript_include_tag(url.to_s)
16
24
  end
25
+
17
26
  alias_method :inlude_uploadcare_widget, :include_uploadcare_widget_from_cdn
18
27
  alias_method :uplodacare_widget, :include_uploadcare_widget_from_cdn
19
-
20
-
21
- def uploadcare_settings options={}
22
- settings = UPLOADCARE_SETTINGS.widget_settings
23
- settings.merge!(options)
24
- js_settings = String.new
28
+
29
+ def uploadcare_settings(options = {})
30
+ settings = UPLOADCARE_SETTINGS.widget_settings.merge!(options)
31
+
32
+ js_settings = ''
25
33
  settings.each do |k, v|
26
- if v.is_a?(TrueClass) || v.is_a?(FalseClass)
27
- js_settings << "UPLOADCARE_#{k.to_s.underscore.upcase} = #{v};\n"
28
- else
29
- js_settings << "UPLOADCARE_#{k.to_s.underscore.upcase} = \"#{v}\";\n"
30
- end
34
+ js_settings <<
35
+ if v.is_a?(TrueClass) || v.is_a?(FalseClass)
36
+ "UPLOADCARE_#{ k.to_s.underscore.upcase } = #{ v };\n"
37
+ else
38
+ "UPLOADCARE_#{ k.to_s.underscore.upcase } = \"#{ v }\";\n"
39
+ end
31
40
  end
32
41
 
33
- uc_settings = javascript_tag(js_settings)
42
+ javascript_tag(js_settings)
34
43
  end
35
44
  end
36
45
  end
37
46
 
38
- ActionView::Base.send :include, Uploadcare::Rails::ActionView::IncludeTags
47
+ ActionView::Base.send :include, Uploadcare::Rails::ActionView::IncludeTags
@@ -2,55 +2,54 @@
2
2
 
3
3
  module Uploadcare::Rails::ActionView
4
4
  module UploaderTags
5
- def uploadcare_uploader_options options
6
- options = options.symbolize_keys.deep_merge({
7
- :role => "uploadcare-uploader #{options[:role]}".strip,
8
- :data => {:path_value => true}
9
- })
10
-
5
+ def uploadcare_uploader_options(options)
6
+ options = options.symbolize_keys.deep_merge(
7
+ role: "uploadcare-uploader #{ options[:role] }".strip,
8
+ data: { path_value: true }
9
+ )
10
+
11
11
  # merge uploadcare options into data-attributes
12
12
  # IMPORTANT: custome data-attrs will be overriden by
13
13
  # the uploadcare options in case of collision.
14
- if options[:uploadcare]
15
- # options[:uploadcare] ||= {}
16
- options[:data] = options[:data].merge!(options[:uploadcare])
17
- options.tap {|options| options.delete(:uploadcare)}
18
- else
19
- options
20
- end
14
+ return options unless options[:uploadcare]
15
+
16
+ options[:data] = options[:data].merge!(options[:uploadcare])
17
+ options.except(:uploadcare)
21
18
  end
22
19
 
23
- def uploadcare_uploader_tag name, options = {}
24
- options = uploadcare_uploader_options(options)
25
- hidden_field_tag(name, nil, options)
20
+ def uploadcare_uploader_tag(name, options = {})
21
+ hidden_field_tag(name, nil, uploadcare_uploader_options(options))
26
22
  end
27
23
 
28
- def uploadcare_uploader_field object_name, method, options = {}
29
- options = uploadcare_uploader_options(options)
30
- hidden_field(object_name, method, options)
24
+ def uploadcare_uploader_field(object_name, method, options = {})
25
+ hidden_field(object_name, method, uploadcare_uploader_options(options))
31
26
  end
32
27
 
33
- def self.included(arg)
34
- ActionView::Helpers::FormBuilder.send(:include, Uploadcare::Rails::ActionView::FormBuilder)
28
+ def self.included(_)
29
+ ActionView::Helpers::FormBuilder.
30
+ send(:include, Uploadcare::Rails::ActionView::FormBuilder)
35
31
  end
36
32
  end
37
33
 
38
34
  module FormBuilder
39
35
  # call uploadcare_uploader field for form object
40
36
  # none of options will be overriden and given as-is
41
- def uploadcare_uploader method, options={}
42
- @template.uploadcare_uploader_field(@object_name, method, objectify_options(options))
37
+ def uploadcare_uploader(method, options = {})
38
+ @template.
39
+ uploadcare_uploader_field(
40
+ @object_name, method, objectify_options(options)
41
+ )
43
42
  end
44
43
 
45
44
  # forse-set the data-multiple="false" for uploader
46
- def uploadcare_single_uploader_field method, options={}
45
+ def uploadcare_single_uploader_field(method, options = {})
47
46
  options[:uploadcare] ||= {}
48
47
  options[:uploadcare][:multiple] = false
49
48
  uploadcare_uploader(method, options)
50
49
  end
51
50
 
52
51
  # forse-set the data-multiple="true" for uploader
53
- def uploadcare_multiple_uploader_field method, options={}
52
+ def uploadcare_multiple_uploader_field(method, options = {})
54
53
  options[:uploadcare] ||= {}
55
54
  options[:uploadcare][:multiple] = true
56
55
  uploadcare_uploader(method, options)
@@ -60,10 +59,12 @@ module Uploadcare::Rails::ActionView
60
59
  # and then choose either multiple or single-file upload.
61
60
  # not that this method WILL override custom settings in order
62
61
  # to prevent method collisions
63
- def uploadcare_field method, options={}
64
- if @object.try("has_#{method}_as_uploadcare_file?".to_sym) && !@object.try("has_#{method}_as_uploadcare_group?".to_sym)
62
+ def uploadcare_field(method, options = {})
63
+ if @object.try("has_#{ method }_as_uploadcare_file?".to_sym) &&
64
+ !@object.try("has_#{ method }_as_uploadcare_group?".to_sym)
65
65
  uploadcare_single_uploader_field(method, options)
66
- elsif !@object.try("has_#{method}_as_uploadcare_file?".to_sym) && @object.try("has_#{method}_as_uploadcare_group?".to_sym)
66
+ elsif !@object.try("has_#{ method }_as_uploadcare_file?".to_sym) &&
67
+ @object.try("has_#{ method }_as_uploadcare_group?".to_sym)
67
68
  uploadcare_multiple_uploader_field(method, options)
68
69
  else
69
70
  uploadcare_uploader(method, options)
@@ -71,4 +72,5 @@ module Uploadcare::Rails::ActionView
71
72
  end
72
73
  end
73
74
  end
74
- ActionView::Base.send :include, Uploadcare::Rails::ActionView::UploaderTags
75
+
76
+ ActionView::Base.send :include, Uploadcare::Rails::ActionView::UploaderTags
@@ -3,7 +3,7 @@ require "uploadcare/rails/objects/file"
3
3
  module Uploadcare
4
4
  module Rails
5
5
  module ActiveRecord
6
- def has_uploadcare_file attribute, options={}
6
+ def has_uploadcare_file(attribute, options={})
7
7
 
8
8
  define_method "has_#{attribute}_as_uploadcare_file?" do
9
9
  true
@@ -13,65 +13,53 @@ module Uploadcare
13
13
  false
14
14
  end
15
15
 
16
- define_method "build_file" do
16
+ define_method 'build_file' do
17
17
  cdn_url = attributes[attribute.to_s].to_s
18
18
  return nil if cdn_url.empty?
19
19
 
20
20
  api = ::Rails.application.config.uploadcare.api
21
21
  cache = ::Rails.cache
22
22
 
23
- if file_obj = cache.read(cdn_url)
24
- file = Uploadcare::Rails::File.new api, cdn_url, file_obj
23
+ if file_obj ||= cache.read(cdn_url)
24
+ Uploadcare::Rails::File.new(api, cdn_url, file_obj)
25
25
  else
26
- file = Uploadcare::Rails::File.new api, cdn_url
26
+ Uploadcare::Rails::File.new(api, cdn_url)
27
27
  end
28
-
29
- file
30
28
  end
31
29
 
32
30
  # attribute method - return file object
33
31
  # it is not the ::File but ::Rails::File
34
32
  # it has some helpers for rails enviroment
35
33
  # but it also has all the methods of Uploadcare::File so no worries.
36
- define_method "#{attribute}" do
37
- # cdn_url = attributes[attribute.to_s].to_s
38
-
39
- # return nil if cdn_url.empty?
40
-
41
- # api = Rails.application.config.uploadcare
42
- # cache = ::Rails.cache
43
-
44
- # if file_obj = cache.read(cdn_url)
45
- # file = Uploadcare::Rails::File.new api, cdn_url, file_obj
46
- # else
47
- # file = Uploadcare::Rails::File.new api, cdn_url
48
- # end
34
+ define_method "#{ attribute }" do
49
35
  build_file
50
36
  end
51
37
 
52
- define_method "check_#{attribute}_for_uuid" do
53
- url = self.attributes[attribute.to_s]
38
+ define_method "check_#{ attribute }_for_uuid" do
39
+ url = attributes[attribute.to_s]
54
40
  if url.present?
55
41
  result = Uploadcare::Parser.parse(url)
56
- raise "Invalid Uploadcare file uuid" unless result.is_a?(Uploadcare::Parser::File)
42
+ raise 'Invalid Uploadcare file uuid' unless result.is_a?(Uploadcare::Parser::File)
57
43
  end
58
44
  end
59
45
 
60
- define_method "store_#{attribute}" do
46
+ define_method "store_#{ attribute }" do
61
47
  file = build_file
62
48
 
49
+ return unless file
50
+
63
51
  begin
64
52
  file.store
65
53
  ::Rails.cache.write(file.cdn_url, file.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
66
54
  rescue Exception => e
67
- logger.error "\nError while saving a file #{cdn_url}: #{e.class} (#{e.message}):"
55
+ logger.error "\nError while saving a file #{file.cdn_url}: #{e.class} (#{e.message}):"
68
56
  logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
69
57
  end
70
58
 
71
59
  file
72
60
  end
73
61
 
74
- define_method "delete_#{attribute}" do
62
+ define_method "delete_#{ attribute }" do
75
63
  file = build_file
76
64
 
77
65
  begin
@@ -1,48 +1,49 @@
1
- require "uploadcare/rails/objects/group"
1
+ require 'uploadcare/rails/objects/group'
2
2
 
3
3
  module Uploadcare
4
4
  module Rails
5
5
  module ActiveRecord
6
- def has_uploadcare_group attribute, options={}
7
-
8
- define_method "has_#{attribute}_as_uploadcare_file?" do
6
+ def has_uploadcare_group(attribute, options = {})
7
+ define_method "has_#{ attribute }_as_uploadcare_file?" do
9
8
  false
10
9
  end
11
10
 
12
- define_method "has_#{attribute}_as_uploadcare_group?" do
11
+ define_method "has_#{ attribute }_as_uploadcare_group?" do
13
12
  true
14
13
  end
15
14
 
16
- define_method "build_group" do
15
+ define_method 'build_group' do
17
16
  cdn_url = attributes[attribute.to_s].to_s
18
17
  return nil if cdn_url.empty?
19
-
18
+
20
19
  api = ::Rails.application.config.uploadcare.api
21
20
  cache = ::Rails.cache
22
21
 
23
22
  if group_obj = cache.read(cdn_url)
24
- file = Uploadcare::Rails::Group.new api, cdn_url, group_obj
23
+ Uploadcare::Rails::Group.new(api, cdn_url, group_obj)
25
24
  else
26
- file = Uploadcare::Rails::Group.new api, cdn_url
25
+ Uploadcare::Rails::Group.new(api, cdn_url)
27
26
  end
28
-
29
- file
30
27
  end
31
28
 
32
29
  # attribute method - return file object
33
- define_method "#{attribute}" do
30
+ define_method "#{ attribute }" do
34
31
  build_group
35
32
  end
36
33
 
37
- define_method "check_#{attribute}_for_uuid" do
38
- url = self.attributes[attribute.to_s]
39
- unless url.empty?
34
+ define_method "check_#{ attribute }_for_uuid" do
35
+ url = attributes[attribute.to_s]
36
+
37
+ unless url.blank?
40
38
  result = Uploadcare::Parser.parse(url)
41
- raise "Invalid group uuid" unless result.is_a?(Uploadcare::Parser::Group)
39
+
40
+ unless result.is_a?(Uploadcare::Parser::Group)
41
+ raise 'Invalid group uuid'
42
+ end
42
43
  end
43
44
  end
44
45
 
45
- define_method "store_#{attribute}" do
46
+ define_method "store_#{ attribute }" do
46
47
  group = build_group
47
48
  return unless group.present?
48
49
 
@@ -50,12 +51,12 @@ module Uploadcare
50
51
  group.store
51
52
  ::Rails.cache.write(group.cdn_url, group.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
52
53
  rescue Exception => e
53
- logger.error "\nError while storing a group #{group.cdn_url}: #{e.class} (#{e.message}):"
54
+ logger.error "\nError while storing a group #{ group.cdn_url }: #{ e.class } (#{e.message }):"
54
55
  logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
55
56
  end
56
57
  end
57
58
 
58
- define_method "delete_#{attribute}" do
59
+ define_method "delete_#{ attribute }" do
59
60
  group = build_group
60
61
 
61
62
  begin
@@ -70,11 +71,11 @@ module Uploadcare
70
71
  # before saving we checking what it is a actually file cdn url
71
72
  # or uuid. uuid will do.
72
73
  # group url or uuid should raise an erorr
73
- before_save "check_#{attribute}_for_uuid"
74
+ before_save "check_#{ attribute }_for_uuid"
74
75
 
75
- after_save "store_#{attribute}" if UPLOADCARE_SETTINGS.store_after_save
76
+ after_save "store_#{ attribute }" if UPLOADCARE_SETTINGS.store_after_save
76
77
 
77
- after_destroy "delete_#{attribute}" if UPLOADCARE_SETTINGS.delete_after_destroy
78
+ after_destroy "delete_#{ attribute }" if UPLOADCARE_SETTINGS.delete_after_destroy
78
79
  end
79
80
  end
80
81
  end
@@ -1,10 +1,9 @@
1
1
  module Uploadcare
2
2
  module Rails
3
3
  module ActiveRecord
4
-
5
- # has_object :file, :uc_file as example
6
- def has_object type, attribute
4
+ # has_object(:file, :uc_file) as example
5
+ def has_object(type, attribute)
7
6
  end
8
7
  end
9
8
  end
10
- end
9
+ end