module Uploadcare::Rails::ActiveRecord

Public Instance Methods

has_object(type, attribute) click to toggle source

has_object(:file, :uc_file) as example

# File lib/uploadcare/rails/active_record/has_object.rb, line 5
def has_object(type, attribute)
end
has_uploadcare_file(attribute, options={}) click to toggle source
# 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 = {}) click to toggle source
# 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