rails_kindeditor 0.3.20 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66ce0b82bd4863150a143d291636ef6267e039c7
4
- data.tar.gz: cbd50c7c0129e8c60526bdd77250ca7c4171aeed
3
+ metadata.gz: 1a331d5aea83a1d33f5ed64b196994908278ec25
4
+ data.tar.gz: 2f16cd3b105895b10c7f068e7f229f45774619e4
5
5
  SHA512:
6
- metadata.gz: b5c25553dc71eaee18a1b7c4d11c1411110d624c90b15525aae0ffe3c5171ba563cf63c2e29d37123a23e906b1f1de5236fcb724ad511963dbc2f17384fa66eb
7
- data.tar.gz: 302641a3e917f8d38dc8964a8cb016f6cc30b5a2405a543b2ca97eac495a1f2467abb4191458ec8dbd48b5a3cfbeec8587b9e5f0b4c8d0f59966d6751d513a0b
6
+ metadata.gz: 65dffa2931494481a74d4dde52049510da0ae71ca2a0343f77aee4c33d7e0f9478ca5b6fc39c60a878f2eb98368bb1fa7f790abf10f37a48a8e7d7b2d60764b1
7
+ data.tar.gz: e55a273242e89548448f0b9eab0116acc51dece12c6791f425043b705a00016e3a2f3f8a9693dd6b40040d4d2dcb7a3270eb554980475d47e4aed27b453259e0
data/README.md CHANGED
@@ -10,7 +10,7 @@ rails_kindeditor will helps your rails app integrate with kindeditor, includes i
10
10
  ### Add this to your Gemfile
11
11
 
12
12
  ```ruby
13
- gem 'rails_kindeditor', '~> 0.3.20'
13
+ gem 'rails_kindeditor', '~> 0.4.0'
14
14
  ```
15
15
 
16
16
  ### Run "bundle" command.
@@ -65,6 +65,57 @@ additionally, rails_kindeditor provides one "simple_mode" parameter for render s
65
65
 
66
66
  That's all.
67
67
 
68
+ ### Work with turbolinks
69
+
70
+ rails_kindeditor will not load the scripts under the turbolinks, there's two way to solve this problem:
71
+
72
+ 1.use "'data-no-turbolink' => true" when we need to load kindeditor,this will shut down the turbolinks in this page
73
+
74
+ ```ruby
75
+ <%= link_to 'Edit', edit_article_path(article), 'data-no-turbolink' => true %>
76
+ ```
77
+
78
+ 2.load kindeditor manually, but you should specify the parameters again, include the textarea's id.
79
+
80
+ ```coffeescript
81
+ # coffeescript code
82
+ $(document).on 'page:load', ->
83
+ if $('#article_content').length > 0
84
+ KindEditor.create '#article_content', "width":"100%", "height":300, "allowFileManager":true, "uploadJson":"/kindeditor/upload", "fileManagerJson":"/kindeditor/filemanager"
85
+ ```
86
+
87
+ simple mode
88
+ ```coffeescript
89
+ # coffeescript code
90
+ $(document).on 'page:load', ->
91
+ if $('#article_content').length > 0
92
+ KindEditor.create '#article_content',
93
+ "width":"100%",
94
+ "height":300,
95
+ "allowFileManager":true,
96
+ "uploadJson":"/kindeditor/upload",
97
+ "fileManagerJson":"/kindeditor/filemanager",
98
+ "items":["fontname","fontsize","|","forecolor","hilitecolor","bold","italic","underline","removeformat","|","justifyleft","justifycenter","justifyright","insertorderedlist","insertunorderedlist","|","emoticons","image","link"]
99
+ ```
100
+
101
+ When you need to specify the owner_id:
102
+
103
+ ```ruby
104
+ f.kindeditor :content, owner_id: @article.id, data: {upload: kindeditor_upload_json_path(owner_id: @article.id), filemanager: kindeditor_file_manager_json_path}
105
+ ```
106
+
107
+ ```coffeescript
108
+ # coffeescript code
109
+ $(document).on 'page:load', ->
110
+ if $('#article_content').length > 0
111
+ KindEditor.create '#article_content',
112
+ "width" : "100%",
113
+ "height" : 300,
114
+ "allowFileManager" : true,
115
+ "uploadJson" : $('#article_content').data('upload'),
116
+ "fileManagerJson" : $('#article_content').data('filemanager')
117
+ ```
118
+
68
119
  ### Include javascript files at bottom ? Not in the head tag ? How can I load kindeditor correctly ?
