1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove namespace for isolated namespaced models in forms

This commit is contained in:
Piotr Sarnacki 2010-09-01 01:37:02 +02:00
parent 2607def862
commit 6f3119d3c2
4 changed files with 101 additions and 6 deletions

View file

@ -304,12 +304,12 @@ module ActionView
object_name = record_or_name_or_array object_name = record_or_name_or_array
when Array when Array
object = record_or_name_or_array.last object = record_or_name_or_array.last
object_name = options[:as] || ActiveModel::Naming.singular(object) object_name = options[:as] || ActiveModel::Naming.param_key(object)
apply_form_for_options!(record_or_name_or_array, options) apply_form_for_options!(record_or_name_or_array, options)
args.unshift object args.unshift object
else else
object = record_or_name_or_array object = record_or_name_or_array
object_name = options[:as] || ActiveModel::Naming.singular(object) object_name = options[:as] || ActiveModel::Naming.param_key(object)
apply_form_for_options!([object], options) apply_form_for_options!([object], options)
args.unshift object args.unshift object
end end
@ -539,7 +539,7 @@ module ActionView
object_name = record object_name = record
else else
object = record object = record
object_name = ActiveModel::Naming.singular(object) object_name = ActiveModel::Naming.param_key(object)
end end
builder = options[:builder] || ActionView::Base.default_form_builder builder = options[:builder] || ActionView::Base.default_form_builder
@ -1168,11 +1168,11 @@ module ActionView
end end
when Array when Array
object = record_or_name_or_array.last object = record_or_name_or_array.last
name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]" name = "#{object_name}#{index}[#{ActiveModel::Naming.param_key(object)}]"
args.unshift(object) args.unshift(object)
else else
object = record_or_name_or_array object = record_or_name_or_array
name = "#{object_name}#{index}[#{ActiveModel::Naming.singular(object)}]" name = "#{object_name}#{index}[#{ActiveModel::Naming.param_key(object)}]"
args.unshift(object) args.unshift(object)
end end

View file

@ -149,3 +149,18 @@ class Author < Comment
attr_accessor :post attr_accessor :post
def post_attributes=(attributes); end def post_attributes=(attributes); end
end end
module Blog
def self._railtie
self
end
class Post < Struct.new(:title, :id)
extend ActiveModel::Naming
include ActiveModel::Conversion
def persisted?
id.present?
end
end
end

View file

@ -75,6 +75,8 @@ class FormHelperTest < ActionView::TestCase
@post.body = "Back to the hill and over it again!" @post.body = "Back to the hill and over it again!"
@post.secret = 1 @post.secret = 1
@post.written_on = Date.new(2004, 6, 15) @post.written_on = Date.new(2004, 6, 15)
@blog_post = Blog::Post.new("And his name will be forty and four.", 44)
end end
Routes = ActionDispatch::Routing::RouteSet.new Routes = ActionDispatch::Routing::RouteSet.new
@ -675,6 +677,21 @@ class FormHelperTest < ActionView::TestCase
assert_dom_equal expected, output_buffer assert_dom_equal expected, output_buffer
end end
def test_form_for_with_isolated_namespaced_model
form_for(@blog_post) do |f|
concat f.text_field :title
concat f.submit('Edit post')
end
expected =
"<form accept-charset='UTF-8' action='/posts/44' method='post'>" +
snowman +
"<label for='post_title'>The Title</label>" +
"<input name='post[title]' size='30' type='text' id='post_title' value='And his name will be forty and four.' />" +
"<input name='commit' id='post_submit' type='submit' value='Edit post' />" +
"</form>"
end
def test_form_for_with_symbol_object_name def test_form_for_with_symbol_object_name
form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f| form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f|
concat f.label(:title, :class => 'post_title') concat f.label(:title, :class => 'post_title')

View file

@ -360,7 +360,7 @@ module RailtiesTest
assert_equal "It's a bar.", response[2].body assert_equal "It's a bar.", response[2].body
end end
test "namespaced engine should include only its own routes and helpers" do test "isolated engine should include only its own routes and helpers" do
@plugin.write "lib/bukkits.rb", <<-RUBY @plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits module Bukkits
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
@ -478,5 +478,68 @@ module RailtiesTest
response = AppTemplate::Application.call(env) response = AppTemplate::Application.call(env)
assert_equal "/bukkits/posts/1", response[2].body assert_equal "/bukkits/posts/1", response[2].body
end end
test "isolated engine should avoid namespace in names if that's possible" do
@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
class Engine < ::Rails::Engine
namespace Bukkits
end
end
RUBY
@plugin.write "app/models/bukkits/post.rb", <<-RUBY
module Bukkits
class Post
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :title
def to_param
"1"
end
def persisted?
false
end
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
mount Bukkits::Engine => "/bukkits", :as => "bukkits"
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
scope(:module => :bukkits) do
resources :posts
end
end
RUBY
@plugin.write "app/controllers/bukkits/posts_controller.rb", <<-RUBY
class Bukkits::PostsController < ActionController::Base
def new
end
end
RUBY
@plugin.write "app/views/bukkits/posts/new.html.erb", <<-RUBY
<%= form_for(Bukkits::Post.new) do |f| %>
<%= f.text_field :title %>
<% end %>
RUBY
add_to_config("config.action_dispatch.show_exceptions = false")
boot_rails
env = Rack::MockRequest.env_for("/bukkits/posts/new")
response = AppTemplate::Application.call(env)
assert response[2].body =~ /name="post\[title\]"/
end
end end
end end