redactor2_rails 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +207 -0
  7. data/Rakefile +2 -0
  8. data/app/controller/redactor2_rails/files_controller.rb +28 -0
  9. data/app/controller/redactor2_rails/images_controller.rb +28 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +7 -0
  12. data/config/routes.rb +4 -0
  13. data/lib/generators/redactor2/config_generator.rb +18 -0
  14. data/lib/generators/redactor2/install_generator.rb +75 -0
  15. data/lib/generators/redactor2/templates/active_record/carrierwave/devise_migration.rb +28 -0
  16. data/lib/generators/redactor2/templates/active_record/carrierwave/migration.rb +25 -0
  17. data/lib/generators/redactor2/templates/active_record/carrierwave/redactor2/asset.rb +6 -0
  18. data/lib/generators/redactor2/templates/active_record/carrierwave/redactor2/file.rb +7 -0
  19. data/lib/generators/redactor2/templates/active_record/carrierwave/redactor2/image.rb +7 -0
  20. data/lib/generators/redactor2/templates/base/carrierwave/uploaders/redactor2_rails_file_uploader.rb +15 -0
  21. data/lib/generators/redactor2/templates/base/carrierwave/uploaders/redactor2_rails_image_uploader.rb +54 -0
  22. data/lib/generators/redactor2/templates/config.js +14 -0
  23. data/lib/generators/redactor2/templates/mongoid/carrierwave/redactor/asset.rb +9 -0
  24. data/lib/generators/redactor2/templates/mongoid/carrierwave/redactor/file.rb +7 -0
  25. data/lib/generators/redactor2/templates/mongoid/carrierwave/redactor/picture.rb +7 -0
  26. data/lib/redactor2_rails.rb +40 -0
  27. data/lib/redactor2_rails/backend/carrierwave.rb +64 -0
  28. data/lib/redactor2_rails/devise.rb +11 -0
  29. data/lib/redactor2_rails/engine.rb +9 -0
  30. data/lib/redactor2_rails/http.rb +89 -0
  31. data/lib/redactor2_rails/orm/active_record.rb +31 -0
  32. data/lib/redactor2_rails/orm/base.rb +38 -0
  33. data/lib/redactor2_rails/orm/mongoid.rb +37 -0
  34. data/lib/redactor2_rails/version.rb +3 -0
  35. data/redactor2_rails.gemspec +28 -0
  36. data/vendor/assets/javascripts/redactor2_rails/config.js +14 -0
  37. data/vendor/assets/javascripts/redactor2_rails/index.js +2 -0
  38. data/vendor/assets/javascripts/redactor2_rails/langs/ar.js +50 -0
  39. data/vendor/assets/javascripts/redactor2_rails/langs/de.js +50 -0
  40. data/vendor/assets/javascripts/redactor2_rails/langs/en.js +50 -0
  41. data/vendor/assets/javascripts/redactor2_rails/langs/es.js +50 -0
  42. data/vendor/assets/javascripts/redactor2_rails/langs/fa.js +50 -0
  43. data/vendor/assets/javascripts/redactor2_rails/langs/fi.js +50 -0
  44. data/vendor/assets/javascripts/redactor2_rails/langs/fr.js +50 -0
  45. data/vendor/assets/javascripts/redactor2_rails/langs/hu.js +50 -0
  46. data/vendor/assets/javascripts/redactor2_rails/langs/it.js +50 -0
  47. data/vendor/assets/javascripts/redactor2_rails/langs/ja.js +50 -0
  48. data/vendor/assets/javascripts/redactor2_rails/langs/ko.js +50 -0
  49. data/vendor/assets/javascripts/redactor2_rails/langs/nl.js +50 -0
  50. data/vendor/assets/javascripts/redactor2_rails/langs/pl.js +50 -0
  51. data/vendor/assets/javascripts/redactor2_rails/langs/pt_br.js +50 -0
  52. data/vendor/assets/javascripts/redactor2_rails/langs/ru.js +50 -0
  53. data/vendor/assets/javascripts/redactor2_rails/langs/sv.js +50 -0
  54. data/vendor/assets/javascripts/redactor2_rails/langs/tr.js +50 -0
  55. data/vendor/assets/javascripts/redactor2_rails/langs/zh_cn.js +52 -0
  56. data/vendor/assets/javascripts/redactor2_rails/langs/zh_tw.js +50 -0
  57. data/vendor/assets/javascripts/redactor2_rails/plugins.js +4 -0
  58. data/vendor/assets/javascripts/redactor2_rails/plugins/clips.js +60 -0
  59. data/vendor/assets/javascripts/redactor2_rails/plugins/fullscreen.js +159 -0
  60. data/vendor/assets/javascripts/redactor2_rails/plugins/source.js +195 -0
  61. data/vendor/assets/javascripts/redactor2_rails/plugins/textdirection.js +36 -0
  62. data/vendor/assets/javascripts/redactor2_rails/redactor.js +9989 -0
  63. data/vendor/assets/javascripts/redactor2_rails/redactor.min.js +13 -0
  64. data/vendor/assets/stylesheets/redactor2_rails/css/redactor.css +931 -0
  65. data/vendor/assets/stylesheets/redactor2_rails/index.css +1 -0
  66. data/vendor/assets/stylesheets/redactor2_rails/plugins.css +1 -0
  67. data/vendor/assets/stylesheets/redactor2_rails/plugins/clips.css +10 -0
  68. metadata +183 -0
