Sha256: 34dab7fe2afd3c669f9406fa51ffa993ce9903b2e8d98263563787fd5bd3d6ef

Contents?: true

Size: 822 Bytes

Versions: 3

Compression:

Stored size: 822 Bytes

Contents

module Fogged
  module ActsAsHavingManyResources
    extend ActiveSupport::Concern

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

      def acts_as_having_many_resources(*args)
        options = args.extract_options!
        unless options.include?(:through)
          fail(ArgumentError, ":through option is mandatory")
        end
        has_many :resources, DEFAULT_OPTIONS.merge(options)
        validate :_check_resources, :unless => "resources.empty?"
      end
    end

    private

    def _check_resources
      return if resources.to_a.select(&:uploading).empty?
      errors.add(:resources, I18n.t("fogged.resources.still_uploading"))
    end
  end
end

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

Version data entries

3 entries across 3 versions & 1 rubygems

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