Permalink
Browse files

r2495@sedna: jeremy | 2005-05-27 22:07:18 -0700

 Branch to have scaffold use new render methods
 r2496@sedna:  jeremy | 2005-05-28 10:50:51 -0700
 axe unused process_request method
 r2497@sedna:  jeremy | 2005-05-28 10:51:58 -0700
 sanity assertions in process method check that @controller, @request, @response are set
 r2498@sedna:  jeremy | 2005-05-28 10:53:42 -0700
 clean up escaping.  use instance_variable_get instead of instance_eval.  put id in form action instead of hidden input (like the generated scaffolding)
 r2499@sedna:  jeremy | 2005-05-28 10:54:46 -0700
 clean up interpolation, remove tabs from stylesheet
 r2500@sedna:  jeremy | 2005-05-28 10:56:40 -0700
 modernize dynamic scaffolding.  use params[:id] instead of @params[id].  use consolidated render method instead of render_action, render_file, etc.  use builtin inflections rather than module methods.
 r2501@sedna:  jeremy | 2005-05-28 11:00:26 -0700
 modernize generated scaffolding.  use new-style assertions; use params[:id] instead of @params[id]; use consolidated render method instead of render_*
 r2542@sedna:  jeremy | 2005-06-12 02:24:06 -0700
 use :id => model instead of :id => model.id.  clean up the form helper method.  use flash[:notice] instead of flash['notice'].
 r2543@sedna:  jeremy | 2005-06-12 02:33:22 -0700
 whitespace
 r2544@sedna:  jeremy | 2005-06-12 02:56:57 -0700
 update changelogs
 r2545@sedna:  jeremy | 2005-06-12 03:26:23 -0700
 test dynamic form help when @post is not a new record
 r2546@sedna:  jeremy | 2005-06-12 03:44:14 -0700
 touch up TestCase#process sanity assertions


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1404 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent dedb9c8 commit 3cb26e9cb3c1f3a069e7654988c650ca12322ede @jeremy jeremy committed Jun 12, 2005
View
@@ -1,5 +1,11 @@
*SVN*
+* Modernize scaffolding to match the generator: use the new render method and change style from the warty @params["id"] to the sleek params[:id]. #1367
+
+* Include :id in the action generated by the form helper method. Then, for example, the controller can do Model.find(params[:id]) for both edit and update actions. Updated scaffolding to take advantage. #1367
+
+* Add assertions with friendly messages to TestCase#process to ensure that @controller, @request, and @response are set. #1367
+
* Arrays sent via multipart posts are converted to strings #1032 [dj@omelia.org]
* render(:layout => true) is a synonym for render(:layout => nil)
@@ -27,12 +27,12 @@ def self.append_features(base)
# end
#
# def show
- # @entry = Entry.find(@params["id"])
+ # @entry = Entry.find(params[:id])
# render_scaffold
# end
#
# def destroy
- # Entry.find(@params["id"]).destroy
+ # Entry.find(params[:id]).destroy
# redirect_to :action => "list"
# end
#
@@ -42,27 +42,27 @@ def self.append_features(base)
# end
#
# def create
- # @entry = Entry.new(@params["entry"])
+ # @entry = Entry.new(params[:entry])
# if @entry.save
- # flash["notice"] = "Entry was successfully created"
+ # flash[:notice] = "Entry was successfully created"
# redirect_to :action => "list"
# else
# render_scaffold('new')
# end
# end
#
# def edit
- # @entry = Entry.find(@params["id"])
+ # @entry = Entry.find(params[:id])
# render_scaffold
# end
#
# def update
- # @entry = Entry.find(@params["entry"]["id"])
- # @entry.attributes = @params["entry"]
+ # @entry = Entry.find(params[:id])
+ # @entry.attributes = params[:entry]
#
# if @entry.save
- # flash["notice"] = "Entry was successfully updated"
- # redirect_to :action => "show/" + @entry.id.to_s
+ # flash[:notice] = "Entry was successfully updated"
+ # redirect_to :action => "show", :id => @entry
# else
# render_scaffold('edit')
# end
@@ -84,9 +84,9 @@ module ClassMethods
def scaffold(model_id, options = {})
validate_options([ :class_name, :suffix ], options.keys)
- singular_name = model_id.id2name
- class_name = options[:class_name] || Inflector.camelize(singular_name)
- plural_name = Inflector.pluralize(singular_name)
+ singular_name = model_id.to_s
+ class_name = options[:class_name] || singular_name.camelize
+ plural_name = singular_name.pluralize
suffix = options[:suffix] ? "_#{singular_name}" : ""
unless options[:suffix]
@@ -104,12 +104,12 @@ def list#{suffix}
end
def show#{suffix}
- @#{singular_name} = #{class_name}.find(@params["id"])
+ @#{singular_name} = #{class_name}.find(params[:id])
render#{suffix}_scaffold
end
def destroy#{suffix}
- #{class_name}.find(@params["id"]).destroy
+ #{class_name}.find(params[:id]).destroy
redirect_to :action => "list#{suffix}"
end
@@ -119,27 +119,27 @@ def new#{suffix}
end
def create#{suffix}
- @#{singular_name} = #{class_name}.new(@params["#{singular_name}"])
+ @#{singular_name} = #{class_name}.new(params[:#{singular_name}])
if @#{singular_name}.save
- flash["notice"] = "#{class_name} was successfully created"
+ flash[:notice] = "#{class_name} was successfully created"
redirect_to :action => "list#{suffix}"
else
render#{suffix}_scaffold('new')
end
end
def edit#{suffix}
- @#{singular_name} = #{class_name}.find(@params["id"])
+ @#{singular_name} = #{class_name}.find(params[:id])
render#{suffix}_scaffold
end
def update#{suffix}
- @#{singular_name} = #{class_name}.find(@params["#{singular_name}"]["id"])
- @#{singular_name}.attributes = @params["#{singular_name}"]
+ @#{singular_name} = #{class_name}.find(params[:id])
+ @#{singular_name}.attributes = params[:#{singular_name}]
if @#{singular_name}.save
- flash["notice"] = "#{class_name} was successfully updated"
- redirect_to :action => "show#{suffix}", :id => @#{singular_name}.id.to_s
+ flash[:notice] = "#{class_name} was successfully updated"
+ redirect_to :action => "show#{suffix}", :id => @#{singular_name}
else
render#{suffix}_scaffold('edit')
end
@@ -148,18 +148,23 @@ def update#{suffix}
private
def render#{suffix}_scaffold(action = caller_method_name(caller))
if template_exists?("\#{self.class.controller_path}/\#{action}")
- render_action(action)
+ render(:action => action)
else
@scaffold_class = #{class_name}
@scaffold_singular_name, @scaffold_plural_name = "#{singular_name}", "#{plural_name}"
@scaffold_suffix = "#{suffix}"
add_instance_variables_to_assigns
@content_for_layout = @template.render_file(scaffold_path(action.sub(/#{suffix}$/, "")), false)
- self.active_layout ? render_file(self.active_layout, "200 OK", true) : render_file(scaffold_path("layout"))
+
+ if active_layout?
+ render :file => active_layout, :use_full_path => true
+ else
+ render :file => scaffold_path("layout")
+ end
end
end
-
+
def scaffold_path(template_name)
File.dirname(__FILE__) + "/templates/scaffolds/" + template_name + ".rhtml"
end
@@ -1,7 +1,7 @@
<h1>Editing <%= @scaffold_singular_name %></h1>
<%= error_messages_for(@scaffold_singular_name) %>
-<%= form(@scaffold_singular_name, :action => "update" + @scaffold_suffix) %>
+<%= form(@scaffold_singular_name, :action => "update#{@scaffold_suffix}") %>
-<%= link_to "Show", :action => "show#{@scaffold_suffix}", :id => instance_variable_get("@#{@scaffold_singular_name}").id %> |
+<%= link_to "Show", :action => "show#{@scaffold_suffix}", :id => instance_variable_get("@#{@scaffold_singular_name}") %> |
<%= link_to "Back", :action => "list#{@scaffold_suffix}" %>
@@ -28,40 +28,40 @@
#ErrorExplanation {
width: 400px;
- border: 2px solid 'red';
- padding: 7px;
- padding-bottom: 12px;
- margin-bottom: 20px;
- background-color: #f0f0f0;
+ border: 2px solid 'red';
+ padding: 7px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
}
#ErrorExplanation h2 {
- text-align: left;
- font-weight: bold;
- padding: 5px 5px 5px 15px;
- font-size: 12px;
- margin: -7px;
- background-color: #c00;
- color: #fff;
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ background-color: #c00;
+ color: #fff;
}
#ErrorExplanation p {
- color: #333;
- margin-bottom: 0;
- padding: 5px;
+ color: #333;
+ margin-bottom: 0;
+ padding: 5px;
}
#ErrorExplanation ul li {
- font-size: 12px;
- list-style: square;
+ font-size: 12px;
+ list-style: square;
}
</style>
</head>
<body>
-<p style="color: green"><%= flash['notice'] %></p>
+<p style="color: green"><%= flash[:notice] %></p>
<%= @content_for_layout %>
</body>
-</html>
+</html>
@@ -12,9 +12,9 @@
<% for column in @scaffold_class.content_columns %>
<td><%= entry.send(column.name) %></td>
<% end %>
- <td><%= link_to "Show", :action => "show#{@scaffold_suffix}", :id => entry.id %></td>
- <td><%= link_to "Edit", :action => "edit#{@scaffold_suffix}", :id => entry.id %></td>
- <td><%= link_to "Destroy", :action => "destroy#{@scaffold_suffix}", :id => entry.id, :confirm => "Are you sure?" %></td>
+ <td><%= link_to "Show", :action => "show#{@scaffold_suffix}", :id => entry %></td>
+ <td><%= link_to "Edit", :action => "edit#{@scaffold_suffix}", :id => entry %></td>
+ <td><%= link_to "Destroy", :action => "destroy#{@scaffold_suffix}", :id => entry, :confirm => "Are you sure?" %></td>
</tr>
<% end %>
</table>
@@ -1,6 +1,6 @@
<h1>New <%= @scaffold_singular_name %></h1>
<%= error_messages_for(@scaffold_singular_name) %>
-<%= form(@scaffold_singular_name, :action => "create" + @scaffold_suffix) %>
+<%= form(@scaffold_singular_name, :action => "create#{@scaffold_suffix}") %>
-<%= link_to "Back", :action => "list#{@scaffold_suffix}" %>
+<%= link_to "Back", :action => "list#{@scaffold_suffix}" %>
@@ -5,5 +5,5 @@
</p>
<% end %>
-<%= link_to "Edit", :action => "edit#{@scaffold_suffix}", :id => instance_variable_get("@#{@scaffold_singular_name}").id %> |
+<%= link_to "Edit", :action => "edit#{@scaffold_suffix}", :id => instance_variable_get("@#{@scaffold_singular_name}") %> |
<%= link_to "Back", :action => "list#{@scaffold_suffix}" %>
@@ -247,6 +247,11 @@ class TestCase #:nodoc:
private
# execute the request and set/volley the response
def process(action, parameters = nil, session = nil, flash = nil)
+ # Sanity check for required instance variables so we can give an understandable error message.
+ %w(controller request response).each do |iv_name|
+ assert_not_nil instance_variable_get("@#{iv_name}"), "@#{iv_name} is nil: make sure you set it in your test's setup method."
+ end
+
@html_document = nil
@request.env['REQUEST_METHOD'] ||= "GET"
@request.action = action.to_s
@@ -56,20 +56,22 @@ def input(record_name, method)
# form << content_tag("b", "Department")
# form << collection_select("department", "id", @departments, "id", "name")
# end
- def form(record_name, options = nil)
- options = (options || {}).symbolize_keys
- record = instance_eval("@#{record_name}")
+ def form(record_name, options = {})
+ record = instance_variable_get("@#{record_name}")
+ options = options.symbolize_keys
options[:action] ||= record.new_record? ? "create" : "update"
- action = url_for(:action => options[:action])
+ action = url_for(:action => options[:action], :id => record)
submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
- id_field = record.new_record? ? "" : InstanceTag.new(record_name, "id", self).to_input_field_tag("hidden")
+ contents = ''
+ contents << hidden_field(record_name, :id) unless record.new_record?
+ contents << all_input_tags(record, record_name, options)
+ yield contents if block_given?
+ contents << submit_tag(submit_value)
- formtag = %(<form action="#{action}" method="post">#{id_field}) + all_input_tags(record, record_name, options)
- yield formtag if block_given?
- formtag + %(<input type="submit" value="#{submit_value}" /></form>)
+ content_tag('form', contents, :action => action, :method => 'post')
end
# Returns a string containing the error message attached to the +method+ on the +object+, if one exists.
@@ -83,8 +85,8 @@ def form(record_name, options = nil)
# <%= error_message_on "post", "title", "Title simply ", " (or it won't work)", "inputError" %> =>
# <div class="inputError">Title simply can't be empty (or it won't work)</div>
def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
- if errors = instance_eval("@#{object}").errors.on(method)
- "<div class=\"#{css_class}\">#{prepend_text + (errors.is_a?(Array) ? errors.first : errors) + append_text}</div>"
+ if errors = instance_variable_get("@#{object}").errors.on(method)
+ content_tag("div", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class)
end
end
@@ -96,7 +98,7 @@ def error_message_on(object, method, prepend_text = "", append_text = "", css_cl
# * <tt>class</tt> - The class of the error div (default: errorExplanation)
def error_messages_for(object_name, options = {})
options = options.symbolize_keys
- object = instance_eval "@#{object_name}"
+ object = instance_variable_get("@#{object_name}")
unless object.errors.empty?
content_tag("div",
content_tag(
@@ -117,7 +119,7 @@ def all_input_tags(record, record_name, options)
end
def default_input_block
- Proc.new { |record, column| "<p><label for=\"#{record}_#{column.name}\">#{column.human_name}</label><br />#{input(record, column.name)}</p>" }
+ Proc.new { |record, column| %(<p><label for="#{record}_#{column.name}">#{column.human_name}</label><br />#{input(record, column.name)}</p>) }
end
end
Oops, something went wrong.

0 comments on commit 3cb26e9

Please sign in to comment.