@@ -0,0 +1,28 @@
1
+ class CreateRedactor2Assets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :redactor2_assets do |t|
4
+ # Change column name and override Redactor2Rails.devise_user_key
5
+ t.integer :user_id
6
+
7
+ t.string :data_file_name, :null => false
8
+ t.string :data_content_type
9
+ t.integer :data_file_size
10
+
11
+ t.integer :assetable_id
12
+ t.string :assetable_type, :limit => 30
13
+ t.string :type, :limit => 30
14
+
15
+ t.integer :width
16
+ t.integer :height
17
+
18
+ t.timestamps
19
+ end
20
+
21
+ add_index 'redactor2_assets', ['assetable_type', 'type', 'assetable_id'], :name => 'idx_redactor2_assetable_type'
22
+ add_index 'redactor2_assets', ['assetable_type', 'assetable_id'], :name => 'idx_redactor2_assetable'
23
+ end
24
+
25
+ def self.down
26
+ drop_table :redactor2_assets
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ class CreateRedactor2Assets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :redactor2_assets do |t|
4
+ t.string :data_file_name, :null => false
5
+ t.string :data_content_type
6
+ t.integer :data_file_size
7
+
8
+ t.integer :assetable_id
9
+ t.string :assetable_type, :limit => 30
10
+ t.string :type, :limit => 30
11
+
12
+ t.integer :width
13
+ t.integer :height
14
+
15
+ t.timestamps
16
+ end
17
+
18
+ add_index 'redactor2_assets', ['assetable_type', 'type', 'assetable_id'], :name => 'idx_redactor2_assetable_type'
19
+ add_index 'redactor2_assets', ['assetable_type', 'assetable_id'], :name => 'idx_redactor2_assetable'
20
+ end
21
+
22
+ def self.down
23
+ drop_table :redactor2_assets
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ class Redactor2Rails::Asset < ActiveRecord::Base
2
+ include Redactor2Rails::Orm::ActiveRecord::AssetBase
3
+
4
+ delegate :url, :current_path, :size, :content_type, :filename, :to => :data
5
+ validates_presence_of :data
6
+ end
@@ -0,0 +1,7 @@
1
+ class Redactor2Rails::File < Redactor2Rails::Asset
2
+ mount_uploader :data, Redactor2RailsFileUploader, :mount_on => :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Redactor2Rails::Image < Redactor2Rails::Asset
2
+ mount_uploader :data, Redactor2RailsImageUploader, :mount_on => :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ class Redactor2RailsFileUploader < CarrierWave::Uploader::Base
3
+ include Redactor2Rails::Backend::CarrierWave
4
+
5
+ # storage :fog
6
+ storage :file
7
+
8
+ def store_dir
9
+ "system/redactor2_assets/files/#{model.id}"
10
+ end
11
+
12
+ def extension_white_list
13
+ Redactor2Rails.files_file_types
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ class Redactor2RailsImageUploader < CarrierWave::Uploader::Base
3
+ include Redactor2Rails::Backend::CarrierWave
4
+
5
+ # Include RMagick or ImageScience support:
6
+ # include CarrierWave::RMagick
7
+ include CarrierWave::MiniMagick
8
+ # include CarrierWave::ImageScience
9
+
10
+ # Choose what kind of storage to use for this uploader:
11
+ # storage :fog
12
+ storage :file
13
+
14
+ # Override the directory where uploaded files will be stored.
15
+ # This is a sensible default for uploaders that are meant to be mounted:
16
+ def store_dir
17
+ "system/redactor2_assets/images/#{model.id}"
18
+ end
19
+
20
+ # Provide a default URL as a default if there hasn't been a file uploaded:
21
+ # def default_url
22
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
23
+ # end
24
+
25
+ # Process files as they are uploaded:
26
+ # process :scale => [200, 300]
27
+ #
28
+ # def scale(width, height)
29
+ # # do something
30
+ # end
31
+
32
+ process :read_dimensions
33
+
34
+ # Create different versions of your uploaded files:
35
+ version :thumb do
36
+ process :resize_to_fill => [100, 100]
37
+ end
38
+
39
+ version :content do
40
+ process :resize_to_limit => [800, 800]
41
+ end
42
+
43
+ # Add a white list of extensions which are allowed to be uploaded.
44
+ # For images you might use something like this:
45
+ def extension_white_list
46
+ Redactor2Rails.images_file_types
47
+ end
48
+
49
+ # Override the filename of the uploaded files:
50
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
51
+ # def filename
52
+ # "something.jpg" if original_filename
53
+ # end
54
+ end
@@ -0,0 +1,14 @@
1
+ $(function () {
2
+ // Pass authenticity_token
3
+ var params = '[name="authenticity_token"]';
4
+ // Set global settings
5
+ $.Redactor.settings = {
6
+ //plugins: ['source', 'fullscreen', 'textdirection', 'clips'],
7
+ imageUpload: '/redactor2_rails/images',
8
+ imageUploadFields: params,
9
+ fileUpload: '/redactor2_rails/files',
10
+ fileUploadFields: params
11
+ };
12
+ // Initialize Redactor
13
+ $('.redactor').redactor();
14
+ });
@@ -0,0 +1,9 @@
1
+ class Redactor2Rails::Asset
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ include Redactor2Rails::Orm::Mongoid::AssetBase
6
+
7
+ delegate :url, :current_path, :size, :content_type, :filename, :to => :data
8
+ validates_presence_of :data
9
+ end
@@ -0,0 +1,7 @@
1
+ class Redactor2Rails::File < Redactor2Rails::Asset
2
+ mount_uploader :data, Redactor2RailsFileUploader, :mount_on => :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Redactor2Rails::Picture < Redactor2Rails::Asset
2
+ mount_uploader :data, Redactor2RailsImageUploader, :mount_on => :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,40 @@
1
+ require 'redactor2_rails/version'
2
+ require 'orm_adapter'
3
+
4
+ module Redactor2Rails
5
+ IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/pjpeg', 'image/tiff', 'image/x-png']
6
+
7
+ FILE_TYPES = ['application/msword', 'application/pdf', 'text/plain', 'text/rtf', 'application/vnd.ms-excel']
8
+
9
+ autoload :Http, 'redactor2_rails/http'
10
+ autoload :Devise, 'redactor2_rails/devise'
11
+
12
+ module Backend
13
+ autoload :CarrierWave, 'redactor2_rails/backend/carrierwave'
14
+ end
15
+ require 'redactor2_rails/orm/base'
16
+ require 'redactor2_rails/orm/active_record'
17
+ require 'redactor2_rails/orm/mongoid'
18
+ require 'redactor2_rails/engine'
19
+
20
+ mattr_accessor :images_file_types, :files_file_types
21
+ @@images_file_types = ['jpg', 'jpeg', 'png', 'gif', 'tiff']
22
+ @@files_file_types = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'rtf', 'txt']
23
+
24
+
25
+ def self.image_model
26
+ Redactor2Rails::Image
27
+ end
28
+
29
+ def self.file_model
30
+ Redactor2Rails::File
31
+ end
32
+
33
+ def self.devise_user
34
+ :user
35
+ end
36
+
37
+ def self.devise_user_key
38
+ "#{self.devise_user.to_s}_id".to_sym
39
+ end
40
+ end
@@ -0,0 +1,64 @@
1
+ require 'mime/types'
2
+ require 'mini_magick'
3
+
4
+ module Redactor2Rails
5
+ module Backend
6
+ module CarrierWave
7
+ def self.included(base)
8
+ base.send(:include, InstanceMethods)
9
+ base.send(:extend, ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def self.extended(base)
14
+ base.class_eval do
15
+ process :extract_content_type
16
+ process :set_size
17
+ end
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ # process :strip
23
+ def strip
24
+ manipulate! do |img|
25
+ img.strip
26
+ img = yield(img) if block_given?
27
+ img
28
+ end
29
+ end
30
+
31
+ # process :quality => 85
32
+ def quality(percentage)
33
+ manipulate! do |img|
34
+ img.quality(percentage)
35
+ img = yield(img) if block_given?
36
+ img
37
+ end
38
+ end
39
+
40
+ def extract_content_type
41
+ if file.content_type == 'application/octet-stream' || file.content_type.blank?
42
+ content_type = MIME::Types.type_for(original_filename).first
43
+ else
44
+ content_type = file.content_type
45
+ end
46
+
47
+ model.data_content_type = content_type.to_s
48
+ end
49
+
50
+ def set_size
51
+ model.data_file_size = file.size
52
+ end
53
+
54
+ def read_dimensions
55
+ if model.image? && model.has_dimensions?
56
+ magick = ::MiniMagick::Image.new(current_path)
57
+ model.width, model.height = magick[:width], magick[:height]
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,11 @@
1
+ module Redactor2Rails
2
+ module Devise
3
+ def redactor2_authenticate_user!
4
+ authenticate_user!
5
+ end
6
+
7
+ def redactor2_current_user
8
+ current_user
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Redactor2Rails
2
+ class Engine < Rails::Engine
3
+ isolate_namespace Redactor2Rails
4
+
5
+ initializer 'redactor2_devise' do |app|
6
+ ActionController::Base.send :include, Redactor2Rails::Devise
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+ require 'digest/sha1'
3
+ require 'mime/types'
4
+
5
+ module Redactor2Rails
6
+ module Http
7
+ # Create tempfile from hash
8
+ class UploadedFile
9
+ attr_accessor :original_filename, :content_type, :tempfile, :headers
10
+
11
+ def initialize(hash)
12
+ @original_filename = hash[:filename]
13
+ @content_type = hash[:type]
14
+ @headers = hash[:head]
15
+ @tempfile = hash[:tempfile]
16
+ raise(ArgumentError, ':tempfile is required') unless @tempfile
17
+ end
18
+
19
+ def open
20
+ @tempfile.open
21
+ end
22
+
23
+ def path
24
+ @tempfile.path
25
+ end
26
+
27
+ def read(*args)
28
+ @tempfile.read(*args)
29
+ end
30
+
31
+ def rewind
32
+ @tempfile.rewind
33
+ end
34
+
35
+ def size
36
+ @tempfile.size
37
+ end
38
+ end
39
+
40
+ # Usage (paperclip example)
41
+ # @asset.data = QqFile.new(params[:qqfile], request)
42
+ class QqFile < ::Tempfile
43
+
44
+ def initialize(filename, request, tmpdir = Dir::tmpdir)
45
+ @original_filename = filename
46
+ @request = request
47
+
48
+ super Digest::SHA1.hexdigest(filename), tmpdir
49
+ fetch
50
+ end
51
+
52
+ def fetch
53
+ self.write(body)
54
+ self.rewind
55
+ self
56
+ end
57
+
58
+ def original_filename
59
+ @original_filename
60
+ end
61
+
62
+ def content_type
63
+ types = MIME::Types.type_for(original_filename)
64
+ types.empty? ? @request.content_type : types.first.to_s
65
+ end
66
+
67
+ def body
68
+ if @request.raw_post.respond_to?(:force_encoding)
69
+ @request.raw_post.force_encoding("UTF-8")
70
+ else
71
+ @request.raw_post
72
+ end
73
+ end
74
+ end
75
+
76
+ # Convert nested Hash to HashWithIndifferentAccess and replace
77
+ # file upload hash with UploadedFile objects
78
+ def self.normalize_param(*args)
79
+ value = args.first
80
+ if Hash === value && value.has_key?(:tempfile)
81
+ UploadedFile.new(value)
82
+ elsif value.is_a?(String)
83
+ QqFile.new(*args)
84
+ else
85
+ value
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,31 @@
1
+ require 'redactor2_rails/orm/base'
2
+
3
+ module Redactor2Rails
4
+ module Orm
5
+ module ActiveRecord
6
+ module AssetBase
7
+ def self.included(base)
8
+ base.send(:include, Base::AssetBase::InstanceMethods)
9
+ base.send(:extend, ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def self.extended(base)
14
+ base.class_eval do
15
+ self.table_name = 'redactor2_assets'
16
+
17
+ belongs_to :assetable, :polymorphic => true
18
+ belongs_to Redactor2Rails.devise_user, :foreign_key => Redactor2Rails.devise_user_key
19
+
20
+ if defined?(ActiveModel::ForbiddenAttributesProtection) && base.ancestors.include?(ActiveModel::ForbiddenAttributesProtection)
21
+ # Ok
22
+ elsif defined?(ActiveModel::MassAssignmentSecurity) && base.ancestors.include?(ActiveModel::MassAssignmentSecurity)
23
+ attr_accessible :data, :assetable_type, :assetable_id, :assetable
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end