Skip to content

Commit

Permalink
Merge pull request #606 from artempartos/feature/support_refile
Browse files Browse the repository at this point in the history
add base refile support
  • Loading branch information
galetahub committed Sep 24, 2015
2 parents 9a2d8c2 + 6a79f18 commit cf7483f
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -28,6 +28,8 @@ platforms :ruby do
gem "carrierwave"
gem "dragonfly"
gem "mini_magick"
gem "refile", require: "refile/rails"
gem "refile-mini_magick"
end

group :mongoid do
Expand Down
34 changes: 32 additions & 2 deletions Gemfile.lock
Expand Up @@ -62,12 +62,16 @@ GEM
cocaine (0.5.4)
climate_control (>= 0.0.3, < 1.0)
columnize (0.8.9)
domain_name (0.5.24)
unf (>= 0.0.5, < 1.0.0)
dragonfly (1.0.7)
addressable (~> 2.3)
multi_json (~> 1.0)
rack
erubis (2.7.0)
hike (1.2.3)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.6.11)
journey (1.0.4)
json (1.8.1)
Expand All @@ -78,7 +82,7 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
mini_magick (4.0.1)
mini_magick (4.3.3)
mini_portile (0.6.1)
mongoid (3.1.6)
activemodel (~> 3.2)
Expand All @@ -94,6 +98,7 @@ GEM
multi_json (1.10.1)
mynyml-redgreen (0.7.1)
term-ansicolor (>= 1.0.4)
netrc (0.10.3)
nokogiri (1.6.5)
mini_portile (~> 0.6.0)
origin (1.1.0)
Expand All @@ -104,9 +109,11 @@ GEM
cocaine (~> 0.5.3)
mime-types
polyglot (0.3.5)
rack (1.4.5)
rack (1.4.7)
rack-cache (1.2)
rack (>= 0.4)
rack-protection (1.5.3)
rack
rack-ssl (1.3.4)
rack
rack-test (0.6.2)
Expand All @@ -131,11 +138,26 @@ GEM
rbx-require-relative (0.0.9)
rdoc (3.12.2)
json (~> 1.4)
refile (0.6.1)
mime-types
rest-client (~> 1.8)
sinatra (~> 1.4.5)
refile-mini_magick (0.2.0)
mini_magick (~> 4.0)
refile (~> 0.5)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sprockets (2.2.3)
hike (~> 1.2)
multi_json (~> 1.0)
Expand All @@ -151,6 +173,9 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.42)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.1)
unicorn (4.0.1)
kgio (~> 2.4)
rack
Expand All @@ -175,6 +200,11 @@ DEPENDENCIES
mynyml-redgreen (~> 0.7.1)
paperclip (~> 4.1.1)
rails (= 3.2.21)
refile
refile-mini_magick
ruby-debug
sqlite3
unicorn (~> 4.0.1)

