Sha256: 0b6b0c44b5d0ff2c2f81a0a0e11431903689e0f02d5673c37ab845b8fbcf78ab

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'active_support/concern'
require 'carrierwave/mongoid'
require 'media_magick/attachment_uploader'

module MediaMagick
  module Model
    extend ActiveSupport::Concern

    module ClassMethods
      def attachs_many(name, options = {}, &block)
        klass = Class.new do
          include Mongoid::Document
          extend CarrierWave::Mount

          field :priority, type: Integer, default: 0
          default_scope asc(:priority)

          embedded_in :attachmentable, polymorphic: true
          mount_uploader name.to_s.singularize, (options[:uploader] || AttachmentUploader)

          self.const_set "TYPE", options[:type] || :image
          self.const_set "ATTACHMENT", name.to_s.singularize

          class_eval(&block) if block_given?

          def method_missing(method, args = nil)
            return self.send(self.class::ATTACHMENT).file.filename if method == :filename
            self.send(self.class::ATTACHMENT).send(method)
          end
        end

        name_camelcase = name.to_s.camelcase
        Object.const_set "#{self.name}#{name_camelcase}", klass

        embeds_many(name, :as => :attachmentable, class_name: "#{self}#{name_camelcase}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
media_magick-0.0.1 lib/media_magick/model.rb