Sha256: 42aa59430a3f265e9c032613080e35e599ae7646539540a7aea7bfe25ee5f274

Contents?: true

Size: 822 Bytes

Versions: 9

Compression:

Stored size: 822 Bytes

Contents

module Fogged
  module HasOneResource
    extend ActiveSupport::Concern

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

      def has_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::HasOneResource)

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
fogged-0.0.13 lib/fogged/has_one_resource.rb
fogged-0.0.12 lib/fogged/has_one_resource.rb
fogged-0.0.11 lib/fogged/has_one_resource.rb
fogged-0.0.10 lib/fogged/has_one_resource.rb
fogged-0.0.9 lib/fogged/has_one_resource.rb
fogged-0.0.8 lib/fogged/has_one_resource.rb
fogged-0.0.7 lib/fogged/has_one_resource.rb
fogged-0.0.6 lib/fogged/has_one_resource.rb
fogged-0.0.5 lib/fogged/has_one_resource.rb