Skip to content

Fix variable name when generate scaffold with namespace & model-name #2694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/generators/rspec/scaffold/templates/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
let(:<%= ns_file_name %>) {
let(:<%= singular_table_name %>) {
<%= class_name %>.create!(<%= ')' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
<%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
Expand All @@ -11,13 +11,13 @@
}

before(:each) do
assign(:<%= ns_file_name %>, <%= ns_file_name %>)
assign(:<%= singular_table_name %>, <%= singular_table_name %>)
end

it "renders the edit <%= ns_file_name %> form" do
render

assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(<%= ns_file_name %>), "post" do
assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(<%= singular_table_name %>), "post" do
<% for attribute in output_attributes -%>
<%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
before(:each) do
assign(:<%= ns_file_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
assign(:<%= singular_table_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
<%= attribute.name %>: <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
before(:each) do
assign(:<%= ns_file_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
assign(:<%= singular_table_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
<%= attribute.name %>: <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
Expand Down
60 changes: 60 additions & 0 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe "(.*)\/edit", #{type_metatag(:view)}/) }
it { is_expected.to contain(/assign\(:post, post\)/) }
it { is_expected.to contain(/it "renders the edit (.*) form"/) }
end

Expand All @@ -200,6 +201,7 @@
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe "(.*)\/index", #{type_metatag(:view)}/) }
it { is_expected.to contain(/assign\(:posts, /) }
it { is_expected.to contain(/it "renders a list of (.*)"/) }
end

Expand All @@ -208,6 +210,7 @@
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe "(.*)\/new", #{type_metatag(:view)}/) }
it { is_expected.to contain(/assign\(:post, /) }
it { is_expected.to contain(/it "renders new (.*) form"/) }
end

Expand All @@ -216,6 +219,7 @@
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe "(.*)\/show", #{type_metatag(:view)}/) }
it { is_expected.to contain(/assign\(:post, /) }
it { is_expected.to contain(/it "renders attributes in <p>"/) }
end
end
Expand Down Expand Up @@ -251,6 +255,62 @@
end
end

describe 'with namespace' do
before { run_generator %w[admin/posts] }

describe 'edit' do
subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_post, admin_post\)/) }
end

describe 'index' do
subject { file("spec/views/admin/posts/index.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_posts, /) }
end

describe 'new' do
subject { file("spec/views/admin/posts/new.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_post, /) }
end

describe 'show' do
subject { file("spec/views/admin/posts/show.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:admin_post, /) }
end
end

describe 'with namespace and --model-name' do
before { run_generator %w[admin/posts --model-name=Post] }

describe 'edit' do
subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:post, post\)/) }
end

describe 'index' do
subject { file("spec/views/admin/posts/index.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:posts, /) }
end

describe 'new' do
subject { file("spec/views/admin/posts/new.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:post, /) }
end

describe 'show' do
subject { file("spec/views/admin/posts/show.html.erb_spec.rb") }
it { is_expected.to exist }
it { is_expected.to contain(/assign\(:post, /) }
end
end

describe 'with --no-template-engine' do
before { run_generator %w[posts --no-template-engine] }
describe 'edit' do
Expand Down