caterpillar 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,8 +12,9 @@ module Caterpillar
12
12
  def self.show
13
13
  STDOUT.puts 'Usage:'
14
14
  STDOUT.puts ' See "%s --describe" for an overview of the tasks.' % $0
15
- STDOUT.puts
16
- STDOUT.puts ' cd to Rails root, and run the "%s pluginize" task' % $0
15
+ STDOUT.puts
16
+ STDOUT.puts 'How to start up a new rails-portlet project?'
17
+ STDOUT.puts ' caterpillar rails project_name'
17
18
  STDOUT.puts
18
19
  end
19
20
  end
@@ -10,17 +10,18 @@ module Caterpillar
10
10
  class Util
11
11
  class << self
12
12
 
13
- # Reads the configuration
14
- def eval_configuration(config=nil)
15
- cf = File.join([RAILS_ROOT,Caterpillar::Config::FILE])
16
- STDERR.puts 'Caterpillar configuration file could not be found' unless File.exists?(cf)
17
-
18
- if config.nil? && File.exists?(cf)
19
- config = eval(File.open(cf) {|f| f.read})
13
+ # Reads and evaluates the configuration.
14
+ # If parameter is not given, read from default location (RAILS_ROOT/config/portlets.rb)
15
+ def eval_configuration(conf_file=nil)
16
+ return Config.new if not (conf_file or defined?(RAILS_ROOT))
17
+ # else . . .
18
+ conf_file ||= File.join([RAILS_ROOT,Caterpillar::Config::FILE])
19
+ if File.exists?(conf_file)
20
+ #$stdout.puts "Reading configuration from #{conf_file}"
21
+ config = eval(File.open(conf_file) {|f| f.read})
20
22
  end
21
- config ||= Config.new
22
23
  unless config.kind_of? Config
23
- warn "Portlet config not provided by override in initializer or #{Config::FILE}; using defaults"
24
+ $stderr.puts "Configuration was not parsed properly"
24
25
  config = Config.new
25
26
  end
26
27
  return config
@@ -28,39 +29,59 @@ module Caterpillar
28
29
 
29
30
  # Collects Rails' named routes
30
31
  def parse_routes(config)
31
- # taken from Rails' "routes" task
32
- # routes = ActionController::Routing::Routes.routes.collect do |route|
33
- # name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s
34
- # verb = route.conditions[:method].to_s.upcase
35
- # segs = route.segments.inject("") { |str,s| str << s.to_s }
36
- # segs.chop! if segs.length > 1
37
- # reqs = route.requirements.empty? ? "" : route.requirements.inspect
38
- # {:name => name, :verb => verb, :segs => segs, :reqs => reqs}
39
- # end
40
-
41
- ActionController::Routing::Routes.named_routes.collect do |route|
42
-
43
- # Ruby 1.9
44
- if route.class == Symbol
45
- name = route
46
- _route = ActionController::Routing::Routes.named_routes.routes[route]
47
- # Ruby 1.8
48
- elsif route.class == Array
49
- name = route[0]
50
- _route = route[1] # 'ActionController::Routing::Route'
32
+ require 'action_controller'
33
+ require File.join(CATERPILLAR_LIBS, '..','portlet_test_bench', 'routing')
34
+ ActionController::Routing::RouteSet::Mapper.send :include, Caterpillar::Routing::MapperExtensions
35
+
36
+ routes = []
37
+ config.instances.each do |portlet|
38
+
39
+ # clear old routes from memory and reload ActionController
40
+ ActionController::Routing::Routes.clear!
41
+
42
+ # prefer portlet rails_root
43
+ if portlet[:rails_root]
44
+ rails_root = portlet[:rails_root]
45
+ elsif config.rails_root
46
+ rails_root = config.rails_root
47
+ else
48
+ next
51
49
  end
