• Stars
    star
    2,223
  • Rank 19,884 (Top 0.5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ckeditor 4.x integration gem for rails

Ckeditor

Build Status Code Climate

CKEditor is a WYSIWYG text editor designed to simplify web content creation. It brings common word processing features directly to your web pages. Enhance your website experience with our community maintained editor. Currently this gem supports ckeditor 4 only.

Features

  • CKEditor version 4.x (https://ckeditor.com/ckeditor-4/)
  • Rails 5.x, 4.2.x integration
  • Files browser
  • HTML5 file uploader
  • Hooks for formtastic and simple_form forms generators
  • Integrated with authorization frameworks CanCanCan and Pundit

Installation

For basic usage just include the ckeditor gem:

gem 'ckeditor'

or if you'd like to use the latest version from Github:

gem 'ckeditor', github: 'galetahub/ckeditor'

Include this inside your config/initializers/assets.rb:

Rails.application.config.assets.precompile += %w[ckeditor/config.js]

For file upload support, you must generate the necessary file storage models. The currently supported backends are:

  • ActiveRecord (active_storage, paperclip, carrierwave, dragonfly)
  • Mongoid (paperclip, carrierwave, dragonfly)

How to generate models to store uploaded files

ActiveRecord + paperclip

To use the active_record orm with paperclip (i.e. the default settings):

gem 'paperclip'

rails generate ckeditor:install --orm=active_record --backend=paperclip

ActiveRecord + active_storage

gem "mini_magick"

rails active_storage:install # if you haven't installed active_storage yet
rails generate ckeditor:install --orm=active_record --backend=active_storage

ActiveRecord + carrierwave

gem 'carrierwave'
gem 'mini_magick'

rails generate ckeditor:install --orm=active_record --backend=carrierwave

ActiveRecord + dragonfly

Requires Dragonfly 1.0 or higher.

gem 'dragonfly'

rails generate ckeditor:install --orm=active_record --backend=dragonfly

Mongoid + paperclip

gem 'mongoid-paperclip', require: 'mongoid_paperclip'

rails generate ckeditor:install --orm=mongoid --backend=paperclip

Mongoid + active_storage

Active Storage support only Active Record, see for more info

Mongoid + carrierwave

gem 'carrierwave-mongoid', require: 'carrierwave/mongoid'
gem 'mini_magick'

rails generate ckeditor:install --orm=mongoid --backend=carrierwave

Load generated models

All ckeditor models will be generated in the app/models/ckeditor directory. Models are autoloaded in Rails 4. For earlier Rails versions, you need to add them to the autoload path (in application.rb):

config.autoload_paths += %w(#{config.root}/app/models/ckeditor)

Mount the Ckeditor::Engine to your routes (config/routes.rb):

mount Ckeditor::Engine => '/ckeditor'

Usage

Load editor via CKEditor CDN

Setup editor version to load (more info here http://cdn.ckeditor.com/)

# in config/initializers/ckeditor.rb

Ckeditor.setup do |config|
  # //cdn.ckeditor.com/<version.number>/<distribution>/ckeditor.js
  config.cdn_url = "//cdn.ckeditor.com/4.6.1/basic/ckeditor.js"
end

In view template include ckeditor CDN:

= javascript_include_tag Ckeditor.cdn_url

Precompile ckeditor/config.js:

# in config/initializers/assets.rb

Rails.application.config.assets.precompile += %w[ckeditor/config.js]

Form helpers

= form_for @page do |form|
  = form.cktext_area :notes, class: 'someclass', ckeditor: { language: 'uk'}
  = form.cktext_area :content, value: 'Default value', id: 'sometext'
  = cktext_area :page, :info, cols: 40, ckeditor: { uiColor: '#AADC6E', toolbar: 'mini' }

It also works with bootstrap-form

= bootstrap_form_for resource do |form|
  = form.cktext_area :text, ckeditor: { language: 'uk'}

Customize ckeditor

All ckeditor options can be found here

In order to configure the ckeditor default options, create the following files:

app/assets/javascripts/ckeditor/config.js

app/assets/javascripts/ckeditor/contents.css

Custom toolbars example

Adding a custom toolbar:

# in app/assets/javascripts/ckeditor/config.js

CKEDITOR.editorConfig = function (config) {
  // ... other configuration ...

  config.toolbar_mini = [
    ["Bold",  "Italic",  "Underline",  "Strike",  "-",  "Subscript",  "Superscript"],
  ];
  config.toolbar = "mini";

  // ... rest of the original config.js  ...
}

When overriding the default config.js file, you must set all configuration options yourself as the bundled config.js will not be loaded. To see the default configuration, run bundle open ckeditor, copy app/assets/javascripts/ckeditor/config.js into your project and customize it to your needs.

Install additional plugins

You should download necessary plugins with all dependencies and extract them in app/assets/javascripts/ckeditor/plugins/. After that you can include your plugins in app/assets/javascripts/ckeditor/config.js in this way:

CKEDITOR.editorConfig = function (config) {
  config.extraPlugins = 'eqneditor,autosave,';
}

AJAX

jQuery sample:

<script type='text/javascript' charset='UTF-8'>
  $(document).ready(function(){
    $('form[data-remote]').bind('ajax:before', function(){
      for (instance in CKEDITOR.instances){
        CKEDITOR.instances[instance].updateElement();
      }
    });
  });
</script>

Formtastic integration

= form.input :content, as: :ckeditor
= form.input :content, as: :ckeditor, input_html: { ckeditor: { height: 400 } }

SimpleForm integration

Note that the toolbar option should match the case specified in the config. If the config is not found it defaults to all available toolbar items.

i.e. config.toolbar_mini becomes {toolbar: 'mini'} in the form.

= form.input :content, as: :ckeditor, input_html: { ckeditor: { toolbar: 'Full' } }

Turbolink integration

Create a file app/assets/javascripts/init_ckeditor.coffee

ready = ->
  $('.ckeditor').each ->
    CKEDITOR.replace $(this).attr('id')

$(document).ready(ready)
$(document).on('page:load', ready)

Make sure the file is loaded from your app/assets/javascripts/application.js

CanCanCan integration

To use cancan with Ckeditor, add this to an initializer:

# in config/initializers/ckeditor.rb

Ckeditor.setup do |config|
  config.authorize_with :cancancan
end

At this point, all authorization will fail and no one will be able to access the filebrowser pages. To grant access, add the following abilities (usually ability.rb)

# Always performed
can :access, :ckeditor   # needed to access Ckeditor filebrowser

# Performed checks for actions:
can [:read, :create, :destroy], Ckeditor::Picture
can [:read, :create, :destroy], Ckeditor::AttachmentFile

Pundit or Action Policy Integration

Setup Pundit and Action Policy similar. Just like CanCanCan, you can write this code in your config/initializers/ckeditor.rb file:

Ckeditor.setup do |config|
  config.authorize_with :pundit # or :action_policy
end

Then, generate the policy files for model Picture and AttachmentFile

rails g ckeditor:pundit_policy # or ckeditor:action_policy

By this command, you will got two files:

app/policies/ckeditor/picture_policy.rb app/policies/ckeditor/attachment_file_policy.rb

By default, only the user that logged in can access the models.

You can customize these two policy files as you like.

Engine configuration

  • To override the default CKEditor routes create a config.js file within the host application at app/assets/javascripts/ckeditor/config.js

  • By default, the engine inherits from ApplicationController. To override the default parent controller:

# in config/initializers/ckeditor.rb

Ckeditor.setup do |config|
  config.parent_controller = 'MyCKEditorBaseController'
end

Based on this, if you want to secure CKEditor controller actions and you can't authenticate in ApplicationController, you could do so with a custom controller after configuring the override above, like so:

class MyCKEditorBaseController < ActionController::Base

  before_action :authenticate_user! # or some other auth/permission logic here, like Pundit

end

I18n

en:
  ckeditor:
    page_title: 'CKEditor Files Manager'
    confirm_delete: 'Delete file?'
    buttons:
      cancel: 'Cancel'
      upload: 'Upload'
      delete: 'Delete'
      next: 'Next'

Tests

$> rake test CKEDITOR_BACKEND=paperclip
$> rake test CKEDITOR_BACKEND=active_storage
$> rake test CKEDITOR_BACKEND=carrierwave
$> rake test CKEDITOR_BACKEND=dragonfly
$> rake test CKEDITOR_BACKEND=shrine
$> rake test CKEDITOR_ORM=mongoid

$> rake test:controllers
$> rake test:generators
$> rake test:integration
$> rake test:models

This project rocks and uses the MIT-LICENSE.

More Repositories

1

rails-ckeditor

Rails plugin for integration ckeditor 3.x
JavaScript
310
star
2

web_video

WebVideo allows you to inspect and process video files (ffmpeg, mencoder, flvtool2).
Ruby
25
star
3

loginza

OpenID 2.0 provider
Ruby
19
star
4

rails-ckeditor-demo-app

Demo app for rails-ckeditor plugin usage
Ruby
12
star
5

vkontakte

The easiest way to access Vkontakte API and some other utils.
Ruby
10
star
6

sunrise-core

SunRize CMS Core
Ruby
10
star
7

ruby2xlsx

Export your data to Excel. Provide two ways for export data: template or renderer.
Ruby
8
star
8

poker_history

Poker hands history parser
Ruby
7
star
9

sunrise

Mini and free CMS
Ruby
7
star
10

rails-marker

Easy way to edit zoom, longitude and latitude
Ruby
6
star
11

sunrise-file-upload

Upload files
Ruby
5
star
12

redress

Build maintainable Ruby apps
Ruby
5
star
13

track_tweets

Tracking tweets count, users count
Ruby
5
star
14

vkontakte_oauth

Vkontakte Open API (ะ’ะšะพะฝั‚ะฐะบั‚ะต)
Ruby
5
star
15

cancan_namespace

Add namespace for cancan authorization library
Ruby
5
star
16

social_profile

Wrapper for Omniauth profile hash
Ruby
4
star
17

falcon

Encoding video files
Ruby
4
star
18

ruby-lessons

Ruby Lessons
JavaScript
4
star
19

mapia_api

Tool for mapia api
3
star
20

share_checker

Get count of shared URL's in social sites (twitter, facebook, vkontakte and odnoklassniki)
Ruby
3
star
21

yml_translator

Translate your I18n files locales in one click
Ruby
2
star
22

mini_rocket

Simple CMS
CSS
2
star
23

sunrise-votes

Sunrise votes, ratings and charts
JavaScript
2
star
24

simple-god

Simple monitoring via God
Ruby
2
star
25

freeberry

Aimbulance project template (CMS)
Ruby
2
star
26

ballot_box

Votes, ratings via rack
Ruby
2
star
27

jquery-plugins

My jQuery plugins
JavaScript
2
star
28

sunrise-feedbacks

Feedbacks for sunrise CMS
Ruby
1
star
29

sunrise-deploy

Simple capistrano recipes
Ruby
1
star
30

benchmark-suite

Benchmark suite with bundle for everysing
Ruby
1
star
31

attach_file_field

Easy upload files via Swfupload
JavaScript
1
star
32

sunrise-widgets

Widgets for sunrice CMS
JavaScript
1
star
33

rfid_api

Client for RFID API
Ruby
1
star
34

feedburner

Ruby wrapper for Google FeedBurner API
Ruby
1
star
35

sunrise-questions

Sunrise CMS Questions
JavaScript
1
star
36

differ

Ruby application that can compare the content of two files
Ruby
1
star
37

page_parts

Easy page contents
Ruby
1
star
38

sunrise-scaffold

SunRize CMS Scaffold
Ruby
1
star
39

sunrise-posts

SunRize CMS Posts
Ruby
1
star
40

sunrise-comments

Sunrise CMS Comments
Ruby
1
star
41

catch_exception

Catch Exception
Ruby
1
star
42

freeberry_auth

Social authorization system via Loginza or RPX
Ruby
1
star
43

track_tweets_client

Client for Track Tweets API
Ruby
1
star