Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove namespace for isolated namespaced models in forms

  • Loading branch information...
commit 6f3119d3c298c007e7a4eed8375d9fc30b961d06 1 parent 2607def
@drogus drogus authored
View
10 actionpack/lib/action_view/helpers/form_helper.rb
@@ -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
@@ -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
@@ -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
View
15 actionpack/test/lib/controller/fake_models.rb
@@ -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
View
17 actionpack/test/template/form_helper_test.rb
@@ -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
@@ -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')
View
65 railties/test/railties/engine_test.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.