52
-
53
- # segments; the path
54
- segs = _route.segments.inject("") { |str,s| str << s.to_s }
55
- segs.chop! if segs.length > 1
56
- # controller and action
57
- reqs = _route.requirements
58
- # extra variables
59
- keys = _route.significant_keys
60
- vars = keys - [:action, :controller]
61
-
62
- {:name => name, :path => segs, :reqs => reqs, :vars => vars}
50
+ # load routes
51
+ f = File.open(
52
+ File.join(
53
+ rails_root,'config','routes.rb'
54
+ ))
55
+ eval(f.read())
56
+ f.close()
57
+
58
+ routes <<
59
+ ActionController::Routing::Routes.named_routes.collect do |route|
60
+ # Ruby 1.9
61
+ if route.class == Symbol
62
+ name = route
63
+ _route = ActionController::Routing::Routes.named_routes.routes[route]
64
+ defaults = {} #TODO: Get default values in ruby 1.9
65
+ # Ruby 1.8
66
+ elsif route.class == Array
67
+ name = route[0]
68
+ _route = route[1] # 'ActionController::Routing::Route'
69
+ defaults = route[-1].defaults
70
+ end
71
+
72
+ # segments; the path
73
+ segs = _route.segments.inject("") { |str,s| str << s.to_s }
74
+ segs.chop! if segs.length > 1
75
+ # controller and action
76
+ reqs = _route.requirements
77
+ # extra variables
78
+ keys = _route.significant_keys
79
+ vars = keys - [:action, :controller]
80
+
81
+ {:name => name, :path => segs, :reqs => reqs, :vars => vars, :defaults => defaults}
82
+ end
63
83
  end
84
+ return routes.flatten
64
85
  end
65
86
 
66
87
  # Reorganizes the portlets hash by category.
@@ -94,6 +115,12 @@ module Caterpillar
94
115
  ret.update(category => _portlets)
95
116
  end
96
117
 
118
+ # add portlets without category
119
+ uncategorized = portlets.select {|p| p[:category].nil?}
120
+ if uncategorized.any?
121
+ ret.update('undefined' => uncategorized)
122
+ end
123
+
97
124
  return ret
98
125
  end
99
126
 
@@ -31,7 +31,7 @@ class RailsGemChooser
31
31
  STDERR.puts 'Could not detect Rails version'
32
32
  return nil
33
33
  end
34
- end
34
+ end
35
35
  # don't attempt to load Rails if building a Rubygem..!
36
36
  if $0[/gem$/] or !File.exist?(config_file)
37
37
  return nil
@@ -47,10 +47,13 @@ class RailsGemChooser
47
47
  end
48
48
 
49
49
  # Load a specific GEM
50
- def __load_gem(name,version)
51
- #p 'require gem %s v%s' % [name,version]
52
- version ? gem(name, '= '+version) : gem(name)
53
- require name
50
+ def __load_gem(require_name, gem_name, version)
51
+ version ? gem(gem_name, '= '+version) : gem(gem_name)
52
+ begin
53
+ require require_name
54
+ rescue LoadError
55
+ require gem_name
56
+ end
54
57
  end
55
58
 
56
59
  # Either define +rails_gem_version+ or +config_file+
@@ -60,19 +63,20 @@ class RailsGemChooser
60
63
  rails_gem_version ||= version(config_file) # also detects ENV['RAILS_GEM_VERSION']
61
64
 
62
65
  #STDOUT.puts 'Loading Rails version %s' % rails_gem_version
66
+ # the gem without underline will be removed in Rails3..
67
+ #rails_gems = %w{ active_support action_pack active_record }
68
+ # except that with the underline divider the gem is not found ..
69
+ #rails_gems = %w{ activesupport actionpack activerecord }
70
+
71
+ rails_gems = {
72
+ # require name gem name
73
+ "active_support" => "activesupport",
74
+ "action_pack" => "actionpack",
75
+ "active_record" => "activerecord"
76
+ }
63
77
 
64
- # XXX: silly hack because gem loading seems to have a problem..
65
- # >> gem('active_support', '=2.3.5')
66
- # Gem::LoadError: Could not find RubyGem active_support (= 2.3.5)
67
- # >> require 'activesupport'
68
- # DEPRECATION WARNING: require "activesupport" is deprecated and will be removed in Rails 3. Use require "active_support" instead..
69
- require 'active_support'
70
- rails_gems = %w{ actionpack activerecord }
71
-
72
- ActiveSupport::Deprecation.silence do
73
- rails_gems.each do |rg|
74
- __load_gem(rg,rails_gem_version)
75
- end
78
+ rails_gems.keys.each do |rg_key|
79
+ __load_gem(rg_key, rails_gems[rg_key], rails_gem_version)
76
80
  end
