uploadcare-rails 1.0.6 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -1,13 +1,13 @@
1
- require "rails/engine"
2
- require "uploadcare/rails/settings"
1
+ require 'rails/engine'
2
+ require 'uploadcare/rails/settings'
3
3
 
4
4
  module Uploadcare
5
5
  module Rails
6
6
  class Engine < ::Rails::Engine
7
7
  initializer 'uploadcare-rails.load' do
8
8
  # load actual rails extentions
9
-
10
9
  # active record extention for stand-alone file models and models has files
10
+
11
11
  ActiveSupport.on_load :active_record do
12
12
  require 'uploadcare/rails/active_record/has_file'
13
13
  require 'uploadcare/rails/active_record/has_group'
@@ -1,9 +1,9 @@
1
1
  module Uploadcare::Rails::Formtastic
2
2
  class UploadcareInput < Formtastic::Inputs::HiddenInput
3
3
  include Uploadcare::Rails::ActionView::UploaderTags
4
-
4
+
5
5
  def role
6
- "#{@options[:role]}".strip
6
+ @options[:role].strip
7
7
  end
8
8
 
9
9
  def input_html_options
@@ -11,12 +11,14 @@ module Uploadcare::Rails::Formtastic
11
11
  super.merge role: role, data: @options[:data]
12
12
  end
13
13
 
14
- def is_file?
15
- @builder.object.try("has_#{@attribute}_as_uploadcare_file?".to_sym) && !@builder.object.try("has_#{@attribute}_as_uploadcare_group?".to_sym)
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)
16
17
  end
17
18
 
18
- def is_group?
19
- @builder.object.try("has_#{@attribute}_as_uploadcare_file?".to_sym) && !@builder.object.try("has_#{@attribute}_as_uploadcare_group?".to_sym)
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)
20
22
  end
21
23
  end
22
24
 
@@ -24,22 +26,24 @@ module Uploadcare::Rails::Formtastic
24
26
  def input
25
27
  @options ||= {}
26
28
  @options[:uploadcare] ||= {}
27
- if is_file?
28
- @options[:uploadcare][:multiple] = false
29
- elsif is_group?
30
- @options[:uploadcare][:multiple] = true
31
- end
29
+
30
+ @options[:uploadcare][:multiple] =
31
+ if file?
32
+ false
33
+ elsif group?
34
+ true
35
+ end
32
36
  super
33
37
  end
34
38
  end
35
39
 
36
- class UploadcareSingleUploaderInput < Uploadcare::Rails::Formtastic::UploadcareInput
40
+ class UploadcareSingleUploaderInput < Uploadcare::Rails::Formtastic::UploadcareInput
37
41
  def input
38
42
  @options ||= {}
39
43
  @options[:uploadcare] ||= {}
40
44
  @options[:uploadcare][:multiple] = false
41
45
  super
42
- end
46
+ end
43
47
  end
44
48
 
45
49
  class UploadcareMultipleUploaderInput < Uploadcare::Rails::Formtastic::UploadcareInput
@@ -48,8 +52,8 @@ module Uploadcare::Rails::Formtastic
48
52
  @options[:uploadcare] ||= {}
49
53
  @options[:uploadcare][:multiple] = true
50
54
  super
51
- end
55
+ end
52
56
  end
53
57
  end
54
58
 
55
- Formtastic::Inputs.send :include, Uploadcare::Rails::Formtastic
59
+ Formtastic::Inputs.send :include, Uploadcare::Rails::Formtastic
@@ -1,37 +1,25 @@
1
1
  module Uploadcare
2
2
  module Rails
3
3
  class File < Uploadcare::Api::File
4
-
5
- # override default to string method
6
- # instead of string representation of object it will return simple cdn url of a file
7
- def to_s
8
- cdn_url
4
+ def url(operations = nil)
5
+ cdn_url unless operations
6
+ cdn_url + prepared_operations(operations)
9
7
  end
10
8
 
11
- # construct image tag for file
12
- def image with_operations=true, options={}
13
- if with_operations
14
- url = cdn_url_with_operations
15
- else
16
- url = cdn_url
17
- end
18
-
19
- image_tag url, options
9
+ def prepared_operations(operations)
10
+ Uploadcare::Rails::Operations.new(operations).to_s
20
11
  end
21
12
 
