Class: BookmarkletRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/bookmarklet_renderer.rb

Class Method Summary collapse

Class Method Details

.bodyObject


22
23
24
25
26
27
28
29
# File 'lib/bookmarklet_renderer.rb', line 22

def body
  unless File.exist?(cached_path) || Rails.application.config.assets.compile
    raise "Please run the rake task to compile the bookmarklet: `bin/rake assets:precompile`"
  end

  compile if Rails.application.config.assets.compile
  @body ||= File.read(cached_path)
end

.cached_nameObject


6
7
8
9
10
11
12
# File 'lib/bookmarklet_renderer.rb', line 6

def cached_name
  @cached_name ||= if Rails.application.config.assets.compile
                     "bookmarklet.js"
                   else
                     Rails.application.assets_manifest.assets["bookmarklet.js"]
                   end
end

.cached_pathObject


14
15
16
# File 'lib/bookmarklet_renderer.rb', line 14

def cached_path
  @cached_path ||= Rails.root.join("public", "assets", cached_name)
end

.compileObject


31
32
33
34
35
36
# File 'lib/bookmarklet_renderer.rb', line 31

def compile
  src = File.read(source)
  @body = Uglifier.compile(src)
  FileUtils.mkdir_p cached_path.dirname
  File.open(cached_path, "w") {|f| f.write(@body) }
end

.sourceObject


18
19
20
# File 'lib/bookmarklet_renderer.rb', line 18

def source
  @source ||= Rails.application.assets["bookmarklet.js"].pathname.to_s
end