Sha256: 52bd202712f8294ad20450f49b43eda573aa6375a6079a4bacd30d02480593ea

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require "uploadcare/rails/group"

module Uploadcare
  module Rails
    module ActiveRecord
      def has_uploadcare_group attribute, options={}

        define_method "has_uploadcare_file?" do
          false
        end

        define_method "has_uploadcare_group?" do
          true
        end

        # attribute method - return file object
        define_method "#{attribute}" do
          cdn_url = attributes[attribute.to_s].to_s
          
          return nil if cdn_url.empty?

          api = UPLOADCARE_SETTINGS.api
          file = Uploadcare::Rails::Group.new api, cdn_url
        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"

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

ActiveRecord::Base.extend Uploadcare::Rails::ActiveRecord

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uploadcare-rails-1.0.0.rc4 lib/uploadcare/rails/active_record_has_group.rb