69
120
 
70
121
  For some reasons, you includes javascript files at bottom in your template, rails_kindeditor provides a options for lazyload:
@@ -150,6 +201,40 @@ rails_kindeditor can save upload file information into database.
150
201
  rake db:migrate
151
202
  ```
152
203
 
204
+ ### Delete uploaded files automatically (only for active_record)
205
+
206
+ 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.
207
+
208
+ 1. specify the owner_id for kindeditor
209
+
210
+ ```ruby
211
+ <%= form_for @article do |f| %>
212
+ ...
213
+ <%= f.kindeditor :content, :owner_id => @article.id %>
214
+ ...
215
+ <% end %>
216
+ ```
217
+ Warnning: the @article must be created before this scene, the @article.id should not be empty.
218
+
219
+ 2. add has_many_kindeditor_assets in your own model
220
+
221
+ ```ruby
222
+ class Article < ActiveRecord::Base
223
+ has_many_kindeditor_assets :attachments, :dependent => :destroy
224
+ # has_many_kindeditor_assets :attachments, :dependent => :nullify
225
+ # has_many_kindeditor_assets :your_name, :dependent => :destroy
226
+ end
227
+ ```
228
+
229
+ 3. relationship
230
+
231
+ ```ruby
232
+ article = Article.first
233
+ article.attachments # => the article's assets uploaded by kindeditor
234
+ asset = article.attachments.first
235
+ asset.owner # => aritcle
236
+ ```
237
+
153
238
  ### If you're using mongoid, please add 'gem "carrierwave-mongoid"' in your Gemfile
154
239
 
155
240
  ```ruby
@@ -172,7 +257,7 @@ rails_kindeditor可以帮助你的rails程序集成kindeditor,包括了图片和
172
257
  ### 将下面代码加入Gemfile:
173
258
 
174
259
  ```ruby
175
- gem 'rails_kindeditor', '~> 0.3.20'
260
+ gem 'rails_kindeditor', '~> 0.4.0'
176
261
  ```
177
262
 
178
263
  ### 运行"bundle"命令:
@@ -225,6 +310,59 @@ rails_kindeditor可以帮助你的rails程序集成kindeditor,包括了图片和
225
310
 
226
311
  完毕!
227
312
 
313
+ ### 如何在turbolinks下使用
314
+
315
+ rails_kindeditor在turbolinks下不会正常加载,只有当页面刷新时才正常。turbolinks的机制就是这样的,页面根本没刷新,这和pjax是一样的,所以kindeditor没加载很正常。
316
+
317
+ 有两个办法可以解决:
318
+
319
+ 1.在需要加载kindeditor的链接加入 'data-no-turbolink' => true ,此时相当在这个页面于关闭turbolinks。
320
+
321
+ ```ruby
322
+ <%= link_to 'Edit', edit_article_path(article), 'data-no-turbolink' => true %>
323
+ ```
324
+
325
+ 2.在turbolinks载入完毕后手动加载kindeditor,不过所有参数都要设置,而且需要知道并设定textarea的id。
326
+
327
+ ```coffeescript
328
+ # coffeescript code
329
+ $(document).on 'page:load', ->
330
+ if $('#article_content').length > 0
331
+ KindEditor.create '#article_content', "width":"100%", "height":300, "allowFileManager":true, "uploadJson":"/kindeditor/upload", "fileManagerJson":"/kindeditor/filemanager"
332
+ ```
333
+
334
+ simple模式也需要手动设定
335
+ ```coffeescript
336
+ # coffeescript code
337
+ $(document).on 'page:load', ->
338
+ if $('#article_content').length > 0
339
+ KindEditor.create '#article_content',
340
+ "width":"100%",
341
+ "height":300,
342
+ "allowFileManager":true,
343
+ "uploadJson":"/kindeditor/upload",
344
+ "fileManagerJson":"/kindeditor/filemanager",
345
+ "items":["fontname","fontsize","|","forecolor","hilitecolor","bold","italic","underline","removeformat","|","justifyleft","justifycenter","justifyright","insertorderedlist","insertunorderedlist","|","emoticons","image","link"]
346
+ ```
347
+
348
+ 需要指定owner_id的方法:
349
+
350
+ ```ruby
351
+ f.kindeditor :content, owner_id: @article.id, data: {upload: kindeditor_upload_json_path(owner_id: @article.id), filemanager: kindeditor_file_manager_json_path}
352
+ ```
353
+
354
+ ```coffeescript
355
+ # coffeescript code
356
+ $(document).on 'page:load', ->
357
+ if $('#article_content').length > 0
358
+ KindEditor.create '#article_content',
359
+ "width" : "100%",
360
+ "height" : 300,
361
+ "allowFileManager" : true,
362
+ "uploadJson" : $('#article_content').data('upload'),
363
+ "fileManagerJson" : $('#article_content').data('filemanager')
364
+ ```
365
+
228
366
  ### 把javascript放在模板最下方,不放在head里面,如何正确加载kindeditor?
