mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make generated test work even when using virtual attributes
The virtual attributes(`attachment` and `rich_text`) can't set value with `fill_in`. So avoid using it. Once #35885 is merged, will be modified to use it. Also, add checking attachment attached or not for avoiding `DelegationError` when attachment didn't attach.
This commit is contained in:
parent
ac1ba44f8d
commit
85a8bc644b
6 changed files with 56 additions and 7 deletions
|
@ -4,7 +4,7 @@
|
|||
<p>
|
||||
<strong><%= attribute.human_name %>:</strong>
|
||||
<% if attribute.attachment? -%>
|
||||
<%%= link_to @<%= singular_table_name %>.<%= attribute.column_name %>.filename, @<%= singular_table_name %>.<%= attribute.column_name %> %>
|
||||
<%%= link_to @<%= singular_table_name %>.<%= attribute.column_name %>.filename, @<%= singular_table_name %>.<%= attribute.column_name %> if @<%= singular_table_name %>.<%= attribute.column_name %>.attached? %>
|
||||
<% elsif attribute.attachments? -%>
|
||||
<%% @<%= singular_table_name %>.<%= attribute.column_name %>.each do |<%= attribute.singular_name %>| %>
|
||||
<div><%%= link_to <%= attribute.singular_name %>.filename, <%= attribute.singular_name %> %></div>
|
||||
|
|
|
@ -187,7 +187,6 @@ module Rails
|
|||
|
||||
def attributes_names # :doc:
|
||||
@attributes_names ||= attributes.each_with_object([]) do |a, names|
|
||||
next if a.attachments?
|
||||
names << a.column_name
|
||||
names << "password_confirmation" if a.password_digest?
|
||||
names << "#{a.name}_type" if a.polymorphic?
|
||||
|
|
|
@ -36,9 +36,15 @@ module Rails
|
|||
private
|
||||
|
||||
def permitted_params
|
||||
params = attributes_names.map { |name| ":#{name}" }.join(", ")
|
||||
params += attributes.select(&:attachments?).map { |a| ", #{a.name}: []" }.join
|
||||
params
|
||||
attachments, others = attributes_names.partition { |name| attachments?(name) }
|
||||
params = others.map { |name| ":#{name}" }
|
||||
params += attachments.map { |name| "#{name}: []" }
|
||||
params.join(", ")
|
||||
end
|
||||
|
||||
def attachments?(name)
|
||||
attribute = attributes.find { |attr| attr.name == name }
|
||||
attribute&.attachments?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -49,16 +49,21 @@ module TestUnit # :nodoc:
|
|||
attributes_names.map do |name|
|
||||
if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?)
|
||||
["#{name}", "'secret'"]
|
||||
else
|
||||
elsif !virtual?(name)
|
||||
["#{name}", "@#{singular_table_name}.#{name}"]
|
||||
end
|
||||
end.sort.to_h
|
||||
end.compact.sort.to_h
|
||||
end
|
||||
|
||||
def boolean?(name)
|
||||
attribute = attributes.find { |attr| attr.name == name }
|
||||
attribute&.type == :boolean
|
||||
end
|
||||
|
||||
def virtual?(name)
|
||||
attribute = attributes.find { |attr| attr.name == name }
|
||||
attribute&.virtual?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -89,6 +89,15 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_controller_permit_attachments_attributes_only
|
||||
run_generator ["Message", "photos:attachments"]
|
||||
|
||||
assert_file "app/controllers/messages_controller.rb" do |content|
|
||||
assert_match(/def message_params/, content)
|
||||
assert_match(/params\.require\(:message\)\.permit\(photos: \[\]\)/, content)
|
||||
end
|
||||
end
|
||||
|
||||
def test_helper_are_invoked_with_a_pluralized_name
|
||||
run_generator
|
||||
assert_file "app/helpers/users_helper.rb", /module UsersHelper/
|
||||
|
|
|
@ -487,6 +487,36 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
|
|||
assert_match(/^\W{4}<%= form\.file_field :video %>/, content)
|
||||
assert_match(/^\W{4}<%= form\.file_field :photos, multiple: true %>/, content)
|
||||
end
|
||||
|
||||
assert_file "app/views/messages/show.html.erb" do |content|
|
||||
assert_match(/^\W{2}<%= link_to @message\.video\.filename, @message\.video if @message\.video\.attached\? %>/, content)
|
||||
assert_match(/^\W{4}<div><%= link_to photo\.filename, photo %>/, content)
|
||||
end
|
||||
|
||||
assert_file "test/system/messages_test.rb" do |content|
|
||||
assert_no_match(/fill_in "Video"/, content)
|
||||
assert_no_match(/fill_in "Photos"/, content)
|
||||
end
|
||||
end
|
||||
|
||||
def test_scaffold_generator_rich_text
|
||||
run_generator ["message", "content:rich_text"]
|
||||
|
||||
assert_file "app/models/message.rb", /rich_text :content/
|
||||
|
||||
assert_file "app/controllers/messages_controller.rb" do |content|
|
||||
assert_instance_method :message_params, content do |m|
|
||||
assert_match(/permit\(:content\)/, m)
|
||||
end
|
||||
end
|
||||
|
||||
assert_file "app/views/messages/_form.html.erb" do |content|
|
||||
assert_match(/^\W{4}<%= form\.rich_text_area :content %>/, content)
|
||||
end
|
||||
|
||||
assert_file "test/system/messages_test.rb" do |content|
|
||||
assert_no_match(/fill_in "Content"/, content)
|
||||
end
|
||||
end
|
||||
|
||||
def test_scaffold_generator_database
|
||||
|
|
Loading…
Reference in a new issue