提交 cf56397c 编写于 作者: Y yalab

scaffold nested name controller should be fine.

上级 801ccab2
<%%= form_with(model: <%= singular_table_name %>, local: true) do |form| %>
<%%= form_with(model: <%= model_resource_name %>, local: true) do |form| %>
<%% if <%= singular_table_name %>.errors.any? %>
<div id="error_explanation">
<h2><%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
......
......@@ -18,9 +18,9 @@
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end -%>
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>
<td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td>
<td><%%= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%%= link_to 'Show', <%= model_resource_name %> %></td>
<td><%%= link_to 'Edit', edit_<%= singular_route_name %>_path(<%= singular_table_name %>) %></td>
<td><%%= link_to 'Destroy', <%= model_resource_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<%% end %>
</tbody>
......@@ -28,4 +28,4 @@
<br>
<%%= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_table_name %>_path %>
<%%= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_route_name %>_path %>
......@@ -100,11 +100,11 @@ def uncountable? # :doc:
end
def index_helper # :doc:
uncountable? ? "#{plural_table_name}_index" : plural_table_name
uncountable? ? "#{plural_route_name}_index" : plural_route_name
end
def show_helper # :doc:
"#{singular_table_name}_url(@#{singular_table_name})"
"#{singular_route_name}_url(@#{singular_table_name})"
end
def edit_helper # :doc:
......@@ -112,7 +112,7 @@ def edit_helper # :doc:
end
def new_helper # :doc:
"new_#{singular_table_name}_url"
"new_#{singular_route_name}_url"
end
def field_id(attribute_name)
......@@ -152,6 +152,35 @@ def application_name # :doc:
end
end
def redirect_resource_name # :doc:
model_resource_name(prefix: "@")
end
def model_resource_name(prefix: "") # :doc:
resource_name = "#{prefix}#{singular_table_name}"
if controller_class_path.empty?
resource_name
else
"[#{controller_class_path.map { |name| ":" + name }.join(", ")}, #{resource_name}]"
end
end
def singular_route_name # :doc:
if controller_class_path.empty?
singular_table_name
else
"#{controller_class_path.join('_')}_#{singular_table_name}"
end
end
def plural_route_name # :doc:
if controller_class_path.empty?
plural_table_name
else
"#{controller_class_path.join('_')}_#{plural_table_name}"
end
end
def assign_names!(name)
@class_path = name.include?("/") ? name.split("/") : name.split("::")
@class_path.map!(&:underscore)
......
......@@ -29,7 +29,7 @@ def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
if @<%= orm_instance.save %>
redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
else
render :new
end
......@@ -38,7 +38,7 @@ def create
# PATCH/PUT <%= route_url %>/1
def update
if @<%= orm_instance.update("#{singular_table_name}_params") %>
redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
redirect_to <%= redirect_resource_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
else
render :edit
end
......
......@@ -131,6 +131,19 @@ def test_scaffold_plural_names_with_model_name_option
assert_name g, "admin/foos", :controller_file_path
assert_name g, "foos", :controller_file_name
assert_name g, "admin.foos", :controller_i18n_scope
assert_name g, "admin_user", :singular_route_name
assert_name g, "admin_users", :plural_route_name
assert_name g, "[:admin, @user]", :redirect_resource_name
assert_name g, "[:admin, user]", :model_resource_name
assert_name g, "admin_users", :index_helper
end
def test_scaffold_plural_names
g = generator ["User"]
assert_name g, "@user", :redirect_resource_name
assert_name g, "user", :model_resource_name
assert_name g, "user", :singular_route_name
assert_name g, "users", :plural_route_name
end
private
......
......@@ -174,6 +174,29 @@ def test_model_name_option
assert_instance_method :index, content do |m|
assert_match("@users = User.all", m)
end
assert_instance_method :create, content do |m|
assert_match("redirect_to [:admin, @user]", m)
end
assert_instance_method :update, content do |m|
assert_match("redirect_to [:admin, @user]", m)
end
end
assert_file "app/views/admin/users/index.html.erb" do |content|
assert_match("'Show', [:admin, user]", content)
assert_match("'Edit', edit_admin_user_path(user)", content)
assert_match("'Destroy', [:admin, user]", content)
assert_match("'New User', new_admin_user_path", content)
end
assert_file "app/views/admin/users/new.html.erb" do |content|
assert_match("'Back', admin_users_path", content)
end
assert_file "app/views/admin/users/_form.html.erb" do |content|
assert_match("model: [:admin, user]", content)
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册