229
367
 
230
368
  有时候,为了加快页面载入速度,也许你会把javascript引用放在template的底部,rails_kindeditor提供了一个参数可以确保正常加载:
@@ -306,6 +444,40 @@ rails_kindeditor 可以将上传文件信息记录入数据库,以便扩展应
306
444
  rake db:migrate
307
445
  ```
308
446
 
447
+ ### 自动删除上传的文件(仅在active_record下工作)
448
+
449
+ 你可以为上传的文件指定归属,比如一名用户,或者一篇文章,当用户或者文章被删除时,所有属于该用户或者该文章的上传文件将会被自动删除。
450
+
451
+ 1. 为kindeditor指定owner_id
452
+
453
+ ```ruby
454
+ <%= form_for @article do |f| %>
455
+ ...
456
+ <%= f.kindeditor :content, :owner_id => @article.id %>
457
+ ...
458
+ <% end %>
459
+ ```
460
+ 警告: @article应该事先被创建,@article.id不应该是空的。
461
+
462
+ 2. 在你自己的模型里加入has_many_kindeditor_assets
463
+
464
+ ```ruby
465
+ class Article < ActiveRecord::Base
466
+ has_many_kindeditor_assets :attachments, :dependent => :destroy
467
+ # has_many_kindeditor_assets :attachments, :dependent => :nullify
468
+ # has_many_kindeditor_assets :your_name, :dependent => :destroy
469
+ end
470
+ ```
471
+
472
+ 3. 相互关系
473
+
474
+ ```ruby
475
+ article = Article.first
476
+ article.attachments # => the article's assets uploaded by kindeditor
477
+ asset = article.attachments.first
478
+ asset.owner # => aritcle
479
+ ```
480
+
309
481
  ### 如果你使用的是mongoid, 请在你的Gemfile里加入'gem "carrierwave-mongoid"'
310
482
 
311
483
  ```ruby
@@ -8,6 +8,8 @@ class Kindeditor::AssetsController < ApplicationController
8
8
  if Kindeditor::AssetUploader.save_upload_info? # save upload info into database
9
9
  begin
10
10
  @asset = "Kindeditor::#{@dir.camelize}".constantize.new(:asset => @imgFile)
11
+ @asset.owner_id = params[:owner_id] ? params[:owner_id] : 0
12
+ @asset.asset_type = @dir
11
13
  if @asset.save
12
14
  render :text => ({:error => 0, :url => @asset.asset.url}.to_json)
13
15
  else
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'carrierwave/processing/mime_types'
4
+
3
5
  class Kindeditor::AssetUploader < CarrierWave::Uploader::Base
4
6
 
