Sha256: dbcb156a79e7ca0569ba0a787815c6c6ba204c16ef50821559d7366f085bebeb

Contents?: true

Size: 1.42 KB

Versions: 11

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module RailsCriticalCss::Actions
  extend ActiveSupport::Concern

  included do
    include RailsCriticalCss::Helpers

    attr_accessor :lazy_css_blocks,
                  :critical_css_assets,
                  :critical_css_cache,
                  :critical_css_enabled

    helper_method :append_critical_css_asset,
                  :append_css_tags_assets
  end

  def extract_assets_from_css_tags(str)
    return [] unless str.present?

    str
      .scan(/assets\/([^?"]*)-[^?-]+.css/)
      .flatten
      .map { |i| i.sub('.self', '') }
  end

  def append_critical_css_asset(file:, critical: false)
    (@critical_css_assets ||= []) << {
      file: file,
      critical: critical,
    }
  end

  def append_css_tags_assets(str)
    extract_assets_from_css_tags(str).each do |asset|
      append_critical_css_asset file: asset
    end
  end

  def full_html_response
    response.body
  end

  class_methods do
    def action_critical_css(*actions)
      options = actions.extract_options!
      return unless cache_configured?

      options[:unless] ||= -> { request.query_string.present? }
      options[:cache_prefix] ||= 'critical-css'

      filter_options = options.extract!(:if, :unless).merge(only: actions)
      around_action RailsCriticalCss::Actions::BeforeRender.new(options), filter_options
      after_action RailsCriticalCss::Actions::AfterRender.new(options), filter_options
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rails_critical_css-0.5.1 lib/rails_critical_css/actions.rb
rails_critical_css-0.5.0 lib/rails_critical_css/actions.rb
rails_critical_css-0.4.1 lib/rails_critical_css/actions.rb
rails_critical_css-0.4.0 lib/rails_critical_css/actions.rb
rails_critical_css-0.3.9 lib/rails_critical_css/actions.rb
rails_critical_css-0.3.8 lib/rails_critical_css/actions.rb
rails_critical_css-0.3.7 lib/rails_critical_css/actions.rb
rails_critical_css-0.3.6 lib/rails_critical_css/actions.rb
rails_critical_css-0.3.5 lib/rails_critical_css/actions.rb
rails_critical_css-0.3.4 lib/rails_critical_css/actions.rb
rails_critical_css-0.3.3 lib/rails_critical_css/actions.rb