Sha256: 44c4633049cd6ce9f0536c9cd5f99662a3240801ccb83f079a856f6c43ce46ff

Contents?: true

Size: 851 Bytes

Versions: 3

Compression:

Stored size: 851 Bytes

Contents

module Fogged
  module ActsAsHavingOneResource
    extend ActiveSupport::Concern

    module ClassMethods
      DEFAULT_OPTIONS = {
        :dependent => :destroy,
        :class_name => "Fogged::Resource"
      }

      def acts_as_having_one_resource(*args)
        belongs_to :resource, DEFAULT_OPTIONS.merge(args.extract_options!)
        validate :_check_resource, :unless => "resource.blank?"

        define_method(:resource_id) do
          resource.try(:id)
        end

        define_method(:resource_id=) do |id|
          self.resource = id.blank? ? nil : Resource.find(id)
        end
      end
    end

    private

    def _check_resource
      return unless resource.uploading
      errors.add(:resource, I18n.t("fogged.resource.still_uploading"))
    end
  end
end

ActiveRecord::Base.send(:include, Fogged::ActsAsHavingOneResource)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fogged-0.0.4 lib/fogged/acts_as_having_one_resource.rb
fogged-0.0.3 lib/fogged/acts_as_having_one_resource.rb
fogged-0.0.2 lib/fogged/acts_as_having_one_resource.rb