summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormakoto kuwata <kwa@kuwata-lab.com>2008-01-29 08:40:39 +0000
committermakoto kuwata <kwa@kuwata-lab.com>2008-01-29 08:40:39 +0000
commit62ebde5610bb41ff0335ffd3d11699a532726975 (patch)
treeac81969eb540466fd0cb12bd3b065fe3dc52aee7
parent4ca35ae82dc946df548e5b399625a2ffb72946e9 (diff)
downloaderubis-62ebde5610bb41ff0335ffd3d11699a532726975.tar.gz
- [enhance] add 'helpers/rails_form_helper.rb'
- [change] helpers/rails_helper.rb: rename _expr() to _p() - [enhance] helpers/rails_helper.rb: add _P() - [enhance] helpers/rails_helper.rb: support Rails 2.0 - [update] users-guide.txt - [change] rename 'CHANGES' to 'CHANGES.txt'
-rw-r--r--CHANGES.txt (renamed from CHANGES)50
-rw-r--r--ChangeLog.txt8
-rw-r--r--Rookbook.props2
-rw-r--r--Rookbook.yaml6
-rw-r--r--doc/users-guide.html192
-rw-r--r--doc/users-guide.txt218
-rw-r--r--erubis.gemspec2
-rw-r--r--lib/erubis/helpers/rails_form_helper.rb190
-rw-r--r--lib/erubis/helpers/rails_helper.rb83
9 files changed, 609 insertions, 142 deletions
diff --git a/CHANGES b/CHANGES.txt
index befcf3c..7abdcb8 100644
--- a/CHANGES
+++ b/CHANGES.txt
@@ -2,6 +2,56 @@
# $Release:$
# $Copyright$
+
+- release: 2.5.0
+ date: 2008-02-01
+ enhancements:
+
+ - |
+ Ruby on Rails 2.0 supported.
+
+ - |
+ Rails form helper methods for preprocessing are added.
+ These helper methos are available with preprocessing.
+
+ ex. _form.rhtml
+ --------------------
+ <p>
+ Name: <%= text_field :user, :name %>
+ </p>
+ <p>
+ Name: [%= pp_text_field :user, :name %]
+ </p>
+ --------------------
+
+ preprocessed:
+ --------------------
+ <p>
+ Name: <%= text_field :user, :name %>
+ </p>
+ <p>
+ Name: {{*<input id="stock_name" name="stock[name]" size="30" type="text" value="<%=h @stock.name%>" />*}}
+ </p>
+ --------------------
+
+ Ruby code:
+ --------------------
+ _buf << ' <p>
+ Name: '; _buf << ( text_field :stock, :name ).to_s; _buf << '
+ '; _buf << ' </p>
+ <p>
+ Name: <input id="stock_name" name="stock[name]" size="30" type="text" value="'; _buf << (h @stock.name).to_s; _buf << '" />
+ </p>
+ ';
+ --------------------
+
+ This shows that text_filed() is called every time when rendering,
+ but pp_text_filed() is called only once when loading template,
+ so pp_text_field() with prepocessing is much faster than text_field().
+
+ See User's guide for details.
+ http://www.kuwata-lab.local:8080/erubis/users-guide.05.html#rails-formhelpers
+
#
- release: 2.4.1
date: 2007-09-25
diff --git a/ChangeLog.txt b/ChangeLog.txt
index 3541352..78611f2 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -2,6 +2,14 @@
.?lastupdate: $Date$
.?version: $Rev$
+: Rev.98 (2008-01-29)
+ .- [enhance] add 'helpers/rails_form_helper.rb'
+ .- [change] helpers/rails_helper.rb: rename _expr() to _p()
+ .- [enhance] helpers/rails_helper.rb: add _P()
+ .- [enhance] helpers/rails_helper.rb: support Rails 2.0
+ .- [update] users-guide.txt
+ .- [change] rename 'CHANGES' to 'CHANGES.txt'
+
: Rev.97 (2007-10-23)
.- [change] remove 'website/'
diff --git a/Rookbook.props b/Rookbook.props
new file mode 100644
index 0000000..d862bb6
--- /dev/null
+++ b/Rookbook.props
@@ -0,0 +1,2 @@
+release: 2.5.0
+copyright: copyright(c) 2006-2008 kuwata-lab.com all rights reserved.
diff --git a/Rookbook.yaml b/Rookbook.yaml
index b3c7bf9..5937da4 100644
--- a/Rookbook.yaml
+++ b/Rookbook.yaml
@@ -5,13 +5,13 @@
###
properties:
- - release : 2.3.0
- - copyright : copyright(c) 2006-2007 kuwata-lab all rights reserved.
+ - release : 2.5.0
+ - copyright : copyright(c) 2006-2008 kuwata-lab all rights reserved.
parameters:
- project : erubis
- - text_files : [CHANGES, README.txt, MIT-LICENSE, setup.rb, erubis.gemspec]
+ - text_files : [CHANGES.txt, README.txt, MIT-LICENSE, setup.rb, erubis.gemspec]
- guide : doc/users-guide.html
- doc_files : [$(guide), doc/docstyle.css]
- apidocdir : doc-api
diff --git a/doc/users-guide.html b/doc/users-guide.html
index 2ef6d7b..7f18497 100644
--- a/doc/users-guide.html
+++ b/doc/users-guide.html
@@ -145,6 +145,8 @@ It has the following features.
</li>
<li><a href="#rails-preprocessing">Preprosessing</a>
</li>
+ <li><a href="#rails-formhelpers">Form Helpers for Preprocessing</a>
+ </li>
<li><a href="#rails-others">Others</a>
</li>
</ul>
@@ -2322,71 +2324,55 @@ This section describes how to use Erubis with Ruby on Rails.
</p>
<a name="rails-settings"></a>
<h3 class="section2">Settings</h3>
-<ol type="1">
-<li>Add the following code to your 'config/environment.rb'.
+<p>Add the following code to your 'config/environment.rb' and restart web server.
+This replaces ERB in Rails by Erubis entirely.
+</p>
<div class="program_caption">
config/environment.rb</div>
<pre class="program">require 'erubis/helpers/rails_helper'
#Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
#Erubis::Helpers::RailsHelper.init_properties = {}
-#Erubis::Helpers::RailsHelper.show_src = false
-#Erubis::Helpers::RailsHelper.preprocessing = true
+#Erubis::Helpers::RailsHelper.show_src = nil
+#Erubis::Helpers::RailsHelper.preprocessing = false
</pre>
-<p> This will replace ERB in Rails by Erubis entirely.
+<p>Options:
</p>
-</li>
-<li>(Optional) apply the following patch to 'action_pack/lib/action_view/base.rb'.
-<div class="program_caption">
-action_view_base_rb.patch</div>
-<pre class="program">--- lib/action_view/base.rb (original)
-+++ lib/action_view/base.rb (working copy)
-@@ -445,6 +445,11 @@
- end
- end
-
-+ # convert template into ruby code
-+ def convert_template_into_ruby_code(template)
-+ ERB.new(template, nil, @@erb_trim_mode).src
-+ end
-+
- # Create source code for given template
- def create_template_source(extension, template, render_symbol, locals)
- if template_requires_setup?(extension)
-@@ -458,7 +463,7 @@
- "update_page do |page|\n#{template}\nend"
- end
- else
-- body = ERB.new(template, nil, @@erb_trim_mode).src
-+ body = convert_template_into_ruby_code(template)
- end
-
- @@template_args[render_symbol] ||= {}
-</pre>
-<p> This patch is included in erubis_2.X.X/contrib directory and the following is an
- example to apply this patch.
+<dl class="dl2">
+<dt class="dt2">
+Erubis::Helpers::RailsHelper.engine_class (=Erubis::Eruby)</dt>
+<dd class="dd2">
+<p> Erubis engine class (default Erubis::Eruby).
</p>
-<div class="terminal_caption">
-how to apply patch:</div>
-<pre class="terminal">$ cd /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_view/
-$ sudo patch -p1 &lt; /tmp/erubis_2.X.X/contrib/action_view_base_rb.patch
-</pre>
-<p> Notice that this patch is not necessary if you are using Ruby on Rails ver 1.1 or 1.2, but it is recommended.
+</dd>
+<dt class="dt2">
+Erubis::Helpers::RailsHelper.init_properties (={})</dt>
+<dd class="dd2">
+<p> Optional arguments for Erubis::Eruby#initialize() method (default {}).
</p>
-</li>
-<li>Restart web server.
-<pre class="terminal">$ ruby script/server
-</pre>
-</li>
-</ol>
-<p>The setting is above all.
+</dd>
+<dt class="dt2">
+Erubis::Helpers::RailsHelper.show_src (=nil)</dt>
+<dd class="dd2">
+<p> Whether to print converted Ruby code into log file.
+ If true, Erubis prints coverted code into log file.
+ If false, Erubis doesn't.
+ If nil, Erubis prints when ENV['RAILS_ENV'] == 'development'.
+ Default is nil.
</p>
+</dd>
+<dt class="dt2">
+Erubis::Helpers::RailsHelper.preprocessing (=false)</dt>
+<dd class="dd2">
+<p> Enable preprocessing if true (default false).
+</p>
+</dd>
+</dl>
<br>
<a name="rails-preprocessing"></a>
<h3 class="section2">Preprosessing</h3>
-<p><strong>(Experimental)</strong>
-Erubis supports preprocessing of template files.
+<p>Erubis supports preprocessing of template files.
Preprocessing make your Ruby on Rails application about 20-40 percent faster.
To enable preprocessing, set Erubis::Helpers::RailsHelper.preprocessing to true in your 'environment.rb' file.
</p>
@@ -2463,6 +2449,103 @@ because tag helpers generate different html code when form parameter has errors
Preprocessing is not available with these methods.
</li>
</ul>
+<p><span style="color:#FF0000">CAUTHON:</span> link_to() in Ruby on Rails 2.0 doesn't work correctly with preprocessing.
+</p>
+<br>
+
+
+<a name="rails-formhelpers"></a>
+<h3 class="section2">Form Helpers for Preprocessing</h3>
+<p>Erubis provides form helper methods for preprocessing.
+These are defined in 'erubis/helpers/rails_form_helper.rb'.
+If you want to use it, require it and include Erubis::Helpers::RailsFormHelper in 'app/helpers/applition_helper.rb'
+</p>
+<div class="program_caption">
+app/helpers/xxx_helper.rb</div>
+<pre class="program">require 'erubis/helpers/rails_form_helper'
+module ApplicationHelper
+ include Erubis::Helpers::RailsFormHelper
+end
+</pre>
+<p>Form helper methods defined in Erubis::Helpers::RailsFormHelper are named as 'pp_xxxx'
+('pp' represents preprocessing).
+</p>
+<p>Assume the following view template:
+</p>
+<div class="program_caption">
+_form.rhtml</div>
+<pre class="program"> &lt;p&gt;
+ Name: &lt;%= text_field :user, :name %&gt;
+ &lt;/p&gt;
+ &lt;p&gt;
+ Name: <strong>[%= pp_text_field :user, :name %]</strong>
+ &lt;/p&gt;
+</pre>
+<p>Erubis preprocessor converts it to the following eRuby string:
+</p>
+<div class="program_caption">
+preprocessed</div>
+<pre class="program"> &lt;p&gt;
+ Name: &lt;%= text_field :user, :name %&gt;
+ &lt;/p&gt;
+ &lt;p&gt;
+ Name: <strong>&lt;input id="stock_name" name="stock[name]" size="30" type="text" value="&lt;%=h @stock.name%&gt;" /&gt;</strong>
+ &lt;/p&gt;
+</pre>
+<p>Erubis converts it to the following Ruby code:
+</p>
+<div class="program_caption">
+Ruby code</div>
+<pre class="program"> _buf &lt;&lt; ' &lt;p&gt;
+ Name: '; _buf &lt;&lt; ( text_field :stock, :name ).to_s; _buf &lt;&lt; '
+'; _buf &lt;&lt; ' &lt;/p&gt;
+ &lt;p&gt;
+ Name: &lt;input id="stock_name" name="stock[name]" size="30" type="text" value="'; _buf &lt;&lt; (h @stock.name).to_s; _buf &lt;&lt; '" /&gt;
+ &lt;/p&gt;
+';
+</pre>
+<p>The above Ruby code shows that text_field() is called everytime when rendering,
+but pp_text_field() is called only once when template is loaded.
+This means that pp_text_field() with preprocessing makes view layer very fast.
+</p>
+<p>Module Erubis::Helpers::RailsFormHelper defines the following form helper methods.
+</p>
+<ul type="disc">
+<li>pp_render_partial(basename)
+</li>
+<li>pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &amp;block)
+</li>
+<li>pp_text_field(object_name, method, options={})
+</li>
+<li>pp_password_field(object_name, method, options={})
+</li>
+<li>pp_hidden_field(object_name, method, options={})
+</li>
+<li>pp_file_field(object_name, method, options={})
+</li>
+<li>pp_text_area(object_name, method, options={})
+</li>
+<li>pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
+</li>
+<li>pp_radio_button(object_name, method, tag_value, options={})
+</li>
+<li>pp_select(object, method, collection, options={}, html_options={})
+</li>
+<li>pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
+</li>
+<li>pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
+</li>
+<li>pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
+</li>
+<li>pp_submit_tag(value="Save changes", options={})
+</li>
+<li>pp_image_submit_tag(source, options={})
+</li>
+</ul>
+<p>Notice that pp_form_for() is not provided.
+</p>
+<p><span style="color:#FF0000">CAUTION:</span> These are not tested in Ruby on Rails 2.0.
+</p>
<br>
@@ -2473,12 +2556,13 @@ because tag helpers generate different html code when form parameter has errors
</li>
</ul>
<ul type="disc">
-<li>If Erubis::Helper::Rails.show_src is ture, Erubis prints converted Ruby code into log file (ex. 'log/development.log').
+<li>link_to() method in Ruby on Rails 2.0 doesn't work correctly with preprocessing.
+</li>
+</ul>
+<ul type="disc">
+<li>Form helper methods are not tested in Ruby on Rails 2.0.
</li>
</ul>
-<p>If it is nil (default), Erubis prints converted Ruby code into log file only when development mode.
- It is useful for debugging.
-</p>
<ul type="disc">
<li>ERB::Util.h() is redefined if you require 'erubis/helpers/rails_helper.rb'.
Original definition of ERB::Util.h() is the following and it is slow
diff --git a/doc/users-guide.txt b/doc/users-guide.txt
index 927d478..5ad14b8 100644
--- a/doc/users-guide.txt
+++ b/doc/users-guide.txt
@@ -2423,71 +2423,85 @@ This section describes how to use Erubis with Ruby on Rails.
.$$ Settings | rails-settings
-.1) Add the following code to your 'config/environment.rb'.
+Add the following code to your 'config/environment.rb' and restart web server.
+This replaces ERB in Rails by Erubis entirely.
- .? config/environment.rb
- .--------------------
- require 'erubis/helpers/rails_helper'
- #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
- #Erubis::Helpers::RailsHelper.init_properties = {}
- #Erubis::Helpers::RailsHelper.show_src = false
- #Erubis::Helpers::RailsHelper.preprocessing = true
- .--------------------
+.? config/environment.rb
+.--------------------
+require 'erubis/helpers/rails_helper'
+#Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
+#Erubis::Helpers::RailsHelper.init_properties = {}
+#Erubis::Helpers::RailsHelper.show_src = nil
+#Erubis::Helpers::RailsHelper.preprocessing = false
+.--------------------
+
+Options:
+
+.% Erubis::Helpers::RailsHelper.engine_class (=Erubis::Eruby)
+ Erubis engine class (default Erubis::Eruby).
+.% Erubis::Helpers::RailsHelper.init_properties (={})
+ Optional arguments for Erubis::Eruby#initialize() method (default {}).
+.% Erubis::Helpers::RailsHelper.show_src (=nil)
+ Whether to print converted Ruby code into log file.
+ If true, Erubis prints coverted code into log file.
+ If false, Erubis doesn't.
+ If nil, Erubis prints when ENV['RAILS_ENV'] == 'development'.
+ Default is nil.
+.% Erubis::Helpers::RailsHelper.preprocessing (=false)
+ Enable preprocessing if true (default false).
- This will replace ERB in Rails by Erubis entirely.
-.2) (Optional) apply the following patch to 'action_pack/lib/action_view/base.rb'.
-
- .? action_view_base_rb.patch
- .--------------------
- --- lib/action_view/base.rb (original)
- +++ lib/action_view/base.rb (working copy)
- @@ -445,6 +445,11 @@
- end
- end
-
- + # convert template into ruby code
- + def convert_template_into_ruby_code(template)
- + ERB.new(template, nil, @@erb_trim_mode).src
- + end
- +
- # Create source code for given template
- def create_template_source(extension, template, render_symbol, locals)
- if template_requires_setup?(extension)
- @@ -458,7 +463,7 @@
- "update_page do |page|\n#{template}\nend"
- end
- else
- - body = ERB.new(template, nil, @@erb_trim_mode).src
- + body = convert_template_into_ruby_code(template)
- end
-
- @@template_args[render_symbol] ||= {}
- .--------------------
-
- This patch is included in erubis_2.X.X/contrib directory and the following is an
- example to apply this patch.
-
- .? how to apply patch:
- .====================
- $ cd /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_view/
- $ sudo patch -p1 < /tmp/erubis_2.X.X/contrib/action_view_base_rb.patch
- .====================
-
- Notice that this patch is not necessary if you are using Ruby on Rails ver 1.1 or 1.2, but it is recommended.
-
-.3) Restart web server.
- .====================
- $ ruby script/server
- .====================
-
-The setting is above all.
+.#.2) (Optional) apply the following patch to 'action_pack/lib/action_view/base.rb'.
+.#
+.# .? action_view_base_rb.patch
+.# .--------------------
+.# --- lib/action_view/base.rb (original)
+.# +++ lib/action_view/base.rb (working copy)
+.# @@ -445,6 +445,11 @@
+.# end
+.# end
+.#
+.# + # convert template into ruby code
+.# + def convert_template_into_ruby_code(template)
+.# + ERB.new(template, nil, @@erb_trim_mode).src
+.# + end
+.# +
+.# # Create source code for given template
+.# def create_template_source(extension, template, render_symbol, locals)
+.# if template_requires_setup?(extension)
+.# @@ -458,7 +463,7 @@
+.# "update_page do |page|\n#{template}\nend"
+.# end
+.# else
+.# - body = ERB.new(template, nil, @@erb_trim_mode).src
+.# + body = convert_template_into_ruby_code(template)
+.# end
+.#
+.# @@template_args[render_symbol] ||= {}
+.# .--------------------
+.#
+.# This patch is included in erubis_2.X.X/contrib directory and the following is an
+.# example to apply this patch.
+.#
+.# .? how to apply patch:
+.# .====================
+.# $ cd /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_view/
+.# $ sudo patch -p1 < /tmp/erubis_2.X.X/contrib/action_view_base_rb.patch
+.# .====================
+.#
+.# Notice that this patch is not necessary if you are using Ruby on Rails ver 1.1 or 1.2, but it is recommended.
+.#
+.#.2) Restart web server.
+.# .====================
+.# $ ruby script/server
+.# .====================
+.#
+.#The setting is above all.
.$$ Preprosessing | rails-preprocessing
-{{*(Experimental)*}}
Erubis supports preprocessing of template files.
Preprocessing make your Ruby on Rails application about 20-40 percent faster.
To enable preprocessing, set Erubis::Helpers::RailsHelper.preprocessing to true in your 'environment.rb' file.
@@ -2576,13 +2590,97 @@ Helper methods of Ruby on Rails are divided into two groups.
Preprocessing is not available with these methods.
+{{!CAUTHON:!}} link_to() in Ruby on Rails 2.0 doesn't work correctly with preprocessing.
+
+
+.$$ Form Helpers for Preprocessing | rails-formhelpers
+
+Erubis provides form helper methods for preprocessing.
+These are defined in 'erubis/helpers/rails_form_helper.rb'.
+If you want to use it, require it and include Erubis::Helpers::RailsFormHelper in 'app/helpers/applition_helper.rb'
+
+.? app/helpers/xxx_helper.rb
+.--------------------
+require 'erubis/helpers/rails_form_helper'
+module ApplicationHelper
+ include Erubis::Helpers::RailsFormHelper
+end
+.--------------------
+
+Form helper methods defined in Erubis::Helpers::RailsFormHelper are named as 'pp_xxxx'
+('pp' represents preprocessing).
+
+Assume the following view template:
+
+.? _form.rhtml
+.--------------------
+ <p>
+ Name: <%= text_field :user, :name %>
+ </p>
+ <p>
+ Name: {{*[%= pp_text_field :user, :name %]*}}
+ </p>
+.--------------------
+
+Erubis preprocessor converts it to the following eRuby string:
+
+.? preprocessed
+.--------------------
+ <p>
+ Name: <%= text_field :user, :name %>
+ </p>
+ <p>
+ Name: {{*<input id="stock_name" name="stock[name]" size="30" type="text" value="<%=h @stock.name%>" />*}}
+ </p>
+.--------------------
+
+Erubis converts it to the following Ruby code:
+
+.? Ruby code
+.--------------------
+ _buf << ' <p>
+ Name: '; _buf << ( text_field :stock, :name ).to_s; _buf << '
+'; _buf << ' </p>
+ <p>
+ Name: <input id="stock_name" name="stock[name]" size="30" type="text" value="'; _buf << (h @stock.name).to_s; _buf << '" />
+ </p>
+';
+.--------------------
+
+The above Ruby code shows that text_field() is called everytime when rendering,
+but pp_text_field() is called only once when template is loaded.
+This means that pp_text_field() with preprocessing makes view layer very fast.
+
+Module Erubis::Helpers::RailsFormHelper defines the following form helper methods.
+
+.* pp_render_partial(basename)
+.* pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &block)
+.* pp_text_field(object_name, method, options={})
+.* pp_password_field(object_name, method, options={})
+.* pp_hidden_field(object_name, method, options={})
+.* pp_file_field(object_name, method, options={})
+.* pp_text_area(object_name, method, options={})
+.* pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
+.* pp_radio_button(object_name, method, tag_value, options={})
+.* pp_select(object, method, collection, options={}, html_options={})
+.* pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
+.* pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
+.* pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
+.* pp_submit_tag(value="Save changes", options={})
+.* pp_image_submit_tag(source, options={})
+
+Notice that pp_form_for() is not provided.
+
+{{!CAUTION:!}} These are not tested in Ruby on Rails 2.0.
+
+
.$$ Others | rails-others
.* ActionView::Helpers::CaptureHelper#capture() and ActionView::Helpers::Texthelper#concat() are available.
-.* If Erubis::Helper::Rails.show_src is ture, Erubis prints converted Ruby code into log file (ex. 'log/development.log').
-If it is nil (default), Erubis prints converted Ruby code into log file only when development mode.
- It is useful for debugging.
+.* link_to() method in Ruby on Rails 2.0 doesn't work correctly with preprocessing.
+
+.* Form helper methods are not tested in Ruby on Rails 2.0.
.* ERB::Util.h() is redefined if you require 'erubis/helpers/rails_helper.rb'.
Original definition of ERB::Util.h() is the following and it is slow
diff --git a/erubis.gemspec b/erubis.gemspec
index a8ad887..b07001d 100644
--- a/erubis.gemspec
+++ b/erubis.gemspec
@@ -38,7 +38,7 @@ spec = Gem::Specification.new do |s|
files += Dir.glob('examples/**/*')
files += Dir.glob('test/**/*')
files += Dir.glob('doc/**/*')
- files += %w[README.txt CHANGES MIT-LICENSE setup.rb]
+ files += %w[README.txt CHANGES.txt MIT-LICENSE setup.rb]
files += Dir.glob('contrib/**/*')
files += Dir.glob('benchmark/**/*')
files += Dir.glob('doc-api/**/*')
diff --git a/lib/erubis/helpers/rails_form_helper.rb b/lib/erubis/helpers/rails_form_helper.rb
new file mode 100644
index 0000000..bc1520e
--- /dev/null
+++ b/lib/erubis/helpers/rails_form_helper.rb
@@ -0,0 +1,190 @@
+###
+### $Rev$
+### $Release: $
+### $Copyright$
+###
+
+
+module Erubis
+ module Helpers
+ module RailsFormHelper
+ end
+ end
+end
+
+
+module Erubis::Helpers::RailsFormHelper
+
+ def pp_template_filename(basename)
+ return "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.rhtml"
+ end
+
+ def pp_render_partial(basename)
+ basename = "_#{basename}" unless basename[0] == ?_
+ filename = pp_template_filename(basename)
+ klass = Erubis::Helpers::RailsHelper::PreprocessingEruby
+ eruby = klass.new(File.read(filename))
+ return eruby.evaluate(self)
+ end
+
+ def pp_error_on(object_name, method, &block)
+ s = ''
+ s << "<% _stag, _etag = _pp_error_tags(@#{object_name}.errors.on('#{method}'))%>"
+ s << "<%=_stag%>"
+ s << yield(object_name, method)
+ s << "<%=_etag%>"
+ return s
+ end
+
+ def _pp_error_tags(value)
+ return value ? ['<div class="fieldWithErrors">', '</div>'] : ['', '']
+ end
+
+ def _pp_remove_error_div(s)
+ s.sub!(/\A<div class="fieldWithErrors">(.*)<\/div>\z/, '\1')
+ return s
+ end
+
+ def pp_tag_helper(helper, object_name, method, options={})
+ if object_name.is_a?(ActionView::Helpers::FormHelper)
+ object_name = object_name.object_name
+ end
+ unless options.key?(:value) || options.key?('value')
+ options['value'] = _?("h @#{object_name}.#{method}")
+ end
+ $stderr.puts "*** debug: pp_tag_helper(): options=#{options.inspect}"
+ return pp_error_on(object_name, method) {
+ s = __send__(helper, object_name, method, options)
+ _pp_remove_error_div(s)
+ }
+ end
+
+ def pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &block)
+ return form_tag(url_for_options, options, *parameters_for_url, &block)
+ end
+
+ #--
+ #def pp_form_for(object_name, *args, &block)
+ # return form_for(object_name, *args, &block)
+ #end
+ #++
+
+ def pp_text_field(object_name, method, options={})
+ return pp_tag_helper(:text_field, object_name, method, options)
+ end
+
+ def pp_password_field(object_name, method, options={})
+ return pp_tag_helper(:password_field, object_name, method, options)
+ end
+
+ def pp_hidden_field(object_name, method, options={})
+ return pp_tag_helper(:hidden_field, object_name, method, options)
+ end
+
+ def pp_file_field(object_name, method, options={})
+ return pp_tag_helper(:file_field, object_name, method, options)
+ end
+
+ def pp_text_area(object_name, method, options={})
+ return pp_tag_helper(:text_area, object_name, method, options)
+ end
+
+ def pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
+ s = check_box(object_name, method, options, checked_value, unchecked_value)
+ s.sub!(/\schecked=\"checked\"/, '')
+ s.sub!(/type="checkbox"/, "\\&<%= _pp_check_box_checked?(@#{object_name}.#{method}, #{checked_value.inspect}) ? ' checked=\"checked\"' : '' %>")
+ return pp_error_on(object_name, method) { _pp_remove_error_div(s) }
+ end
+
+ def _pp_check_box_checked?(value, checked_value)
+ return ActionView::Helpers::InstanceTag::check_box_checked?(value, checked_value)
+ end
+
+ def pp_radio_button(object_name, method, tag_value, options={})
+ s = radio_button(object_name, method, tag_value, options)
+ s.sub!(/\schecked=\"checked\"/, '')
+ s.sub!(/type="radio"/, "\\&<%= _pp_radio_button_checked?(@#{object_name}.#{method}, #{tag_value.inspect}) ? ' checked=\"checked\"' : '' %>")
+ return pp_error_on(object_name, method) { _pp_remove_error_div(s) }
+ end
+
+ def _pp_radio_button_checked?(value, tag_value)
+ return ActionView::Helpers::InstanceTag::radio_button_checked?(value, tag_value)
+ end
+
+ def _pp_select(object, method, collection, priority_collection, options={}, html_options={})
+ return pp_error_on(object, method) do
+ s = ""
+ ## start tag
+ s << "<select id=\"#{object}_#{method}\" name=\"#{object}[#{method}]\""
+ for key, val in html_options:
+ s << " #{key}=\"#{val}\""
+ end
+ s << ">\n"
+ ## selected table
+ key = options.key?(:value) ? :value : (options.key?('value') ? 'value' : nil)
+ if key.nil? ; selected = "@#{object}.#{method}"
+ elsif (val=options[key]).nil? ; selected = nil
+ elsif val =~ /\A<%=(.*)%>\z/ ; selected = $1
+ else ; selected = val.inspect
+ end
+ s << "<% _table = {#{selected}=>' selected=\"selected\"'} %>\n" if selected
+ ## <option> tags
+ if options[:include_blank] || options['include_blank']
+ s << "<option value=\"\"></option>\n"
+ end
+ unless priority_collection.blank?
+ _pp_select_options(s, priority_collection, selected, 'delete')
+ s << "<option value=\"\">-------------</option>\n"
+ end
+ _pp_select_options(s, collection, selected, '[]')
+ ## end tag
+ s << "</select>"
+ s
+ end
+ end
+
+ def _pp_select_options(s, collection, selected, operator)
+ for item in collection
+ value, text = item.is_a?(Array) ? item : [item, item]
+ if !selected
+ t = ''
+ elsif operator == 'delete'
+ t = "<%= _table.delete(#{value.inspect}) %>"
+ else
+ t = "<%= _table[#{value.inspect}] %>"
+ end
+ s << "<option value=\"#{h value}\"#{t}>#{h text}</option>\n"
+ end
+ end
+
+ def pp_select(object, method, collection, options={}, html_options={})
+ return _pp_select(object, method, collection, nil, options, html_options)
+ end
+
+ def pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
+ collection2 = collection.collect { |e|
+ [e.__send__(value_method), e.__send__(text_method)]
+ }
+ return _pp_select(object, method, collection2, nil, options, html_options)
+ end
+
+ def pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
+ collection = ActionView::Helpers::FormOptionsHelper::COUNTRIES
+ return _pp_select(object, method, collection, priority_countries, options, html_options)
+ end
+
+ def pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
+ model = options[:model] || options['model'] || TimeZone
+ collection = model.all.collect { |e| [e.name, e.to_s] }
+ return _pp_select(object, method, collection, priority_zones, options, html_options)
+ end
+
+ def pp_submit_tag(value="Save changes", options={})
+ return submit_tag(value, options)
+ end
+
+ def pp_image_submit_tag(source, options={})
+ return image_submit_tag(source, options)
+ end
+
+end
diff --git a/lib/erubis/helpers/rails_helper.rb b/lib/erubis/helpers/rails_helper.rb
index 79d28bd..a055f85 100644
--- a/lib/erubis/helpers/rails_helper.rb
+++ b/lib/erubis/helpers/rails_helper.rb
@@ -34,15 +34,11 @@ module Erubis
## #Erubis::Helpers::RailsHelper.show_src = false # set true for debugging
## #Erubis::Helpers::RailsHelper.preprocessing = true # set true to enable preprocessing
##
- ## 2. (optional) apply the patch for 'action_view/base.rb'
+ ## 2. restart web server.
##
- ## $ cd /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/
- ## $ sudo patch -p1 < /tmp/erubis_2.X.X/contrib/action_view_base_rb.patch
- ##
- ## 3. restart web server.
- ##
- ## if Erubis::Helper::Rails.show_src is ture, Erubis prints converted Ruby code
- ## into log file ('log/development.log' or so). This may be useful for debug.
+ ## if Erubis::Helper::Rails.show_src is true, Erubis prints converted Ruby code
+ ## into log file ('log/development.log' or so). if false, it doesn't.
+ ## if nil, Erubis prints converted Ruby code if ENV['RAILS_ENV'] == 'development'.
##
module RailsHelper
@@ -70,7 +66,7 @@ module Erubis
end
#cattr_accessor :show_src
- @@show_src = false
+ @@show_src = nil
def self.show_src
@@show_src
@@ -80,7 +76,7 @@ module Erubis
@@show_src = flag
end
- ##----- preprocessor: experimental -----
+ ##----- preprocessing --------------------
@@preprocessing = false
def self.preprocessing
@@preprocessing
@@ -126,19 +122,21 @@ unless ActionView::Base.private_instance_methods.include?(method_name) ||
end
-if ActionPack::VERSION::MINOR <= 12 ### Rails 1.1
+if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
- # Create source code for given template
+ # Method to create the source code for a given template.
def create_template_source(extension, template, render_symbol, locals)
if template_requires_setup?(extension)
body = case extension.to_sym
- when :rxml
+ when :rxml, :builder
+ content_type_handler = (controller.respond_to?(:response) ? "controller.response" : "controller")
+ "#{content_type_handler}.content_type ||= Mime::XML\n" +
"xml = Builder::XmlMarkup.new(:indent => 2)\n" +
- "@controller.headers['Content-Type'] ||= 'application/xml'\n" +
- template
+ template +
+ "\nxml.target!\n"
when :rjs
- "@controller.headers['Content-Type'] ||= 'text/javascript'\n" +
+ "controller.response.content_type ||= Mime::JS\n" +
"update_page do |page|\n#{template}\nend"
end
else
@@ -152,14 +150,14 @@ if ActionPack::VERSION::MINOR <= 12 ### Rails 1.1
locals_code = ""
locals_keys.each do |key|
- locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
end
"def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
end
-else ### Rails 1.2 or later
+elsif ActionPack::VERSION::MINOR > 12 ### Rails 1.2
# Create source code for given template
@@ -192,6 +190,39 @@ else ### Rails 1.2 or later
end
+elsif ActionPack::VERSION::MINOR <= 12 ### Rails 1.1
+
+
+ # Create source code for given template
+ def create_template_source(extension, template, render_symbol, locals)
+ if template_requires_setup?(extension)
+ body = case extension.to_sym
+ when :rxml
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
+ "@controller.headers['Content-Type'] ||= 'application/xml'\n" +
+ template
+ when :rjs
+ "@controller.headers['Content-Type'] ||= 'text/javascript'\n" +
+ "update_page do |page|\n#{template}\nend"
+ end
+ else
+ #body = ERB.new(template, nil, @@erb_trim_mode).src
+ body = convert_template_into_ruby_code(template)
+ end
+
+ @@template_args[render_symbol] ||= {}
+ locals_keys = @@template_args[render_symbol].keys | locals
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
+
+ locals_code = ""
+ locals_keys.each do |key|
+ locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
+ end
+
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
+ end
+
+
end ###
@@ -210,10 +241,12 @@ class ActionView::Base # :nodoc:
klass = Erubis::Helpers::RailsHelper.engine_class
properties = Erubis::Helpers::RailsHelper.init_properties
show_src = Erubis::Helpers::RailsHelper.show_src
- ##----- preprocessing: experimental -----
+ show_src = ENV['RAILS_ENV'] == 'development' if show_src.nil?
+ ##----- preprocessing -------------------
if Erubis::Helpers::RailsHelper.preprocessing
preprocessor = Erubis::Helpers::RailsHelper::PreprocessingEruby.new(template)
- template = self.instance_eval(preprocessor.src)
+ #template = self.instance_eval(preprocessor.src)
+ template = preprocessor.evaluate(self)
logger.debug "** Erubis: preprocessed==<<'END'\n#{template}END\n" if show_src
end
##----------------------------------------
@@ -223,12 +256,14 @@ class ActionView::Base # :nodoc:
src
end
- ##----- preprocessing: experimental -----
- def _expr(arg)
+ ##----- preprocessing -------------------
+ def _p(arg)
return "<%=#{arg}%>"
end
- alias _x _expr
- alias _? _expr
+ def _P(arg)
+ return "<%=h(#{arg})%>"
+ end
+ alias _? _p
def _decode(arg)
arg = arg.to_s
arg.gsub!(/%3C%25%3D(.*?)%25%3E/) { "<%=#{CGI.unescape($1)}%>" }