redactor3_rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) 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/redactor3_rails/files_controller.rb +47 -0
  9. data/app/controller/redactor3_rails/images_controller.rb +45 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +7 -0
  12. data/config/routes.rb +4 -0
  13. data/lib/generators/redactor3/config_generator.rb +18 -0
  14. data/lib/generators/redactor3/install_generator.rb +75 -0
  15. data/lib/generators/redactor3/templates/active_record/carrierwave/devise_migration.rb +29 -0
  16. data/lib/generators/redactor3/templates/active_record/carrierwave/migration.rb +25 -0
  17. data/lib/generators/redactor3/templates/active_record/carrierwave/redactor2/asset.rb +6 -0
  18. data/lib/generators/redactor3/templates/active_record/carrierwave/redactor2/file.rb +7 -0
  19. data/lib/generators/redactor3/templates/active_record/carrierwave/redactor2/image.rb +7 -0
  20. data/lib/generators/redactor3/templates/base/carrierwave/uploaders/redactor3_rails_file_uploader.rb +15 -0
  21. data/lib/generators/redactor3/templates/base/carrierwave/uploaders/redactor3_rails_image_uploader.rb +54 -0
  22. data/lib/generators/redactor3/templates/config.js +16 -0
  23. data/lib/generators/redactor3/templates/mongoid/carrierwave/redactor/asset.rb +9 -0
  24. data/lib/generators/redactor3/templates/mongoid/carrierwave/redactor/file.rb +7 -0
  25. data/lib/generators/redactor3/templates/mongoid/carrierwave/redactor/picture.rb +7 -0
  26. data/lib/redactor3_rails/backend/carrierwave.rb +64 -0
  27. data/lib/redactor3_rails/devise.rb +11 -0
  28. data/lib/redactor3_rails/engine.rb +9 -0
  29. data/lib/redactor3_rails/http.rb +89 -0
  30. data/lib/redactor3_rails/orm/active_record.rb +31 -0
  31. data/lib/redactor3_rails/orm/base.rb +38 -0
  32. data/lib/redactor3_rails/orm/mongoid.rb +37 -0
  33. data/lib/redactor3_rails/version.rb +3 -0
  34. data/lib/redactor3_rails.rb +40 -0
  35. data/redactor3_rails.gemspec +28 -0
  36. data/vendor/assets/javascripts/redactor3_rails/config.js +16 -0
  37. data/vendor/assets/javascripts/redactor3_rails/index.js +1 -0
  38. metadata +152 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: abc1b66b53f02bdddd3a0c9afbba1b38345d1c6d6a55a1baf18ebb71641e6939
