lib/media_magick/model.rb in media_magick-0.1.0 vs lib/media_magick/model.rb in media_magick-0.1.1

- old
+ new

@@ -1,96 +1,89 @@ require 'active_support/concern' require 'carrierwave/mongoid' require 'media_magick/attachment_uploader' +require 'media_magick/video/parser' module MediaMagick module Model extend ActiveSupport::Concern module ClassMethods - def attachs_many(name, options = {}, &block) - warn "[DEPRECATION] `attachs_many` is deprecated. Please use `attaches_many` instead." - attaches_many(name, options, &block) - end + def attaches_many(name, options = {}) + attaches_block = block_given? ? Proc.new : nil - # - # TODO - # * refactor these methods to remove duplication - # - def attaches_many(name, options = {}, &block) - klass = Class.new do - include Mongoid::Document - extend CarrierWave::Mount + name_camelcase = create_attaches_class(name, options, attaches_block) do + create_video_methods(name) if options[:allow_videos] field :priority, type: Integer, default: 0 + default_scope asc(:priority) - if options[:relation] == :referenced - belongs_to :attachmentable, polymorphic: true - else - embedded_in :attachmentable, polymorphic: true - end + embedded_in(:attachmentable, polymorphic: true) + end - mount_uploader name.to_s.singularize, (options[:uploader] || AttachmentUploader) + embeds_many(name, :as => :attachmentable, class_name: "#{self}#{name_camelcase}") + end - self.const_set "TYPE", options[:type] || :image - self.const_set "ATTACHMENT", name.to_s.singularize + def attaches_one(name, options = {}) + attaches_block = block_given? ? Proc.new : nil - class_eval(&block) if block_given? + name_camelcase = create_attaches_class(name, options, attaches_block) do + create_video_methods(name) if options[:allow_videos] - 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 + embedded_in(name) end - name_camelcase = name.to_s.camelcase - Object.const_set "#{self}#{name_camelcase}", klass + embeds_one(name, class_name: "#{self}#{name_camelcase}", cascade_callbacks: true) + end - if options[:relation] == :referenced - klass.collection_name = "#{self.name}_#{name.to_s.camelcase}".parameterize + private - has_many(name, :as => :attachmentable, class_name: "#{self}#{name_camelcase}") + def create_attaches_class(name, options, attaches_block, &block) + klass = Class.new do + include Mongoid::Document + extend CarrierWave::Mount - field "#{name.to_s.singularize}_ids", type: Array + field :type, type: String, default: options[:as] || 'image' - before_create do - ids = self.send("#{name.to_s.singularize}_ids") || [] + def self.create_video_methods(name) + field :video, type: String - ids.each do |id| - "#{self.class}#{name_camelcase}".constantize.find(id).update_attributes(attachmentable: self) + def video=(url) + self.type = 'video' + super + + video = MediaMagick::Video::Parser.new(url) + + send(self.class::ATTACHMENT).store!(video.to_image) if video.valid? end + + def source(options = {}) + video = MediaMagick::Video::Parser.new(self.video) + + video.to_html(options) if video.valid? + end end - else - embeds_many(name, :as => :attachmentable, class_name: "#{self}#{name_camelcase}") - end - end - def attaches_one(name, options = {}, &block) - klass = Class.new do - include Mongoid::Document - extend CarrierWave::Mount + class_eval(&block) if block_given? - embedded_in name mount_uploader name.to_s.singularize, (options[:uploader] || AttachmentUploader) - accepts_nested_attributes_for name.to_s.singularize - self.const_set "TYPE", options[:type] || :image self.const_set "ATTACHMENT", name.to_s.singularize - class_eval(&block) if block_given? + class_eval(&attaches_block) if attaches_block 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_camelcase}", klass - embeds_one name, class_name: "#{self}#{name_camelcase}", cascade_callbacks: true + return name_camelcase end end end end