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.
- checksums.yaml +4 -4
- data/config/initializers/uploadcare.rb +22 -23
- data/lib/uploadcare-rails.rb +6 -5
- data/lib/uploadcare/rails/action_view/include_tags.rb +35 -26
- data/lib/uploadcare/rails/action_view/uploader_tags.rb +31 -29
- data/lib/uploadcare/rails/active_record/has_file.rb +14 -26
- data/lib/uploadcare/rails/active_record/has_group.rb +23 -22
- data/lib/uploadcare/rails/active_record/has_object.rb +3 -4
- data/lib/uploadcare/rails/engine.rb +3 -3
- data/lib/uploadcare/rails/formtastic/formtastic.rb +19 -15
- data/lib/uploadcare/rails/objects/file.rb +13 -25
- data/lib/uploadcare/rails/objects/group.rb +45 -22
- data/lib/uploadcare/rails/operations.rb +57 -0
- data/lib/uploadcare/rails/settings.rb +26 -30
- data/lib/uploadcare/rails/simple_form/simple_form.rb +22 -20
- data/lib/uploadcare/rails/version.rb +1 -1
- data/spec/caching/file_caching_spec.rb +16 -24
- data/spec/caching/group_caching_spec.rb +23 -26
- data/{app → spec/dummy/app}/assets/javascripts/uploadcare-1.5.5.min.js +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +2 -2
- data/spec/dummy/config/routes.rb +2 -2
- data/spec/helpers/include_tags_spec.rb +41 -29
- data/spec/models/has_both_file_and_group_spec.rb +19 -17
- data/spec/models/has_file_spec.rb +46 -47
- data/spec/models/has_group_spec.rb +46 -32
- data/spec/objects/file_spec.rb +9 -11
- data/spec/objects/group_spec.rb +40 -29
- data/spec/operations_spec.rb +28 -0
- data/spec/spec_helper.rb +17 -5
- metadata +235 -199
- data/MIT-LICENSE +0 -20
- data/app/assets/javascripts/uploadcare-0.17.0.js +0 -8248
- data/app/assets/javascripts/uploadcare-0.17.0.min.js +0 -17
- data/app/assets/javascripts/uploadcare-0.18.3.js +0 -8484
- data/app/assets/javascripts/uploadcare-0.18.3.min.js +0 -17
- data/app/assets/javascripts/uploadcare-1.0.1.js +0 -8430
- data/app/assets/javascripts/uploadcare-1.0.1.min.js +0 -17
- data/app/assets/javascripts/uploadcare-1.3.1.js +0 -8563
- data/app/assets/javascripts/uploadcare-1.3.1.min.js +0 -17
- data/app/assets/javascripts/uploadcare-1.5.5.js +0 -9278
- data/app/assets/javascripts/uploadcare.js +0 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +0 -0
- data/spec/dummy/log/test.log +0 -2166
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 0113e45365a82f1af9474db0a516cae93dc19513
         | 
| 4 | 
            +
              data.tar.gz: 4fe03ac56616d9adcff45499832871d206f1d23d
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 9a112c863d5016ae1d7b191e64cb2a8b0a0c3d86cbac74a18a04abf95e8dbfe37afd30fec8676a39c4afc84622a6aa2d98ff66178ce423d1616c9e473523716f
         | 
| 7 | 
            +
              data.tar.gz: de5e0f0f18f2e5ee4f8325bd8e80b368dcce7e7ac899d99fbd08708476005a93618892448bda5aa0dfddbf8c55e9d83506826c5f643d5793766720d589d02eaa
         | 
| @@ -1,27 +1,26 @@ | |
| 1 | 
            -
            config_file =  | 
| 1 | 
            +
            config_file = Rails.root.join('config', 'uploadcare.yml')
         | 
| 2 2 |  | 
| 3 | 
            -
             | 
| 4 | 
            -
               | 
| 5 | 
            -
             | 
| 6 | 
            -
               | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
                  \e[ | 
| 14 | 
            -
                 | 
| 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 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
               | 
| 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
         | 
    
        data/lib/uploadcare-rails.rb
    CHANGED
    
    | @@ -1,11 +1,12 @@ | |
| 1 | 
            -
            require  | 
| 2 | 
            -
            require  | 
| 3 | 
            -
            require  | 
| 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:  | 
| 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  | 
| 4 | 
            -
                  settings = | 
| 5 | 
            -
                     | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
                   | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 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 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
                   | 
| 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 | 
            -
                     | 
| 27 | 
            -
                       | 
| 28 | 
            -
             | 
| 29 | 
            -
                       | 
| 30 | 
            -
             | 
| 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 | 
            -
                   | 
| 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 | 
| 6 | 
            -
                  options = options.symbolize_keys.deep_merge( | 
| 7 | 
            -
                    : | 
| 8 | 
            -
                    : | 
| 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 | 
            -
                   | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 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 | 
| 24 | 
            -
                   | 
| 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 | 
| 29 | 
            -
                   | 
| 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( | 
| 34 | 
            -
                  ActionView::Helpers::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 | 
| 42 | 
            -
                  @template. | 
| 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 | 
| 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 | 
| 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 | 
| 64 | 
            -
                  if @object.try("has_#{method}_as_uploadcare_file?".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) && | 
| 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 | 
            -
             | 
| 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 | 
| 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  | 
| 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  | 
| 24 | 
            -
                         | 
| 23 | 
            +
                      if file_obj ||= cache.read(cdn_url)
         | 
| 24 | 
            +
                        Uploadcare::Rails::File.new(api, cdn_url, file_obj)
         | 
| 25 25 | 
             
                      else
         | 
| 26 | 
            -
                         | 
| 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 =  | 
| 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  | 
| 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  | 
| 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 | 
| 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  | 
| 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 | 
            -
                         | 
| 23 | 
            +
                        Uploadcare::Rails::Group.new(api, cdn_url, group_obj)
         | 
| 25 24 | 
             
                      else
         | 
| 26 | 
            -
                         | 
| 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 =  | 
| 39 | 
            -
             | 
| 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 | 
            -
             | 
| 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
         |