Sha256: a43b69faa35f4e0242524a271a4b41b272233bdf99e2f6759e3b581850dbbbee

Contents?: true

Size: 1.7 KB

Versions: 43

Compression:

Stored size: 1.7 KB

Contents

require 'sass'
require 'uglifier'

module RbbtRESTHelpers
  def recorded_js_files
    @recorded_js_files ||= []
  end

  def recorded_css_files
    @recorded_css_files ||= []
  end


  def record_js(file)
    recorded_js_files << file
  end

  def record_css(file)
    recorded_css_files << file
  end

  def link_css(file)
    file += '.css' unless file =~ /.css$/
    file << "?_update=reload" if @debug_css
    html_tag('link', nil, :rel => 'stylesheet', :type => 'text/css', :href => file)
  end

  def link_js(file)
    file += '.js' unless file =~ /.js$/
    html_tag('script', " ", :src => file, :type => 'text/javascript')
  end

  def serve_js(compress = true)
    if production? and compress and not @debug_js 
      md5 = Misc.digest(recorded_js_files * ",")
      filename = File.join(settings.file_dir, "all_js-#{md5}.js")

      if not File.exists?(filename)
        Log.debug{ "Regenerating JS Compressed file: #{ filename }" }

        text = recorded_js_files.collect{|file| 
          begin
            path = locate_javascript(file)
          rescue
            path = locate_javascript(file.split("/")[2..-1] * "/")
          end

          Open.read(path)
        } * "\n"

        FileUtils.mkdir_p File.dirname(filename) unless File.exists? File.dirname(filename)
        Open.write(filename, Uglifier.compile(text))
      end

      res = "<script src='/files/#{File.basename(filename)}' type='text/javascript'></script>"
    else
      res = recorded_js_files.collect{|file|
        link_js(file)
      } * "\n"

    end

    recorded_js_files.clear

    res
  end

  def serve_css
    res = recorded_css_files.collect{|file|
      link_css(file)
    } * "\n"

    recorded_css_files.clear

    res
  end

end
 

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
rbbt-rest-1.3.29 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.28 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.27 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.26 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.25 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.24 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.23 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.22 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.21 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.20 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.19 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.18 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.17 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.16 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.15 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.14 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.13 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.12 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.11 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.3.10 lib/rbbt/rest/common/resources.rb