tsechingho / ckeditor-rails

Integrate CKEditor javascript library with Rails asset pipeline
https://github.com/tsechingho/ckeditor-rails
MIT License
213 stars 132 forks source link

Rails 4 support #18

Closed bricker closed 11 years ago

bricker commented 11 years ago

I can do it, I just want to figure out how it needs to be done. The main problem is that with the Rails 4 asset pipeline, compiled assets must be referenced by their digested filename - so no longer can we access the CKEditor javascript/css files directly (which it does a lot), unless you turn on config.assets.compile, which I don't think people will be willing to do just for this library.

I propose a new object inside of the CKEDITOR object, assetPaths, which would be a manifest mapping any file that ckeditor.js is referencing directly to the corresponding digested asset:

CKEDITOR.assetPaths = {
  "config.js": "<%= asset_path('ckeditor/config.js') %>"
  "dialogs/templates.js": "<%= asset_path('ckeditor/dialogs/templates.js') %>"
  # ... etc
}

And then gsub the ckeditor.js file to replace any instance of, say, config.js, with CKEDITOR.assetPaths['config.js'] when pulling a new version.

I'm not 100% sold on this solution but it's the best I can come up with on a whim. The biggest problem is the gsubbing and suddenly having to keep a list of all the file references in ckeditor.js - something which could possibly change frequently. Can anybody think of something better? Maybe we could turn ckeditor.js into ckeditor.js.erb, and wrap every file reference in asset_path?

bricker commented 11 years ago

For anyone else trying to figure this out, I've finally decided that there is just no way to get this gem to work as-is with the Rails 4 Asset Pipeline, and any updates or workarounds were more complex than just using the static CKEditor source.

To get it to work: drop the CKEditor source into public/assets/ckeditor (this gem uses the "Full" version). Then copy the jquery plugin (link) and place it in public/assets/ckeditor/. You should then update public/assets/ckeditor/config.js as necessary.

In your application.html.erb:

  <%= javascript_include_tag "application" %>

  <script>
    // You could also put these at the end of your application.js file
    var CKEDITOR_BASEPATH = '/assets/ckeditor/';
    var APPLICATION_CSS   = '<%= asset_path("application.css") %>';
  </script>

  <script src="/assets/ckeditor/ckeditor.js" type="text/javascript"></script>
  <script src="/assets/ckeditor/jquery.js" type="text/javascript"></script>

The APPLICATION_CSS variable is for use in config.js, because I need the body of the editor to use the application.css file:

CKEDITOR.editorConfig = function(config) {
  // ...
  config.contentsCss = APPLICATION_CSS;
  // ...  
return true;
};

Drop any plugins right into public/assets/ckeditor/plugins.

Of course, the major downside to this is that none of the files are in the asset pipeline - sprockets-rails is just accepting a request for these files, not finding them in the manifest, and passing the request through directly to the public folder. Because of this, you'll need to make sure config.serve_static_assets is turned on (or you're using nginx/Apache), and you might need to manually bust the asset cache for the ckeditor.js file if you change something in the config (just add a query param to the end of the javascript URL - .../ckeditor.js?bust=1).

eric1234 commented 11 years ago

My current solution is to use a rake task to copy the digest versions into non-digest versions (restoring how it worked in Rails 3).

I'm not sure the long term solution. I think the need to integrate with other systems means accessing ONLY through digest names is not practical. Rails should be able to generate non-digest version (like my rake task). But I can see that not all projects need the non-digest version. So maybe the task isn't called by default but maybe Rails does make it available?

elrolito commented 11 years ago

Using the app/assets/javascripts/basepath.js.erb (this should be updated in the README, as the file will give an error about var being a keyword if you use a .coffee extension), anywho...

I am using rails-4.0.0.rc2 and moved any plugins I have to vendor/javascripts/ckeditor/plugins/ and used the ckeditor-jquery require in my application.js and everything seems to be working now.

tsechingho commented 11 years ago

Since Rails 4 would be released on 6/25, I will try to give a solution for this issue ASAP.

elrolito commented 11 years ago

@tsechingho what I meant to say is: in development and test environments, the gem works as is with 4.0.0.rc2 as long as you also add the basepath.js.erb file with CKEDITOR_BASEPATH set.

tsechingho commented 11 years ago

@elrolito Yes, for now, this gem should work in development and test environment in rails4.

I am saying the solution for non-digest versions of assets in production mode.

BTW, I am not clear the keyword limitation, like var, in coffeescript. I don't like implicit naming.

eric1234 commented 11 years ago

I think I know a solution to this. I found out today that if you define two rake tasks with the same name that the Rake system appends them. So:

task :the_task do
  p "one"
end

task :the_task do
  p "two"
end

%> rake the_task
one
two

This means we can append our own behavior to the assets:precompile rake task. So using my rake task referenced above as a model, we could include a task in ckeditor which appends assets:precompile so fingerprinted assets are copied into non-fingerprinted.

My task copies all assets (duplicating the behavior of Rails 3). But if you didn't want to be that intrusive we could just copy the assets under the "ckeditor" directory.

This would make everything transparently work again without having to come up with something convoluted. If this sounds like a reasonable approach I will submit a patch.

lonny commented 11 years ago

Pardon me, but I don't completely follow. Does this mean that when released it will drop-in work with Rails 4?

And if so, when is the planned release?

eric1234 commented 11 years ago

@lonny - Yes. It should work in Rails 4 out-of-the-box. As to release schedule, I don't know but you can always point to the git repo in your bundle.