forked from galetahub/ckeditor
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
219 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...enerators/ckeditor/templates/models/active_record/carrierwave/ckeditor/attachment_file.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/generators/ckeditor/templates/models/active_record/carrierwave/ckeditor/picture.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
lib/generators/ckeditor/templates/models/active_record/carrierwave/migration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
lib/generators/ckeditor/templates/models/active_record/paperclip/ckeditor/asset.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
2 changes: 2 additions & 0 deletions
2
...ckeditor/templates/models/base/carrierwave/uploaders/ckeditor_attachment_file_uploader.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/generators/ckeditor/templates/models/mongoid/carrierwave/ckeditor/attachment_file.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/generators/ckeditor/templates/models/mongoid/carrierwave/ckeditor/picture.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
lib/generators/ckeditor/templates/models/mongoid/paperclip/ckeditor/asset.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
5 changes: 1 addition & 4 deletions
5
test/dummy/db/migrate/20110705195648_create_ckeditor_assets.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |