Administrate: Custom javascript

Created on 7 Nov 2015  路  7Comments  路  Source: thoughtbot/administrate

@graysonwright How do we add javascripts to the tree for custom fields? I put a ckeditor.js file that initializes ckeditor in:

app/assets/javascripts/administrate

However, it's not being required by the master administrate application.js

Any ideas? Cheers!

Most helpful comment

573 adds a few helper methods that you can use in an initializer to add stylesheets and javascripts to Administrate.

Administrate::Engine.add_javascript "my_plugin/script"
Administrate::Engine.add_stylesheet "my_plugin/styles"

For an example, see https://github.com/graysonwright/administrate-field-nested_has_many/blob/master/lib/administrate/field/nested_has_many.rb#L12-L14

All 7 comments

I think I got this working by dropping a application.js in app/assets/javascripts which does the require_tree dance. I then dropped my my extra js files in app/assets/javascripts like any normal rails app.

@caseyhelbling But does your main frontend now require your dashboard scripts?

I think the plugins need a bit of work (#123) so that we can include custom styles and scripts and enable them to be packaged nicely.

My temporary workaround is to do something like this and add the new compiled script to assets.rb

<% content_for :javascript do %>
    <%= javascript_include_tag "administrate/froala" %>
<% end %>

Hey, folks! Sorry for the delayed response.

Yeah, I definitely think this is something we need, and when we settle on a solution we should be able to get it out quickly.

A few options:

  • Automatically include any files in app/assets/javascripts/administrate/. This might be a bit too magical
  • Add a generator that copies over all of Administrate's javascripts to app/assets/javascripts/administrate. This is probably the most flexible for end-user developers, but might be complicated for plugin creators to build for.

Thoughts? Other ideas also welcome.

@rikkipitt - you raise a good concern. I'd like to keep admin-specific js and css separate from the main application's js/css. If an application needs to share styles or scripts across both sides of the app, that will be fairly simple to do with require or @import within the asset files.

Copying assets to app/assets/javascripts/administrate does sound like a good solution.

Another idea: Have an empty file at app/assets/javascripts/administrate/extensions.js that people can overwrite in their app and have it required in app/assets/javascripts/administrate/application.js. Could that work? A possible problem I see with that approach is load order. The files from extensions.js would be loaded last. Does it make sense to offer both options? An extensions.js for the simple approach and a generator if people need more control over the assets?

573 adds a few helper methods that you can use in an initializer to add stylesheets and javascripts to Administrate.

Administrate::Engine.add_javascript "my_plugin/script"
Administrate::Engine.add_stylesheet "my_plugin/styles"

For an example, see https://github.com/graysonwright/administrate-field-nested_has_many/blob/master/lib/administrate/field/nested_has_many.rb#L12-L14

As @graysonwright mentioned, the way to go is to add those lines in an initializer.
So I created a file in /config/initializers called administrate.rb and there I added this line: Administrate::Engine.add_javascript('application')

After that in /assets/javascript/application.js we can add this line //= require_tree . and everything inside /assets/javascript/ will be included 馃槃

Was this page helpful?
0 / 5 - 0 ratings