diff --git a/lib/rails_kindeditor/Gemfile b/lib/rails_kindeditor/Gemfile
new file mode 100644
index 000000000..0af516a49
--- /dev/null
+++ b/lib/rails_kindeditor/Gemfile
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+gemspec
+
+gem 'carrierwave'
+gem 'mini_magick'
diff --git a/lib/rails_kindeditor/README.md b/lib/rails_kindeditor/README.md
new file mode 100644
index 000000000..20084474e
--- /dev/null
+++ b/lib/rails_kindeditor/README.md
@@ -0,0 +1,497 @@
+# Kindeditor for Ruby on Rails [](http://badge.fury.io/rb/rails_kindeditor)
+
+Kindeditor is a WYSIWYG javascript editor, visit http://www.kindsoft.net for details.
+rails_kindeditor will helps your rails app integrate with kindeditor, includes images and files uploading.
+
+
+
+## Installation and usage
+
+### Add this to your Gemfile
+
+```ruby
+ gem 'rails_kindeditor'
+```
+
+### Run "bundle" command.
+
+```bash
+ bundle
+```
+
+### Run install generator:
+
+```bash
+ rails generate rails_kindeditor:install
+```
+notice: rails_kindeditor needs applications.js in your project.
+
+### Rails4 in production mode
+
+In Rails 4.0, precompiling assets no longer automatically copies non-JS/CSS assets from vendor/assets and lib/assets. see https://github.com/rails/rails/pull/7968
+In Rails 4.0's production mode, please run 'rake kindeditor:assets', this method just copy kindeditor into public folder.
+
+```bash
+ rake kindeditor:assets
+```
+
+### Usage:
+
+```ruby
+ 1. <%= kindeditor_tag :content, 'default content value' %>
+ # or <%= kindeditor_tag :content, 'default content value', :width => 800, :height => 300 %>
+ # or <%= kindeditor_tag :content, 'default content value', :allowFileManager => false %>
+```
+
+```ruby
+ 2. <%= form_for @article do |f| %>
+ ...
+ <%= f.kindeditor :content %>
+ # or <%= f.kindeditor :content, :width => 800, :height => 300 %>
+ # or <%= f.kindeditor :content, :allowFileManager => false %>
+ ...
+ <% end %>
+```
+You can use kindeditor's initial parameters as usual, please visit http://www.kindsoft.net/docs/option.html for details.
+
+additionally, rails_kindeditor provides one "simple_mode" parameter for render simple mode quickly.
+
+
+
+```ruby
+ kindeditor_tag :content, 'default content value', :simple_mode => true
+ f.kindeditor :content, :simple_mode => true
+ f.input :content, :as => :kindeditor, :input_html => { :simple_mode => true } # simple_form & formtastic
+```
+
+That's all.
+
+### Work with turbolinks
+
+rails_kindeditor will not load the scripts under the turbolinks, there's two way to solve this problem:
+
+1.use "'data-no-turbolink' => true" when we need to load kindeditor,this will shut down the turbolinks in this page
+
+```ruby
+ <%= link_to 'Edit', edit_article_path(article), 'data-no-turbolink' => true %>
+```
+
+2.load kindeditor manually, but you should specify the parameters again, include the textarea's id.
+
+```coffeescript
+ # coffeescript code
+ $(document).on 'page:load', ->
+ if $('#article_content').length > 0
+ KindEditor.create '#article_content', "width":"100%", "height":300, "allowFileManager":true, "uploadJson":"/kindeditor/upload", "fileManagerJson":"/kindeditor/filemanager"
+```
+
+simple mode
+```coffeescript
+ # coffeescript code
+ $(document).on 'page:load', ->
+ if $('#article_content').length > 0
+ KindEditor.create '#article_content',
+ "width":"100%",
+ "height":300,
+ "allowFileManager":true,
+ "uploadJson":"/kindeditor/upload",
+ "fileManagerJson":"/kindeditor/filemanager",
+ "items":["fontname","fontsize","|","forecolor","hilitecolor","bold","italic","underline","removeformat","|","justifyleft","justifycenter","justifyright","insertorderedlist","insertunorderedlist","|","emoticons","image","link"]
+```
+
+When you need to specify the owner_id:
+
+```ruby
+f.kindeditor :content, owner_id: @article.id, data: {upload: kindeditor_upload_json_path(owner_id: @article.id), filemanager: kindeditor_file_manager_json_path}
+```
+
+```coffeescript
+ # coffeescript code
+ $(document).on 'page:load', ->
+ if $('#article_content').length > 0
+ KindEditor.create '#article_content',
+ "width" : "100%",
+ "height" : 300,
+ "allowFileManager" : true,
+ "uploadJson" : $('#article_content').data('upload'),
+ "fileManagerJson" : $('#article_content').data('filemanager')
+```
+
+### Include javascript files at bottom ? Not in the head tag ? How can I load kindeditor correctly ?
+
+For some reasons, you includes javascript files at bottom in your template, rails_kindeditor provides a options for lazyload:
+
+```ruby
+ <%= f.kindeditor :content, :window_onload => true %>
+```
+
+Warning: Kindeditor will load after all the objects loaded.
+
+## SimpleForm and Formtastic integration
+
+### simple_form:
+
+```ruby
+ <%= form.input :content, :as => :kindeditor %>
+ # or
+ <%= form.input :content, :as => :kindeditor, :label => false, :input_html => { :width => 800, :height => 300 } %>
+```
+
+### formtastic:
+
+```ruby
+ <%= form.input :content, :as => :kindeditor %>
+ # or
+ <%= form.input :content, :as => :kindeditor, :input_html => { :height => 300 } %>
+```
+
+## How to get kindeditor's content
+
+```ruby
+ <%= form_for @article do |f| %>
+ <%= f.kindeditor :content, :editor_id => 'my_editor' %>
+ <% end %>
+```
+
+You can get content like this:
+
+```javascript
+ // Javascript code
+ my_editor.html();
+```
+
+## Upload options configuration
+
+When you run "rails generate rails_kindeditor:install", installer will copy configuration files in config/initializers folder.
+You can customize some option for uploading.
+
+```ruby
+ # Specify the subfolders in public directory.
+ # You can customize it , eg: config.upload_dir = 'this/is/my/folder'
+ config.upload_dir = 'uploads'
+
+ # Allowed file types for upload.
+ config.upload_image_ext = %w[gif jpg jpeg png bmp]
+ config.upload_flash_ext = %w[swf flv]
+ config.upload_media_ext = %w[swf flv mp3 wav wma wmv mid avi mpg asf rm rmvb]
+ config.upload_file_ext = %w[doc docx xls xlsx ppt htm html txt zip rar gz bz2]
+
+ # Porcess upload image size, need mini_magick
+ # before => after
+ # eg: 1600x1600 => 800x800
+ # 1600x800 => 800x400
+ # 400x400 => 400x400 # No Change
+ # config.image_resize_to_limit = [800, 800]
+```
+
+## Save upload file information into database(optional)
+
+rails_kindeditor can save upload file information into database.
+
+### Just run migration generate, there are two ORM options for you: 1.active_record 2.mongoid, default is active_record.
+
+```bash
+ rails generate rails_kindeditor:migration
+ or
+ rails generate rails_kindeditor:migration -o mongoid
+```
+
+### The generator will copy model and migration to your application. When you are done, remember run rake db:migrate:
+
+```bash
+ rake db:migrate
+```
+
+### Delete uploaded files automatically (only for active_record)
+
+You can specify the owner for uploaded files, when the owner was destroying, all the uploaded files(belongs to the owner) will be destroyed automatically.
+
+####1. specify the owner_id for kindeditor
+
+```ruby
+ <%= form_for @article do |f| %>
+ ...
+ <%= f.kindeditor :content, :owner_id => @article.id %>
+ ...
+ <% end %>
+```
+
+```ruby
+Warnning: the @article must be created before this scene, the @article.id should not be empty.
+```
+
+####2. add has_many_kindeditor_assets in your own model
+
+```ruby
+ class Article < ActiveRecord::Base
+ has_many_kindeditor_assets :attachments, :dependent => :destroy
+ # has_many_kindeditor_assets :attachments, :dependent => :nullify
+ # has_many_kindeditor_assets :your_name, :dependent => :destroy
+ end
+```
+
+####3. relationship
+
+```ruby
+ article = Article.first
+ article.attachments # => the article's assets uploaded by kindeditor
+ asset = article.attachments.first
+ asset.owner # => aritcle
+```
+
+### If you're using mongoid, please add 'gem "carrierwave-mongoid"' in your Gemfile
+
+```ruby
+ gem 'carrierwave-mongoid'
+```
+
+## License
+
+MIT License.
+
+
+
+# Kindeditor for Ruby on Rails 中文文档
+
+Kindeditor是国产的所见即所得javascript富文本编辑器, 访问 http://www.kindsoft.net 获取更多信息.
+rails_kindeditor可以帮助你的rails程序集成kindeditor,包括了图片和附件上传功能,文件按照类型、日期进行存储。
+
+## 安装及使用
+
+### 将下面代码加入Gemfile:
+
+```ruby
+ gem 'rails_kindeditor'
+```
+
+### 运行"bundle"命令:
+
+```bash
+ bundle
+```
+
+### 安装Kindeditor,运行下面的代码:
+
+```bash
+ rails generate rails_kindeditor:install
+```
+注意: 在你的工程中需要有application.js文件。
+
+### Rails4 in production mode
+
+从Rails 4.0开始, precompiling assets不再自动从vendor/assets和lib/assets拷贝非JS/CSS文件. 参见 https://github.com/rails/rails/pull/7968
+如果要使用Rails 4.0的生产模式,请运行'rake kindeditor:assets', 此方法可将kindeditor自动拷贝到你的public/assets目录.
+
+```bash
+ rake kindeditor:assets
+```
+
+### 使用方法:
+
+```ruby
+ 1. <%= kindeditor_tag :content, 'default content value' %>
+ # or <%= kindeditor_tag :content, 'default content value', :width => 800, :height => 300 %>
+ # or <%= kindeditor_tag :content, 'default content value', :allowFileManager => false %>
+```
+
+```ruby
+ 2. <%= form_for @article do |f| -%>
+ ...
+ <%= f.kindeditor :content %>
+ # or <%= f.kindeditor :content, :width => 800, :height => 300 %>
+ # or <%= f.kindeditor :content, :allowFileManager => false %>
+ ...
+ <% end -%>
+```
+你可以像往常那样使用kindeditor自身的初始化参数,请访问 http://www.kindsoft.net/docs/option.html 查看更多参数。
+
+另外,rails_kindeditor还额外提供一个"simple_mode"参数,以便快捷使用简单模式的kindeditor。
+
+```ruby
+ kindeditor_tag :content, 'default content value', :simple_mode => true
+ f.kindeditor :content, :simple_mode => true
+ f.input :content, :as => :kindeditor, :input_html => { :simple_mode => true } # simple_form & formtastic
+```
+
+完毕!
+
+### 如何在turbolinks下使用
+
+rails_kindeditor在turbolinks下不会正常加载,只有当页面刷新时才正常。turbolinks的机制就是这样的,页面根本没刷新,这和pjax是一样的,所以kindeditor没加载很正常。
+
+有两个办法可以解决:
+
+1.在需要加载kindeditor的链接加入 'data-no-turbolink' => true ,此时相当在这个页面于关闭turbolinks。
+
+```ruby
+ <%= link_to 'Edit', edit_article_path(article), 'data-no-turbolink' => true %>
+```
+
+2.在turbolinks载入完毕后手动加载kindeditor,不过所有参数都要设置,而且需要知道并设定textarea的id。
+
+```coffeescript
+ # coffeescript code
+ $(document).on 'page:load', ->
+ if $('#article_content').length > 0
+ KindEditor.create '#article_content', "width":"100%", "height":300, "allowFileManager":true, "uploadJson":"/kindeditor/upload", "fileManagerJson":"/kindeditor/filemanager"
+```
+
+simple模式也需要手动设定
+```coffeescript
+ # coffeescript code
+ $(document).on 'page:load', ->
+ if $('#article_content').length > 0
+ KindEditor.create '#article_content',
+ "width":"100%",
+ "height":300,
+ "allowFileManager":true,
+ "uploadJson":"/kindeditor/upload",
+ "fileManagerJson":"/kindeditor/filemanager",
+ "items":["fontname","fontsize","|","forecolor","hilitecolor","bold","italic","underline","removeformat","|","justifyleft","justifycenter","justifyright","insertorderedlist","insertunorderedlist","|","emoticons","image","link"]
+```
+
+需要指定owner_id的方法:
+
+```ruby
+f.kindeditor :content, owner_id: @article.id, data: {upload: kindeditor_upload_json_path(owner_id: @article.id), filemanager: kindeditor_file_manager_json_path}
+```
+
+```coffeescript
+ # coffeescript code
+ $(document).on 'page:load', ->
+ if $('#article_content').length > 0
+ KindEditor.create '#article_content',
+ "width" : "100%",
+ "height" : 300,
+ "allowFileManager" : true,
+ "uploadJson" : $('#article_content').data('upload'),
+ "fileManagerJson" : $('#article_content').data('filemanager')
+```
+
+### 把javascript放在模板最下方,不放在head里面,如何正确加载kindeditor?
+
+有时候,为了加快页面载入速度,也许你会把javascript引用放在template的底部,rails_kindeditor提供了一个参数可以确保正常加载:
+
+```ruby
+ <%= f.kindeditor :content, :window_onload => true %>
+```
+
+警告:Kindeditor会在页面所有的内容加载完毕后才进行加载,所以需谨慎使用
+
+## SimpleForm与Formtastic集成:
+
+### simple_form:
+
+```ruby
+ <%= form.input :content, :as => :kindeditor, :label => false, :input_html => { :width => 800, :height => 300 } %>
+```
+
+### formtastic:
+
+```ruby
+ <%= form.input :content, :as => :kindeditor %>
+ <%= form.input :content, :as => :kindeditor, :input_html => { :height => 300 } %>
+```
+## 如何获取kindeditor的内容
+
+```ruby
+ <%= form_for @article do |f| %>
+ <%= f.kindeditor :content, :editor_id => 'my_editor' %>
+ <% end %>
+```
+
+可通过下面的Javascript代码获取内容:
+
+```javascript
+ // Javascript code
+ my_editor.html();
+```
+
+## 上传图片及文件配置
+
+当你运行"rails generate rails_kindeditor:install"的时候,安装器会将配置文件拷贝到config/initializers文件夹。
+你可以配置以下上传选项:
+
+```ruby
+ # 指定上传目录,目录可以指定多级,都存储在public目录下.
+ # You can customize it , eg: config.upload_dir = 'this/is/my/folder'
+ config.upload_dir = 'uploads'
+
+ # 指定允许上传的文件类型.
+ config.upload_image_ext = %w[gif jpg jpeg png bmp]
+ config.upload_flash_ext = %w[swf flv]
+ config.upload_media_ext = %w[swf flv mp3 wav wma wmv mid avi mpg asf rm rmvb]
+ config.upload_file_ext = %w[doc docx xls xlsx ppt htm html txt zip rar gz bz2]
+
+ # 处理上传文件,需要mini_magick
+ # 处理以前 => 处理以后
+ # eg: 1600x1600 => 800x800
+ # 1600x800 => 800x400
+ # 400x400 => 400x400 # 图片小于该限制尺寸则不作处理
+ # config.image_resize_to_limit = [800, 800]
+```
+
+## 将上传文件信息记录入数据库(可选)
+
+rails_kindeditor 可以将上传文件信息记录入数据库,以便扩展应用.
+
+### 运行下面的代码,有两项选项:1.active_record 2.mongoid,默认是active_record。
+
+```bash
+ rails generate rails_kindeditor:migration
+ or
+ rails generate rails_kindeditor:migration -o mongoid
+```
+
+### 运行下面的代码:
+
+```bash
+ rake db:migrate
+```
+
+### 自动删除上传的文件(仅在active_record下工作)
+
+你可以为上传的文件指定归属,比如一名用户,或者一篇文章,当用户或者文章被删除时,所有属于该用户或者该文章的上传文件将会被自动删除。
+
+####1. 为kindeditor指定owner_id
+
+```ruby
+ <%= form_for @article do |f| %>
+ ...
+ <%= f.kindeditor :content, :owner_id => @article.id %>
+ ...
+ <% end %>
+```
+
+```ruby
+警告: @article应该事先被创建,@article.id不应该是空的。
+```
+
+####2. 在你自己的模型里加入has_many_kindeditor_assets
+
+```ruby
+ class Article < ActiveRecord::Base
+ has_many_kindeditor_assets :attachments, :dependent => :destroy
+ # has_many_kindeditor_assets :attachments, :dependent => :nullify
+ # has_many_kindeditor_assets :your_name, :dependent => :destroy
+ end
+```
+
+####3. 相互关系
+
+```ruby
+ article = Article.first
+ article.attachments # => the article's assets uploaded by kindeditor
+ asset = article.attachments.first
+ asset.owner # => aritcle
+```
+
+### 如果你使用的是mongoid, 请在你的Gemfile里加入'gem "carrierwave-mongoid"'
+
+```ruby
+ gem 'carrierwave-mongoid'
+```
+
+## License
+
+MIT License.
\ No newline at end of file
diff --git a/lib/rails_kindeditor/Rakefile b/lib/rails_kindeditor/Rakefile
new file mode 100644
index 000000000..14cfe0b57
--- /dev/null
+++ b/lib/rails_kindeditor/Rakefile
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
diff --git a/lib/rails_kindeditor/app/assets/javascripts/kindeditor.js b/lib/rails_kindeditor/app/assets/javascripts/kindeditor.js
new file mode 100644
index 000000000..5d7dcec3e
--- /dev/null
+++ b/lib/rails_kindeditor/app/assets/javascripts/kindeditor.js
@@ -0,0 +1 @@
+//= require kindeditor/kindeditor.js
diff --git a/lib/rails_kindeditor/app/controllers/kindeditor/assets_controller.rb b/lib/rails_kindeditor/app/controllers/kindeditor/assets_controller.rb
new file mode 100644
index 000000000..0e1177563
--- /dev/null
+++ b/lib/rails_kindeditor/app/controllers/kindeditor/assets_controller.rb
@@ -0,0 +1,127 @@
+#coding: utf-8
+require "find"
+class Kindeditor::AssetsController < ApplicationController
+ skip_before_filter :verify_authenticity_token
+ def create
+ @imgFile, @dir = params[:imgFile], params[:dir]
+ unless @imgFile.nil?
+ if Kindeditor::AssetUploader.save_upload_info? # save upload info into database
+ begin
+ @asset = "Kindeditor::#{@dir.camelize}".constantize.new(:asset => @imgFile)
+ @asset.owner_id = params[:owner_id] ? params[:owner_id] : 0
+ @asset.owner_type = params[:owner_type] ? params[:owner_type] : ""
+ logger.warn '========= Warning: the owner_id is 0, "delete uploaded files automatically" will not work. =========' if defined?(logger) && @asset.owner_id == 0
+ @asset.asset_type = @dir
+ if @asset.save
+ render :text => ({:error => 0, :url => @asset.asset.url,:asset_id => @asset.id}.to_json)
+ else
+ show_error(@asset.errors.full_messages)
+ end
+ rescue Exception => e
+ show_error(e.to_s)
+ end
+ else # do not touch database
+ begin
+ uploader = "Kindeditor::#{@dir.camelize}Uploader".constantize.new
+ uploader.store!(@imgFile)
+ render :text => ({:error => 0, :url => uploader.url}.to_json)
+ rescue CarrierWave::UploadError => e
+ show_error(e.message)
+ rescue Exception => e
+ show_error(e.to_s)
+ end
+ end
+ else
+ show_error("No File Selected!")
+ end
+ end
+
+ def list
+ @root_path = "#{Rails.public_path}/#{RailsKindeditor.upload_store_dir}/"
+ @root_url = "/#{RailsKindeditor.upload_store_dir}/"
+ @img_ext = Kindeditor::AssetUploader::EXT_NAMES[:image]
+ @dir = params[:dir].strip || ""
+ unless Kindeditor::AssetUploader::EXT_NAMES.keys.map(&:to_s).push("").include?(@dir)
+ render :text => "Invalid Directory name."
+ return
+ end
+
+ Dir.chdir(Rails.public_path)
+ RailsKindeditor.upload_store_dir.split('/').each do |dir|
+ Dir.mkdir(dir) unless Dir.exist?(dir)
+ Dir.chdir(dir)
+ end
+
+ Dir.mkdir(@dir) unless Dir.exist?(@dir)
+
+ @root_path += @dir + "/"
+ @root_url += @dir + "/"
+
+ @path = params[:path].strip || ""
+ if @path.empty?
+ @current_path = @root_path
+ @current_url = @root_url
+ @current_dir_path = ""
+ @moveup_dir_path = ""
+ else
+ @current_path = @root_path + @path + "/"
+ @current_url = @root_url + @path + "/"
+ @current_dir_path = @path
+ @moveup_dir_path = @current_dir_path.gsub(/(.*?)[^\/]+\/$/, "")
+ end
+ @order = %w(name size type).include?(params[:order].downcase) ? params[:order].downcase : "name"
+ if !@current_path.match(/\.\./).nil?
+ render :text => "Access is not allowed."
+ return
+ end
+ if @current_path.match(/\/$/).nil?
+ render :text => "Parameter is not valid."
+ return
+ end
+ if !File.exist?(@current_path) || !File.directory?(@current_path)
+ render :text => "Directory does not exist."
+ return
+ end
+ @file_list = []
+ Dir.foreach(@current_path) do |filename|
+ hash = {}
+ if filename != "." and filename != ".." and filename != ".DS_Store"
+ file = @current_path + filename
+ if File.directory?(file)
+ hash[:is_dir] = true
+ hash[:has_file] = (Dir.foreach(file).count > 2)
+ hash[:filesize] = 0
+ hash[:is_photo] = false
+ hash[:filetype] = ""
+ else
+ hash[:is_dir] = false
+ hash[:has_file] = false
+ hash[:filesize] = File.size(file)
+ hash[:dir_path] = ""
+ file_ext = file.gsub(/.*\./,"")
+ hash[:is_photo] = @img_ext.include?(file_ext)
+ hash[:filetype] = file_ext
+ end
+ hash[:filename] = filename
+ hash[:datetime] = File.mtime(file).to_s(:db)
+ @file_list << hash
+ end
+ end
+
+ @file_list.sort! {|a, b| a["file#{@order}".to_sym] <=> b["file#{@order}".to_sym]}
+
+ @result = {}
+ @result[:moveup_dir_path] = @moveup_dir_path
+ @result[:current_dir_path] = @current_dir_path
+ @result[:current_url] = @current_url
+ @result[:total_count] = @file_list.count
+ @result[:file_list] = @file_list
+ render :text => @result.to_json
+ end
+
+ private
+ def show_error(msg)
+ render :text => ({:error => 1, :message => msg}.to_json)
+ end
+
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/app/uploaders/kindeditor/asset_uploader.rb b/lib/rails_kindeditor/app/uploaders/kindeditor/asset_uploader.rb
new file mode 100644
index 000000000..b8cf1511e
--- /dev/null
+++ b/lib/rails_kindeditor/app/uploaders/kindeditor/asset_uploader.rb
@@ -0,0 +1,91 @@
+# encoding: utf-8
+
+require 'carrierwave/processing/mime_types'
+
+class Kindeditor::AssetUploader < CarrierWave::Uploader::Base
+
+ EXT_NAMES = {:image => RailsKindeditor.upload_image_ext,
+ :flash => RailsKindeditor.upload_flash_ext,
+ :media => RailsKindeditor.upload_media_ext,
+ :file => RailsKindeditor.upload_file_ext}
+
+ # Include RMagick or ImageScience support:
+ # include CarrierWave::RMagick
+ # include CarrierWave::ImageScience
+ # include CarrierWave::MiniMagick
+
+ # Choose what kind of storage to use for this uploader:
+ storage :file
+ # storage :fog
+
+ # Override the directory where uploaded files will be stored.
+ # This is a sensible default for uploaders that are meant to be mounted:
+ def store_dir
+ if Kindeditor::AssetUploader.save_upload_info?
+ "#{RailsKindeditor.upload_store_dir}/#{model.asset_type.to_s.underscore.gsub(/(kindeditor\/)|(_uploader)/, '')}/#{model.created_at.strftime("%Y%m")}"
+ else
+ "#{RailsKindeditor.upload_store_dir}/#{self.class.to_s.underscore.gsub(/(kindeditor\/)|(_uploader)/, '')}/#{Time.now.strftime("%Y%m")}"
+ end
+ end
+
+ def cache_dir
+ "#{Rails.root}/tmp/uploads"
+ end
+
+ # Provide a default URL as a default if there hasn't been a file uploaded:
+ # def default_url
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
+ # end
+
+ # Process files as they are uploaded:
+ # process :scale => [200, 300]
+ #
+ # def scale(width, height)
+ # # do something
+ # end
+
+ # Create different versions of your uploaded files:
+ # version :thumb do
+ # process :scale => [50, 50]
+ # end
+
+ # Add a white list of extensions which are allowed to be uploaded.
+ # For images you might use something like this:
+
+ # Override the filename of the uploaded files:
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
+ before :store, :remember_cache_id
+ after :store, :delete_tmp_dir
+
+ # store! nil's the cache_id after it finishes so we need to remember it for deletition
+ def remember_cache_id(new_file)
+ @cache_id_was = cache_id
+ end
+
+ def delete_tmp_dir(new_file)
+ # make sure we don't delete other things accidentally by checking the name pattern
+ if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
+ FileUtils.rm_rf(File.join(cache_dir, @cache_id_was))
+ end
+ end
+
+ def filename
+ if original_filename
+ @name ||= Digest::MD5.hexdigest(File.dirname(current_path)).slice(0, 12)
+ "#{@name}.#{file.extension}"
+ end
+ end
+
+ def self.save_upload_info?
+ begin
+ %w(asset file flash image media).each do |s|
+ "Kindeditor::#{s.camelize}".constantize
+ end
+ return true
+ rescue
+ return false
+ end
+ end
+
+end
+
diff --git a/lib/rails_kindeditor/app/uploaders/kindeditor/file_uploader.rb b/lib/rails_kindeditor/app/uploaders/kindeditor/file_uploader.rb
new file mode 100644
index 000000000..c6e2666ed
--- /dev/null
+++ b/lib/rails_kindeditor/app/uploaders/kindeditor/file_uploader.rb
@@ -0,0 +1,10 @@
+# encoding: utf-8
+
+class Kindeditor::FileUploader < Kindeditor::AssetUploader
+
+ def extension_white_list
+ EXT_NAMES[:file]
+ end
+
+end
+
diff --git a/lib/rails_kindeditor/app/uploaders/kindeditor/flash_uploader.rb b/lib/rails_kindeditor/app/uploaders/kindeditor/flash_uploader.rb
new file mode 100644
index 000000000..a36968026
--- /dev/null
+++ b/lib/rails_kindeditor/app/uploaders/kindeditor/flash_uploader.rb
@@ -0,0 +1,10 @@
+# encoding: utf-8
+
+class Kindeditor::FlashUploader < Kindeditor::AssetUploader
+
+ def extension_white_list
+ EXT_NAMES[:flash]
+ end
+
+end
+
diff --git a/lib/rails_kindeditor/app/uploaders/kindeditor/image_uploader.rb b/lib/rails_kindeditor/app/uploaders/kindeditor/image_uploader.rb
new file mode 100644
index 000000000..f86cfd8bd
--- /dev/null
+++ b/lib/rails_kindeditor/app/uploaders/kindeditor/image_uploader.rb
@@ -0,0 +1,10 @@
+# encoding: utf-8
+
+class Kindeditor::ImageUploader < Kindeditor::AssetUploader
+
+ def extension_white_list
+ EXT_NAMES[:image]
+ end
+
+end
+
diff --git a/lib/rails_kindeditor/app/uploaders/kindeditor/media_uploader.rb b/lib/rails_kindeditor/app/uploaders/kindeditor/media_uploader.rb
new file mode 100644
index 000000000..4cbed5dc1
--- /dev/null
+++ b/lib/rails_kindeditor/app/uploaders/kindeditor/media_uploader.rb
@@ -0,0 +1,10 @@
+# encoding: utf-8
+
+class Kindeditor::MediaUploader < Kindeditor::AssetUploader
+
+ def extension_white_list
+ EXT_NAMES[:media]
+ end
+
+end
+
diff --git a/lib/rails_kindeditor/config/routes.rb b/lib/rails_kindeditor/config/routes.rb
new file mode 100644
index 000000000..7ebb0b748
--- /dev/null
+++ b/lib/rails_kindeditor/config/routes.rb
@@ -0,0 +1,6 @@
+Rails.application.routes.draw do
+ namespace :kindeditor do
+ post "/upload" => "assets#create"
+ get "/filemanager" => "assets#list"
+ end
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/USAGE b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/USAGE
new file mode 100644
index 000000000..fc2c41557
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/USAGE
@@ -0,0 +1,10 @@
+Description:
+ install kindeditor for your application
+
+Example:
+ rails generate rails_kindeditor:install
+
+ This will create:
+ config/kindeditor.rb
+
+
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/install_generator.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/install_generator.rb
new file mode 100644
index 000000000..97e20ff8d
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/install_generator.rb
@@ -0,0 +1,23 @@
+module RailsKindeditor
+ class InstallGenerator < Rails::Generators::Base
+ source_root File.expand_path('../templates', __FILE__)
+ desc "Install kindeditor for your application."
+
+ def copy_kindeditor_files
+ if ::Rails.version < "3.1.0"
+ warn "Warning: rails_kindeditor ~> v0.3.0 only support Rails3.1+!"
+ warn "If you're using rails3.0.x, please check rails_kindeditor v0.2.8"
+ else
+ template "rails_kindeditor.rb", "config/initializers/rails_kindeditor.rb"
+ end
+ end
+
+ def insert_or_copy_js_files
+ if File.exist?('app/assets/javascripts/application.js')
+ insert_into_file "app/assets/javascripts/application.js", "//= require kindeditor\n", :after => "jquery_ujs\n"
+ else
+ copy_file "application.js", "app/assets/javascripts/application.js"
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/templates/application.js b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/templates/application.js
new file mode 100644
index 000000000..c7bc9e835
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/templates/application.js
@@ -0,0 +1,17 @@
+// This is a manifest file that'll be compiled into application.js, which will include all the files
+// listed below.
+//
+// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
+// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
+//
+// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
+// the compiled file.
+//
+// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
+// GO AFTER THE REQUIRES BELOW.
+//
+//= require jquery
+//= require jquery_ujs
+//= require kindeditor
+//= require_tree .
+
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/templates/rails_kindeditor.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/templates/rails_kindeditor.rb
new file mode 100644
index 000000000..25de36160
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/install/templates/rails_kindeditor.rb
@@ -0,0 +1,19 @@
+RailsKindeditor.setup do |config|
+
+ # Specify the subfolders in public directory.
+ # You can customize it , eg: config.upload_dir = 'this/is/my/folder'
+ config.upload_dir = 'uploads'
+
+ # Allowed file types for upload.
+ config.upload_image_ext = %w[gif jpg jpeg png bmp]
+ config.upload_flash_ext = %w[swf flv]
+ config.upload_media_ext = %w[swf flv mp3 wav wma wmv mid avi mpg asf rm rmvb]
+ config.upload_file_ext = %w[doc docx xls xlsx ppt htm html txt zip rar gz bz2]
+
+ # Porcess upload image size
+ # eg: 1600x1600 => 800x800
+ # 1600x800 => 800x400
+ # 400x400 => 400x400 # No Change
+ # config.image_resize_to_limit = [800, 800]
+
+end
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/USAGE b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/USAGE
new file mode 100644
index 000000000..2d9d40f69
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/USAGE
@@ -0,0 +1,14 @@
+Description:
+ Copy model, migration and uploader to your application.
+
+Example:
+ rails generate rails_kindeditor:migration
+
+ This will create:
+ app/models/kindeditor/asset.rb
+ app/models/kindeditor/file.rb
+ app/models/kindeditor/flash.rb
+ app/models/kindeditor/image.rb
+ app/models/kindeditor/media.rb
+ db/migrate/xxxxxxxxx_create_kindeditor_assets.rb
+
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/migration_generator.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/migration_generator.rb
new file mode 100644
index 000000000..82b03e9b5
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/migration_generator.rb
@@ -0,0 +1,36 @@
+module RailsKindeditor
+ class MigrationGenerator < Rails::Generators::Base
+ include Rails::Generators::Migration
+ source_root File.expand_path('../templates', __FILE__)
+ desc "Copy model and migration to your application."
+ class_option :orm, :type => :string, :aliases => "-o", :default => "active_record", :desc => "ORM options: active_record or mongoid"
+
+ def copy_files
+ orm = options[:orm].to_s
+ orm = "active_record" unless %w{active_record mongoid}.include?(orm)
+ %w(asset file flash image media).each do |file|
+ copy_model(orm, file)
+ end
+ if Rails.version < '4.0.0' && Rails.version >= '3.0.0' # insert code for rails3
+ insert_into_file "app/models/kindeditor/asset.rb", " attr_accessible :asset\n", :after => "before_save :update_asset_attributes\n"
+ end
+ if orm == "active_record"
+ migration_template "migration/migration.rb", "db/migrate/create_kindeditor_assets.rb"
+ end
+ end
+
+ def self.next_migration_number(dirname)
+ if ActiveRecord::Base.timestamped_migrations
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+
+ private
+ def copy_model(orm, name)
+ template "models/#{orm}/kindeditor/#{name}.rb", "app/models/kindeditor/#{name}.rb"
+ end
+ end
+end
+
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/migration/migration.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/migration/migration.rb
new file mode 100644
index 000000000..19a108b2e
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/migration/migration.rb
@@ -0,0 +1,17 @@
+class CreateKindeditorAssets < ActiveRecord::Migration
+ def self.up
+ create_table :kindeditor_assets do |t|
+ t.string :asset
+ t.integer :file_size
+ t.string :file_type
+ t.integer :owner_id
+ t.string :asset_type # list by kindeditor: image, file, media, flash
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :kindeditor_assets
+ end
+end
+
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/asset.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/asset.rb
new file mode 100644
index 000000000..0217d0f7e
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/asset.rb
@@ -0,0 +1,14 @@
+class Kindeditor::Asset < ActiveRecord::Base
+ self.table_name = 'kindeditor_assets'
+ mount_uploader :asset, Kindeditor::AssetUploader
+ validates_presence_of :asset
+ before_save :update_asset_attributes
+
+ private
+ def update_asset_attributes
+ if asset.present? && asset_changed?
+ self.file_size = asset.file.size
+ self.file_type = asset.file.content_type
+ end
+ end
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/file.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/file.rb
new file mode 100644
index 000000000..4b5f11441
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/file.rb
@@ -0,0 +1,3 @@
+class Kindeditor::File < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::FileUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/flash.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/flash.rb
new file mode 100644
index 000000000..efaf5de9b
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/flash.rb
@@ -0,0 +1,3 @@
+class Kindeditor::Flash < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::FlashUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/image.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/image.rb
new file mode 100644
index 000000000..a400c816d
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/image.rb
@@ -0,0 +1,3 @@
+class Kindeditor::Image < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::ImageUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/media.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/media.rb
new file mode 100644
index 000000000..071763319
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/active_record/kindeditor/media.rb
@@ -0,0 +1,3 @@
+class Kindeditor::Media < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::MediaUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/asset.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/asset.rb
new file mode 100644
index 000000000..e97e31f64
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/asset.rb
@@ -0,0 +1,27 @@
+require 'carrierwave/mongoid'
+
+class Kindeditor::Asset
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ field :file_size, :type => Integer
+ field :file_type, :type => String
+ field :owner_id, :type => Integer
+ field :asset_type, :type => String
+
+ mount_uploader :asset, Kindeditor::AssetUploader
+ validates_presence_of :asset
+ before_save :update_asset_attributes
+
+ def self.collection_name
+ :kindeditor_assets
+ end
+
+ private
+ def update_asset_attributes
+ if asset.present? && asset_changed?
+ self.file_size = asset.file.size
+ self.file_type = asset.file.content_type
+ end
+ end
+end
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/file.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/file.rb
new file mode 100644
index 000000000..4b5f11441
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/file.rb
@@ -0,0 +1,3 @@
+class Kindeditor::File < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::FileUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/flash.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/flash.rb
new file mode 100644
index 000000000..efaf5de9b
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/flash.rb
@@ -0,0 +1,3 @@
+class Kindeditor::Flash < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::FlashUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/image.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/image.rb
new file mode 100644
index 000000000..a400c816d
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/image.rb
@@ -0,0 +1,3 @@
+class Kindeditor::Image < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::ImageUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/media.rb b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/media.rb
new file mode 100644
index 000000000..071763319
--- /dev/null
+++ b/lib/rails_kindeditor/lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/media.rb
@@ -0,0 +1,3 @@
+class Kindeditor::Media < Kindeditor::Asset
+ mount_uploader :asset, Kindeditor::MediaUploader
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/rails_kindeditor.rb b/lib/rails_kindeditor/lib/rails_kindeditor.rb
new file mode 100644
index 000000000..a9a7ba906
--- /dev/null
+++ b/lib/rails_kindeditor/lib/rails_kindeditor.rb
@@ -0,0 +1,55 @@
+require 'rails_kindeditor/engine'
+require 'rails_kindeditor/helper'
+require 'rails_kindeditor/active_record'
+require 'carrierwave'
+require 'mini_magick'
+
+module RailsKindeditor
+
+ mattr_accessor :upload_dir
+ @@upload_dir = 'uploads'
+
+ mattr_accessor :upload_image_ext
+ @@upload_image_ext = %w[gif jpg jpeg png bmp]
+
+ mattr_accessor :upload_flash_ext
+ @@upload_flash_ext = %w[swf flv]
+
+ mattr_accessor :upload_media_ext
+ @@upload_media_ext = %w[swf flv mp3 wav wma wmv mid avi mpg asf rm rmvb]
+
+ mattr_accessor :upload_file_ext
+ @@upload_file_ext = %w[doc docx xls xlsx ppt htm html txt zip rar gz bz2]
+
+ mattr_accessor :image_resize_to_limit
+
+ def self.root_path
+ @root_path ||= Pathname.new(File.dirname(File.expand_path('../', __FILE__)))
+ end
+
+ def self.assets
+ Dir[root_path.join('vendor/assets/javascripts/kindeditor/**', '*.{js,css}')].inject([]) do |assets, path|
+ assets << Pathname.new(path).relative_path_from(root_path.join('vendor/assets/javascripts'))
+ end
+ end
+
+ def self.upload_store_dir
+ dirs = upload_dir.gsub(/^\/+/,'').gsub(/\/+$/,'').split('/')
+ dirs.each { |dir| dir.gsub!(/\W/, '') }
+ dirs.delete('')
+ dirs.join('/')
+ end
+
+ def self.resize_to_limit
+ if !image_resize_to_limit.nil? && image_resize_to_limit.is_a?(Array)
+ [image_resize_to_limit[0], image_resize_to_limit[1]]
+ else
+ [800, 800]
+ end
+ end
+
+ def self.setup
+ yield self
+ end
+
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/rails_kindeditor/active_record.rb b/lib/rails_kindeditor/lib/rails_kindeditor/active_record.rb
new file mode 100644
index 000000000..5120acf0a
--- /dev/null
+++ b/lib/rails_kindeditor/lib/rails_kindeditor/active_record.rb
@@ -0,0 +1,14 @@
+if defined?(ActiveRecord)
+ ActiveRecord::Base.class_eval do
+ def self.has_many_kindeditor_assets(*args)
+ options = args.extract_options!
+ asset_name = args[0] ? args[0].to_s : 'assets'
+ has_many asset_name.to_sym, :class_name => 'Kindeditor::Asset', :foreign_key => 'owner_id', :dependent => options[:dependent]
+
+ class_name = self.name
+ Kindeditor::Asset.class_eval do
+ belongs_to :owner, :class_name => class_name, :foreign_key => 'owner_id'
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/rails_kindeditor/engine.rb b/lib/rails_kindeditor/lib/rails_kindeditor/engine.rb
new file mode 100644
index 000000000..7ade0c659
--- /dev/null
+++ b/lib/rails_kindeditor/lib/rails_kindeditor/engine.rb
@@ -0,0 +1,34 @@
+require "rails_kindeditor"
+require "rails"
+require "action_controller"
+
+module RailsKindeditor
+ class Engine < Rails::Engine
+
+ initializer "rails_kindeditor.assets_precompile" do |app|
+ app.config.assets.precompile += RailsKindeditor.assets
+ end
+
+ initializer "rails_kindeditor.simple_form_and_formtastic" do
+ require "rails_kindeditor/simple_form" if Object.const_defined?("SimpleForm")
+ require "rails_kindeditor/formtastic" if Object.const_defined?("Formtastic")
+ end
+
+ initializer "rails_kindeditor.helper_and_builder" do
+ ActiveSupport.on_load :action_view do
+ ActionView::Base.send(:include, RailsKindeditor::Helper)
+ ActionView::Helpers::FormBuilder.send(:include, RailsKindeditor::Builder)
+ end
+ end
+
+ initializer "rails_kindeditor.image_process" do
+ unless RailsKindeditor.image_resize_to_limit.nil?
+ Kindeditor::ImageUploader.class_eval do
+ include CarrierWave::MiniMagick
+ process :resize_to_limit => RailsKindeditor.resize_to_limit
+ end
+ end
+ end
+
+ end
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/rails_kindeditor/formtastic.rb b/lib/rails_kindeditor/lib/rails_kindeditor/formtastic.rb
new file mode 100644
index 000000000..1d65a389e
--- /dev/null
+++ b/lib/rails_kindeditor/lib/rails_kindeditor/formtastic.rb
@@ -0,0 +1,12 @@
+require "formtastic"
+
+class KindeditorInput
+ include ::Formtastic::Inputs::Base
+
+ def to_html
+ input_wrapping do
+ label_html <<
+ builder.kindeditor(method, input_html_options)
+ end
+ end
+end
diff --git a/lib/rails_kindeditor/lib/rails_kindeditor/helper.rb b/lib/rails_kindeditor/lib/rails_kindeditor/helper.rb
new file mode 100644
index 000000000..db2be85fb
--- /dev/null
+++ b/lib/rails_kindeditor/lib/rails_kindeditor/helper.rb
@@ -0,0 +1,100 @@
+module RailsKindeditor
+ module Helper
+
+ def kindeditor_tag(name, content = nil, options = {})
+ id = sanitize_to_id(name)
+ input_html = { :id => id }.merge(options.delete(:input_html) || {})
+ output = ActiveSupport::SafeBuffer.new
+ output << text_area_tag(name, content, input_html)
+ output << javascript_tag(js_replace(id, options))
+ end
+
+ def kindeditor(name, method, options = {})
+ # TODO: Refactory options: 1. kindeditor_option 2. html_option
+ input_html = (options.delete(:input_html) || {}).stringify_keys
+ output_buffer = ActiveSupport::SafeBuffer.new
+ output_buffer << build_text_area_tag(name, method, self, options, input_html)
+ output_buffer << javascript_tag(js_replace(input_html['id'], options))
+ end
+
+ def kindeditor_upload_json_path(*args)
+ options = args.extract_options!
+ owner_id_query_string = options[:owner_id] ? "?owner_id=#{options[:owner_id]}" : ''
+ owner_type_query_string = options[:owner_type] ? owner_id_query_string + "&owner_type=#{options[:owner_type]}" : owner_id_query_string
+ "#{main_app_root_url}kindeditor/upload#{owner_type_query_string}"
+ end
+
+ def kindeditor_file_manager_json_path
+ "#{main_app_root_url}kindeditor/filemanager"
+ end
+
+ private
+
+ def main_app_root_url
+ begin
+ main_app.root_url.slice(0, main_app.root_url.rindex(main_app.root_path)) + '/'
+ rescue
+ '/'
+ end
+ end
+
+ def js_replace(dom_id, options = {})
+ editor_id = options[:editor_id].nil? ? '' : "#{options[:editor_id].to_s.downcase} = "
+ if options[:window_onload]
+ require 'securerandom'
+ random_name = SecureRandom.hex;
+ "var old_onload_#{random_name};
+ if(typeof window.onload == 'function') old_onload_#{random_name} = window.onload;
+ window.onload = function() {
+ #{editor_id}KindEditor.create('##{dom_id}', #{get_options(options).to_json});
+ if(old_onload_#{random_name}) old_onload_#{random_name}();
+ }"
+ else
+ "KindEditor.ready(function(K){
+ #{editor_id}K.create('##{dom_id}', #{get_options(options).to_json});
+ });"
+ end
+ end
+
+ def get_options(options)
+ options.delete(:editor_id)
+ options.delete(:window_onload)
+ options.reverse_merge!(:width => '100%')
+ options.reverse_merge!(:height => 300)
+ options.reverse_merge!(:allowFileManager => true)
+ options.merge!(:uploadJson => kindeditor_upload_json_path(:owner_id => options.delete(:owner_id),:owner_type => options.delete(:owner_type)))
+ options.merge!(:fileManagerJson => kindeditor_file_manager_json_path)
+ if options[:simple_mode] == true
+ options.merge!(:items => %w{fontname fontsize | forecolor hilitecolor bold italic underline removeformat | justifyleft justifycenter justifyright insertorderedlist insertunorderedlist | emoticons image link})
+ end
+ options.delete(:simple_mode)
+ options
+ end
+
+
+
+ def build_text_area_tag(name, method, template, options, input_html)
+ if Rails.version >= '4.0.0'
+ text_area_tag = ActionView::Helpers::Tags::TextArea.new(name, method, template, options)
+ text_area_tag.send(:add_default_name_and_id, input_html)
+ text_area_tag.render
+ elsif Rails.version >= '3.1.0'
+ text_area_tag = ActionView::Base::InstanceTag.new(name, method, template, options.delete(:object))
+ text_area_tag.send(:add_default_name_and_id, input_html)
+ text_area_tag.to_text_area_tag(input_html)
+ elsif Rails.version >= '3.0.0'
+ raise 'Please use rails_kindeditor v0.2.8 for Rails v3.0.x'
+ else
+ raise 'Please upgrade your Rails !'
+ end
+ end
+ end
+
+
+
+ module Builder
+ def kindeditor(method, options = {})
+ @template.send("kindeditor", @object_name, method, objectify_options(options))
+ end
+ end
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/rails_kindeditor/simple_form.rb b/lib/rails_kindeditor/lib/rails_kindeditor/simple_form.rb
new file mode 100644
index 000000000..47edad038
--- /dev/null
+++ b/lib/rails_kindeditor/lib/rails_kindeditor/simple_form.rb
@@ -0,0 +1,11 @@
+module RailsKindeditor
+ module SimpleForm
+ class KindeditorInput < ::SimpleForm::Inputs::Base
+ def input
+ @builder.kindeditor(attribute_name, input_html_options)
+ end
+ end
+ end
+end
+
+::SimpleForm::FormBuilder.map_type :kindeditor, :to => RailsKindeditor::SimpleForm::KindeditorInput
\ No newline at end of file
diff --git a/lib/rails_kindeditor/lib/rails_kindeditor/version.rb b/lib/rails_kindeditor/lib/rails_kindeditor/version.rb
new file mode 100644
index 000000000..ffac2c9d4
--- /dev/null
+++ b/lib/rails_kindeditor/lib/rails_kindeditor/version.rb
@@ -0,0 +1,4 @@
+module RailsKindeditor
+ VERSION = "0.4.5"
+end
+
diff --git a/lib/rails_kindeditor/lib/tasks/assets.rake b/lib/rails_kindeditor/lib/tasks/assets.rake
new file mode 100644
index 000000000..515bd8bf6
--- /dev/null
+++ b/lib/rails_kindeditor/lib/tasks/assets.rake
@@ -0,0 +1,9 @@
+namespace :kindeditor do
+ desc "copy kindeditor into public folder"
+ task :assets do
+ puts "copying kindeditor into public/assets folder ..."
+ dest_path = "#{Rails.root}/public/assets"
+ FileUtils.mkdir_p dest_path
+ FileUtils.cp_r "#{RailsKindeditor.root_path}/vendor/assets/javascripts/kindeditor/", dest_path
+ end
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/rails_kindeditor.gemspec b/lib/rails_kindeditor/rails_kindeditor.gemspec
new file mode 100644
index 000000000..f06083cba
--- /dev/null
+++ b/lib/rails_kindeditor/rails_kindeditor.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "rails_kindeditor/version"
+
+Gem::Specification.new do |s|
+ s.name = "rails_kindeditor"
+ s.version = RailsKindeditor::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = "Macrow"
+ s.email = "Macrow_wh@163.com"
+ s.homepage = "http://github.com/Macrow"
+ s.summary = "Kindeditor for Ruby on Rails"
+ s.description = "rails_kindeditor will helps your rails app integrate with kindeditor, including images and files uploading."
+ s.license = 'MIT'
+
+ s.rubyforge_project = "rails_kindeditor"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.require_paths = ["lib"]
+
+ s.add_dependency("carrierwave")
+ s.add_dependency("mini_magick")
+end
\ No newline at end of file
diff --git a/lib/rails_kindeditor/screenshots/rails_kindeditor.png b/lib/rails_kindeditor/screenshots/rails_kindeditor.png
new file mode 100644
index 000000000..7c7931640
Binary files /dev/null and b/lib/rails_kindeditor/screenshots/rails_kindeditor.png differ
diff --git a/lib/rails_kindeditor/screenshots/simple_mode.png b/lib/rails_kindeditor/screenshots/simple_mode.png
new file mode 100644
index 000000000..4db0b86a0
Binary files /dev/null and b/lib/rails_kindeditor/screenshots/simple_mode.png differ
diff --git a/lib/rails_kindeditor/vendor/assets/javascripts/kindeditor/kindeditor.js b/lib/rails_kindeditor/vendor/assets/javascripts/kindeditor/kindeditor.js
new file mode 100644
index 000000000..e58e4b2be
--- /dev/null
+++ b/lib/rails_kindeditor/vendor/assets/javascripts/kindeditor/kindeditor.js
@@ -0,0 +1,5963 @@
+/*******************************************************************************
+* KindEditor - WYSIWYG HTML Editor for Internet
+* Copyright (C) 2006-2013 kindsoft.net
+*
+* @author Roddy