77
81
  require 'action_controller'
78
82
 
@@ -9,17 +9,17 @@ Caterpillar includes this portlet into a vanilla Rails application:
9
9
  cd new_app
10
10
  caterpillar pluginize
11
11
 
12
- Edit the config/routes as instructed by the previous command.
12
+ Add "map.caterpillar" to your config/routes.rb!
13
13
 
14
- The command
14
+ Now the command
15
15
  caterpillar portlets
16
16
 
17
17
  Should output
18
18
 
19
19
  * Portlet configuration ***********************
20
- Caterpillar
21
- Rails-portlet test bench "portlet_test_bench" []
20
+ Caterpillar
21
+ Rails-portlet test bench /caterpillar/test_bench
22
22
 
23
23
 
24
- Now launch the Webrick server and navigate to http://localhost:3000/caterpillar/test_bench
24
+ Launch the Webrick server and navigate to http://localhost:3000/caterpillar/test_bench
25
25
 
@@ -4,8 +4,19 @@
4
4
  class Caterpillar::HttpMethodsController < Caterpillar::ApplicationController
5
5
 
6
6
  def post
7
- @msg = params[:msg] if request.post?
8
- @checkbox = params[:checkbox]
7
+ @postcode = 'SW1A 0AA'
8
+
9
+ if request.post?
10
+ @msg = params[:msg]
11
+ @checkbox = params[:checkbox]
12
+ if params[:postcode]
13
+ @msg = params[:postcode][@postcode]
14
+ end
15
+ if params[:'_encoding_']
16
+ @msg << " with IE hack"
17
+ end
18
+ end
19
+
9
20
  render :action => :post
10
21
  end
11
22
 
@@ -1,8 +1,4 @@
1
1
  # encoding: utf-8
2
-
3
-
4
- # encoding: utf-8
5
-
6
2
  class Caterpillar::JunitController < Caterpillar::ApplicationController
7
3
 
8
4
  layout false
@@ -16,7 +12,13 @@ class Caterpillar::JunitController < Caterpillar::ApplicationController
16
12
  render :nothing => true
17
13
  end
18
14
 
19
- def basic_tags
15
+ def images
16
+ @host = request.host
17
+ @port = request.port
18
+ @netloc = 'http://%s:%s' % [@host, @port]
19
+ end
20
+
21
+ def links
20
22
  @host = request.host
21
23
  @port = request.port
22
24
  @netloc = 'http://%s:%s' % [@host, @port]
@@ -104,7 +106,7 @@ class Caterpillar::JunitController < Caterpillar::ApplicationController
104
106
 
105
107
  def preferences
106
108
  render :text => "Preferences view"
107
- end
109
+ end
108
110
 
109
111
  # Sets a session value so the single SESSION_KEY cookie is set.
110
112
  # The output XML prints the session ID and the JUnit test compares this
@@ -1,11 +1,29 @@
1
1
  <div id="http_post">
2
- <h1>POST test</h1>
2
+ <h1>POST tests</h1>
3
3
 
4
- <div id="http_postform">
4
+ <%# information of received POST -%>
5
+ <% if @msg -%>
6
+ <span id="post_info">
7
+ Message from last POST:
8
+ <span id="post_msg">
9
+ <%= @msg -%>
10
+ </span>
11
+ </span>
12
+ <% end -%>
13
+
14
+ <% if @checkbox -%>
15
+ <span id="post_info">
16
+ Checkbox was checked
17
+ </span>
18
+ <% end -%>
19
+
20
+
21
+ <div class="http_postform">
22
+ <h2>Regular POST</h2>
5
23
  <%= form_tag :action => "post" %>
6
24
  <p>
7
25
  <span>Input text:</span>
8
- <%= text_field_tag "msg", "The Java language was created by James Gosling in June 1991", :size => 42 %>
26
+ <%= text_field_tag "msg", "The Java language was created by James Gosling in June 1991", :size => 89 %>
9
27
  </p>
10
28
 
11
29
  <p>
@@ -13,40 +31,77 @@
13
31
  <input type="checkbox" name="checkbox" value="yay" true />
14
32
  </p>
15
33
 
