Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom javascript #187

Closed
rikkipitt opened this issue Nov 7, 2015 · 7 comments
Closed

Custom javascript #187

rikkipitt opened this issue Nov 7, 2015 · 7 comments

Comments

@rikkipitt
Copy link
Contributor

@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!

@caseyhelbling
Copy link

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.

@rikkipitt
Copy link
Contributor Author

@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 %>

@c-lliope
Copy link
Contributor

c-lliope commented Nov 8, 2015

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.

@c-lliope
Copy link
Contributor

c-lliope commented Nov 8, 2015

@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.

@5minpause
Copy link
Contributor

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?

@c-lliope
Copy link
Contributor

#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

@dani24gl
Copy link

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants