Use the :as option to create the form css class the same way Rails does

It sounds confusing, I know, but Rails uses the :as option as prefix
with the current action, when the option is given, otherwise it uses
dom_class, which behaves differently. For instance, given a @user object:

    With :as => 'user'          => 'user_new' or 'user_edit'
    Without :as, uses dom_class => 'new_user' or 'edit_user'

Weird huh? =)
This commit is contained in:
Carlos Antonio da Silva 2011-12-04 01:39:13 -02:00
parent a765a830c1
commit 3af639660d
2 changed files with 7 additions and 6 deletions

View File

@ -64,7 +64,7 @@ module SimpleForm
else
record = record.last if record.is_a?(Array)
action = record.respond_to?(:persisted?) && record.persisted? ? :edit : :new
as ? "#{action}_#{as}" : dom_class(record, action)
as ? "#{as}_#{action}" : dom_class(record, action)
end
end
end

View File

@ -37,7 +37,7 @@ class FormHelperTest < ActionView::TestCase
end
end
test 'simple form should add object name as css class to form when object is not present' do
test 'simple form should add object name as css class to form when object is not present and the :as option is not given' do
concat(simple_form_for(:user) do |f| end)
assert_select 'form.simple_form.user'
end
@ -45,6 +45,7 @@ class FormHelperTest < ActionView::TestCase
test 'simple form should add :as option as css class to form when object is not present' do
concat(simple_form_for(:user, :as => 'superuser') do |f| end)
assert_select 'form.simple_form.superuser'
assert_no_select 'form.simple_form.user'
end
test 'simple form should add object class name with new prefix as css class to form if record is not persisted' do
@ -56,7 +57,7 @@ class FormHelperTest < ActionView::TestCase
test 'simple form should add :as option with new prefix as css class to form if record is not persisted' do
@user.new_record!
concat(simple_form_for(@user, :as => 'superuser') do |f| end)
assert_select 'form.simple_form.new_superuser'
assert_select 'form.simple_form.superuser_new'
end
test 'simple form should add edit class prefix as css class to form if record is persisted' do
@ -66,16 +67,16 @@ class FormHelperTest < ActionView::TestCase
test 'simple form should add :as options with edit prefix as css class to form if record is persisted' do
concat(simple_form_for(@user, :as => 'superuser') do |f| end)
assert_select 'form.simple_form.edit_superuser'
assert_select 'form.simple_form.superuser_edit'
end
test 'simple form should not add object class to form if css_class is specified' do
concat(simple_form_for(:user, :html => {:class => nil}) do |f| end)
concat(simple_form_for(:user, :html => { :class => nil }) do |f| end)
assert_no_select 'form.user'
end
test 'simple form should add custom class to form if css_class is specified' do
concat(simple_form_for(:user, :html => {:class => 'my_class'}) do |f| end)
concat(simple_form_for(:user, :html => { :class => 'my_class' }) do |f| end)
assert_select 'form.my_class'
end