grape-swagger-rails icon indicating copy to clipboard operation
grape-swagger-rails copied to clipboard

how to override default assets?

Open TangMonk opened this issue 10 years ago • 12 comments

I wanna to custom style for swagger,

I copy application.css from grape-swagger-rails and place into myapp/app/assets/stylesheets/grape_swagger_rails.

/*
*= require reset
*= require screen
*= require_self
*/

ul {
  margin-left: 10px;
}

but it throw error:

couldn't find file 'reset'

TangMonk avatar Aug 27 '15 03:08 TangMonk

I am not familiar with where Rails looks for these files, but I suspect that this require is relative, so it works when with other grape-swagger-rails assets. Maybe you need to tell Rails to add a CSS path from the gem? If you find out it would be great to document how to properly customize this.

Also, maybe you don't need to copy all this. Just add a stylesheet to your app and make sure it loads with the grape-swagger UI and override whatever styles you need?

dblock avatar Aug 27 '15 15:08 dblock

I put a custom CSS file into app/assets/stylesheets/grape_swagger_rails, and also have a copy of grape-swagger-rails's index.html.erb in app/views/grape_swagger_rails/application. In my own index.html.erb, I add a css link tag to include the custom CSS file.

/app/assets/stylesheets/grape_swagger_rails/custom.css.scss

ul {
  margin-left: 10px;
}

/app/views/grape_swagger_rails/index.html.erb

...
<head>
  <title>...</title>
  <%= stylesheet_link_tag 'grape_swagger_rails/application.css' %>
  <%= stylesheet_link_tag 'grape_swagger_rails/custom.css' %>
  ...
</head>
...

/config/initializers/assets.rb

Rails.application.config.assets.precompile += %w(
  grape_swagger_rails/custom.css
  # other assets...
)

calfzhou avatar Sep 25 '15 14:09 calfzhou

@calfzhou, is your solution working ? I can't make it work for me.

abarre avatar Sep 26 '15 13:09 abarre

@abarre, yes, it works well for me. By the way, I'm using master branch, latest commit.

calfzhou avatar Sep 28 '15 03:09 calfzhou

The idea of being able to customize the layout that the UI renders inside of seems like it would be a nice addition and within the scope of the gem. Maybe using some content_for statements within the index.html.erb in this project would allow dropping in a layout without without breaking any compatibility.

I'll leave this issue open as a reminder. If anybody would like to attempt a solution, feel free to take a whack at it.

aschuster3 avatar May 21 '16 19:05 aschuster3

@calfzhou Thanks so much for this solution, however this is a small mistake which might trip some people up. For the location of the index.html.erb the file path should be

/app/views/grape_swagger_rails/application/index.html.erb

SimonDKnight avatar May 24 '16 23:05 SimonDKnight

Hey everyone, I just pushed a new test branch that splits out layout components so that you simply need to have a different layout file that contains a yield :head and yield statement, meaning that you're left with an essentially clean slate to impose your own style.

To change the default layout, simply overwrite the GrapeSwaggerRails.options.swagger_ui_layout with the location of your custom layout.

If you want to test out the gem locally, you can replace

gem 'grape-swagger-rails'

with

gem 'grape-swagger-rails', github: 'ruby-grape/grape-swagger-rails', branch: 'change-layout-test-branch'

and then run bundle update grape-swagger-rails.

Currently the features are undocumented and have no tests. If anybody has any recommendations or preferences, please share them here and I'll take them into consideration as I work on it. If you run into compatibility issues, please let me know.

aschuster3 avatar May 27 '16 15:05 aschuster3

This is awesome - thanks! :+1: Only issue I have is being able to access url helpers and things I've set up in my ApplicationController (like current_user) etc, but I can probably work round that for now. Any tips would be appreciated if there's anything obvious I can do though.

pezholio avatar Aug 05 '16 08:08 pezholio

@TangMonk

It should be:

/*
*= require grape_swagger_rails/reset
*= require grape_swagger_rails/screen
*= require_self
*/

not

/*
*= require reset
*= require screen
*= require_self
*/

hubertjakubiak avatar May 16 '19 09:05 hubertjakubiak

We wanted to keep most of what was there, so we forked the original repo and just added an option for pulling in our own stylesheet.

https://github.com/assignr/grape-swagger-rails

jwigal avatar Dec 09 '20 22:12 jwigal

Why don't you PR this upstream @jwigal ?

dblock avatar Dec 10 '20 00:12 dblock

Sure thing! See #103

jwigal avatar Dec 10 '20 12:12 jwigal