Module: Uploadcare::Rails::ActiveRecord

Defined in:
lib/uploadcare/rails/active_record/has_file.rb,
lib/uploadcare/rails/active_record/has_group.rb,
lib/uploadcare/rails/active_record/has_object.rb

Instance Method Summary collapse

Instance Method Details

#has_object(type, attribute) ⇒ Object

has_object(:file, :uc_file) as example



5
6
# File 'lib/uploadcare/rails/active_record/has_object.rb', line 5

def has_object(type, attribute)
end

#has_uploadcare_file(attribute, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/uploadcare/rails/active_record/has_file.rb', line 6

def has_uploadcare_file(attribute, options={})

  define_method "has_#{attribute}_as_uploadcare_file?" do
    true
  end

  define_method "has_#{attribute}_as_uploadcare_group?" do
    false
  end

  define_method 'build_file' do
    cdn_url = attributes[attribute.to_s].to_s
    return nil if cdn_url.empty?

    api = ::Rails.application.config.uploadcare.api
    cache = ::Rails.cache

    if file_obj ||= cache.read(cdn_url)
      Uploadcare::Rails::File.new(api, cdn_url, file_obj)
    else
      Uploadcare::Rails::File.new(api, cdn_url)
    end
  end

  # attribute method - return file object
  # it is not the ::File but ::Rails::File
  # it has some helpers for rails enviroment
  # but it also has all the methods of Uploadcare::File so no worries.
  define_method "#{ attribute }" do
    build_file
  end

  define_method "check_#{ attribute }_for_uuid" do
    url = attributes[attribute.to_s]
    if url.present?
      result = Uploadcare::Parser.parse(url)
      raise 'Invalid Uploadcare file uuid' unless result.is_a?(Uploadcare::Parser::File)
    end
  end

  define_method "store_#{ attribute }" do
    file = build_file

    return unless file

    begin
      file.store
      ::Rails.cache.write(file.cdn_url, file.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
    rescue Exception => e
      logger.error "\nError while saving a file #{file.cdn_url}: #{e.class} (#{e.message}):"
      logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
    end

    file
  end

  define_method "delete_#{ attribute }" do
    file = build_file

    begin
      file.delete
      ::Rails.cache.write(file.cdn_url, file.marshal_dump) if UPLOADCARE_SETTINGS.cache_files
    rescue Exception => e
      logger.error "\nError while deleting a file #{cdn_url}: #{e.class} (#{e.message}):"
      logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
    end

    file
  end

  # before saving we checking what it is a actually file cdn url
  # or uuid. uuid will do.
  # group url or uuid should raise an erorr
  before_save "check_#{attribute}_for_uuid"

  after_save "store_#{attribute}" if UPLOADCARE_SETTINGS.store_after_save

  after_destroy "delete_#{attribute}" if UPLOADCARE_SETTINGS.delete_after_destroy
end

#has_uploadcare_group(attribute, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/uploadcare/rails/active_record/has_group.rb', line 6

def has_uploadcare_group(attribute, options = {})
  define_method "has_#{ attribute }_as_uploadcare_file?" do
    false
  end

  define_method "has_#{ attribute }_as_uploadcare_group?" do
    true
  end

  define_method 'build_group' do
    cdn_url = attributes[attribute.to_s].to_s
    return nil if cdn_url.empty?

    api = ::Rails.application.config.uploadcare.api
    cache = ::Rails.cache

    if group_obj = cache.read(cdn_url)
      Uploadcare::Rails::Group.new(api, cdn_url, group_obj)
    else
      Uploadcare::Rails::Group.new(api, cdn_url)
    end
  end

  # attribute method - return file object
  define_method "#{ attribute }" do
    build_group
  end

  define_method "check_#{ attribute }_for_uuid" do
    url = attributes[attribute.to_s]

    unless url.blank?
      result = Uploadcare::Parser.parse(url)

      unless result.is_a?(Uploadcare::Parser::Group)
        raise 'Invalid group uuid'
      end
    end
  end

  define_method "store_#{ attribute }" do
    group = build_group
    return unless group.present?

    begin
      group.store
      ::Rails.cache.write(group.cdn_url, group.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
    rescue Exception => e
      logger.error "\nError while storing a group #{ group.cdn_url }: #{ e.class } (#{e.message }):"
      logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
    end
  end

  define_method "delete_#{ attribute }" do
    group = build_group

    begin
      group.delete
      ::Rails.cache.write(group.cdn_url, group.marshal_dump) if UPLOADCARE_SETTINGS.cache_groups
    rescue Exception => e
      logger.error "\nError while deleting a group #{group.cdn_url}: #{e.class} (#{e.message}):"
      logger.error "#{::Rails.backtrace_cleaner.clean(e.backtrace).join("\n ")}"
    end
  end

  # before saving we checking what it is a actually file cdn url
  # or uuid. uuid will do.
  # group url or uuid should raise an erorr
  before_save "check_#{ attribute }_for_uuid"

  after_save "store_#{ attribute }" if UPLOADCARE_SETTINGS.store_after_save

  after_destroy "delete_#{ attribute }" if UPLOADCARE_SETTINGS.delete_after_destroy
end