Sha256: 04a13b550b31e23f96ed2753d63d9f1de787408144f919e6a55ba9314a9aea41

Contents?: true

Size: 1.69 KB

Versions: 49

Compression:

Stored size: 1.69 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 = settings.file_dir["all_js-#{md5}.js"].find

      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

49 entries across 49 versions & 1 rubygems

Version Path
rbbt-rest-1.7.2 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.7.1 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.7.0 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.28 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.26 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.25 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.24 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.23 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.22 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.21 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.20 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.19 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.18 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.17 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.16 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.15 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.14 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.13 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.12 lib/rbbt/rest/common/resources.rb
rbbt-rest-1.6.11 lib/rbbt/rest/common/resources.rb