22
- def load_data
23
- super
24
- ::Rails.cache.write(cdn_url, self.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
25
- self
13
+ # construct image tag for file
14
+ def image(operations = nil)
15
+ image_tag(url(operations))
26
16
  end
27
- alias_method :load, :load_data
28
17
 
29
- def load_data!
30
- super
31
- ::Rails.cache.write(cdn_url, self.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
32
- self
18
+ # override default to string method
19
+ # instead of string representation of object it will return simple cdn url of a file
20
+ def to_s
21
+ cdn_url
33
22
  end
34
- alias_method :load!, :load_data!
35
23
 
36
24
  def to_builder
37
25
  marshal_dump
@@ -41,7 +29,7 @@ module Uploadcare
41
29
  marshal_dump
42
30
  end
43
31
 
44
- def as_json options={}
32
+ def as_json(_options = {})
45
33
  marshal_dump
46
34
  end
47
35
 
@@ -2,35 +2,50 @@ module Uploadcare
2
2
  module Rails
3
3
  class Group < Uploadcare::Api::Group
4
4
 
5
- # override default to string method
6
- # for group we just will return the uuid
7
- def to_s
8
- uuid
5
+ # warkaround to build images urls without API request
6
+ # builds array of image urls including operations
7
+ def urls(operations = nil)
8
+ (0..files_count.to_i - 1).to_a.map do |i|
9
+ [
10
+ @api.options[:static_url_base],
11
+ uuid,
12
+ 'nth',
13
+ i,
14
+ Operations.new(operations).to_s
15
+ ].join('/')
16
+ end
9
17
  end
10
18
 
11
19
  def load_data
12
- unless is_loaded?
13
- load_data!
14
- end
20
+ load_data! unless is_loaded?
15
21
  self
16
22
  end
23
+
17
24
  alias_method :load, :load_data
18
25
 
19
26
  def load_data!
20
- data = @api.get "/groups/#{uuid}/"
21
- set_data data
22
- ::Rails.cache.write(cdn_url, self.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
27
+ set_data(@api.get("/groups/#{ uuid }/"))
28
+ cache_data
23
29
  self
24
30
  end
31
+
25
32
  alias_method :load!, :load_data!
26
33
 
34
+ def cache_data
35
+ return unless UPLOADCARE_SETTINGS.cache_groups
36
+ ::Rails.cache.write(cdn_url, marshal_dump)
37
+ self
38
+ end
39
+
27
40
  def marshal_dump
28
41
  table = @table.deep_dup.stringify_keys!
29
- if table["files"]
30
- table["files"].map! do |file|
42
+
43
+ if table['files']
44
+ table['files'].map! do |file|
31
45
  file.marshal_dump.stringify_keys
32
46
  end
33
47
  end
48
+
34
49
  table
35
50
  end
36
51
 
@@ -38,22 +53,30 @@ module Uploadcare
38
53
  marshal_dump
39
54
  end
40
55
 
41
- def as_json options={}
56
+ def as_json(_options = {})
42
57
  marshal_dump
43
58
  end
44
59
 
60
+ # override default to string method
61
+ # for group we just will return the uuid
62
+ def to_s
63
+ uuid
64
+ end
65
+
45
66
  private
46
- def map_files data
47
- data.stringify_keys!
48
- data["files"].map! do |file|
49
- unless file.nil?
50
- Uploadcare::Rails::File.new(@api, file["uuid"], file)
51
- else
52
- file
53
- end
67
+
68
+ def map_files(data)
69
+ data.stringify_keys!
70
+ data['files'].map! do |file|
71
+ if file.nil?
72
+ file
73
+ else
74
+ Uploadcare::Rails::File.new(@api, file['uuid'], file)
54
75
  end
55
- data
56
76
  end
77
+
78
+ data
79
+ end
57
80
  end
58
81
  end
59
82
  end
@@ -0,0 +1,57 @@
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,24 +1,25 @@
1
- require "ostruct"
1
+ require 'ostruct'
2
2
 
3
3
  module Uploadcare
4
4
  module Rails
5
5
  class Settings < OpenStruct
6
- include ActiveModel::Validations
6
+ include ::ActiveModel::Validations
7
7
 
8
8
  # note that i did not include pub and private key even for demo
9
9
  # point here to store them in one place and one place only
10
10
 
11
11
  # settings validation (hey, why not? we already have all rails stack loaded :)
12
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
13
+ validates :public_key, presence: true
14
+ validates :private_key, presence: true
15
15
 
16
16
  # TODO: ALL the keys god damn it
17
- PUBLIC_ALLOWED_KEYS = [:public_key,
17
+ PUBLIC_ALLOWED_KEYS = [
18
+ :public_key,
18
19
  :locale,
19
- :images_only,
20
+ :images_only ,
20
21
  :multiple,
21
- :multiple_min,
22
+ :multiple_min ,
22
23
  :multiple_max,
23
24
  :preview_step,
24
25
  :crop,
@@ -30,57 +31,52 @@ module Uploadcare
30
31
  :path_value
31
32
  ]
32
33
 
33
- def initialize config
34
+
35
+ def initialize(config)
34
36
  # extract envaroments settings
35
- settings = config["#{::Rails.env}"]
36
- raise ArgumentError.new "config is empty or not given at all" unless settings.present?
37
+ settings = config[::Rails.env]
38
+ unless settings.present?
39
+ raise ArgumentError, 'config is empty or not given at all'
40
+ end
37
41
 
38
42
  # build settings object (basicly openstruct)
39
43
  # merge defaults with actual settings
40
44
 
41
45
  # strip defaults suplied by uploadcare-ruby gem from private/pub key
42
- uc_defaults = Uploadcare::DEFAULT_SETTINGS.except!(:public_key, :private_key)
46
+ uc_defaults =
47
+ Uploadcare::DEFAULT_SETTINGS.except!(:public_key, :private_key)
48
+
43
49
  defaults = Uploadcare::Rails::DEFAULT_SETTINGS.merge!(uc_defaults)
44
50
  settings = defaults.merge!(settings)
45
51
  super settings
46
52
 
47
53
  # validates settings atributes.
48
54
  unless valid?
49
- raise ArgumentError.new "Private or public key options was not provided"
55
+ raise ArgumentError, 'Private or public key options were not provided'
50
56
  end
51
57
  end
52
58
 
53
-
54
59
  def api_settings
55
60
  @api_settings ||= build_api_settings
56
61
  end
57
62
 
58
-
59
63
  def widget_settings
60
64
  @widget_settings ||= build_widget_settings
61
65
  end
62
66
 
63
- def build_widget_settings
64
- settings = marshal_dump.slice(*PUBLIC_ALLOWED_KEYS)
65
- end
66
- private :build_widget_settings
67
-
68
-
69
- def build_api_settings
70
- marshal_dump
67
+ def api
68
+ @api ||= Uploadcare::Api.new(api_settings)
71
69
  end
72
- private :build_api_settings
73
70
 
71
+ private
74
72
 
75
- def api
76
- @api ||= build_api
73
+ def build_widget_settings
74
+ marshal_dump.slice(*PUBLIC_ALLOWED_KEYS)
77
75
  end
78
76
 
79
-
80
- def build_api
81
- Uploadcare::Api.new api_settings
77
+ def build_api_settings
78
+ marshal_dump
82
79
  end
83
- private :build_api
84
80
  end
85
81
  end
86
- end
82
+ end
@@ -2,60 +2,62 @@ module Uploadcare::Rails::SimpleForm
2
2
  class UploadcareInput < SimpleForm::Inputs::HiddenInput
3
3
  include Uploadcare::Rails::ActionView::UploaderTags
4
4
 
5
- def input wrapper_options=nil
5
+ def input(wrapper_options = nil)
6
6
  @options = uploadcare_uploader_options(@options)
7
7
  super
8
8
  end
9
9
 
10
10
  def role
11
- "#{@input_html_options[:role]} uploadcare-uploader".strip
11
+ "#{ @input_html_options[:role] } uploadcare-uploader".strip
12
12
  end
13
13
 
14
14
  def input_html_options
15
15
  @input_html_options.merge role: role, data: @options[:data]
16
16
  end
17
17
 
18
- def is_file?
19
- @builder.object.try("has_#{@attribute}_as_uploadcare_file?".to_sym) && !@builder.object.try("has_#{@attribute}_as_uploadcare_group?".to_sym)
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)
20
21
  end
21
22
 
22
- def is_group?
23
- @builder.object.try("has_#{@attribute}_as_uploadcare_file?".to_sym) && !@builder.object.try("has_#{@attribute}_as_uploadcare_group?".to_sym)
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)
24
26
  end
25
27
  end
26
28
 
27
-
28
-
29
29
  class UploadcareUploaderInput < Uploadcare::Rails::SimpleForm::UploadcareInput
30
- def input wrapper_options=nil
30
+ def input(wrapper_options = nil)
31
31
  @options ||= {}
32
32
  @options[:uploadcare] ||= {}
33
- if is_file?
34
- @options[:uploadcare][:multiple] = false
35
- elsif is_group?
36
- @options[:uploadcare][:multiple] = true
37
- end
33
+
34
+ @options[:uploadcare][:multiple] =
35
+ if file?
36
+ false
37
+ elsif group?
38
+ true
39
+ end
38
40
  super
39
41
  end
40
42
  end
41
43
 
42
- class UploadcareSingleUploaderInput < Uploadcare::Rails::SimpleForm::UploadcareInput
43
- def input wrapper_options=nil
44
+ class UploadcareSingleUploaderInput < Uploadcare::Rails::SimpleForm::UploadcareInput
45
+ def input(wrapper_options = nil)
44
46
  @options ||= {}
45
47
  @options[:uploadcare] ||= {}
46
48
  @options[:uploadcare][:multiple] = false
47
49
  super
48
- end
50
+ end
49
51
  end
50
52
 
51
53
  class UploadcareMultipleUploaderInput < Uploadcare::Rails::SimpleForm::UploadcareInput
52
- def input wrapper_options=nil
54
+ def input(wrapper_options = nil)
53
55
  @options ||= {}
54
56
  @options[:uploadcare] ||= {}
55
57
  @options[:uploadcare][:multiple] = true
56
58
  super
57
- end
59
+ end
58
60
  end
59
61
  end
60
62
 
61
- SimpleForm::Inputs.send :include, Uploadcare::Rails::SimpleForm
63
+ SimpleForm::Inputs.send :include, Uploadcare::Rails::SimpleForm