Sha256: 62d66bbf6345e901b846eb53132d83abb335756956bfdf9e6e5200238e173324

Contents?: true

Size: 793 Bytes

Versions: 9

Compression:

Stored size: 793 Bytes

Contents

module Fogged
  module HasManyResources
    extend ActiveSupport::Concern

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

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