spree-contrib / spree_editor

Rich text editor for Spree with Image and File uploading in-place.
http://guides.spreecommerce.org
BSD 3-Clause "New" or "Revised" License
112 stars 183 forks source link

Does not support activestorage #139

Open laoyouzi opened 6 years ago

Kulgar commented 5 years ago

Spree editor relies on ckeditor gem, they added activestorage support only since last month, and it isn't within a release yet...

So I'd suggest to fork the spree_editor gem and update ckeditor dependency to

s.add_dependency 'ckeditor', '~> 5.0.0'

And then, in your Gemfile, use the version of ckeditor that adds Active Storage support: gem 'ckeditor', github: 'galetahub/ckeditor', branch: 'master', ref: '4385f23b90f84c25f7b912bd75c233c8f27b5f58'

After bundle install, run the generator:

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

You will also have to update the following things in your own spree_editor version or in your spree app:

In config/initializers/ckeditor.rb, you have to add these lines:

  config.authorize_with :cancancan, Spree::Ability

  # Asset model classes
  config.picture_model { Ckeditor::Picture }
  config.attachment_file_model { Ckeditor::AttachmentFile }

    # //cdn.ckeditor.com/<version.number>/<distribution>/ckeditor.js | distributions available here: https://ckeditor.com/cke4/presets-all
    config.cdn_url = "//cdn.ckeditor.com/4.11.3/standard/ckeditor.js"

In app/views/shared/editor_engine/_ck_editor.html.erb replace:

<%= javascript_include_tag Ckeditor.cdn_url %>

<script>
  $(function() {
    <% ids.each do |id| %>
      if ($("#<%= id %>").length > 0) {
        CKEDITOR.replace("<%= id %>");
      }
    <% end %>
  });
</script>

By:

<%= javascript_include_tag Ckeditor.cdn_url %>
<%= javascript_include_tag 'ckeditor/config.js' %>

  <script>
    $(function() {
      <% ids.each do |id| %>
        if ($("#<%= id %>").length > 0) {
          CKEDITOR.replace("<%= id %>", editorConfig);
        }
      <% end %>
    });
  </script>

And then in ckeditor/config.js:

editorConfig = {
  filebrowserBrowseUrl: "/ckeditor/attachment_files",
  filebrowserFlashBrowseUrl: "/ckeditor/attachment_files",
  filebrowserFlashUploadUrl: "/ckeditor/attachment_files",
  ...
};

They removed the init.js.erb file... so that solution works if you use the CDN. And it will allow you to easily update ckeditor through your initializer file.

And that's should be it.

We can't do a pull request until ckeditor has released a new official version supporting active storage. But as soon as ckeditor gem will have a new official release supporting active support, creating a PR would be a good thing to do for this gem ^^