Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
config.assets.precompile += [
# precompile any CSS or JS file that doesn't start with _
/(^inline[^_\/]|\/[^_])[^\/]*.(js|css)$/,
...
def read_file_contents(stylesheet)
if %w(test development).include?(Rails.env.to_s)
# if we're running the full asset pipeline,
# just grab the body of the final output
stylesheet.body
else
# in a production-like environment, read the
# fingerprinted and compiled file
File.read(File.join(Rails.root, 'public', 'assets', stylesheet.digest_path))
end
end
def inline_file(asset_path)
file = Rails.application.assets.find_asset(asset_path)
file.nil? ? '' : read_file_contents(file)
end
def inline_js(asset_path)
asset_path = asset_path.include?('.js') ? asset_path : "#{asset_path}.js"
raw "<script>#{inline_file asset_path}</script>"
end
def inline_css(asset_path)
asset_path = asset_path.include?('.css') ? asset_path : "#{asset_path}.css"
raw "<style>#{inline_file asset_path}</style>"
end
# use a method like this to automatically load CSS that
# follows the current controller/action name structure
# example: assets/stylesheets/views/users/show.sass
def current_view_stylesheet
inline_css("views/#{params[:controller]}/#{params[:action]}") +
end
@pavelloz

This comment has been minimized.

Copy link
Owner Author

commented Jun 19, 2015

Added simple extension check. For some reason without .css is was taking .js file of the same name.

Added also 'raw' because of html escaping in rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.