Module: PgHeroAssets

Defined in:
lib/pghero_assets/prepare_assets.rb,
lib/pghero_assets/railtie.rb,
lib/pghero_assets/version.rb,
lib/pghero_assets/asset_root.rb,
lib/pghero_assets/middleware.rb,
lib/pghero_assets/disable_api_check.rb

Overview

prepares assets for PgHero lazily / on demand based on discussion in github.com/ankane/pghero/pull/421

Defined Under Namespace

Classes: Middleware, Railtie

Constant Summary collapse

VERSION =
'1.0.0'
PREPARE_ASSETS_MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.asset_rootObject



2
3
4
# File 'lib/pghero_assets/asset_root.rb', line 2

def self.asset_root
  Rails.root.join("tmp/pghero_assets/#{PgHero::VERSION}/#{PgHeroAssets::VERSION}")
end

.disable_pghero_api_checkObject



2
3
4
5
6
7
# File 'lib/pghero_assets/disable_api_check.rb', line 2

def self.disable_pghero_api_check
  require 'pghero'
  require PgHero::Engine.root.join('app/helpers/pg_hero/home_helper')
  require PgHero::Engine.root.join('app/controllers/pg_hero/home_controller')
  PgHero::HomeController.define_method(:check_api) {}
end

.prepare_assetsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pghero_assets/prepare_assets.rb', line 8

def self.prepare_assets
  root_dir = PgHeroAssets.asset_root
  PREPARE_ASSETS_MUTEX.synchronize do
    root_dir.exist? ? return : FileUtils.mkdir_p(root_dir)
  end

  # Create application.js file for PgHero
  asset_src = PgHero::Engine.root.join('app/assets')
  js_file = root_dir.join('javascripts/pghero/application.js')
  js_file.dirname.mkpath
  files = Dir[asset_src.join('javascripts/**/*.js')].sort_by { _1.index(/application\.\w+\z/) || 0 }
  File.open(js_file, 'w') { |main| files.each { |f| main << File.read(f) << "\n\n" } }

  # Create application.css file for PgHero
  css_file = root_dir.join('stylesheets/pghero/application.css')
  css_file.dirname.mkpath
  files = Dir[asset_src.join('stylesheets/**/*.css')].sort_by { _1.index(/application\.\w+\z/) || 0 }
  File.open(css_file, 'w') { |main| files.each { |f| main << File.read(f) << "\n\n" } }

  # Symlink to PgHero's images
  FileUtils.ln_sf asset_src.join('images'), root_dir.join('images')
end