4
+ data.tar.gz: 40934b6a18c82e86cc133e260416024b940e8e95afec51de4e43515e1e450581
5
+ SHA512:
6
+ metadata.gz: 0d21a5abe2b26251b4fbbe67b7c72177e06127a71ec70e7aca02b0e9e861734a4fc74c02b8e2e51c6569ca4ada3ace789491f60044993f9df37b96a82277c15f
7
+ data.tar.gz: e94992c4413d26c4156bd373648e8e994e39a116defb5697b5c504081da8f44c139070975b84285245003794c26ffceafbf2f7f29de6af5d0e1fdf47053797c7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redactor3_rails.gemspec
4
+ gemspec
5
+ gem 'rails', '>= 3.2'
6
+
7
+ platforms :ruby do
8
+ group :active_record do
9
+ gem 'carrierwave'
10
+ gem 'mini_magick'
11
+ end
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Oleg Sulyanov
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,207 @@
1
+ # Imperavi Redactor for Rails 5 Asset Pipeline
2
+
3
+ redactor3_rails integrates Imperavi Redactor for Rails Asset Pipeline (Rails 4, 3.x versions are supported)
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/redactor3_rails.svg)](https://badge.fury.io/rb/redactor3_rails)
6
+
7
+ In order to use this gem, you must purchase a license from Imperavi
8
+ (https://imperavi.com/redactor/buy), download Redactor II files from them,
9
+ and place redactor.js file in the following location:
10
+
11
+ `app/assets/javascripts/`
12
+
13
+ And redactor.css file in the following location:
14
+
15
+ `app/assets/stylesheets/`
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'redactor3_rails'
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install redactor3_rails
30
+
31
+ ### Generate models for store uploading files
32
+
33
+ #### ActiveRecord + carrierwave
34
+ Add this lines to your application's Gemfile:
35
+
36
+ gem "carrierwave"
37
+ gem "mini_magick"
38
+
39
+ And then execute:
40
+
41
+ $ rails generate redactor3:install
42
+
43
+ or
44
+
45
+ $ rails generate redactor3:install --devise
46
+
47
+ # --devise option generate user_id attribute for asset(Imeg, File) models.
48
+ # For more details show Devise gem.
49
+ # Now, Pictures and Documents uploading available only for signed in users
50
+ # All uploaded files will stored with current user_id
51
+ # User will choose only own uploaded Images and Files
52
+
53
+ $ rake db:migrate
54
+
55
+ #### Mongoid + carrierwave
56
+ Add this lines to your application's Gemfile:
57
+
58
+ gem "carrierwave"
59
+ gem "carrierwave-mongoid", :require => "carrierwave/mongoid"
60
+ gem "mini_magick"
61
+
62
+ $ rails generate redactor3:install
63
+
64
+ ### Include the Redactor assets
65
+
66
+ Add to your `application.js`:
67
+
68
+ //= require redactor3_rails
69
+ //= require redactor
70
+
71
+ Add to your `application.css`:
72
+
73
+ *= require redactor
74
+
75
+ ### Initialize Redactor
76
+
77
+ For each textarea that you want to use with Redactor,
78
+ add the "redactor" class and ensure it has a unique ID:
79
+
80
+ <%= text_area_tag :editor, "", :class => "redactor", :rows => 40, :cols => 120 %>
81
+
82
+ You need to put your textarea inside the form with `authenticity_token` field.
83
+
84
+ ### Custom Your redactor
85
+
86
+ If you need change some config in redactor, you can
87
+
88
+ $ rails generate redactor3:config
89
+
90
+ Then generate `app/assets/javascripts/redactor3_rails/config.js`.
91
+
92
+ See the [Redactor Documentation](http://imperavi.com/redactor/docs/settings/) for a full list of configuration options.
93
+
94
+
95
+ If You Want To setup a new language in Redactor you should do three things:
96
+
97
+ In you file `app/assets/javascripts/redactor3_rails/config.js` set option
98
+
99
+ "lang":'ru'
100
+
101
+ Place lang files in the following location:
102
+
103
+ `app/assets/javascripts/langs/`
104
+
105
+ and
106
+
107
+ Add to your `application.js`:
108
+
109
+ //= require langs/ru
110
+
111
+ #### Setting a max image size with carrierwave
112
+
113
+ If you want to set a maximum image size used when a user uploads an image via carrierwave, open the uploader file and add add the following:
114
+
115
+ # app/uploaders/redactor3_rails_picture_uploader.rb:33
116
+
117
+ process :resize_to_limit => [500, -1]
118
+
119
+ The above example will set the image to have a maximum width of 500px.
120
+
121
+ ### Using plugins
122
+
123
+ After including the desired plugins they can be configured in the redactor config file as normal.
124
+
125
+ To add it into the editor add 'plugins' attributes to config.js file and specify which ones do you want to use:
126
+
127
+ $('.redactor').redactor(
128
+ { "plugins": ['fullscreen',
129
+ 'textdirection',
130
+ 'clips']
131
+ });
132
+
133
+ Full details of these can be found at [Redactor Plugins](http://imperavi.com/redactor/plugins/)
134
+
135
+ ### Defining a Devise User Model
136
+
137
+ By default redactor3_rails uses the `User` model.
138
+
139
+ You may use a different model by:
140
+
141
+ 1. Run a migration to update the user_id column in the
142
+ 2. Overriding the user class in an initializer.
143
+ 3. Overriding the authentication helpers in your controller.
144
+
145
+ Create a new Migration: `rails g rename_user_id_to_new_user_id`
146
+
147
+ # db/migrate/...rename_user_id_to_new_user_id.rb
148
+
149
+ class RenameUserIdToNewUserId < ActiveRecord::Migration
150
+ def up
151
+ rename_column :redactor3_assets, :user_id, :admin_user_id
152
+ end
153
+
154
+ def down
155
+ rename_column :redactor3_assets, :admin_user_id, :user_id
156
+ end
157
+ end
158
+
159
+ # config/initializers/redactor3.rb
160
+ # Overrides the user class
161
+
162
+ module RedactorRails
163
+ def self.devise_user
164
+ %s(admin_user) # name of your user class
165
+ end
166
+
167
+ # You may override this to support legacy schema.
168
+ # def self.devise_user_key
169
+ # "#{self.devise_user.to_s}_id".to_sym
170
+ # end
171
+ end
172
+
173
+ # app/controllers/application_controller.rb
174
+
175
+ class ApplicationController < ActionController::Base
176
+ ...
177
+
178
+ def redactor3_authenticate_user!
179
+ authenticate_admin_user! # devise before_filter
180
+ end
181
+
182
+ def redactor3_current_user
183
+ current_admin_user # devise user helper
184
+ end
185
+ end
186
+
187
+ ## Statement
188
+
189
+ `redactor3_rails` base on [SammyLin/redactor-rails](https://github.com/SammyLin/redactor-rails) project.
190
+
191
+
192
+ ## Contributing
193
+
194
+ 1. Fork it
195
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
196
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
197
+ 4. Push to the branch (`git push origin my-new-feature`)
198
+ 5. Create new Pull Request
199
+
200
+ ## License
201
+
202
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
203
+
204
+ You may use `Redactor` for non-commercial websites for free, however, we do not guarantee any technical support.
205
+
206
+ Redactor has [3 different licenses](http://imperavi.com/redactor/download/) for commercial use.
207
+ For details please see [License Agreement](http://imperavi.com/redactor/license/).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,47 @@
1
+ class Redactor3Rails::FilesController < ApplicationController
2
+ before_filter :redactor3_authenticate_user!
3
+
4
+ def create
5
+ json = {}
6
+ error = nil
7
+ params[:file].each_with_index do |file, i|
8
+ @file = Redactor3Rails.file_model.new
9
+ @file.data = Redactor3Rails::Http.normalize_param(file, request)
10
+ if @file.has_attribute?(:"#{Redactor3Rails.devise_user_key}")
11
+ @file.send("#{Redactor3Rails.devise_user}=", redactor3_current_user)
12
+ @file.assetable = redactor3_current_user
13
+ end
14
+ @file.custom_file_name = file.original_filename
15
+
16
+ if @file.save
17
+ json["file-#{i}"] = { id: @file.id, url: @file.url, name: @file.filename, size: number_to_human_size(@file.data_file_size) }
18
+ else
19
+ error = @file.errors
20
+ end
21
+ end
22
+ if json.empty?
23
+ json = { error: true, message: error.full_messages.to_sentence }
24
+ end
25
+ render json: json
26
+ end
27
+
28
+ def index
29
+ json = Redactor3Rails.file_model.order('id DESC').map do |file|
30
+ {
31
+ title: file.custom_file_name,
32
+ size: number_to_human_size(file.data_file_size),
33
+ url: file.url,
34
+ id: file.id
35
+ }
36
+ end
37
+ render json: json
38
+ end
39
+
40
+ private
41
+
42
+ def redactor3_authenticate_user!
43
+ if Redactor3Rails.file_model.new.has_attribute?(Redactor3Rails.devise_user)
44
+ super
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ class Redactor3Rails::ImagesController < ApplicationController
2
+ before_filter :redactor3_authenticate_user!
3
+
4
+ def create
5
+ json = {}
6
+ error = nil
7
+ params[:file].each_with_index do |file, i|
8
+ @image = Redactor3Rails.image_model.new
9
+ @image.data = Redactor3Rails::Http.normalize_param(file, request)
10
+ if @image.has_attribute?(:"#{Redactor3Rails.devise_user_key}")
11
+ @image.send("#{Redactor3Rails.devise_user}=", redactor3_current_user)
12
+ @image.assetable = redactor3_current_user
13
+ end
14
+
15
+ if @image.save
16
+ json["file-#{i}"] = { id: @image.id, url: @image.url(:content) }
17
+ else
18
+ error = @image.errors
19
+ end
20
+ end
21
+ if json.empty?
22
+ json = { error: true, message: error.full_messages.to_sentence }
23
+ end
24
+ render json: json
25
+ end
26
+
27
+ def index
28
+ json = Redactor3Rails.image_model.order('id DESC').map do |image|
29
+ {
30
+ thumb: image.data.thumb.url,
31
+ url: image.url(:content),
32
+ id: image.id
33
+ }
34
+ end
35
+ render json: json
36
+ end
37
+
38
+ private
39
+
40
+ def redactor3_authenticate_user!
41
+ if Redactor3Rails.image_model.new.has_attribute?(Redactor3Rails.devise_user)
42
+ super
43
+ end
44
+ end
45
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redactor3_rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Redactor3Rails::Engine.routes.draw do
2
+ resources :images, only: [:create, :index]
3
+ resources :files, only: [:create, :index]
4
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+
3
+ module Redactor3
4
+ module Generators
5
+ class ConfigGenerator < ::Rails::Generators::Base
6
+ desc 'Generates redactor3_rails config'
7
+
8
+ def self.source_root
9
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
10
+ end
11
+
12
+
13
+ def create_config
14
+ template 'config.js', File.join('app/assets/javascripts/redactor3_rails', 'config.js')
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,75 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ module Redactor3
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ include ::Rails::Generators::Migration
7
+ desc 'Generates redactor3_rails models, uploaders and migration'
8
+
9
+ class_option :orm, :type => :string, :default => 'active_record',
10
+ :desc => 'ORM (active_record/mongoid)'
11
+
12
+ class_option :upload_processor, :type => :string, :default => 'carrierwave',
13
+ :desc => 'Image processor (carrierwave)'
14
+
15
+ def self.source_root
16
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
17
+ end
18
+
19
+ def self.next_migration_number(dirname)
20
+ Time.now.strftime('%Y%m%d%H%M%S')
21
+ end
22
+
23
+ def mount_engine
24
+ route "mount Redactor3Rails::Engine => '/redactor3_rails'"
25
+ end
26
+
27
+ def create_models
28
+ [:asset, :image, :file].each do |filename|
29
+ template "#{generator_dir}/redactor3/#{filename}.rb",
30
+ File.join('app/models', redactor3_dir, "#{filename}.rb")
31
+ end
32
+
33
+ if upload_processor.to_s == 'carrierwave'
34
+ [:image, :file].each do |filename|
35
+ template "#{uploaders_dir}/uploaders/redactor3_rails_#{filename}_uploader.rb",
36
+ File.join("app/uploaders", "redactor3_rails_#{filename}_uploader.rb")
37
+ end
38
+ end
39
+ end
40
+
41
+ def create_migrations
42
+ if orm.to_s == "active_record"
43
+ if ARGV.include?('--devise')
44
+ migration_template "#{generator_dir}/devise_migration.rb", File.join('db/migrate', 'create_redactor3_assets.rb')
45
+ else
46
+ migration_template "#{generator_dir}/migration.rb", File.join('db/migrate', 'create_redactor3_assets.rb')
47
+ end
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def redactor3_dir
54
+ 'redactor3_rails'
55
+ end
56
+
57
+ def generator_dir
58
+ @generator_dir ||= [orm, upload_processor].join('/')
59
+ end
60
+
61
+ def uploaders_dir
62
+ @uploaders_dir ||= ['base', 'carrierwave'].join('/')
63
+ end
64
+
65
+ def orm
66
+ options[:orm] || 'active_record'
67
+ end
68
+
69
+ def upload_processor
70
+ options[:upload_processor] || 'carrierwave'
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,29 @@
1
+ class CreateRedactor3Assets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :redactor3_assets do |t|
4
+ # Change column name and override Redactor3Rails.devise_user_key
5
+ t.integer :user_id
6
+
7
+ t.string :custom_file_name
8
+ t.string :data_file_name, :null => false
9
+ t.string :data_content_type
10
+ t.integer :data_file_size
11
+
12
+ t.integer :assetable_id
13
+ t.string :assetable_type, :limit => 30
14
+ t.string :type, :limit => 30
15
+
16
+ t.integer :width
17
+ t.integer :height
18
+
19
+ t.timestamps
20
+ end
21
+
22
+ add_index 'redactor3_assets', ['assetable_type', 'type', 'assetable_id'], :name => 'idx_redactor3_assetable_type'
23
+ add_index 'redactor3_assets', ['assetable_type', 'assetable_id'], :name => 'idx_redactor3_assetable'
24
+ end
25
+
26
+ def self.down
27
+ drop_table :redactor3_assets
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ class CreateRedactor3Assets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :redactor3_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 'redactor3_assets', ['assetable_type', 'type', 'assetable_id'], :name => 'idx_redactor3_assetable_type'
19
+ add_index 'redactor3_assets', ['assetable_type', 'assetable_id'], :name => 'idx_redactor3_assetable'
20
+ end
21
+
22
+ def self.down
23
+ drop_table :redactor3_assets
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ class Redactor3Rails::Asset < ActiveRecord::Base
2
+ include Redactor3Rails::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 Redactor3Rails::File < Redactor3Rails::Asset
2
+ mount_uploader :data, Redactor3RailsFileUploader, :mount_on => :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Redactor3Rails::Image < Redactor3Rails::Asset
2
+ mount_uploader :data, Redactor3RailsImageUploader, :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 Redactor3RailsFileUploader < CarrierWave::Uploader::Base
3
+ include Redactor3Rails::Backend::CarrierWave
4
+
5
+ # storage :fog
6
+ storage :file
7
+
8
+ def store_dir
9
+ "system/redactor3_assets/files/#{model.id}"
10
+ end
11
+
12
+ def extension_white_list
13
+ Redactor3Rails.files_file_types
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ class Redactor3RailsImageUploader < CarrierWave::Uploader::Base
3
+ include Redactor3Rails::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/redactor3_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
+ Redactor3Rails.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,16 @@
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: '/redactor3_rails/images',
8
+ imageManagerJson: '/redactor3_rails/images',
9
+ imageUploadFields: params,
10
+ fileUpload: '/redactor3_rails/files',
11
+ fileManagerJson: '/redactor3_rails/files',
12
+ fileUploadFields: params
13
+ };
14
+ // Initialize Redactor
15
+ $('.redactor').redactor();
16
+ });
@@ -0,0 +1,9 @@
1
+ class Redactor3Rails::Asset
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ include Redactor3Rails::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 Redactor3Rails::File < Redactor3Rails::Asset
2
+ mount_uploader :data, Redactor3RailsFileUploader, :mount_on => :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Redactor3Rails::Picture < Redactor3Rails::Asset
2
+ mount_uploader :data, Redactor3RailsImageUploader, :mount_on => :data_file_name
3
+
4
+ def url_content
5
+ url(:content)
6
+ end
7
+ end
@@ -0,0 +1,64 @@
1
+ require 'mime/types'
2
+ require 'mini_magick'
3
+
4
+ module Redactor3Rails
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 Redactor3Rails
2
+ module Devise
3
+ def redactor3_authenticate_user!
4
+ authenticate_user!
5
+ end
6
+
7
+ def redactor3_current_user
8
+ current_user
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Redactor3Rails
2
+ class Engine < Rails::Engine
3
+ isolate_namespace Redactor3Rails
4
+
5
+ initializer 'redactor3_devise' do |app|
6
+ ActionController::Base.send :include, Redactor3Rails::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 Redactor3Rails
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 'redactor3_rails/orm/base'
2
+
3
+ module Redactor3Rails
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 = 'redactor3_assets'
16
+
17
+ belongs_to :assetable, :polymorphic => true
18
+ belongs_to Redactor3Rails.devise_user, :foreign_key => Redactor3Rails.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
@@ -0,0 +1,38 @@
1
+ module Redactor3Rails
2
+ module Orm
3
+ module Base
4
+ module AssetBase
5
+ module InstanceMethods
6
+ def has_dimensions?
7
+ respond_to?(:width) && respond_to?(:height)
8
+ end
9
+
10
+ def image?
11
+ Redactor3Rails::IMAGE_TYPES.include?(data_content_type)
12
+ end
13
+
14
+ def image
15
+ url
16
+ end
17
+
18
+ def thumb
19
+ url(:thumb)
20
+ end
21
+
22
+ def as_json_methods
23
+ [:image, :thumb]
24
+ end
25
+
26
+ def as_json(options = nil)
27
+ options = {
28
+ :methods => as_json_methods,
29
+ :root => false
30
+ }
31
+
32
+ super options
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'redactor3_rails/orm/base'
2
+
3
+ module Redactor3Rails
4
+ module Orm
5
+ module Mongoid
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
+ store_in :collection => 'redactor3_assets'
16
+
17
+ belongs_to :assetable, :polymorphic => true
18
+
19
+ field :data_file_name, :type => String
20
+ field :data_content_type, :type => String
21
+ field :data_file_size, :type => Integer
22
+
23
+ field :type, :type => String
24
+
25
+ field :width, :type => Integer
26
+ field :height, :type => Integer
27
+
28
+ index(:assetable => 1, :type => 1)
29
+ index(:assetable => 1)
30
+
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Redactor3Rails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'redactor3_rails/version'
2
+ require 'orm_adapter'
3
+
4
+ module Redactor3Rails
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, 'redactor3_rails/http'
10
+ autoload :Devise, 'redactor3_rails/devise'
11
+
12
+ module Backend
13
+ autoload :CarrierWave, 'redactor3_rails/backend/carrierwave'
14
+ end
15
+ require 'redactor3_rails/orm/base'
16
+ require 'redactor3_rails/orm/active_record'
17
+ require 'redactor3_rails/orm/mongoid'
18
+ require 'redactor3_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
+ Redactor3Rails::Image
27
+ end
28
+
29
+ def self.file_model
30
+ Redactor3Rails::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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redactor3_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redactor3_rails"
8
+ spec.version = Redactor3Rails::VERSION
9
+ spec.authors = ["Oleg Sulyanov"]
10
+ spec.email = ["oleg@sulyanov.com"]
11
+
12
+ spec.summary = %q{Imperavi Redactor for Rails 3.x - 4 Asset Pipeline}
13
+ spec.homepage = "https://github.com/marsz/redactor3_rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency("mime-types")
23
+ spec.add_dependency("orm_adapter")
24
+ spec.add_dependency("devise")
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.10"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
@@ -0,0 +1,16 @@
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: '/redactor3_rails/images',
8
+ imageManagerJson: '/redactor3_rails/images',
9
+ imageUploadFields: params,
10
+ fileUpload: '/redactor3_rails/files',
11
+ fileManagerJson: '/redactor3_rails/files',
12
+ fileUploadFields: params
13
+ };
14
+ // Initialize Redactor
15
+ $('.redactor').redactor();
16
+ });
@@ -0,0 +1 @@
1
+ //= require ./config
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redactor3_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Sulyanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mime-types
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: orm_adapter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: devise
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description:
84
+ email:
85
+ - oleg@sulyanov.com
86
+ executables:
87
+ - console
88
+ - setup
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - app/controller/redactor3_rails/files_controller.rb
99
+ - app/controller/redactor3_rails/images_controller.rb
100
+ - bin/console
101
+ - bin/setup
102
+ - config/routes.rb
103
+ - lib/generators/redactor3/config_generator.rb
104
+ - lib/generators/redactor3/install_generator.rb
105
+ - lib/generators/redactor3/templates/active_record/carrierwave/devise_migration.rb
106
+ - lib/generators/redactor3/templates/active_record/carrierwave/migration.rb
107
+ - lib/generators/redactor3/templates/active_record/carrierwave/redactor2/asset.rb
108
+ - lib/generators/redactor3/templates/active_record/carrierwave/redactor2/file.rb
109
+ - lib/generators/redactor3/templates/active_record/carrierwave/redactor2/image.rb
110
+ - lib/generators/redactor3/templates/base/carrierwave/uploaders/redactor3_rails_file_uploader.rb
111
+ - lib/generators/redactor3/templates/base/carrierwave/uploaders/redactor3_rails_image_uploader.rb
112
+ - lib/generators/redactor3/templates/config.js
113
+ - lib/generators/redactor3/templates/mongoid/carrierwave/redactor/asset.rb
114
+ - lib/generators/redactor3/templates/mongoid/carrierwave/redactor/file.rb
115
+ - lib/generators/redactor3/templates/mongoid/carrierwave/redactor/picture.rb
116
+ - lib/redactor3_rails.rb
117
+ - lib/redactor3_rails/backend/carrierwave.rb
118
+ - lib/redactor3_rails/devise.rb
119
+ - lib/redactor3_rails/engine.rb
120
+ - lib/redactor3_rails/http.rb
121
+ - lib/redactor3_rails/orm/active_record.rb
122
+ - lib/redactor3_rails/orm/base.rb
123
+ - lib/redactor3_rails/orm/mongoid.rb
124
+ - lib/redactor3_rails/version.rb
125
+ - redactor3_rails.gemspec
126
+ - vendor/assets/javascripts/redactor3_rails/config.js
127
+ - vendor/assets/javascripts/redactor3_rails/index.js
128
+ homepage: https://github.com/marsz/redactor3_rails
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.7.6
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Imperavi Redactor for Rails 3.x - 4 Asset Pipeline
152
+ test_files: []