Class: Ext::JsHelpers::Controllers::JoinJavascripts

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

Overview

Returns a big script with all the requested scripts. -and- is the separator.

It will look in it’s JoinJavascripts.lookup_paths accessor for paths where the scripts exist. [‘.’] is the default.

If a script is not found, it will throw a javascript error.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.lookup_pathsObject

Returns the value of attribute lookup_paths.



61
62
63
# File 'lib/ext/js_helpers.rb', line 61

def lookup_paths
  @lookup_paths
end

Class Method Details

.urlsObject



62
# File 'lib/ext/js_helpers.rb', line 62

def urls; ['/join_js/([^\/\.]*).js'] end

Instance Method Details

#get(scripts) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ext/js_helpers.rb', line 67

def get(scripts)
  @headers['Content-Type'] = 'text/javascript'
  scripts = scripts.split('-and-').map{|s|s+'.js'}

  return "throw 'no files passed to JoinJavascript controller'" if scripts.empty?

  data = ""
  scripts.each do |script|
    message = "#{script} not found"
    self.class.lookup_paths.each do |path|
      path = File.join(path, script)
      if File.exists?(path)
        begin
          data << File.read(path)
          message = nil
          break
        rescue => ex
          message = ex.message
        end
      end
    end
    data << "throw '#{message.gsub("'", "\\\\'")}'\n" if message
  end

  return data
end