uploadcare-rails 1.2.0.pre.alpha → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +5 -5
  2. data/Rakefile +5 -16
  3. data/lib/generators/templates/uploadcare_config_template.erb +138 -0
  4. data/lib/generators/uploadcare_config_generator.rb +7 -4
  5. data/lib/uploadcare/errors/type_error.rb +7 -0
  6. data/lib/uploadcare/rails/action_view/uploadcare_include_tags.rb +48 -0
  7. data/lib/uploadcare/rails/action_view/uploadcare_uploader_tags.rb +57 -0
  8. data/lib/uploadcare/rails/active_record/mount_uploadcare_file.rb +62 -0
  9. data/lib/uploadcare/rails/active_record/mount_uploadcare_file_group.rb +58 -0
  10. data/lib/uploadcare/rails/api/rest/base.rb +14 -0
  11. data/lib/uploadcare/rails/api/rest/conversion_api.rb +42 -0
  12. data/lib/uploadcare/rails/api/rest/file_api.rb +69 -0
  13. data/lib/uploadcare/rails/api/rest/group_api.rb +48 -0
  14. data/lib/uploadcare/rails/api/rest/project_api.rb +27 -0
  15. data/lib/uploadcare/rails/api/rest/webhook_api.rb +45 -0
  16. data/lib/uploadcare/rails/api/upload/base.rb +13 -0
  17. data/lib/uploadcare/rails/api/upload/upload_api.rb +53 -0
  18. data/lib/uploadcare/rails/configuration.rb +57 -0
  19. data/lib/uploadcare/rails/engine.rb +13 -17
  20. data/lib/uploadcare/rails/jobs/delete_file_job.rb +14 -0
  21. data/lib/uploadcare/rails/jobs/store_file_job.rb +14 -0
  22. data/lib/uploadcare/rails/jobs/store_group_job.rb +14 -0
  23. data/lib/uploadcare/rails/objects/concerns/loadable.rb +46 -0
  24. data/lib/uploadcare/rails/objects/file.rb +40 -20
  25. data/lib/uploadcare/rails/objects/group.rb +51 -57
  26. data/lib/uploadcare/rails/services/files_count_extractor.rb +14 -0
  27. data/lib/uploadcare/rails/services/id_extractor.rb +15 -0
  28. data/lib/uploadcare/rails/transformations/image_transformations.rb +45 -0
  29. data/lib/uploadcare/rails/version.rb +3 -1
  30. data/lib/uploadcare/rails.rb +10 -0
  31. data/lib/uploadcare-rails.rb +27 -9
  32. data/spec/fixtures/kitten.jpeg +0 -0
  33. data/spec/fixtures/vcr_cassettes/conversion_api_convert_document.yml +62 -0
  34. data/spec/fixtures/vcr_cassettes/conversion_api_convert_document_with_error.yml +62 -0
  35. data/spec/fixtures/vcr_cassettes/conversion_api_convert_video.yml +62 -0
  36. data/spec/fixtures/vcr_cassettes/conversion_api_convert_video_with_error.yml +62 -0
  37. data/spec/fixtures/vcr_cassettes/conversion_api_get_document_conversion_status.yml +59 -0
  38. data/spec/fixtures/vcr_cassettes/conversion_api_get_video_conversion_status.yml +59 -0
  39. data/spec/fixtures/vcr_cassettes/file_api_copy_file.yml +60 -0
  40. data/spec/fixtures/vcr_cassettes/file_api_delete_file.yml +58 -0
  41. data/spec/fixtures/vcr_cassettes/file_api_delete_files.yml +58 -0
  42. data/spec/fixtures/vcr_cassettes/file_api_get_file.yml +58 -0
  43. data/spec/fixtures/vcr_cassettes/file_api_get_files.yml +61 -0
  44. data/spec/fixtures/vcr_cassettes/file_api_load_file.yml +57 -0
  45. data/spec/fixtures/vcr_cassettes/file_api_store_file.yml +58 -0
  46. data/spec/fixtures/vcr_cassettes/file_api_store_files.yml +57 -0
  47. data/spec/fixtures/vcr_cassettes/group_api_create_group.yml +57 -0
  48. data/spec/fixtures/vcr_cassettes/group_api_get_group.yml +52 -0
  49. data/spec/fixtures/vcr_cassettes/group_api_get_groups.yml +57 -0
  50. data/spec/fixtures/vcr_cassettes/group_api_store_group.yml +57 -0
  51. data/spec/fixtures/vcr_cassettes/project_api_get_project.yml +59 -0
  52. data/spec/fixtures/vcr_cassettes/upload_upload_many_files.yml +55 -0
  53. data/spec/fixtures/vcr_cassettes/upload_upload_one_file.yml +105 -0
  54. data/spec/fixtures/vcr_cassettes/upload_upload_one_file_from_cdn.yml +111 -0
  55. data/spec/fixtures/vcr_cassettes/webhook_api_create_webhook.yml +59 -0
  56. data/spec/fixtures/vcr_cassettes/webhook_api_delete_webhook.yml +57 -0
  57. data/spec/fixtures/vcr_cassettes/webhook_api_get_webhooks.yml +59 -0
  58. data/spec/fixtures/vcr_cassettes/webhook_api_update_webhook.yml +59 -0
  59. data/spec/generators/uploadcare_config_generator_spec.rb +18 -26
  60. data/spec/spec_helper.rb +17 -62
  61. data/spec/support/generators.rb +8 -5
  62. data/spec/support/vcr.rb +14 -0
  63. data/spec/tmp/config/initializers/uploadcare.rb +138 -0
  64. data/spec/uploadcare/rails/action_view/uploadcare_uploader_tags_spec.rb +40 -0
  65. data/spec/uploadcare/rails/action_view/uploadcare_widget_tags_spec.rb +46 -0
  66. data/spec/uploadcare/rails/active_record/mount_uploadcare_file_spec.rb +33 -0
  67. data/spec/uploadcare/rails/active_record/mount_uploadcare_group_spec.rb +32 -0
  68. data/spec/uploadcare/rails/api/rest/conversion_api_spec.rb +110 -0
  69. data/spec/uploadcare/rails/api/rest/file_api_spec.rb +85 -0
  70. data/spec/uploadcare/rails/api/rest/group_api_spec.rb +61 -0
  71. data/spec/uploadcare/rails/api/rest/project_api_spec.rb +33 -0
  72. data/spec/uploadcare/rails/api/rest/webhook_api_spec.rb +59 -0
  73. data/spec/uploadcare/rails/api/upload/upload_api_spec.rb +82 -0
  74. data/spec/uploadcare/rails/jobs/delete_file_job_spec.rb +15 -0
  75. data/spec/uploadcare/rails/jobs/store_file_job_spec.rb +15 -0
  76. data/spec/uploadcare/rails/jobs/store_group_job_spec.rb +15 -0
  77. data/spec/uploadcare/rails/objects/file_spec.rb +77 -0
  78. data/spec/uploadcare/rails/objects/group_spec.rb +87 -0
  79. data/spec/uploadcare/rails/services/id_extractor_spec.rb +26 -0
  80. data/spec/uploadcare/rails/transformations/image_transformations_spec.rb +120 -0
  81. data/spec/uploadcare/rails_spec.rb +7 -0
  82. metadata +131 -346
  83. data/config/initializers/uploadcare.rb +0 -26
  84. data/config/uploadcare_defaults.yml +0 -12
  85. data/lib/generators/templates/uploadcare_config_template.yml +0 -71
  86. data/lib/tasks/uploadcare_rails_tasks.rake +0 -4
  87. data/lib/uploadcare/rails/action_view/include_tags.rb +0 -47
  88. data/lib/uploadcare/rails/action_view/uploader_tags.rb +0 -76
  89. data/lib/uploadcare/rails/active_record/has_file.rb +0 -89
  90. data/lib/uploadcare/rails/active_record/has_group.rb +0 -84
  91. data/lib/uploadcare/rails/active_record/has_object.rb +0 -9
  92. data/lib/uploadcare/rails/formtastic/formtastic.rb +0 -73
  93. data/lib/uploadcare/rails/operations.rb +0 -57
  94. data/lib/uploadcare/rails/settings.rb +0 -81
  95. data/lib/uploadcare/rails/simple_form/simple_form.rb +0 -63
  96. data/spec/caching/file_caching_spec.rb +0 -26
  97. data/spec/caching/group_caching_spec.rb +0 -40
  98. data/spec/dummy/README.rdoc +0 -28
  99. data/spec/dummy/Rakefile +0 -6
  100. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  101. data/spec/dummy/app/assets/javascripts/post_with_collections.js +0 -2
  102. data/spec/dummy/app/assets/javascripts/posts.js +0 -2
  103. data/spec/dummy/app/assets/javascripts/posts_with_collection_and_files.js +0 -2
  104. data/spec/dummy/app/assets/javascripts/uploadcare-1.5.5.min.js +0 -16
  105. data/spec/dummy/app/assets/stylesheets/application.css +0 -13
  106. data/spec/dummy/app/assets/stylesheets/post_with_collections.css +0 -4
  107. data/spec/dummy/app/assets/stylesheets/posts.css +0 -4
  108. data/spec/dummy/app/assets/stylesheets/posts_with_collection_and_files.css +0 -4
  109. data/spec/dummy/app/assets/stylesheets/scaffold.css +0 -56
  110. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  111. data/spec/dummy/app/controllers/post_with_collections_controller.rb +0 -58
  112. data/spec/dummy/app/controllers/posts_controller.rb +0 -58
  113. data/spec/dummy/app/controllers/posts_with_collection_and_files_controller.rb +0 -58
  114. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  115. data/spec/dummy/app/helpers/post_with_collections_helper.rb +0 -2
  116. data/spec/dummy/app/helpers/posts_helper.rb +0 -2
  117. data/spec/dummy/app/helpers/posts_with_collection_and_files_helper.rb +0 -2
  118. data/spec/dummy/app/models/post.rb +0 -3
  119. data/spec/dummy/app/models/post_with_collection.rb +0 -3
  120. data/spec/dummy/app/models/posts_with_collection_and_file.rb +0 -4
  121. data/spec/dummy/app/views/layouts/application.html.erb +0 -16
  122. data/spec/dummy/app/views/post_with_collections/_form.html.erb +0 -25
  123. data/spec/dummy/app/views/post_with_collections/edit.html.erb +0 -6
  124. data/spec/dummy/app/views/post_with_collections/index.html.erb +0 -29
  125. data/spec/dummy/app/views/post_with_collections/new.html.erb +0 -5
  126. data/spec/dummy/app/views/post_with_collections/show.html.erb +0 -14
  127. data/spec/dummy/app/views/post_with_collections/show.json.jbuilder +0 -1
  128. data/spec/dummy/app/views/posts/_form.html.erb +0 -31
  129. data/spec/dummy/app/views/posts/edit.html.erb +0 -6
  130. data/spec/dummy/app/views/posts/index.html.erb +0 -37
  131. data/spec/dummy/app/views/posts/new.html.erb +0 -5
  132. data/spec/dummy/app/views/posts/post.json.builder +0 -1
  133. data/spec/dummy/app/views/posts/show.html.erb +0 -15
  134. data/spec/dummy/app/views/posts_with_collection_and_files/_form.html.erb +0 -27
  135. data/spec/dummy/app/views/posts_with_collection_and_files/edit.html.erb +0 -6
  136. data/spec/dummy/app/views/posts_with_collection_and_files/index.html.erb +0 -25
  137. data/spec/dummy/app/views/posts_with_collection_and_files/new.html.erb +0 -5
  138. data/spec/dummy/app/views/posts_with_collection_and_files/show.html.erb +0 -4
  139. data/spec/dummy/app/views/posts_with_collection_and_files/show.json.jbuilder +0 -1
  140. data/spec/dummy/bin/bundle +0 -3
  141. data/spec/dummy/bin/rails +0 -4
  142. data/spec/dummy/bin/rake +0 -4
  143. data/spec/dummy/config/application.rb +0 -23
  144. data/spec/dummy/config/boot.rb +0 -5
  145. data/spec/dummy/config/database.yml +0 -25
  146. data/spec/dummy/config/environment.rb +0 -5
  147. data/spec/dummy/config/environments/development.rb +0 -29
  148. data/spec/dummy/config/environments/production.rb +0 -80
  149. data/spec/dummy/config/environments/test.rb +0 -36
  150. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  151. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  152. data/spec/dummy/config/initializers/formtastic.rb +0 -111
  153. data/spec/dummy/config/initializers/inflections.rb +0 -16
  154. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  155. data/spec/dummy/config/initializers/secret_token.rb +0 -12
  156. data/spec/dummy/config/initializers/session_store.rb +0 -3
  157. data/spec/dummy/config/initializers/simple_form.rb +0 -167
  158. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  159. data/spec/dummy/config/locales/en.yml +0 -23
  160. data/spec/dummy/config/locales/simple_form.en.yml +0 -31
  161. data/spec/dummy/config/routes.rb +0 -63
  162. data/spec/dummy/config/uploadcare.yml +0 -68
  163. data/spec/dummy/config.ru +0 -4
  164. data/spec/dummy/db/migrate/20171012001801_create_tables.rb +0 -27
  165. data/spec/dummy/db/schema.rb +0 -37
  166. data/spec/dummy/lib/templates/erb/scaffold/_form.html.erb +0 -13
  167. data/spec/dummy/public/404.html +0 -58
  168. data/spec/dummy/public/422.html +0 -58
  169. data/spec/dummy/public/500.html +0 -57
  170. data/spec/dummy/public/favicon.ico +0 -0
  171. data/spec/helpers/form_helpers_has_file_spec.rb +0 -30
  172. data/spec/helpers/form_helpers_has_group_spec.rb +0 -30
  173. data/spec/helpers/form_helpers_spec.rb +0 -33
  174. data/spec/helpers/formtastic_spec.rb +0 -25
  175. data/spec/helpers/include_tags_spec.rb +0 -54
  176. data/spec/helpers/simple_form_spec.rb +0 -25
  177. data/spec/helpers/uploader_tags_spec.rb +0 -21
  178. data/spec/models/has_both_file_and_group_spec.rb +0 -43
  179. data/spec/models/has_file_spec.rb +0 -46
  180. data/spec/models/has_group_spec.rb +0 -57
  181. data/spec/objects/file_spec.rb +0 -27
  182. data/spec/objects/group_spec.rb +0 -51
  183. data/spec/operations_spec.rb +0 -28
  184. data/spec/uploadcare/rails/settings_spec.rb +0 -136
  185. data/spec/uploadcare_rails_settings_spec.rb +0 -50
  186. data/spec/view.png +0 -0
  187. data/spec/view2.jpg +0 -0
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :uploadcare_rails do
3
- # # Task goes here
4
- # end
@@ -1,47 +0,0 @@
1
- module Uploadcare::Rails::ActionView
2
- module IncludeTags
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
- 'libs',
15
- 'widget',
16
- settings[:version],
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)
24
- end
25
-
26
- alias_method :inlude_uploadcare_widget, :include_uploadcare_widget_from_cdn
27
- alias_method :uplodacare_widget, :include_uploadcare_widget_from_cdn
28
-
29
- def uploadcare_settings(options = {})
30
- settings = UPLOADCARE_SETTINGS.widget_settings.merge!(options)
31
-
32
- js_settings = ''
33
- settings.each do |k, v|
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
40
- end
41
-
42
- javascript_tag(js_settings)
43
- end
44
- end
45
- end
46
-
47
- ActionView::Base.send :include, Uploadcare::Rails::ActionView::IncludeTags
@@ -1,76 +0,0 @@
1
- # require "action_view_form_builder"
2
-
3
- module Uploadcare::Rails::ActionView
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
-
11
- # merge uploadcare options into data-attributes
12
- # IMPORTANT: custome data-attrs will be overriden by
13
- # the uploadcare options in case of collision.
14
- return options unless options[:uploadcare]
15
-
16
- options[:data] = options[:data].merge!(options[:uploadcare])
17
- options.except(:uploadcare)
18
- end
19
-
20
- def uploadcare_uploader_tag(name, options = {})
21
- hidden_field_tag(name, nil, uploadcare_uploader_options(options))
22
- end
23
-
24
- def uploadcare_uploader_field(object_name, method, options = {})
25
- hidden_field(object_name, method, uploadcare_uploader_options(options))
26
- end
27
-
28
- def self.included(_)
29
- ActionView::Helpers::FormBuilder.
30
- send(:include, Uploadcare::Rails::ActionView::FormBuilder)
31
- end
32
- end
33
-
34
- module FormBuilder
35
- # call uploadcare_uploader field for form object
36
- # none of options will be overriden and given as-is
37
- def uploadcare_uploader(method, options = {})
38
- @template.
39
- uploadcare_uploader_field(
40
- @object_name, method, objectify_options(options)
41
- )
42
- end
43
-
44
- # forse-set the data-multiple="false" for uploader
45
- def uploadcare_single_uploader_field(method, options = {})
46
- options[:uploadcare] ||= {}
47
- options[:uploadcare][:multiple] = false
48
- uploadcare_uploader(method, options)
49
- end
50
-
51
- # forse-set the data-multiple="true" for uploader
52
- def uploadcare_multiple_uploader_field(method, options = {})
53
- options[:uploadcare] ||= {}
54
- options[:uploadcare][:multiple] = true
55
- uploadcare_uploader(method, options)
56
- end
57
-
58
- # smart method to detect wich of - file or group - pesent
59
- # and then choose either multiple or single-file upload.
60
- # not that this method WILL override custom settings in order
61
- # to prevent method collisions
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
- uploadcare_single_uploader_field(method, options)
66
- elsif !@object.try("has_#{ method }_as_uploadcare_file?".to_sym) &&
67
- @object.try("has_#{ method }_as_uploadcare_group?".to_sym)
68
- uploadcare_multiple_uploader_field(method, options)
69
- else
70
- uploadcare_uploader(method, options)
71
- end
72
- end
73
- end
74
- end
75
-
76
- ActionView::Base.send :include, Uploadcare::Rails::ActionView::UploaderTags
@@ -1,89 +0,0 @@
1
- require "uploadcare/rails/objects/file"
2
-
3
- module Uploadcare
4
- module Rails
5
- module ActiveRecord
6
- def has_uploadcare_file(attribute, options={})
7
-
8
- define_method "has_#{attribute}_as_uploadcare_file?" do
9
- true
10
- end
11
-
12
- define_method "has_#{attribute}_as_uploadcare_group?" do
13
- false
14
- end
15
-
16
- define_method 'build_file' do
17
- cdn_url = attributes[attribute.to_s].to_s
18
- return nil if cdn_url.empty?
19
-
20
- api = ::Rails.application.config.uploadcare.api
21
- cache = ::Rails.cache
22
-
23
- if file_obj ||= cache.read(cdn_url)
24
- Uploadcare::Rails::File.new(api, cdn_url, file_obj)
25
- else
26
- Uploadcare::Rails::File.new(api, cdn_url)
27
- end
28
- end
29
-
30
- # attribute method - return file object
31
- # it is not the ::File but ::Rails::File
32
- # it has some helpers for rails enviroment
33
- # but it also has all the methods of Uploadcare::File so no worries.
34
- define_method "#{ attribute }" do
35
- build_file
36
- end
37
-
38
- define_method "check_#{ attribute }_for_uuid" do
39
- url = attributes[attribute.to_s]
40
- if url.present?
41
- result = Uploadcare::Parser.parse(url)
42
- raise 'Invalid Uploadcare file uuid' unless result.is_a?(Uploadcare::Parser::File)
43
- end
44
- end
45
-
46
- define_method "store_#{ attribute }" do
47
- file = build_file
48
-
49
- return unless file
50
-
51
- begin
52
- file.store
53
- ::Rails.cache.write(file.cdn_url, file.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
54
- rescue Exception => e
55
- logger.error "\nError while saving a file #{file.cdn_url}: #{e.class} (#{e.message}):"
56
- logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
57
- end
58
-
59
- file
60
- end
61
-
62
- define_method "delete_#{ attribute }" do
63
- file = build_file
64
-
65
- begin
66
- file.delete
67
- ::Rails.cache.write(file.cdn_url, file.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
68
- rescue Exception => e
69
- logger.error "\nError while deleting a file #{cdn_url}: #{e.class} (#{e.message}):"
70
- logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
71
- end
72
-
73
- file
74
- end
75
-
76
- # before saving we checking what it is a actually file cdn url
77
- # or uuid. uuid will do.
78
- # group url or uuid should raise an erorr
79
- before_save "check_#{attribute}_for_uuid".to_sym
80
-
81
- after_save "store_#{attribute}".to_sym if UPLOADCARE_SETTINGS.store_after_save
82
-
83
- after_destroy "delete_#{attribute}".to_sym if UPLOADCARE_SETTINGS.delete_after_destroy
84
- end
85
- end
86
- end
87
- end
88
-
89
- ActiveRecord::Base.extend Uploadcare::Rails::ActiveRecord
@@ -1,84 +0,0 @@
1
- require 'uploadcare/rails/objects/group'
2
-
3
- module Uploadcare
4
- module Rails
5
- module ActiveRecord
6
- def has_uploadcare_group(attribute, options = {})
7
- define_method "has_#{ attribute }_as_uploadcare_file?" do
8
- false
9
- end
10
-
11
- define_method "has_#{ attribute }_as_uploadcare_group?" do
12
- true
13
- end
14
-
15
- define_method 'build_group' do
16
- cdn_url = attributes[attribute.to_s].to_s
17
- return nil if cdn_url.empty?
18
-
19
- api = ::Rails.application.config.uploadcare.api
20
- cache = ::Rails.cache
21
-
22
- if group_obj = cache.read(cdn_url)
23
- Uploadcare::Rails::Group.new(api, cdn_url, group_obj)
24
- else
25
- Uploadcare::Rails::Group.new(api, cdn_url)
26
- end
27
- end
28
-
29
- # attribute method - return file object
30
- define_method "#{ attribute }" do
31
- build_group
32
- end
33
-
34
- define_method "check_#{ attribute }_for_uuid" do
35
- url = attributes[attribute.to_s]
36
-
37
- unless url.blank?
38
- result = Uploadcare::Parser.parse(url)
39
-
40
- unless result.is_a?(Uploadcare::Parser::Group)
41
- raise 'Invalid group uuid'
42
- end
43
- end
44
- end
45
-
46
- define_method "store_#{ attribute }" do
47
- group = build_group
48
- return unless group.present?
49
-
50
- begin
51
- group.store
52
- ::Rails.cache.write(group.cdn_url, group.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
53
- rescue Exception => e
54
- logger.error "\nError while storing a group #{ group.cdn_url }: #{ e.class } (#{e.message }):"
55
- logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
56
- end
57
- end
58
-
59
- define_method "delete_#{ attribute }" do
60
- group = build_group
61
-
62
- begin
63
- group.delete
64
- ::Rails.cache.write(group.cdn_url, group.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
65
- rescue Exception => e
66
- logger.error "\nError while deleting a group #{group.cdn_url}: #{e.class} (#{e.message}):"
67
- logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
68
- end
69
- end
70
-
71
- # before saving we checking what it is a actually file cdn url
72
- # or uuid. uuid will do.
73
- # group url or uuid should raise an erorr
74
- before_save "check_#{ attribute }_for_uuid".to_sym
75
-
76
- after_save "store_#{ attribute }".to_sym if UPLOADCARE_SETTINGS.store_after_save
77
-
78
- after_destroy "delete_#{ attribute }".to_sym if UPLOADCARE_SETTINGS.delete_after_destroy
79
- end
80
- end
81
- end
82
- end
83
-
84
- ActiveRecord::Base.extend Uploadcare::Rails::ActiveRecord
@@ -1,9 +0,0 @@
1
- module Uploadcare
2
- module Rails
3
- module ActiveRecord
4
- # has_object(:file, :uc_file) as example
5
- def has_object(type, attribute)
6
- end
7
- end
8
- end
9
- end
@@ -1,73 +0,0 @@
1
- module Uploadcare::Rails::Formtastic
2
- class UploadcareInput < Formtastic::Inputs::HiddenInput
3
- include Uploadcare::Rails::ActionView::UploaderTags
4
-
5
- def role
6
- @options[:role].strip
7
- end
8
-
9
- def input_html_options
10
- @options = uploadcare_uploader_options(@options)
11
- super.merge role: role, data: @options[:data]
12
- end
13
-
14
- def file?
15
- @builder.object.try("has_#{ @attribute }_as_uploadcare_file?".to_sym) &&
16
- !@builder.object.try("has_#{ @attribute }_as_uploadcare_group?".to_sym)
17
- end
18
-
19
- def group?
20
- @builder.object.try("has_#{ @attribute }_as_uploadcare_file?".to_sym) &&
21
- !@builder.object.try("has_#{ @attribute }_as_uploadcare_group?".to_sym)
22
- end
23
- end
24
-
25
- class UploadcareUploaderInput < Uploadcare::Rails::Formtastic::UploadcareInput
26
- def input
27
- @options ||= {}
28
- @options[:uploadcare] ||= {}
29
-
30
- @options[:uploadcare][:multiple] =
31
- if file?
32
- false
33
- elsif group?
34
- true
35
- end
36
- super
37
- end
38
- end
39
-
40
- class UploadcareSingleUploaderInput < Uploadcare::Rails::Formtastic::UploadcareInput
41
- def input
42
- @options ||= {}
43
- @options[:uploadcare] ||= {}
44
- @options[:uploadcare][:multiple] = false
45
- super
46
- end
47
-
48
- def input_html_options
49
- super.tap do |opts|
50
- opts[:data] ||= {}
51
- opts[:data].merge!(multiple: false)
52
- end
53
- end
54
- end
55
-
56
- class UploadcareMultipleUploaderInput < Uploadcare::Rails::Formtastic::UploadcareInput
57
- def input
58
- @options ||= {}
59
- @options[:uploadcare] ||= {}
60
- @options[:uploadcare][:multiple] = true
61
- super
62
- end
63
-
64
- def input_html_options
65
- super.tap do |opts|
66
- opts[:data] ||= {}
67
- opts[:data].merge!(multiple: true)
68
- end
69
- end
70
- end
71
- end
72
-
73
- Formtastic::Inputs.send :include, Uploadcare::Rails::Formtastic
@@ -1,57 +0,0 @@
1
- module Uploadcare
2
- module Rails
3
- class Operations
4
- def initialize(operations = nil)
5
- @operations = operations
6
- end
7
-
8
- def to_s
9
- return '' unless @operations
10
-
11
- result = @operations.map do |operation, options|
12
- next unless respond_to?(operation)
13
- send(operation, options)
14
- end
15
-
16
- ['-/', result.join('/-/'), '/'].
17
- join.
18
- gsub(%r{\/+}, '/').
19
- to_s
20
- end
21
-
22
- def format(options)
23
- return unless %w(png jpeg).include?(options.to_s)
24
- "format/#{ options }"
25
- end
26
-
27
- def progressive(options)
28
- return unless %w(yes no).include?(options.to_s)
29
- "progressive/#{ options }"
30
- end
31
-
32
- def quality(options)
33
- available_options = %w(normal better best lighter lightest)
34
- return unless available_options.include?(options.to_s)
35
- "quality/#{ options }"
36
- end
37
-
38
- def preview(options)
39
- if option = options[/^\d+x\d+$/]
40
- "preview/#{ option }"
41
- end
42
- end
43
-
44
- def resize(options)
45
- if option = options[/^(\d+x\d+)$|^(\d+x)$|^(x\d+)$/]
46
- "resize/#{ option }"
47
- end
48
- end
49
-
50
- alias_method :size, :resize
51
-
52
- def inline(options)
53
- options
54
- end
55
- end
56
- end
57
- end
@@ -1,81 +0,0 @@
1
- require 'ostruct'
2
-
3
- module Uploadcare
4
- module Rails
5
- class Settings < OpenStruct
6
- include ::ActiveModel::Validations
7
-
8
- # note that i did not include pub and private key even for demo
9
- # point here to store them in one place and one place only
10
-
11
- # settings validation (hey, why not? we already have all rails stack loaded :)
12
- # so just use the godnes of rails magic and praise the lord!
13
- validates :public_key, presence: true
14
- validates :private_key, presence: true
15
-
16
- # TODO: ALL the keys god damn it
17
- PUBLIC_ALLOWED_KEYS = [
18
- :public_key,
19
- :locale,
20
- :images_only ,
21
- :multiple,
22
- :multiple_min ,
23
- :multiple_max,
24
- :preview_step,
25
- :crop,
26
- :clearable,
27
- :tabs,
28
- :autostore,
29
- :live,
30
- :manual_start,
31
- :path_value
32
- ]
33
-
34
- def initialize(config)
35
- # extract envaroments settings
36
- settings = config.with_indifferent_access[::Rails.env]
37
- unless settings.present?
38
- raise ArgumentError, 'config is empty or not given at all'
39
- end
40
-
41
- # build settings object (basicly openstruct)
42
- # merge defaults with actual settings
43
-
44
- # strip defaults suplied by uploadcare-ruby gem from private/pub key
45
- uc_defaults =
46
- Uploadcare::DEFAULT_SETTINGS.except(:public_key, :private_key)
47
-
48
- defaults = Uploadcare::Rails::DEFAULT_SETTINGS.merge(uc_defaults)
49
- settings = defaults.merge(settings)
50
- super settings
51
-
52
- # validates settings atributes.
53
- unless valid?
54
- raise ArgumentError, 'Private or public key options were not provided'
55
- end
56
- end
57
-
58
- def api_settings
59
- @api_settings ||= build_api_settings
60
- end
61
-
62
- def widget_settings
63
- @widget_settings ||= build_widget_settings
64
- end
65
-
66
- def api
67
- @api ||= Uploadcare::Api.new(api_settings)
68
- end
69
-
70
- private
71
-
72
- def build_widget_settings
73
- marshal_dump.slice(*PUBLIC_ALLOWED_KEYS)
74
- end
75
-
76
- def build_api_settings
77
- marshal_dump
78
- end
79
- end
80
- end
81
- end
@@ -1,63 +0,0 @@
1
- module Uploadcare::Rails::SimpleForm
2
- class UploadcareInput < SimpleForm::Inputs::HiddenInput
3
- include Uploadcare::Rails::ActionView::UploaderTags
4
-
5
- def input(wrapper_options = nil)
6
- @options = uploadcare_uploader_options(@options)
7
- super
8
- end
9
-
10
- def role
11
- "#{ @input_html_options[:role] } uploadcare-uploader".strip
12
- end
13
-
14
- def input_html_options
15
- @input_html_options.merge role: role, data: @options[:data]
16
- end
17
-
18
- def file?
19
- @builder.object.try("has_#{ @attribute }_as_uploadcare_file?".to_sym) &&
20
- !@builder.object.try("has_#{ @attribute }_as_uploadcare_group?".to_sym)
21
- end
22
-
23
- def group?
24
- @builder.object.try("has_#{ @attribute }_as_uploadcare_file?".to_sym) &&
25
- !@builder.object.try("has_#{ @attribute }_as_uploadcare_group?".to_sym)
26
- end
27
- end
28
-
29
- class UploadcareUploaderInput < Uploadcare::Rails::SimpleForm::UploadcareInput
30
- def input(wrapper_options = nil)
31
- @options ||= {}
32
- @options[:uploadcare] ||= {}
33
-
34
- @options[:uploadcare][:multiple] =
35
- if file?
36
- false
37
- elsif group?
38
- true
39
- end
40
- super
41
- end
42
- end
43
-
44
- class UploadcareSingleUploaderInput < Uploadcare::Rails::SimpleForm::UploadcareInput
45
- def input(wrapper_options = nil)
46
- @options ||= {}
47
- @options[:uploadcare] ||= {}
48
- @options[:uploadcare][:multiple] = false
49
- super
50
- end
51
- end
52
-
53
- class UploadcareMultipleUploaderInput < Uploadcare::Rails::SimpleForm::UploadcareInput
54
- def input(wrapper_options = nil)
55
- @options ||= {}
56
- @options[:uploadcare] ||= {}
57
- @options[:uploadcare][:multiple] = true
58
- super
59
- end
60
- end
61
- end
62
-
63
- SimpleForm::Inputs.send :include, Uploadcare::Rails::SimpleForm
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Uploadcare::Rails::File, :vcr do
4
- let(:post) { Post.new(title: 'Post title', file: FILE_CDN_URL) }
5
-
6
- context 'when object is not persisted' do
7
- it 'its file is not loaded' do
8
- expect(post.file).not_to be_loaded
9
- end
10
- end
11
-
12
- skip { expect(Rails.cache.read(post.file.cdn_url)).to be_nil }
13
-
14
- skip 'rails cache should updates after load call' do
15
- post.file.load!
16
- cached = Rails.cache.read FILE_CDN_URL
17
- cached.should be_kind_of(Hash)
18
- cached['datetime_uploaded'].should be_kind_of(String)
19
- end
20
-
21
- skip 'file should stay loaded' do
22
- post.file.loaded?.should == false
23
- post.file.load!
24
- post.file.loaded?.should == true
25
- end
26
- end
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Uploadcare::Rails::Group, :vcr do
4
- let(:post) { PostWithCollection.new(title: 'Title', file: GROUP_CDN_URL) }
5
-
6
- after :each do
7
- Rails.cache.delete(GROUP_CDN_URL)
8
- end
9
-
10
- it 'should be not loaded by default' do
11
- expect(post.file).not_to be_loaded
12
- end
13
-
14
- it 'rails cache should be nil' do
15
- expect(Rails.cache.read(post.file.cdn_url)).to be_nil
16
- end
17
-
18
- it 'rails cache should updates after load call' do
19
- post.file.load!
20
- cached = Rails.cache.read GROUP_CDN_URL
21
-
22
- expect(cached).to be_a(Hash)
23
- expect(cached['datetime_created']).to be_a(String)
24
- end
25
-
26
- it 'group should stay loaded' do
27
- expect(post.file).not_to be_loaded
28
- post.file.load!
29
- expect(post.file).to be_loaded
30
- end
31
-
32
- it 'cached group should contained json representation of files',
33
- vcr: { cassette_name: 'group_cahsing_file_load'} do
34
- post.file.load!
35
- cached = Rails.cache.read GROUP_CDN_URL
36
-
37
- expect(cached).to be_a(Hash)
38
- expect(cached['files'].sample).to be_a(Hash)
39
- end
40
- end
@@ -1,28 +0,0 @@
1
- == README
2
-
3
- This README would normally document whatever steps are necessary to get the
4
- application up and running.
5
-
6
- Things you may want to cover:
7
-
8
- * Ruby version
9
-
10
- * System dependencies
11
-
12
- * Configuration
13
-
14
- * Database creation
15
-
16
- * Database initialization
17
-
18
- * How to run the test suite
19
-
20
- * Services (job queues, cache servers, search engines, etc.)
21
-
22
- * Deployment instructions
23
-
24
- * ...
25
-
26
-
27
- Please feel free to use a different markup language if you do not plan to run
28
- <tt>rake doc:app</tt>.
data/spec/dummy/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- # Add your own tasks in files placed in lib/tasks ending in .rake,
2
- # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
-
4
- require File.expand_path('../config/application', __FILE__)
5
-
6
- Dummy::Application.load_tasks