5
7
  EXT_NAMES = {:image => RailsKindeditor.upload_image_ext,
@@ -20,7 +22,7 @@ class Kindeditor::AssetUploader < CarrierWave::Uploader::Base
20
22
  # This is a sensible default for uploaders that are meant to be mounted:
21
23
  def store_dir
22
24
  if Kindeditor::AssetUploader.save_upload_info?
23
- "#{RailsKindeditor.upload_store_dir}/#{model.class.to_s.underscore.gsub(/(kindeditor\/)|(_uploader)/, '')}/#{model.created_at.strftime("%Y%m")}"
25
+ "#{RailsKindeditor.upload_store_dir}/#{model.asset_type.to_s.underscore.gsub(/(kindeditor\/)|(_uploader)/, '')}/#{model.created_at.strftime("%Y%m")}"
24
26
  else
25
27
  "#{RailsKindeditor.upload_store_dir}/#{self.class.to_s.underscore.gsub(/(kindeditor\/)|(_uploader)/, '')}/#{Time.now.strftime("%Y%m")}"
26
28
  end
@@ -4,6 +4,8 @@ class CreateKindeditorAssets < ActiveRecord::Migration
4
4
  t.string :asset
5
5
  t.integer :file_size
6
6
  t.string :file_type
7
+ t.integer :owner_id
8
+ t.string :asset_type # list by kindeditor: image, file, media, flash
7
9
  t.timestamps
8
10
  end
9
11
  end
@@ -6,7 +6,9 @@ class Kindeditor::Asset < ActiveRecord::Base
6
6
 
7
7
  private
8
8
  def update_asset_attributes
9
- self.file_size = asset.file.size
10
- self.file_type = asset.file.content_type
9
+ if asset.present? && asset_changed?
10
+ self.file_size = asset.file.size
11
+ self.file_type = asset.file.content_type
12
+ end
11
13
  end
12
14
  end
@@ -15,7 +15,9 @@ class Kindeditor::Asset
15
15
 
16
16
  private
17
17
  def update_asset_attributes
18
- self.file_size = asset.file.size
19
- self.file_type = asset.file.content_type
18
+ if asset.present? && asset_changed?
19
+ self.file_size = asset.file.size
20
+ self.file_type = asset.file.content_type
21
+ end
20
22
  end
21
23
  end
@@ -1,5 +1,6 @@
1
1
  require 'rails_kindeditor/engine'
2
2
  require 'rails_kindeditor/helper'
3
+ require 'rails_kindeditor/active_record'
3
4
  require 'carrierwave'
4
5
  require 'mini_magick'
5
6
 
@@ -0,0 +1,14 @@
1
+ if defined?(ActiveRecord)
2
+ ActiveRecord::Base.class_eval do
3
+ def self.has_many_kindeditor_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 => 'Kindeditor::Asset', :foreign_key => 'owner_id', :dependent => options[:dependent]
7
+
8
+ class_name = self.name
9
+ Kindeditor::Asset.class_eval do
10
+ belongs_to :owner, :class_name => class_name, :foreign_key => 'owner_id'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -16,7 +16,22 @@ module RailsKindeditor
16
16
  output_buffer << javascript_tag(js_replace(input_html['id'], options))
17
17
  end
18
18
 
19
+ def kindeditor_upload_json_path(*args)
20
+ options = args.extract_options!
21
+ owner_id_query_string = options[:owner_id] ? "?owner_id=#{options[:owner_id]}" : ''
22
+ "#{root_url}kindeditor/upload#{owner_id_query_string}"
23
+ end
24
+
25
+ def kindeditor_file_manager_json_path
26
+ "#{root_url}kindeditor/filemanager"
27
+ end
28
+
19
29
  private
30
+
31
+ def root_url
32
+ main_app.respond_to?(:root_url) ? main_app.root_url : '/'
33
+ end
34
+
20
35
  def js_replace(dom_id, options = {})
21
36
  editor_id = options[:editor_id].nil? ? '' : "#{options[:editor_id].to_s.downcase} = "
22
37
  if options[:window_onload]
@@ -36,8 +51,8 @@ module RailsKindeditor
36
51
  options.reverse_merge!(:width => '100%')
37
52
  options.reverse_merge!(:height => 300)
38
53
  options.reverse_merge!(:allowFileManager => true)
39
- options.merge!(:uploadJson => "#{main_app.root_url}kindeditor/upload")
40
- options.merge!(:fileManagerJson => "#{main_app.root_url}kindeditor/filemanager")
54
+ options.merge!(:uploadJson => kindeditor_upload_json_path(:owner_id => options.delete(:owner_id)))
55
+ options.merge!(:fileManagerJson => kindeditor_file_manager_json_path)
41
56
  if options[:simple_mode] == true
42
57
  options.merge!(:items => %w{fontname fontsize | forecolor hilitecolor bold italic underline removeformat | justifyleft justifycenter justifyright insertorderedlist insertunorderedlist | emoticons image link})
43
58
  end
@@ -1,4 +1,4 @@
1
1
  module RailsKindeditor
2
- VERSION = "0.3.20"
2
+ VERSION = "0.4.0"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_kindeditor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.20
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Macrow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-27 00:00:00.000000000 Z
11
+ date: 2013-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -75,6 +75,7 @@ files:
75
75
  - lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/image.rb
76
76
  - lib/generators/rails_kindeditor/migration/templates/models/mongoid/kindeditor/media.rb
77
77
  - lib/rails_kindeditor.rb
78
+ - lib/rails_kindeditor/active_record.rb
78
79
  - lib/rails_kindeditor/engine.rb
79
80
  - lib/rails_kindeditor/formtastic.rb
80
81
  - lib/rails_kindeditor/helper.rb
@@ -305,3 +306,4 @@ signing_key:
305
306
  specification_version: 4
306
307
  summary: Kindeditor for Ruby on Rails
307
308
  test_files: []
309
+ has_rdoc: