Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bookmarklet_renderer.rb « lib - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75b2b51f1f69d1c9c5f6f4a38dd2bf047e2e3fc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# frozen_string_literal: true

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

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

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

    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

    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
  end
end