16
- <p id="submit_tags">
17
- <input type="submit" value="Submit values" />
18
- <input type="reset" value="Reset to defaults" />
34
+ <p class="submit_tags">
35
+ <input type="submit" value="Submit" />
19
36
  </p>
20
37
  </form>
21
38
  </div>
22
39
 
23
40
 
24
- <h2>Message from last POST:</h2>
25
- <span id="post_msg">
26
- <%= @msg -%>
27
- </span>
28
- <br />
29
- <span id="post_info">
30
- <% if @checkbox -%>
31
- Checkbox was checked
32
- <% else -%>
33
- <% if @checkbox == false %>
34
- <!--
35
- Checkbox was unchecked
36
- -->
37
- <% end %>
38
- <% end -%>
39
- </span>
40
-
41
- <h1>Exit portlet w/ POST:</h1>
42
- Sometimes it is useful to send an action outside the portlet.
43
- Test this by pressing this button:
44
-
45
- <form action="http://www.google.com/search?exit_portlet=true">
46
- <input name="q" value="success" type="hidden" />
47
- <input type="submit" value="test" />
48
- </form>
41
+ <h2>POST with accept-charset="UTF-8"</h2>
42
+ <div class="http_postform">
43
+ <% form_tag({ :action => "post" }, {:'accept-charset' => "UTF-8"} ) do %>
44
+ <p>
45
+ <%= text_field_tag "msg", "å ä ö ♥", :size => 12 %>
46
+ </p>
47
+ <p class="submit_tags">
48
+ <%= submit_tag 'Submit' %>
49
+ </p>
50
+ <% end %>
51
+ </div>
52
+
53
+
54
+ <h2>POST with awkward field names sent as ISO-8859-1</h2>
55
+ <div class="http_postform">
56
+ <% form_tag({ :action => "post" }, {:'accept-charset' => "ISO-8859-1"} ) do %>
57
+ <p>
58
+ <%= text_field_tag "postcode[#{@postcode}][]", "è" %>
59
+ </p>
60
+ <p class="submit_tags">
61
+ <%= submit_tag 'Submit' %>
62
+ </p>
63
+ <% end %>
64
+ </div>
65
+
66
+
67
+ <h2>POST as ISO-8859-1, with IE hack</h2>
68
+ <script type="text/javascript">
69
+ function isIE() {
70
+ return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
71
+ }
72
+ function formEncodingIEHack(form) {
73
+ if (isIE()) {
74
+ form.appendChild(
75
+ document.createElement("<input name='_encoding_' value='CP1252' type='hidden' />")
76
+ );
77
+ }
78
+ return true;
79
+ }
80
+ </script>
81
+ <div class="http_postform">
82
+ <% form_tag({ :action => "post" }, {:'accept-charset' => "ISO-8859-1", :onsubmit => "return formEncodingIEHack(this);"} ) do %>
83
+ <p>
84
+ <%= text_field_tag "postcode[#{@postcode}][]", "è" %>
85
+ </p>
86
+ <p class="submit_tags">
87
+ <%= submit_tag 'Submit' %>
88
+ </p>
89
+ <% end %>
90
+ </div>
49
91
 
50
92
 
93
+ <h2>Exit portlet w/ POST:</h2>
94
+ <p>
95
+ Sometimes it is useful to send an action outside the portlet.
96
+ Test this with this button:
97
+ </p>
98
+ <div class="http_postform">
99
+ <form action="http://www.google.com/search?exit_portlet=true">
100
+ <input name="q" value="HTTP POST form" type="hidden" />
101
+ <p class="submit_tags">
102
+ <input type="submit" value="Google search" />
103
+ </p>
104
+ </form>
105
+ </div>
51
106
 
52
107
  </div>
