Skip to content

Instantly share code, notes, and snippets.

@averyvery
Last active May 27, 2024 07:35
Show Gist options
  • Save averyvery/6e4576023b395de1aaf5 to your computer and use it in GitHub Desktop.
Save averyvery/6e4576023b395de1aaf5 to your computer and use it in GitHub Desktop.

Revisions

  1. averyvery revised this gist Jan 2, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions application.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    config.assets.precompile += [

    # css
    /(^views[^_\/]|\/[^_])[^\/]*.css$/,
    # precompile any CSS or JS file that doesn't start with _
    /(^inline[^_\/]|\/[^_])[^\/]*.(js|css)$/,
    ...
  2. averyvery revised this gist Jan 2, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion application.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    /(^(desktop|mobile|all)[^_\/]|\/[^_])[^\/]*.css$/,
    config.assets.precompile += [

    # css
  3. averyvery revised this gist Jan 2, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions application.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    /(^(desktop|mobile|all)[^_\/]|\/[^_])[^\/]*.css$/,
    config.assets.precompile += [

    # css
    /(^views[^_\/]|\/[^_])[^\/]*.css$/,
    ...
  4. averyvery revised this gist Jan 2, 2015. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion inline-js-or-css.rb
    Original file line number Diff line number Diff line change
    @@ -21,4 +21,11 @@ def inline_js(asset_path)

    def inline_css(asset_path)
    "<style>#{inline_file asset_path}</style>"
    end
    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
  5. averyvery revised this gist Jan 2, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions inline-js-or-css.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    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
  6. averyvery created this gist Jan 2, 2015.
    20 changes: 20 additions & 0 deletions inline-js-or-css.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    def read_file_contents(stylesheet)
    if %w(test development).include?(Rails.env.to_s)
    stylesheet.body
    else
    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)
    "<script>#{inline_file asset_path}</script>"
    end

    def inline_css(asset_path)
    "<style>#{inline_file asset_path}</style>"
    end