Skip to content

Commit

Permalink
Mongoid and carrierwave support
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed Aug 23, 2011
1 parent cd71177 commit c6eff5a
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,5 +7,6 @@ test/dummy/log/*.log
test/dummy/public/ckeditor_assets/
test/dummy/tmp/
test/tmp
test/dummy/public/uploads/*

*.swp
1 change: 1 addition & 0 deletions README.rdoc
Expand Up @@ -194,5 +194,6 @@ By default, both these methods call "ckeditor_filebrowser_scope" method:

rake test
rake test CKEDITOR_ORM=mongoid
rake test CKEDITOR_BACKEND=carrierwave

This project rocks and uses MIT-LICENSE.
7 changes: 7 additions & 0 deletions Rakefile
Expand Up @@ -39,6 +39,13 @@ Rake::TestTask.new("test:integration") do |t|
t.verbose = false
end

Rake::TestTask.new("test:models") do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/models/*_test.rb'
t.verbose = false
end

task :default => :test

RDoc::Task.new do |rdoc|
Expand Down
5 changes: 5 additions & 0 deletions lib/ckeditor.rb
Expand Up @@ -18,6 +18,11 @@ module Hooks
autoload :SimpleFormBuilder, 'ckeditor/hooks/simple_form'
end

module Backend
autoload :Paperclip, 'ckeditor/backend/paperclip'
autoload :CarrierWave, 'ckeditor/backend/carrierwave'
end

# Allowed image file types for upload.
# Set to nil or [] (empty array) for all file types
mattr_accessor :image_file_types
Expand Down
64 changes: 64 additions & 0 deletions lib/ckeditor/backend/carrierwave.rb
@@ -0,0 +1,64 @@
require 'mime/types'
require 'mini_magick'

module Ckeditor
module Backend
module CarrierWave
def self.included(base)
base.send(:include, InstanceMethods)
base.send(:extend, ClassMethods)
end

module ClassMethods
def self.extended(base)
base.class_eval do
process :extract_content_type
process :set_size
end
end
end

module InstanceMethods
# process :strip
def strip
manipulate! do |img|
img.strip
img = yield(img) if block_given?
img
end
end

# process :quality => 85
def quality(percentage)
manipulate! do |img|
img.quality(percentage)
img = yield(img) if block_given?
img
end
end

def extract_content_type
if file.content_type == 'application/octet-stream' || file.content_type.blank?
content_type = MIME::Types.type_for(original_filename).first
else
content_type = file.content_type
end

model.data_content_type = content_type.to_s
end

def set_size
model.data_file_size = file.size
end

def read_dimensions
if model.image? && model.has_dimensions?
magick = ::MiniMagick::Image.new(current_path)
model.width, model.height = magick[:width], magick[:height]
end
end

end
end
end
end
53 changes: 53 additions & 0 deletions lib/ckeditor/backend/paperclip.rb
@@ -0,0 +1,53 @@
require 'mime/types'

module Ckeditor
module Backend
module Paperclip
def self.included(base)
base.send(:include, InstanceMethods)
base.send(:extend, ClassMethods)
end

module ClassMethods
def self.extended(base)
base.class_eval do
before_validation :extract_content_type
before_create :read_dimensions, :parameterize_filename

delegate :url, :path, :styles, :size, :content_type, :to => :data
end
end
end

module InstanceMethods
def geometry
@geometry ||= ::Paperclip::Geometry.from_file(data.to_file)
end

protected

def parameterize_filename
unless data_file_name.blank?
filename = Ckeditor::Utils.parameterize_filename(data_file_name)
self.data.instance_write(:file_name, filename)
end
end

def read_dimensions
if image? && has_dimensions?
self.width = geometry.width
self.height = geometry.height
end
end

# Extract content_type from filename using mime/types gem
def extract_content_type
if data_content_type == "application/octet-stream" && !data_file_name.blank?
content_types = MIME::Types.type_for(data_file_name)
self.data_content_type = content_types.first.to_s unless content_types.empty?
end
end
end
end
end
end
30 changes: 1 addition & 29 deletions lib/ckeditor/orm/base.rb
Expand Up @@ -14,13 +14,9 @@ def has_dimensions?
def image?
Ckeditor::IMAGE_TYPES.include?(data_content_type)
end

def geometry
@geometry ||= Paperclip::Geometry.from_file(data.to_file)
end

def format_created_at
I18n.l(created_at, :format => "%d.%m.%Y")
self.created_at.strftime("%d.%m.%Y")
end

def url_content
Expand All @@ -43,30 +39,6 @@ def as_json(options = nil)

super options
end

protected

def parameterize_filename
unless data_file_name.blank?
filename = Ckeditor::Utils.parameterize_filename(data_file_name)
self.data.instance_write(:file_name, filename)
end
end

def read_dimensions
if image? && has_dimensions?
self.width = geometry.width
self.height = geometry.height
end
end

# Extract content_type from filename using mime/types gem
def extract_content_type
if data_content_type == "application/octet-stream" && !data_file_name.blank?
content_types = MIME::Types.type_for(data_file_name)
self.data_content_type = content_types.first.to_s unless content_types.empty?
end
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/ckeditor/orm/mongoid.rb
Expand Up @@ -26,6 +26,11 @@ def as_json_methods
module ClassMethods
def self.extended(base)
base.class_eval do
field :data_content_type, :type => String
field :data_file_size, :type => Integer
field :width, :type => Integer
field :height, :type => Integer

belongs_to :assetable, :polymorphic => true

attr_accessible :data, :assetable_type, :assetable_id, :assetable
Expand Down
@@ -1,5 +1,5 @@
class Ckeditor::AttachmentFile < Ckeditor::Asset
mount_uploader :data, CkeditorAttachmentFileUploader
mount_uploader :data, CkeditorAttachmentFileUploader, :mount_on => :data_file_name

def url_thumb
@url_thumb ||= begin
Expand Down
@@ -1,5 +1,5 @@
class Ckeditor::Picture < Ckeditor::Asset
mount_uploader :data, CkeditorPictureUploader
mount_uploader :data, CkeditorPictureUploader, :mount_on => :data_file_name

def url_content
url(:content)
Expand Down
@@ -1,12 +1,18 @@
class CreateCkeditorAssets < ActiveRecord::Migration
def self.up
create_table :ckeditor_assets do |t|
t.string :data, :null => false
t.string :data_file_name, :null => false
t.string :data_content_type
t.integer :data_file_size

t.integer :assetable_id
t.string :assetable_type, :limit => 30
t.string :type, :limit => 30

# Uncomment it to save images dimensions, if your need it
# t.integer :width
# t.integer :height

t.timestamps
end

Expand Down
@@ -1,10 +1,4 @@
require 'mime/types'

class Ckeditor::Asset < ActiveRecord::Base
include Ckeditor::Orm::ActiveRecord::AssetBase

before_validation :extract_content_type
before_create :read_dimensions, :parameterize_filename

delegate :url, :path, :styles, :size, :content_type, :to => :data
include Ckeditor::Backend::Paperclip
end
@@ -1,5 +1,7 @@
# encoding: utf-8
class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave

# Include RMagick or ImageScience support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
Expand Down
@@ -1,5 +1,7 @@
# encoding: utf-8
class CkeditorPictureUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave

# Include RMagick or ImageScience support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
Expand All @@ -25,6 +27,8 @@ def store_dir
# def scale(width, height)
# # do something
# end

process :read_dimensions

# Create different versions of your uploaded files:
version :thumb do
Expand Down
@@ -1,5 +1,5 @@
class Ckeditor::AttachmentFile < Ckeditor::Asset
mount_uploader :data, CkeditorAttachmentFileUploader
mount_uploader :data, CkeditorAttachmentFileUploader, :mount_on => :data_file_name

def url_thumb
@url_thumb ||= begin
Expand Down
@@ -1,5 +1,5 @@
class Ckeditor::Picture < Ckeditor::Asset
mount_uploader :data, CkeditorPictureUploader
mount_uploader :data, CkeditorPictureUploader, :mount_on => :data_file_name

def url_content
url(:content)
Expand Down
@@ -1,11 +1,5 @@
require 'mime/types'

class Ckeditor::Asset
include Ckeditor::Orm::Mongoid::AssetBase
include Mongoid::Paperclip

before_validation :extract_content_type
before_create :read_dimensions, :parameterize_filename

delegate :url, :path, :styles, :size, :content_type, :to => :data
include Ckeditor::Backend::Paperclip
end
@@ -1,13 +1,10 @@
class CreateCkeditorAssets < ActiveRecord::Migration
def self.up
create_table :ckeditor_assets do |t|
t.string :data_file_name
t.string :data_file_name, :null => false
t.string :data_content_type
t.integer :data_file_size

# it's for carrierwave upload support
t.string :data

t.integer :assetable_id
t.string :assetable_type, :limit => 30
t.string :type, :limit => 30
Expand Down
16 changes: 16 additions & 0 deletions test/models/attachment_file_test.rb
@@ -0,0 +1,16 @@
require 'test_helper'

class AttachmentFileTest < ActiveSupport::TestCase
test "Set file content_type and size" do
@attachemnt = create_attachemnt

assert_equal @attachemnt.data_content_type, "application/x-gzip"
# TODO: fix filename parameterization
if CKEDITOR_BACKEND == :paperclip
assert_equal @attachemnt.data_file_name, "rails_tar.gz"
else
assert_equal @attachemnt.data_file_name, "rails.tar.gz"
end
assert_equal @attachemnt.data_file_size, 6823
end
end
16 changes: 16 additions & 0 deletions test/models/picture_test.rb
@@ -0,0 +1,16 @@
require 'test_helper'

class PictureTest < ActiveSupport::TestCase
test "Set file content_type and size" do
@picture = create_picture

assert_equal @picture.data_content_type, "image/png"
assert_equal @picture.data_file_name, "rails.png"
assert_equal @picture.data_file_size, 6646

if @picture.has_dimensions?
assert_equal @picture.width, 50
assert_equal @picture.height, 64
end
end
end
30 changes: 30 additions & 0 deletions test/support/helpers.rb
@@ -0,0 +1,30 @@
require 'active_support/test_case'
require 'action_dispatch/testing/test_process'

class ActiveSupport::TestCase
include ActionDispatch::TestProcess

def new_attachemnt(data = nil)
data ||= fixture_file_upload('files/rails.tar.gz', 'application/x-gzip')

Ckeditor::AttachmentFile.new(:data => data)
end

def create_attachemnt(data = nil)
attachemnt = new_attachemnt(data)
attachemnt.save!
attachemnt
end

def new_picture(data = nil)
data ||= fixture_file_upload('files/rails.png', 'image/png')

Ckeditor::Picture.new(:data => data)
end

def create_picture(data = nil)
picture = new_picture(data)
picture.save!
picture
end
end

0 comments on commit c6eff5a

Please sign in to comment.