Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update some tests and add a to_model to form helpers

  • Loading branch information...
commit 13e18dd94000cef2b2058b96d62de16b7d3a2200 1 parent 5ffaaa7
@wycats wycats authored
View
8 actionpack/lib/action_view/context.rb
@@ -2,7 +2,7 @@ module ActionView
module CompiledTemplates #:nodoc:
# holds compiled template code
end
-
+
# ActionView contexts are supplied to ActionController
# to render template. The default ActionView context
# is ActionView::Base.
@@ -10,7 +10,7 @@ module CompiledTemplates #:nodoc:
# In order to work with ActionController, a Context
# must implement:
#
- # Context.for_controller[controller] Create a new ActionView instance for a
+ # Context.for_controller[controller] Create a new ActionView instance for a
# controller
# Context#_render_partial_from_controller[options]
# - responsible for setting options[:_template]
@@ -36,5 +36,9 @@ module CompiledTemplates #:nodoc:
module Context
include CompiledTemplates
attr_accessor :output_buffer
+
+ def convert_to_model(object)
+ object.respond_to?(:to_model) ? object.to_model : object
+ end
end
end
View
6 actionpack/lib/action_view/helpers/active_record_helper.rb
@@ -77,7 +77,7 @@ def input(record_name, method, options = {})
# * <tt>:submit_value</tt> - The text of the submit button (default: "Create" if a new record, otherwise "Update").
def form(record_name, options = {})
record = instance_variable_get("@#{record_name}")
- record = record.to_model if record.respond_to?(:to_model)
+ record = convert_to_model(record)
options = options.symbolize_keys
options[:action] ||= record.new_record? ? "create" : "update"
@@ -122,7 +122,7 @@ def error_message_on(object, method, *args)
end
options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError')
- object = object.to_model if object.respond_to?(:to_model)
+ object = convert_to_model(object)
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
(errors = obj.errors[method])
@@ -182,7 +182,7 @@ def error_messages_for(*params)
objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
end
- objects.map! {|o| o.respond_to?(:to_model) ? o.to_model : o }
+ objects.map! {|o| convert_to_model(o) }
count = objects.inject(0) {|sum, object| sum + object.errors.count }
unless count.zero?
View
2  actionpack/lib/action_view/helpers/form_helper.rb
@@ -288,6 +288,8 @@ def form_for(record_or_name_or_array, *args, &proc)
def apply_form_for_options!(object_or_array, options) #:nodoc:
object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array
+ object = convert_to_model(object)
+
html_options =
if object.respond_to?(:new_record?) && object.new_record?
{ :class => dom_class(object, :new), :id => dom_id(object), :method => :post }
View
1  actionpack/lib/action_view/test_case.rb
@@ -23,6 +23,7 @@ def _render_template(template, local_assigns = {})
class TestCase < ActiveSupport::TestCase
include ActionDispatch::Assertions
include ActionController::TestProcess
+ include ActionView::Context
class_inheritable_accessor :helper_class
@@helper_class = nil
View
1  actionpack/test/template/active_record_helper_i18n_test.rb
@@ -1,6 +1,7 @@
require 'abstract_unit'
class ActiveRecordHelperI18nTest < Test::Unit::TestCase
+ include ActionView::Context
include ActionView::Helpers::ActiveRecordHelper
attr_reader :request
View
10 actionpack/test/template/form_helper_test.rb
@@ -1,11 +1,9 @@
require 'abstract_unit'
silence_warnings do
- Post = Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
- Post.class_eval do
- alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
- alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
- alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
+ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
+ extend ActiveModel::APICompliant
+
alias_method :secret?, :secret
def new_record=(boolean)
@@ -27,6 +25,8 @@ def tags_attributes=(attributes); end
end
class Comment
+ extend ActiveModel::APICompliant
+
attr_reader :id
attr_reader :post_id
def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
View
2  actionpack/test/template/record_tag_helper_test.rb
@@ -6,7 +6,7 @@ def id
45
end
def body
- "What a wonderful world!"
+ super || "What a wonderful world!"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.