Skip to content

Commit

Permalink
Remove namespace for isolated namespaced models in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
drogus committed Sep 2, 2010
1 parent 8620f66 commit 32c2f6a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
10 changes: 5 additions & 5 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -304,12 +304,12 @@ def form_for(record_or_name_or_array, *args, &proc)
object_name = record_or_name_or_array
when Array
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)
args.unshift object
else
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)
args.unshift object
end
Expand Down Expand Up @@ -539,7 +539,7 @@ def fields_for(record, record_object = nil, options = nil, &block)
object_name = record
else
object = record
object_name = ActiveModel::Naming.singular(object)
object_name = ActiveModel::Naming.param_key(object)
end

builder = options[:builder] || ActionView::Base.default_form_builder
Expand Down Expand Up @@ -1168,11 +1168,11 @@ def fields_for(record_or_name_or_array, *args, &block)
end
when Array
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)
else
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)
end

Expand Down
15 changes: 15 additions & 0 deletions actionpack/test/lib/controller/fake_models.rb
Expand Up @@ -149,3 +149,18 @@ class Author < Comment
attr_accessor :post
def post_attributes=(attributes); 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
17 changes: 17 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -75,6 +75,8 @@ def @post.to_param; '123'; end
@post.body = "Back to the hill and over it again!"
@post.secret = 1
@post.written_on = Date.new(2004, 6, 15)

@blog_post = Blog::Post.new("And his name will be forty and four.", 44)
end

Routes = ActionDispatch::Routing::RouteSet.new
Expand Down Expand Up @@ -675,6 +677,21 @@ def test_form_for_with_format
assert_dom_equal expected, output_buffer
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
form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f|
concat f.label(:title, :class => 'post_title')
Expand Down
65 changes: 64 additions & 1 deletion railties/test/railties/engine_test.rb
Expand Up @@ -360,7 +360,7 @@ def bar
assert_equal "It's a bar.", response[2].body
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
module Bukkits
class Engine < ::Rails::Engine
Expand Down Expand Up @@ -478,5 +478,68 @@ class MyMailer < ActionMailer::Base
response = AppTemplate::Application.call(env)
assert_equal "/bukkits/posts/1", response[2].body
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

0 comments on commit 32c2f6a

Please sign in to comment.