kaigth
kaigth

Reputation: 158

Rails 3 use CKEditor with already existing paperclip model

I have a web application in rails 3 that already has a polymorphic paperclip model. I have attached several models to it already.

I soon realized I need a good wysiwyg editor for a text area, I installed the ckeditor gem with paperclip but it generates it's own models for ckeditor::pictures and asset.

is there a way to override what model the gem is looking for in ckeditor so I can use my existing polymorphic paperclip model with ckeditor?

Upvotes: 0

Views: 621

Answers (4)

pzupan
pzupan

Reputation: 327

If you change the picture model and attachment file model to inherit from your base model:

class Ckeditor::Picture < Asset

end

And include Ckeditor in your base class:

class Asset < ApplicationRecord
  include Ckeditor::Orm::ActiveRecord::AssetBase
  include Ckeditor::Backend::Paperclip

  belongs_to :attachable, :polymorphic => true

  has_attached_file :data,
    storage: :s3,
    s3_region: S3_CONFIG["region"],
    path: "assets/:attachment/:id/:style.:extension",
    :styles => lambda { |a| a.instance.styles },
    s3_credentials: {
         access_key_id: S3_CONFIG["access_key_id"] ,
         secret_access_key: S3_CONFIG["secret_access_key"]
       },
    s3_protocol: 'http',
    bucket: S3_CONFIG["bucket"]
end

And change the table name in your initializer (ckeditor.rb):

module Ckeditor
  module Orm
    module ActiveRecord
      module AssetBase
        def self.included(base)
          base.send(:include, Base::AssetBase::InstanceMethods)
          base.send(:extend, ClassMethods)
        end
        module ClassMethods
          def self.extended(base)
            base.class_eval do
              self.table_name = 'assets'
            end
          end
        end
      end
    end
  end
end

It seems to work.

Upvotes: 0

Chris Hanson
Chris Hanson

Reputation: 2083

I know this question is a bit old, but there is now a way to set the model. In config/initializers/ckeditor.rb there are config.picture_model and config.attachment_file_model settings that you can use to set your models.

Upvotes: 0

kaigth
kaigth

Reputation: 158

I found that you cannot tell the gem ckeditor to use your existing paperclip model, that you must use the paperclip models it generates or not use the editor. You would then have to fight the gem by forking it and making edits to use your current and existing paperclip model.

I would personally recommend not using ckeditor gem if you already have an existing attachments model through paperclip or carrierwave. Simply obtain the ckeditor javascript files from their website and implement the editor that way.

You can do this by downloading the javascript files, place them in your assets/javascript directory. Then include them in your application.js

Upvotes: 2

Agis
Agis

Reputation: 33636

Use this generator provided by the gem itself:

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

Upvotes: 0

Related Questions