uploadcare-rails 1.2.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Rakefile +5 -16
- data/lib/generators/templates/uploadcare_config_template.erb +138 -0
- data/lib/generators/uploadcare_config_generator.rb +7 -4
- data/lib/uploadcare/errors/type_error.rb +7 -0
- data/lib/uploadcare/rails/action_view/uploadcare_include_tags.rb +48 -0
- data/lib/uploadcare/rails/action_view/uploadcare_uploader_tags.rb +57 -0
- data/lib/uploadcare/rails/active_record/mount_uploadcare_file.rb +62 -0
- data/lib/uploadcare/rails/active_record/mount_uploadcare_file_group.rb +58 -0
- data/lib/uploadcare/rails/api/rest/base.rb +14 -0
- data/lib/uploadcare/rails/api/rest/conversion_api.rb +42 -0
- data/lib/uploadcare/rails/api/rest/file_api.rb +69 -0
- data/lib/uploadcare/rails/api/rest/group_api.rb +48 -0
- data/lib/uploadcare/rails/api/rest/project_api.rb +27 -0
- data/lib/uploadcare/rails/api/rest/webhook_api.rb +45 -0
- data/lib/uploadcare/rails/api/upload/base.rb +13 -0
- data/lib/uploadcare/rails/api/upload/upload_api.rb +53 -0
- data/lib/uploadcare/rails/configuration.rb +57 -0
- data/lib/uploadcare/rails/engine.rb +13 -17
- data/lib/uploadcare/rails/jobs/delete_file_job.rb +14 -0
- data/lib/uploadcare/rails/jobs/store_file_job.rb +14 -0
- data/lib/uploadcare/rails/jobs/store_group_job.rb +14 -0
- data/lib/uploadcare/rails/objects/concerns/loadable.rb +46 -0
- data/lib/uploadcare/rails/objects/file.rb +40 -20
- data/lib/uploadcare/rails/objects/group.rb +51 -57
- data/lib/uploadcare/rails/services/files_count_extractor.rb +14 -0
- data/lib/uploadcare/rails/services/id_extractor.rb +15 -0
- data/lib/uploadcare/rails/transformations/image_transformations.rb +45 -0
- data/lib/uploadcare/rails/version.rb +3 -1
- data/lib/uploadcare/rails.rb +10 -0
- data/lib/uploadcare-rails.rb +27 -16
- data/spec/fixtures/kitten.jpeg +0 -0
- data/spec/fixtures/vcr_cassettes/conversion_api_convert_document.yml +62 -0
- data/spec/fixtures/vcr_cassettes/conversion_api_convert_document_with_error.yml +62 -0
- data/spec/fixtures/vcr_cassettes/conversion_api_convert_video.yml +62 -0
- data/spec/fixtures/vcr_cassettes/conversion_api_convert_video_with_error.yml +62 -0
- data/spec/fixtures/vcr_cassettes/conversion_api_get_document_conversion_status.yml +59 -0
- data/spec/fixtures/vcr_cassettes/conversion_api_get_video_conversion_status.yml +59 -0
- data/spec/fixtures/vcr_cassettes/file_api_copy_file.yml +60 -0
- data/spec/fixtures/vcr_cassettes/file_api_delete_file.yml +58 -0
- data/spec/fixtures/vcr_cassettes/file_api_delete_files.yml +58 -0
- data/spec/fixtures/vcr_cassettes/file_api_get_file.yml +58 -0
- data/spec/fixtures/vcr_cassettes/file_api_get_files.yml +61 -0
- data/spec/fixtures/vcr_cassettes/file_api_load_file.yml +57 -0
- data/spec/fixtures/vcr_cassettes/file_api_store_file.yml +58 -0
- data/spec/fixtures/vcr_cassettes/file_api_store_files.yml +57 -0
- data/spec/fixtures/vcr_cassettes/group_api_create_group.yml +57 -0
- data/spec/fixtures/vcr_cassettes/group_api_get_group.yml +52 -0
- data/spec/fixtures/vcr_cassettes/group_api_get_groups.yml +57 -0
- data/spec/fixtures/vcr_cassettes/group_api_store_group.yml +57 -0
- data/spec/fixtures/vcr_cassettes/project_api_get_project.yml +59 -0
- data/spec/fixtures/vcr_cassettes/upload_upload_many_files.yml +55 -0
- data/spec/fixtures/vcr_cassettes/upload_upload_one_file.yml +105 -0
- data/spec/fixtures/vcr_cassettes/upload_upload_one_file_from_cdn.yml +111 -0
- data/spec/fixtures/vcr_cassettes/webhook_api_create_webhook.yml +59 -0
- data/spec/fixtures/vcr_cassettes/webhook_api_delete_webhook.yml +57 -0
- data/spec/fixtures/vcr_cassettes/webhook_api_get_webhooks.yml +59 -0
- data/spec/fixtures/vcr_cassettes/webhook_api_update_webhook.yml +59 -0
- data/spec/generators/uploadcare_config_generator_spec.rb +18 -26
- data/spec/spec_helper.rb +17 -69
- data/spec/support/generators.rb +8 -5
- data/spec/support/vcr.rb +14 -0
- data/spec/tmp/config/initializers/uploadcare.rb +138 -0
- data/spec/uploadcare/rails/action_view/uploadcare_uploader_tags_spec.rb +40 -0
- data/spec/uploadcare/rails/action_view/uploadcare_widget_tags_spec.rb +46 -0
- data/spec/uploadcare/rails/active_record/mount_uploadcare_file_spec.rb +33 -0
- data/spec/uploadcare/rails/active_record/mount_uploadcare_group_spec.rb +32 -0
- data/spec/uploadcare/rails/api/rest/conversion_api_spec.rb +110 -0
- data/spec/uploadcare/rails/api/rest/file_api_spec.rb +85 -0
- data/spec/uploadcare/rails/api/rest/group_api_spec.rb +61 -0
- data/spec/uploadcare/rails/api/rest/project_api_spec.rb +33 -0
- data/spec/uploadcare/rails/api/rest/webhook_api_spec.rb +59 -0
- data/spec/uploadcare/rails/api/upload/upload_api_spec.rb +82 -0
- data/spec/uploadcare/rails/jobs/delete_file_job_spec.rb +15 -0
- data/spec/uploadcare/rails/jobs/store_file_job_spec.rb +15 -0
- data/spec/uploadcare/rails/jobs/store_group_job_spec.rb +15 -0
- data/spec/uploadcare/rails/objects/file_spec.rb +77 -0
- data/spec/uploadcare/rails/objects/group_spec.rb +87 -0
- data/spec/uploadcare/rails/services/id_extractor_spec.rb +26 -0
- data/spec/uploadcare/rails/transformations/image_transformations_spec.rb +120 -0
- data/spec/uploadcare/rails_spec.rb +7 -0
- metadata +129 -351
- data/config/initializers/uploadcare.rb +0 -26
- data/config/uploadcare_defaults.yml +0 -12
- data/lib/generators/templates/uploadcare_config_template.yml +0 -67
- data/lib/tasks/uploadcare_rails_tasks.rake +0 -4
- data/lib/uploadcare/rails/action_view/include_tags.rb +0 -47
- data/lib/uploadcare/rails/action_view/uploader_tags.rb +0 -76
- data/lib/uploadcare/rails/active_record/has_file.rb +0 -92
- data/lib/uploadcare/rails/active_record/has_group.rb +0 -89
- data/lib/uploadcare/rails/active_record/has_object.rb +0 -9
- data/lib/uploadcare/rails/formtastic/formtastic.rb +0 -73
- data/lib/uploadcare/rails/operations.rb +0 -57
- data/lib/uploadcare/rails/settings.rb +0 -81
- data/lib/uploadcare/rails/simple_form/simple_form.rb +0 -63
- data/spec/caching/file_caching_spec.rb +0 -26
- data/spec/caching/group_caching_spec.rb +0 -40
- data/spec/dummy/README.rdoc +0 -28
- data/spec/dummy/Rakefile +0 -6
- data/spec/dummy/app/assets/javascripts/application.js +0 -13
- data/spec/dummy/app/assets/javascripts/post_with_collections.js +0 -2
- data/spec/dummy/app/assets/javascripts/posts.js +0 -2
- data/spec/dummy/app/assets/javascripts/posts_with_collection_and_files.js +0 -2
- data/spec/dummy/app/assets/stylesheets/application.css +0 -13
- data/spec/dummy/app/assets/stylesheets/post_with_collections.css +0 -4
- data/spec/dummy/app/assets/stylesheets/posts.css +0 -4
- data/spec/dummy/app/assets/stylesheets/posts_with_collection_and_files.css +0 -4
- data/spec/dummy/app/assets/stylesheets/scaffold.css +0 -56
- data/spec/dummy/app/controllers/application_controller.rb +0 -5
- data/spec/dummy/app/controllers/post_with_collections_controller.rb +0 -58
- data/spec/dummy/app/controllers/posts_controller.rb +0 -58
- data/spec/dummy/app/controllers/posts_with_collection_and_files_controller.rb +0 -58
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/helpers/post_with_collections_helper.rb +0 -2
- data/spec/dummy/app/helpers/posts_helper.rb +0 -2
- data/spec/dummy/app/helpers/posts_with_collection_and_files_helper.rb +0 -2
- data/spec/dummy/app/models/post.rb +0 -4
- data/spec/dummy/app/models/post_with_collection.rb +0 -4
- data/spec/dummy/app/models/posts_with_collection_and_file.rb +0 -4
- data/spec/dummy/app/views/layouts/application.html.erb +0 -16
- data/spec/dummy/app/views/post_with_collections/_form.html.erb +0 -25
- data/spec/dummy/app/views/post_with_collections/edit.html.erb +0 -6
- data/spec/dummy/app/views/post_with_collections/index.html.erb +0 -29
- data/spec/dummy/app/views/post_with_collections/new.html.erb +0 -5
- data/spec/dummy/app/views/post_with_collections/show.html.erb +0 -14
- data/spec/dummy/app/views/post_with_collections/show.json.jbuilder +0 -1
- data/spec/dummy/app/views/posts/_form.html.erb +0 -31
- data/spec/dummy/app/views/posts/edit.html.erb +0 -6
- data/spec/dummy/app/views/posts/index.html.erb +0 -37
- data/spec/dummy/app/views/posts/new.html.erb +0 -5
- data/spec/dummy/app/views/posts/post.json.builder +0 -1
- data/spec/dummy/app/views/posts/show.html.erb +0 -15
- data/spec/dummy/app/views/posts_with_collection_and_files/_form.html.erb +0 -27
- data/spec/dummy/app/views/posts_with_collection_and_files/edit.html.erb +0 -6
- data/spec/dummy/app/views/posts_with_collection_and_files/index.html.erb +0 -25
- data/spec/dummy/app/views/posts_with_collection_and_files/new.html.erb +0 -5
- data/spec/dummy/app/views/posts_with_collection_and_files/show.html.erb +0 -4
- data/spec/dummy/app/views/posts_with_collection_and_files/show.json.jbuilder +0 -1
- data/spec/dummy/bin/bundle +0 -3
- data/spec/dummy/bin/rails +0 -4
- data/spec/dummy/bin/rake +0 -4
- data/spec/dummy/config/application.rb +0 -23
- data/spec/dummy/config/boot.rb +0 -4
- data/spec/dummy/config/database.yml +0 -25
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -29
- data/spec/dummy/config/environments/production.rb +0 -80
- data/spec/dummy/config/environments/test.rb +0 -36
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy/config/initializers/formtastic.rb +0 -111
- data/spec/dummy/config/initializers/inflections.rb +0 -16
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -12
- data/spec/dummy/config/initializers/session_store.rb +0 -3
- data/spec/dummy/config/initializers/simple_form.rb +0 -167
- data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy/config/locales/en.yml +0 -23
- data/spec/dummy/config/locales/simple_form.en.yml +0 -31
- data/spec/dummy/config/routes.rb +0 -63
- data/spec/dummy/config/uploadcare.yml +0 -64
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/db/migrate/20171012001801_create_tables.rb +0 -27
- data/spec/dummy/db/migrate/20181001132710_add_other_file_to_post.rb +0 -8
- data/spec/dummy/db/migrate/20181001141434_add_other_file_to_post_with_collections.rb +0 -8
- data/spec/dummy/db/schema.rb +0 -39
- data/spec/dummy/lib/templates/erb/scaffold/_form.html.erb +0 -13
- data/spec/dummy/public/404.html +0 -58
- data/spec/dummy/public/422.html +0 -58
- data/spec/dummy/public/500.html +0 -57
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/helpers/form_helpers_has_file_spec.rb +0 -30
- data/spec/helpers/form_helpers_has_group_spec.rb +0 -30
- data/spec/helpers/form_helpers_spec.rb +0 -33
- data/spec/helpers/formtastic_spec.rb +0 -25
- data/spec/helpers/include_tags_spec.rb +0 -54
- data/spec/helpers/simple_form_spec.rb +0 -25
- data/spec/helpers/uploader_tags_spec.rb +0 -21
- data/spec/models/has_both_file_and_group_spec.rb +0 -43
- data/spec/models/has_file_spec.rb +0 -46
- data/spec/models/has_group_spec.rb +0 -57
- data/spec/models/has_several_files_spec.rb +0 -55
- data/spec/models/has_several_groups_spec.rb +0 -62
- data/spec/objects/file_spec.rb +0 -27
- data/spec/objects/group_spec.rb +0 -51
- data/spec/operations_spec.rb +0 -28
- data/spec/uploadcare/rails/settings_spec.rb +0 -136
- data/spec/uploadcare_rails_settings_spec.rb +0 -56
- data/spec/view.png +0 -0
- data/spec/view2.jpg +0 -0
@@ -1,67 +0,0 @@
|
|
1
|
-
defaults: &defaults
|
2
|
-
public_key: "demopublickey" # replace it with your public key
|
3
|
-
private_key: "demoprivatekey" # replace it with your private key
|
4
|
-
|
5
|
-
# instances of widget initialized for you on page load
|
6
|
-
live: true
|
7
|
-
|
8
|
-
# cache files json dumps to prevent calling to server if it posible
|
9
|
-
cache_files: true
|
10
|
-
|
11
|
-
# cache groups json dumps to prevent calling to server if it posible
|
12
|
-
cache_groups: true
|
13
|
-
|
14
|
-
# store file or group after model is created or updated
|
15
|
-
store_after_save: true
|
16
|
-
|
17
|
-
# deletes file or group after model object is deleted
|
18
|
-
delete_after_destroy: true
|
19
|
-
|
20
|
-
# Avaliable options are listed at https://uploadcare.com/docs/uploads/widget/config/
|
21
|
-
# please note, that for options marked as Global: N/A — there is no practical meaning
|
22
|
-
# of putting this option here, as it will not have any effect.
|
23
|
-
# But it also will not break anything.
|
24
|
-
|
25
|
-
# for preview step use:
|
26
|
-
# preview_step: false # true or false
|
27
|
-
|
28
|
-
# for clearable option (allows user to remove uploaded file from widget) use:
|
29
|
-
# clearable: false # true or false
|
30
|
-
|
31
|
-
# for setting tabs use tabs option:
|
32
|
-
# tabs: "url file facebook" # etc
|
33
|
-
# read more here: https://uploadcare.com/docs/uploads/widget/upload_sources/
|
34
|
-
#
|
35
|
-
# Full list of tabs:
|
36
|
-
# |----------------------------------------|
|
37
|
-
# | Code | File Source | Default |
|
38
|
-
# |------------|-----------------|---------|
|
39
|
-
# | url | Any URL | On |
|
40
|
-
# | file | Local disk | On |
|
41
|
-
# | facebook | Facebook | On |
|
42
|
-
# | dropbox | Dropbox | Off |
|
43
|
-
# | gdrive | Google Drive | On |
|
44
|
-
# | box | Box | On |
|
45
|
-
# | skydrive | SkyDrive | On |
|
46
|
-
# | instagram | Instagram | On |
|
47
|
-
# | evernote | Evernote | On |
|
48
|
-
# | vk | VK | Off |
|
49
|
-
# |________________________________________|
|
50
|
-
|
51
|
-
# for locale option use:
|
52
|
-
# locale: "en"
|
53
|
-
|
54
|
-
# for autostore option use:
|
55
|
-
# autostore: true #true or false
|
56
|
-
|
57
|
-
# for manual start (which means you will need to initialize uploaders yourself) use:
|
58
|
-
# manual_start: false # true or false
|
59
|
-
|
60
|
-
development:
|
61
|
-
<<: *defaults
|
62
|
-
|
63
|
-
test:
|
64
|
-
<<: *defaults
|
65
|
-
|
66
|
-
production:
|
67
|
-
<<: *defaults
|
@@ -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,92 +0,0 @@
|
|
1
|
-
require "uploadcare/rails/objects/file"
|
2
|
-
|
3
|
-
module Uploadcare
|
4
|
-
module Rails
|
5
|
-
module ActiveRecord
|
6
|
-
module InstanceMethods
|
7
|
-
def build_file(attribute)
|
8
|
-
cdn_url = attributes[attribute.to_s].to_s
|
9
|
-
return nil if cdn_url.empty?
|
10
|
-
|
11
|
-
api = ::Rails.application.config.uploadcare.api
|
12
|
-
cache = ::Rails.cache
|
13
|
-
|
14
|
-
if file_obj ||= cache.read(cdn_url)
|
15
|
-
Uploadcare::Rails::File.new(api, cdn_url, file_obj)
|
16
|
-
else
|
17
|
-
Uploadcare::Rails::File.new(api, cdn_url)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def has_uploadcare_file(attribute, options={})
|
23
|
-
include Uploadcare::Rails::ActiveRecord::InstanceMethods
|
24
|
-
|
25
|
-
define_method "has_#{attribute}_as_uploadcare_file?" do
|
26
|
-
true
|
27
|
-
end
|
28
|
-
|
29
|
-
define_method "has_#{attribute}_as_uploadcare_group?" do
|
30
|
-
false
|
31
|
-
end
|
32
|
-
|
33
|
-
# attribute method - return file object
|
34
|
-
# it is not the ::File but ::Rails::File
|
35
|
-
# it has some helpers for rails enviroment
|
36
|
-
# but it also has all the methods of Uploadcare::File so no worries.
|
37
|
-
define_method "#{ attribute }" do
|
38
|
-
build_file attribute
|
39
|
-
end
|
40
|
-
|
41
|
-
define_method "check_#{ attribute }_for_uuid" do
|
42
|
-
url = attributes[attribute.to_s]
|
43
|
-
if url.present?
|
44
|
-
result = Uploadcare::Parser.parse(url)
|
45
|
-
raise 'Invalid Uploadcare file uuid' unless result.is_a?(Uploadcare::Parser::File)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
define_method "store_#{ attribute }" do
|
50
|
-
file = build_file attribute
|
51
|
-
return unless file
|
52
|
-
|
53
|
-
begin
|
54
|
-
file.store
|
55
|
-
::Rails.cache.write(file.cdn_url, file.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
|
56
|
-
rescue Exception => e
|
57
|
-
logger.error "\nError while saving a file #{file.cdn_url}: #{e.class} (#{e.message}):"
|
58
|
-
logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
|
59
|
-
end
|
60
|
-
|
61
|
-
file
|
62
|
-
end
|
63
|
-
|
64
|
-
define_method "delete_#{ attribute }" do
|
65
|
-
file = build_file attribute
|
66
|
-
return unless file
|
67
|
-
|
68
|
-
begin
|
69
|
-
file.delete
|
70
|
-
::Rails.cache.write(file.cdn_url, file.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
|
71
|
-
rescue Exception => e
|
72
|
-
logger.error "\nError while deleting a file #{cdn_url}: #{e.class} (#{e.message}):"
|
73
|
-
logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
|
74
|
-
end
|
75
|
-
|
76
|
-
file
|
77
|
-
end
|
78
|
-
|
79
|
-
# before saving we checking what it is a actually file cdn url
|
80
|
-
# or uuid. uuid will do.
|
81
|
-
# group url or uuid should raise an erorr
|
82
|
-
before_save "check_#{attribute}_for_uuid".to_sym
|
83
|
-
|
84
|
-
after_save "store_#{attribute}".to_sym if UPLOADCARE_SETTINGS.store_after_save
|
85
|
-
|
86
|
-
after_destroy "delete_#{attribute}".to_sym if UPLOADCARE_SETTINGS.delete_after_destroy
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
ActiveRecord::Base.extend Uploadcare::Rails::ActiveRecord
|
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'uploadcare/rails/objects/group'
|
2
|
-
|
3
|
-
module Uploadcare
|
4
|
-
module Rails
|
5
|
-
module ActiveRecord
|
6
|
-
module InstanceMethods
|
7
|
-
def build_group(attribute)
|
8
|
-
cdn_url = attributes[attribute.to_s].to_s
|
9
|
-
return nil if cdn_url.empty?
|
10
|
-
|
11
|
-
api = ::Rails.application.config.uploadcare.api
|
12
|
-
cache = ::Rails.cache
|
13
|
-
|
14
|
-
if group_obj = cache.read(cdn_url)
|
15
|
-
Uploadcare::Rails::Group.new(api, cdn_url, group_obj)
|
16
|
-
else
|
17
|
-
Uploadcare::Rails::Group.new(api, cdn_url)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def has_uploadcare_group(attribute, options = {})
|
23
|
-
include Uploadcare::Rails::ActiveRecord::InstanceMethods
|
24
|
-
|
25
|
-
define_method "has_#{ attribute }_as_uploadcare_file?" do
|
26
|
-
false
|
27
|
-
end
|
28
|
-
|
29
|
-
define_method "has_#{ attribute }_as_uploadcare_group?" do
|
30
|
-
true
|
31
|
-
end
|
32
|
-
|
33
|
-
# attribute method - return file object
|
34
|
-
define_method "#{ attribute }" do
|
35
|
-
build_group attribute
|
36
|
-
end
|
37
|
-
|
38
|
-
define_method "check_#{ attribute }_for_uuid" do
|
39
|
-
url = attributes[attribute.to_s]
|
40
|
-
|
41
|
-
unless url.blank?
|
42
|
-
result = Uploadcare::Parser.parse(url)
|
43
|
-
|
44
|
-
unless result.is_a?(Uploadcare::Parser::Group)
|
45
|
-
raise 'Invalid group uuid'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
define_method "store_#{ attribute }" do
|
51
|
-
group = build_group attribute
|
52
|
-
return unless group.present?
|
53
|
-
|
54
|
-
begin
|
55
|
-
group.store
|
56
|
-
::Rails.cache.write(group.cdn_url, group.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
|
57
|
-
rescue Exception => e
|
58
|
-
logger.error "\nError while storing a group #{ group.cdn_url }: #{ e.class } (#{e.message }):"
|
59
|
-
logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
define_method "delete_#{ attribute }" do
|
64
|
-
group = build_group attribute
|
65
|
-
return unless group
|
66
|
-
|
67
|
-
begin
|
68
|
-
group.delete
|
69
|
-
::Rails.cache.write(group.cdn_url, group.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
|
70
|
-
rescue Exception => e
|
71
|
-
logger.error "\nError while deleting a group #{group.cdn_url}: #{e.class} (#{e.message}):"
|
72
|
-
logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
|
73
|
-
end
|
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,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
|