@@ -0,0 +1,18 @@
1
+ <html>
2
+ <head>
3
+ <title>Portlet title</title>
4
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8"></meta>
5
+ <base href="<%= @netloc -%>/images/portlet_test_bench/" />
6
+ </head>
7
+ <body>
8
+ <div id="anchor_absolute_url">
9
+ <a href="<%= @netloc -%>/caterpillar/test_bench">Absolute url</a>
10
+ </div>
11
+ <div id="anchor_absolute_path">
12
+ <a href="/caterpillar/test_bench">Absolute path</a>
13
+ </div>
14
+ <div id="anchor_relative_path">
15
+ <a href="../../caterpillar/test_bench">Relative path</a>
16
+ </div>
17
+ </body>
18
+ </html>
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+ Caterpillar::Config.new do |portlet|
3
+
4
+ # JRUBY_HOME can be set here, unless the environment variable can be used.
5
+ # portlet.class::JRUBY_HOME = '/usr/local/jruby'
6
+
7
+ # The portlet container.
8
+ # By default only portlet.xml is created.
9
+ # Currently only Liferay is supported. You may optionally define the version.
10
+ portlet.container = Liferay
11
+ # portlet.container.version = '5.2.3'
12
+
13
+ # If you want to install the Rails-portlet JAR into the container, the container
14
+ # WEB-INF will be used.
15
+ #
16
+ # Since liferay-display-ext.xml does not exist, all portlets are categorized in
17
+ # liferay-display.xml. Caterpillar parses this file and appends Rails portlets.
18
+ #
19
+ # No changes are made to any of the files in this directory while making XML,
20
+ # only the deploy and install tasks make any changes.
21
+ # portlet.container.root = '/usr/local/liferay/tomcat/'
22
+
23
+ # The server that the container is running on.
24
+ # Possible values:
25
+ # - 'Tomcat' (default)
26
+ # - 'JBoss/Tomcat'
27
+ # portlet.container.server = 'JBoss/Tomcat'
28
+
29
+ # The server dir is only meaningful with JBoss.
30
+ # This is the name of the directory in server/.
31
+ # By default the first entry in the directory is chosen.
32
+ # portlet.container.server_dir = 'default'
33
+
34
+ # Allow to defining the deploy_dir - just the WAR file will be deployed under this directory.
35
+ # Since version 1.3.0
36
+ # portlet.container.deploy_dir = '/opt/myDeployDir'
37
+
38
+ # The hostname and port.
39
+ # By default the values are taken from the request.
40
+ portlet.host = 'http://0.0.0.0:3000'
41
+
42
+ # If the Rails is running inside a servlet container such as Tomcat,
43
+ # you can define the servlet here.
44
+ # By default the servlet is the name of the Rails app.
45
+ # Without Warbler this should be an empty string.
46
+ portlet.servlet = ''
47
+
48
+ # Portlet category. This is only available for Liferay.
49
+ # By default this is the same as the servlet.
50
+ # portlet.category = 'Example Rails app'
51
+
52
+ # Portlet instances.
53
+ #
54
+ # Each named route is mapped to a portlet.
55
+ #
56
+ # All keys except for 'name' are obligatory. If the name does not map to a route,
57
+ # you have to define the route here.
58
+ # You may override the host, servlet and category here.
59
+ # Most likely you will want to let ActionController::Routing to set the route.
60
+ #
61
+ # Available keys are:
62
+ # - :name -- named route
63
+ # - :category -- portlet category (Liferay only)
64
+ # - :title -- the title in portlet container's category (Liferay only)
65
+ # - :edit_mode -- enables edit mode for the portlet, adds <portlet-mode>edit</portlet-mode> to portlet-ext.xml
66
+ # Default value is false
67
+ # - :instanceable -- enables instanceable for the portlet, add <instanceable>true</instanceable> to
68
+ # liferay-portlet-ext.xml. Default value is false
69
+ # - :javascripts -- portlet-specific javascripts that are included at
70
+ # the head of master HTML, such as body onload functions (Liferay only)
71
+ # - :host -- hostname:port of the deployment server
72
+ # - :servlet -- by default, the name of the Rails app (= name of the WAR package)
73
+ # - :path -- unless you're using named routes, you can define the path here
74
+
75
+ # Rails-portlet testing application.
76
+ # NOTE: this needs to be activated by 'map.caterpillar' in RAILS_ROOT/config/routes.rb
77
+ portlet.instances << {
78
+ :name => 'portlet_test_bench',
79
+ :title => 'Rails-portlet test bench',
80
+ :category => 'Caterpillar',
81
+ :path => '/caterpillar/test_bench'
82
+ }
83
+
84
+ # this will include all named routes without further configuration
85
+ portlet.include_all_named_routes = true
86
+
87
+ end