rails_wangeditor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +172 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/wangeditor.js +1 -0
- data/app/assets/stylesheets/wangeditor.css +3 -0
- data/app/controllers/wangeditor/assets_controller.rb +45 -0
- data/app/uploaders/wangeditor/asset_uploader.rb +66 -0
- data/app/uploaders/wangeditor/image_uploader.rb +10 -0
- data/config/routes.rb +6 -0
- data/lib/generators/rails_wangeditor/install/USAGE +8 -0
- data/lib/generators/rails_wangeditor/install/install_generator.rb +30 -0
- data/lib/generators/rails_wangeditor/install/templates/application.css +15 -0
- data/lib/generators/rails_wangeditor/install/templates/application.js +16 -0
- data/lib/generators/rails_wangeditor/install/templates/rails_wangeditor.rb +27 -0
- data/lib/generators/rails_wangeditor/migration/USAGE +11 -0
- data/lib/generators/rails_wangeditor/migration/migration_generator.rb +36 -0
- data/lib/generators/rails_wangeditor/migration/templates/migration/migration.rb +18 -0
- data/lib/generators/rails_wangeditor/migration/templates/models/active_record/wangeditor/asset.rb +14 -0
- data/lib/generators/rails_wangeditor/migration/templates/models/active_record/wangeditor/image.rb +3 -0
- data/lib/generators/rails_wangeditor/migration/templates/models/mongoid/wangeditor/asset.rb +27 -0
- data/lib/generators/rails_wangeditor/migration/templates/models/mongoid/wangeditor/image.rb +3 -0
- data/lib/rails_wangeditor/active_record.rb +13 -0
- data/lib/rails_wangeditor/engine.rb +32 -0
- data/lib/rails_wangeditor/helper.rb +86 -0
- data/lib/rails_wangeditor/simple_form.rb +11 -0
- data/lib/rails_wangeditor/version.rb +4 -0
- data/lib/rails_wangeditor.rb +55 -0
- data/lib/tasks/assets.rake +10 -0
- data/rails_wangeditor.gemspec +24 -0
- data/vendor/assets/javascripts/wangEditor.js +4 -0
- data/vendor/assets/stylesheets/wangEditor/fonts/icomoon.eot +0 -0
- data/vendor/assets/stylesheets/wangEditor/fonts/icomoon.svg +76 -0
- data/vendor/assets/stylesheets/wangEditor/fonts/icomoon.ttf +0 -0
- data/vendor/assets/stylesheets/wangEditor/fonts/icomoon.woff +0 -0
- data/vendor/assets/stylesheets/wangEditor/wangEditor.css +1 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b62dd1f411fa847f9e4f5497d8f449d2899d7875
|
4
|
+
data.tar.gz: 8b1f76430c435b4d6730eecb00ae48a3470db6a4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46c5058aa41d8b9129234cb4f4d2738da734227a20d6d0589a1a6ecd778683514fd6142e28b18e3f577f728cb93a4bf7ba95a018a0971a51e0025d5e768e3e3f
|
7
|
+
data.tar.gz: abe1356868c313f5282b83d64971c19d03248a831146e3e8b8e9bdb0983b986136ba03429b3fed8f64745d84097610a9ae7e3cef1b7f77d7ac988e99a5a8430a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 RubyCat
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
# wangEditor for Ruby on Rails [![Gem Version](https://badge.fury.io/rb/rails_wangeditor.png)](http://badge.fury.io/rb/rails_wangeditor)
|
2
|
+
|
3
|
+
wangEditor is an easy, fast and beautiful WYSIWYG javascript editor, visit [https://github.com/wangfupeng1988/wangEditor](https://github.com/wangfupeng1988/wangEditor) for details.
|
4
|
+
rails_wangeditor will helps your rails app integrate with wangEditor, includes images uploading.
|
5
|
+
|
6
|
+
![](http://images2015.cnblogs.com/blog/138012/201509/138012-20150910004209122-1645253022.png)
|
7
|
+
|
8
|
+
## Installation and usage
|
9
|
+
|
10
|
+
### Add this to your Gemfile
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rails_wangeditor'
|
14
|
+
```
|
15
|
+
|
16
|
+
### Run "bundle" command.
|
17
|
+
|
18
|
+
```bash
|
19
|
+
bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
### Run install generator:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
rails generate rails_wangeditor:install
|
26
|
+
```
|
27
|
+
|
28
|
+
### Rails4 in production mode
|
29
|
+
|
30
|
+
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
|
31
|
+
In Rails 4.0's production mode, please run 'rake wangeditor:assets', this method just copy wangeditor into public folder.
|
32
|
+
|
33
|
+
```bash
|
34
|
+
rake wangeditor:assets
|
35
|
+
```
|
36
|
+
|
37
|
+
### Usage:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
1. <%= wangeditor_tag :content, 'default content value' %>
|
41
|
+
# or <%= wangeditor_tag :content, 'default content value', :input_html =>{style: "500px"} %>
|
42
|
+
```
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
2. <%= form_for @article do |f| %>
|
46
|
+
...
|
47
|
+
<%= f.wangeditor :content %>
|
48
|
+
# or <%= f.wangeditor :content, style: "500px"%>
|
49
|
+
...
|
50
|
+
<% end %>
|
51
|
+
```
|
52
|
+
|
53
|
+
When you need to specify the owner_id:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
<%= f.wangeditor :content, owner: @article%>
|
57
|
+
```
|
58
|
+
|
59
|
+
## SimpleForm integration
|
60
|
+
|
61
|
+
### simple_form:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
<%= f.wangeditor :content, style: "500px"%>
|
65
|
+
```
|
66
|
+
|
67
|
+
## Upload options configuration
|
68
|
+
|
69
|
+
When you run "rails generate rails_wangeditor:install", installer will copy configuration files in config/initializers folder.
|
70
|
+
You can customize some option for uploading.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# Specify the subfolders in public directory.
|
74
|
+
# You can customize it , eg: config.upload_dir = 'this/is/my/folder'
|
75
|
+
config.upload_dir = 'uploads'
|
76
|
+
|
77
|
+
# Allowed file types for upload.
|
78
|
+
config.upload_image_ext = %w[gif jpg jpeg png bmp]
|
79
|
+
|
80
|
+
# replace to your own baidu mapAk
|
81
|
+
config.mapAk = 'zlXbfvvyWN0vrO5toWqGhLspGHMIeFgn';
|
82
|
+
|
83
|
+
# Porcess upload image size, need mini_magick
|
84
|
+
# before => after
|
85
|
+
# eg: 1600x1600 => 800x800
|
86
|
+
# 1600x800 => 800x400
|
87
|
+
# 400x400 => 400x400 # No Change
|
88
|
+
# config.image_resize_to_limit = [800, 800]
|
89
|
+
|
90
|
+
```
|
91
|
+
|
92
|
+
## Asset host options configuration
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
# if you have config in your rails application like this:
|
96
|
+
# /config/enviroments/production.rb
|
97
|
+
# # config.action_controller.asset_host = "http://asset.example.com"
|
98
|
+
# # config.assets.prefix = "assets_prefx"
|
99
|
+
# then you should:
|
100
|
+
#
|
101
|
+
config.asset_url_prefix = "http://asset.example.com/assets_prefx/" if Rails.env.production?
|
102
|
+
```
|
103
|
+
|
104
|
+
|
105
|
+
## Save upload file information into database(optional)
|
106
|
+
|
107
|
+
rails_wangeditor can save upload file information into database.
|
108
|
+
|
109
|
+
### Just run migration generate, there are two ORM options for you: 1.active_record 2.mongoid, default is active_record.
|
110
|
+
|
111
|
+
```bash
|
112
|
+
rails generate rails_wangeditor:migration
|
113
|
+
or
|
114
|
+
rails generate rails_wangeditor:migration -o mongoid
|
115
|
+
```
|
116
|
+
|
117
|
+
### The generator will copy model and migration to your application. When you are done, remember run rake db:migrate:
|
118
|
+
|
119
|
+
```bash
|
120
|
+
rake db:migrate
|
121
|
+
```
|
122
|
+
|
123
|
+
### Delete uploaded files automatically (only for active_record)
|
124
|
+
|
125
|
+
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.
|
126
|
+
|
127
|
+
####1. specify the owner_id for wangeditor
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
<%= form_for @article do |f| %>
|
131
|
+
...
|
132
|
+
<%= f.wangeditor :content, :owner => @article %>
|
133
|
+
...
|
134
|
+
<% end %>
|
135
|
+
```
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
Warnning: the @article must be created before this scene, the @article.id should not be empty.
|
139
|
+
```
|
140
|
+
|
141
|
+
####2. add has_many_wangeditor_assets in your own model
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
class Article < ActiveRecord::Base
|
145
|
+
has_many_wangeditor_assets :attachments, :dependent => :destroy
|
146
|
+
# has_many_wangeditor_assets :attachments, :dependent => :nullify
|
147
|
+
# has_many_wangeditor_assets :your_name, :dependent => :destroy
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
####3. relationship
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
article = Article.first
|
155
|
+
article.attachments # => the article's assets uploaded by wangeditor
|
156
|
+
asset = article.attachments.first
|
157
|
+
asset.owner # => aritcle
|
158
|
+
```
|
159
|
+
|
160
|
+
### If you're using mongoid, please add 'gem "carrierwave-mongoid"' in your Gemfile
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
gem 'carrierwave-mongoid'
|
164
|
+
```
|
165
|
+
|
166
|
+
## Thanks
|
167
|
+
- 1.Macrow, https://github.com/Macrow/rails_kindeditor
|
168
|
+
- 2.wangfupeng1988, https://github.com/wangfupeng1988/wangEditor/
|
169
|
+
|
170
|
+
## License
|
171
|
+
|
172
|
+
MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
//= require wangEditor
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
require "find"
|
3
|
+
class Wangeditor::AssetsController < ApplicationController
|
4
|
+
skip_before_filter :verify_authenticity_token
|
5
|
+
def create
|
6
|
+
@imgFile, @dir = params[:imgFile]
|
7
|
+
@dir = 'Image'
|
8
|
+
unless @imgFile.nil?
|
9
|
+
if Wangeditor::AssetUploader.save_upload_info? # save upload info into database
|
10
|
+
begin
|
11
|
+
@asset = "Wangeditor::#{@dir.camelize}".constantize.new(:asset => @imgFile)
|
12
|
+
@asset.owner_id = params[:owner_id] ? params[:owner_id] : 0
|
13
|
+
@asset.owner_type = params[:owner_type] ? params[:owner_type] : ''
|
14
|
+
logger.warn '=====Warning: the owner have not been created, "delete uploaded files automatically" will not work. ====' if defined?(logger) && @asset.owner_id == 0
|
15
|
+
@asset.asset_type = @dir
|
16
|
+
if @asset.save
|
17
|
+
render :text => @asset.asset.url
|
18
|
+
else
|
19
|
+
show_error(@asset.errors.full_messages)
|
20
|
+
end
|
21
|
+
rescue Exception => e
|
22
|
+
show_error(e.to_s)
|
23
|
+
end
|
24
|
+
else # do not touch database
|
25
|
+
begin
|
26
|
+
uploader = "Wangeditor::#{@dir.camelize}Uploader".constantize.new
|
27
|
+
uploader.store!(@imgFile)
|
28
|
+
render :text => ({:error => 0, :url => uploader.url}.to_json)
|
29
|
+
rescue CarrierWave::UploadError => e
|
30
|
+
show_error(e.message)
|
31
|
+
rescue Exception => e
|
32
|
+
show_error(e.to_s)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
else
|
36
|
+
show_error("No File Selected!")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def show_error(msg)
|
42
|
+
render :text => ("error|#{msg}")
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'carrierwave/processing/mime_types'
|
4
|
+
|
5
|
+
class Wangeditor::AssetUploader < CarrierWave::Uploader::Base
|
6
|
+
|
7
|
+
EXT_NAMES = {:image => RailsWangeditor.upload_image_ext}
|
8
|
+
|
9
|
+
# Include RMagick or ImageScience support:
|
10
|
+
# include CarrierWave::RMagick
|
11
|
+
# include CarrierWave::ImageScience
|
12
|
+
# include CarrierWave::MiniMagick
|
13
|
+
|
14
|
+
# Choose what kind of storage to use for this uploader:
|
15
|
+
storage :file
|
16
|
+
# storage :fog
|
17
|
+
|
18
|
+
# Override the directory where uploaded files will be stored.
|
19
|
+
# This is a sensible default for uploaders that are meant to be mounted:
|
20
|
+
def store_dir
|
21
|
+
if Wangeditor::AssetUploader.save_upload_info?
|
22
|
+
"#{RailsWangeditor.upload_store_dir}/#{model.asset_type.to_s.underscore.gsub(/(wangeditor\/)|(_uploader)/, '')}/#{model.created_at.strftime("%Y%m")}"
|
23
|
+
else
|
24
|
+
"#{RailsWangeditor.upload_store_dir}/#{self.class.to_s.underscore.gsub(/(wangeditor\/)|(_uploader)/, '')}/#{Time.now.strftime("%Y%m")}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def cache_dir
|
29
|
+
"#{Rails.root}/tmp/uploads"
|
30
|
+
end
|
31
|
+
|
32
|
+
before :store, :remember_cache_id
|
33
|
+
after :store, :delete_tmp_dir
|
34
|
+
|
35
|
+
# store! nil's the cache_id after it finishes so we need to remember it for deletition
|
36
|
+
def remember_cache_id(new_file)
|
37
|
+
@cache_id_was = cache_id
|
38
|
+
end
|
39
|
+
|
40
|
+
def delete_tmp_dir(new_file)
|
41
|
+
# make sure we don't delete other things accidentally by checking the name pattern
|
42
|
+
if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
|
43
|
+
FileUtils.rm_rf(File.join(cache_dir, @cache_id_was))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def filename
|
48
|
+
if original_filename
|
49
|
+
@name ||= Digest::MD5.hexdigest(File.dirname(current_path)).slice(0, 12)
|
50
|
+
"#{@name}.#{file.extension}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.save_upload_info?
|
55
|
+
begin
|
56
|
+
%w(asset image).each do |s|
|
57
|
+
"Wangeditor::#{s.camelize}".constantize
|
58
|
+
end
|
59
|
+
return true
|
60
|
+
rescue
|
61
|
+
return false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
data/config/routes.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module RailsWangeditor
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
desc "Install wangEditor for your application."
|
5
|
+
|
6
|
+
def copy_wangeditor_files
|
7
|
+
if ::Rails.version < "3.1.0"
|
8
|
+
warn "Warning: rails_wangeditor only support Rails3.1+!"
|
9
|
+
else
|
10
|
+
template "rails_wangeditor.rb", "config/initializers/rails_wangeditor.rb"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def insert_or_copy_js_files
|
15
|
+
if File.exist?('app/assets/javascripts/application.js')
|
16
|
+
insert_into_file "app/assets/javascripts/application.js", "//= require wangEditor\n", :after => "jquery_ujs\n"
|
17
|
+
else
|
18
|
+
copy_file "application.js", "app/assets/javascripts/application.js"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def insert_or_copy_css_files
|
23
|
+
if File.exist?('app/assets/stylesheets/application.css')
|
24
|
+
insert_into_file "app/assets/stylesheets/application.css", "*= require wangEditor\n", :before => "*= require_self"
|
25
|
+
else
|
26
|
+
copy_file "application.css", "app/assets/stylesheets/application.css"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*= require wangEditor
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,16 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// the compiled file.
|
9
|
+
//
|
10
|
+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
11
|
+
// GO AFTER THE REQUIRES BELOW.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require wangEditor
|
15
|
+
//= require_tree .
|
16
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
RailsWangeditor.setup do |config|
|
2
|
+
|
3
|
+
# Specify the subfolders in public directory.
|
4
|
+
# You can customize it , eg: config.upload_dir = 'this/is/my/folder'
|
5
|
+
config.upload_dir = 'uploads'
|
6
|
+
|
7
|
+
# Allowed file types for upload.
|
8
|
+
config.upload_image_ext = %w[gif jpg jpeg png bmp]
|
9
|
+
|
10
|
+
# replace to your own baidu mapAk
|
11
|
+
config.mapAk = 'zlXbfvvyWN0vrO5toWqGhLspGHMIeFgn';
|
12
|
+
|
13
|
+
# Porcess upload image size
|
14
|
+
# eg: 1600x1600 => 800x800
|
15
|
+
# 1600x800 => 800x400
|
16
|
+
# 400x400 => 400x400 # No Change
|
17
|
+
# config.image_resize_to_limit = [800, 800]
|
18
|
+
|
19
|
+
# if you have config in your rails application like this:
|
20
|
+
# /config/enviroments/production.rb
|
21
|
+
# # config.action_controller.asset_host = "http://asset.example.com"
|
22
|
+
# # config.assets.prefix = "assets_prefx"
|
23
|
+
# then you should:
|
24
|
+
#
|
25
|
+
# config.asset_url_prefix = "http://asset.example.com/assets_prefx/" if Rails.env.production?
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Description:
|
2
|
+
Copy model, migration and uploader to your application.
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate rails_wangeditor:migration
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
app/models/wangeditor/asset.rb
|
9
|
+
app/models/wangeditor/image.rb
|
10
|
+
db/migrate/xxxxxxxxx_create_wangeditor_assets.rb
|
11
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RailsWangeditor
|
2
|
+
class MigrationGenerator < Rails::Generators::Base
|
3
|
+
include Rails::Generators::Migration
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
desc "Copy model and migration to your application."
|
6
|
+
class_option :orm, :type => :string, :aliases => "-o", :default => "active_record", :desc => "ORM options: active_record or mongoid"
|
7
|
+
|
8
|
+
def copy_files
|
9
|
+
orm = options[:orm].to_s
|
10
|
+
orm = "active_record" unless %w{active_record mongoid}.include?(orm)
|
11
|
+
%w(asset image).each do |file|
|
12
|
+
copy_model(orm, file)
|
13
|
+
end
|
14
|
+
if Rails.version < '4.0.0' && Rails.version >= '3.0.0' # insert code for rails3
|
15
|
+
insert_into_file "app/models/wangeditor/asset.rb", " attr_accessible :asset\n", :after => "before_save :update_asset_attributes\n"
|
16
|
+
end
|
17
|
+
if orm == "active_record"
|
18
|
+
migration_template "migration/migration.rb", "db/migrate/create_wangeditor_assets.rb"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.next_migration_number(dirname)
|
23
|
+
if ActiveRecord::Base.timestamped_migrations
|
24
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
25
|
+
else
|
26
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def copy_model(orm, name)
|
32
|
+
template "models/#{orm}/wangeditor/#{name}.rb", "app/models/wangeditor/#{name}.rb"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreateWangeditorAssets < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :wangeditor_assets do |t|
|
4
|
+
t.string :asset
|
5
|
+
t.integer :file_size
|
6
|
+
t.string :file_type
|
7
|
+
t.integer :owner_id
|
8
|
+
t.string :owner_type
|
9
|
+
t.string :asset_type
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :wangeditor_assets
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
data/lib/generators/rails_wangeditor/migration/templates/models/active_record/wangeditor/asset.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Wangeditor::Asset < ActiveRecord::Base
|
2
|
+
self.table_name = 'wangeditor_assets'
|
3
|
+
mount_uploader :asset, Wangeditor::AssetUploader
|
4
|
+
validates_presence_of :asset
|
5
|
+
before_save :update_asset_attributes
|
6
|
+
|
7
|
+
private
|
8
|
+
def update_asset_attributes
|
9
|
+
if asset.present? && asset_changed?
|
10
|
+
self.file_size = asset.file.size
|
11
|
+
self.file_type = asset.file.content_type
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'carrierwave/mongoid'
|
2
|
+
|
3
|
+
class Wangeditor::Asset
|
4
|
+
include Mongoid::Document
|
5
|
+
include Mongoid::Timestamps
|
6
|
+
|
7
|
+
field :file_size, :type => Integer
|
8
|
+
field :file_type, :type => String
|
9
|
+
field :owner_id, :type => Integer
|
10
|
+
field :asset_type, :type => String
|
11
|
+
|
12
|
+
mount_uploader :asset, Wangeditor::AssetUploader
|
13
|
+
validates_presence_of :asset
|
14
|
+
before_save :update_asset_attributes
|
15
|
+
|
16
|
+
def self.collection_name
|
17
|
+
:wangeditor_assets
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def update_asset_attributes
|
22
|
+
if asset.present? && asset_changed?
|
23
|
+
self.file_size = asset.file.size
|
24
|
+
self.file_type = asset.file.content_type
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
if defined?(ActiveRecord)
|
2
|
+
ActiveRecord::Base.class_eval do
|
3
|
+
def self.has_many_wangeditor_assets(*args)
|
4
|
+
options = args.extract_options!
|
5
|
+
asset_name = args[0] ? args[0].to_s : 'assets'
|
6
|
+
has_many asset_name.to_sym, :class_name => 'Wangeditor::Asset', :as => :owner, :dependent => options[:dependent]
|
7
|
+
class_name = self.name
|
8
|
+
Wangeditor::Asset.class_eval do
|
9
|
+
belongs_to :owner, :polymorphic => true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "rails_wangeditor"
|
2
|
+
require "rails"
|
3
|
+
require "action_controller"
|
4
|
+
|
5
|
+
module RailsWangeditor
|
6
|
+
class Engine < Rails::Engine
|
7
|
+
|
8
|
+
initializer "rails_wangeditor.assets_precompile" do |app|
|
9
|
+
app.config.assets.precompile += RailsWangeditor.assets
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer "rails_wangeditor.simple_form" do
|
13
|
+
require "rails_wangeditor/simple_form" if Object.const_defined?("SimpleForm")
|
14
|
+
end
|
15
|
+
|
16
|
+
initializer "rails_wangeditor.helper_and_builder" do
|
17
|
+
ActiveSupport.on_load :action_view do
|
18
|
+
ActionView::Base.send(:include, RailsWangeditor::Helper)
|
19
|
+
ActionView::Helpers::FormBuilder.send(:include, RailsWangeditor::Builder)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
initializer "rails_wangeditor.image_process" do
|
24
|
+
unless RailsWangeditor.image_resize_to_limit.nil?
|
25
|
+
Wangeditor::ImageUploader.class_eval do
|
26
|
+
include CarrierWave::MiniMagick
|
27
|
+
process :resize_to_limit => RailsWangeditor.resize_to_limit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|