Class: ActiveAdminAssets::Middleware
- Inherits:
-
Object
- Object
- ActiveAdminAssets::Middleware
- Defined in:
- lib/activeadmin_assets/middleware.rb
Overview
Catches active admin asset requests and serves them from the gem.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
-
#send_data(path) ⇒ Object
This could be made more efficient with sendfile, perhaps after checking config.action_dispatch.x_sendfile_header.
- #serve(path) ⇒ Object
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
4 5 6 7 |
# File 'lib/activeadmin_assets/middleware.rb', line 4 def initialize(app, *) @app = app @regexp = /\A#{ActiveAdminAssets.path}(.+)/ end |
Instance Method Details
#call(env) ⇒ Object
9 10 11 |
# File 'lib/activeadmin_assets/middleware.rb', line 9 def call(env) serve(env[Rack::PATH_INFO]) || @app.call(env) end |
#send_data(path) ⇒ Object
This could be made more efficient with sendfile, perhaps after checking config.action_dispatch.x_sendfile_header
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/activeadmin_assets/middleware.rb', line 22 def send_data(path) data = File.read(path) headers = { 'cache-control' => 'public, max-age=86400', 'content-encoding' => 'gzip', 'content-length' => data.bytesize.to_s, 'content-type' => path['.css'] ? 'text/css' : 'text/javascript', } [200, headers, [data]] rescue Errno::ENOENT => e Rails.logger.warn("ActiveAdminAssets::Middleware: #{e.class} #{e.}") nil end |
#serve(path) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/activeadmin_assets/middleware.rb', line 13 def serve(path) return unless asset_path = path[@regexp, 1] static_path = File.join(__dir__, 'assets', "#{asset_path}.gz") send_data(static_path) end |