BUNDLED WITH
1.10.6
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -61,6 +61,16 @@ gem 'mini_magick'
rails generate ckeditor:install --orm=active_record --backend=carrierwave
```

#### ActiveRecord + refile

```
gem 'refile', require: "refile/rails"
gem 'refile-mini_magick'
rails generate ckeditor:install --orm=active_record --backend=refile
```


#### ActiveRecord + dragonfly

Requires Dragonfly 1.0 or greater.
Expand Down Expand Up @@ -146,7 +156,7 @@ Adding a custom toolbar:

CKEDITOR.editorConfig = function (config) {
// ... other configuration ...

config.toolbar_mini = [
["Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript"],
];
Expand Down
6 changes: 3 additions & 3 deletions ckeditor.gemspec
Expand Up @@ -5,20 +5,20 @@ require "ckeditor/version"
Gem::Specification.new do |s|
s.name = "ckeditor"
s.version = Ckeditor::Version::GEM.dup
s.platform = Gem::Platform::RUBY
s.platform = Gem::Platform::RUBY
s.summary = "Rails gem for easy integration ckeditor in your application"
s.description = "CKEditor is a WYSIWYG editor to be used inside web pages"
s.authors = ["Igor Galeta"]
s.email = "galeta.igor@gmail.com"
s.licenses = ["MIT"]
s.rubyforge_project = "ckeditor"
s.homepage = "https://github.com/galetahub/ckeditor"

s.files = Dir["{app,config,lib,vendor}/**/*"] + ["MIT-LICENSE", "Rakefile", "Gemfile", "README.md"]
s.test_files = Dir["{test}/**/*"]
s.extra_rdoc_files = ["README.md"]
s.require_paths = ["lib"]

s.add_dependency("cocaine")
s.add_dependency("orm_adapter", "~> 0.5.0")
end
1 change: 1 addition & 0 deletions lib/ckeditor.rb
Expand Up @@ -24,6 +24,7 @@ module Backend
autoload :Paperclip, 'ckeditor/backend/paperclip'
autoload :CarrierWave, 'ckeditor/backend/carrierwave'
autoload :Dragonfly, 'ckeditor/backend/dragonfly'
autoload :Refile, 'ckeditor/backend/refile'
end

IMAGE_TYPES = %w(image/jpeg image/png image/gif image/jpg image/pjpeg image/tiff image/x-png)
Expand Down
37 changes: 37 additions & 0 deletions lib/ckeditor/backend/refile.rb
@@ -0,0 +1,37 @@
module Ckeditor
module Backend
module Refile
extend ActiveSupport::Concern

included do
validates :data, presence: true
delegate :rewind, :download, :to_io, :exists?, :delete, :size, :close, :eof?, :read, to: :data
alias_attribute :data_file_name, :data_filename
alias_attribute :data_file_size, :data_size
end

class_methods do
def attachment_file_types
@attachment_file_types ||= Ckeditor.attachment_file_types.map(&:to_s)
end

def image_file_types
@image_file_types ||= Ckeditor.image_file_types.map(&:to_s)
end
end

def url(*attrs)
::Refile.attachment_url(self, :data, *attrs)
end

def read_dimensions
if model.image? && model.has_dimensions?
magick = MiniMagick::Image.open(model.to_io)
model.width = magick.width
model.height = magick.height
end
end

end
end
end
2 changes: 1 addition & 1 deletion lib/generators/ckeditor/install_generator.rb
Expand Up @@ -13,7 +13,7 @@ class InstallGenerator < Rails::Generators::Base
:desc => "Backend processor for upload support"

class_option :backend, :type => :string, :default => 'paperclip',
:desc => "paperclip (default), carrierwave, or dragonfly"
:desc => "paperclip (default), carrierwave, refile or dragonfly"

def self.source_root
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
Expand Down
@@ -0,0 +1,4 @@
class Ckeditor::Asset < ActiveRecord::Base
include Ckeditor::Backend::Refile
include Ckeditor::Orm::ActiveRecord::AssetBase
end
@@ -0,0 +1,7 @@
class Ckeditor::AttachmentFile < Ckeditor::Asset
attachment :data, extension: attachment_file_types

def url_thumb
@url_thumb ||= Ckeditor::Utils.filethumb(filename)
end
end
@@ -0,0 +1,11 @@
class Ckeditor::Picture < Ckeditor::Asset
attachment :data, extension: image_file_types

def url_content
url
end

def url_thumb
url(:fill, 118, 100)
end
end
@@ -0,0 +1,28 @@
class CreateCkeditorAssets < ActiveRecord::Migration
def self.up
create_table :ckeditor_assets do |t|
t.string :data_id, :null => false
t.string :data_filename, :null => false
t.integer :data_size
t.string :data_content_type


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

add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], :name => "idx_ckeditor_assetable_type"
add_index "ckeditor_assets", ["assetable_type", "assetable_id"], :name => "idx_ckeditor_assetable"
end

def self.down
drop_table :ckeditor_assets
end
end
14 changes: 14 additions & 0 deletions test/generators/install_generator_test.rb
Expand Up @@ -57,6 +57,20 @@ class InstallGeneratorTest < Rails::Generators::TestCase
end
end

test "models and migration for active_record orm via refile" do
run_generator %w(--orm=active_record --backend=refile)

assert_file "app/models/ckeditor/asset.rb"
assert_file "app/models/ckeditor/picture.rb"
assert_file "app/models/ckeditor/attachment_file.rb"

assert_migration "db/migrate/create_ckeditor_assets.rb" do |migration|
assert_class_method :up, migration do |up|
assert_match /create_table/, up
end
end
end

test "models and migration for active_record orm via dragonfly" do
run_generator %w(--orm=active_record --backend=dragonfly)

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -4,7 +4,7 @@
CKEDITOR_BACKEND = (ENV["CKEDITOR_BACKEND"] || :paperclip).to_sym

puts "\n==> Ckeditor.orm = #{CKEDITOR_ORM.inspect}. CKEDITOR_ORM = (active_record|mongoid)"
puts "\n==> Ckeditor.backend = #{CKEDITOR_BACKEND.inspect}. CKEDITOR_BACKEND = (paperclip|carrierwave|dragonfly)"
puts "\n==> Ckeditor.backend = #{CKEDITOR_BACKEND.inspect}. CKEDITOR_BACKEND = (paperclip|carrierwave|refile|dragonfly)"

require File.expand_path("../dummy/config/environment.rb", __FILE__)
require "rails/test_help"
Expand Down

0 comments on commit cf7483f

Please sign in to comment.