require 'abstract_unit'
class ActiveModelHelperTest < ActionView::TestCase
tests ActionView::Helpers::ActiveModelHelper
silence_warnings do
class Post < Struct.new(:title, :author_name, :body, :secret, :written_on)
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class User < Struct.new(:email)
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class Column < Struct.new(:type, :name, :human_name)
extend ActiveModel::Naming
include ActiveModel::Conversion
end
end
class DirtyPost
class Errors
def empty?
false
end
def count
1
end
def full_messages
["Author name can't be empty"]
end
def [](field)
["can't be empty"]
end
end
def errors
Errors.new
end
end
def setup_post
@post = Post.new
def @post.errors
Class.new {
def [](field)
case field.to_s
when "author_name"
["can't be empty"]
when "body"
['foo']
else
[]
end
end
def empty?() false end
def count() 1 end
def full_messages() [ "Author name can't be empty" ] end
}.new
end
def @post.new_record?() true end
def @post.to_param() nil end
def @post.column_for_attribute(attr_name)
Post.content_columns.select { |column| column.name == attr_name }.first
end
silence_warnings do
def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end
end
@post.title = "Hello World"
@post.author_name = ""
@post.body = "Back to the hill and over it again!"
@post.secret = 1
@post.written_on = Date.new(2004, 6, 15)
end
def setup_user
@user = User.new
def @user.errors
Class.new {
def [](field) field == "email" ? ['nonempty'] : [] end
def empty?() false end
def count() 1 end
def full_messages() [ "User email can't be empty" ] end
}.new
end
def @user.new_record?() true end
def @user.to_param() nil end
def @user.column_for_attribute(attr_name)
User.content_columns.select { |column| column.name == attr_name }.first
end
silence_warnings do
def User.content_columns() [ Column.new(:string, "email", "Email") ] end
end
@user.email = ""
end
def protect_against_forgery?
@protect_against_forgery ? true : false
end
attr_accessor :request_forgery_protection_token, :form_authenticity_token
def setup
super
setup_post
setup_user
@response = ActionController::TestResponse.new
@controller = Object.new
def @controller.url_for(options)
options = options.symbolize_keys
[options[:action], options[:id].to_param].compact.join('/')
end
end
def test_generic_input_tag
assert_dom_equal(
%(), input("post", "title")
)
end
def test_text_area_with_errors
assert_dom_equal(
%(
),
text_area("post", "body")
)
end
def test_text_field_with_errors
assert_dom_equal(
%(),
text_field("post", "author_name")
)
end
def test_form_with_string
assert_dom_equal(
%(),
form("post")
)
silence_warnings do
class << @post
def new_record?() false end
def to_param() id end
def id() 1 end
end
end
assert_dom_equal(
%(),
form("post")
)
end
def test_form_with_protect_against_forgery
@protect_against_forgery = true
@request_forgery_protection_token = 'authenticity_token'
@form_authenticity_token = '123'
assert_dom_equal(
%(),
form("post")
)
end
def test_form_with_method_option
assert_dom_equal(
%(),
form("post", :method=>'get')
)
end
def test_form_with_action_option
output_buffer << form("post", :action => "sign")
assert_select "form[action=sign]" do |form|
assert_select "input[type=submit][value=Sign]"
end
end
def test_form_with_date
silence_warnings do
def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
end
assert_dom_equal(
%(),
form("post")
)
end
def test_form_with_datetime
silence_warnings do
def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end
end
@post.written_on = Time.gm(2004, 6, 15, 16, 30)
assert_dom_equal(
%(),
form("post")
)
end
def test_error_for_block
assert_dom_equal %(