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

Defined Under Namespace

Modules: InstanceMethods

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



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
85
86
87
# File 'lib/uploadcare/rails/active_record/has_file.rb', line 22

def has_uploadcare_file(attribute, options={})
  include Uploadcare::Rails::ActiveRecord::InstanceMethods

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

  define_method "has_#{attribute}_as_uploadcare_group?" do
    false
  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 attribute
  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 attribute
    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 attribute
    return unless 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".to_sym

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

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

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



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_group.rb', line 22

def has_uploadcare_group(attribute, options = {})
  include Uploadcare::Rails::ActiveRecord::InstanceMethods

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

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

  # attribute method - return file object
  define_method "#{ attribute }" do
    build_group attribute
  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 attribute
    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 attribute
    return unless 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".to_sym

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

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