Skip to content

Commit

Permalink
Merge pull request #16487 from d-m-u/unique_dialog_field_names
Browse files Browse the repository at this point in the history
Adds field unique validator check to dialog
(cherry picked from commit ece6e89)

https://bugzilla.redhat.com/show_bug.cgi?id=1518267
  • Loading branch information
gmcculloug authored and simaishi committed Nov 28, 2017
1 parent e316988 commit 89ebd84
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/models/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def validate_children
errors.add(:base, _("Dialog %{dialog_label} must have at least one Tab") % {:dialog_label => label})
end

duplicate_field_names = dialog_fields.collect(&:name).duplicates
if duplicate_field_names.present?
errors.add(:base, _("Dialog field name cannot be duplicated on a dialog: %{duplicates}") % {:duplicates => duplicate_field_names.join(', ')})
end

dialog_tabs.each do |dt|
next if dt.valid?
dt.errors.full_messages.each do |err_msg|
Expand Down
21 changes: 21 additions & 0 deletions spec/models/dialog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,27 @@
expect { dialog.save! }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Dialog #{dialog.label} must have at least one Tab")
end

context "unique field names" do
before do
dialog.dialog_tabs << FactoryGirl.create(:dialog_tab, :label => 'tab')
dialog.dialog_tabs.first.dialog_groups << FactoryGirl.create(:dialog_group, :label => 'group')
dialog.dialog_tabs.first.dialog_groups.first.dialog_fields << FactoryGirl.create(:dialog_field, :label => 'field 1', :name => 'field1')
end

it "fails with two identical field names on different groups" do
dialog.dialog_tabs.first.dialog_groups << FactoryGirl.create(:dialog_group, :label => 'group2')
dialog.dialog_tabs.first.dialog_groups.last.dialog_fields << FactoryGirl.create(:dialog_field, :label => 'field 3', :name => 'field1')
expect { dialog.save! }
.to raise_error(ActiveRecord::RecordInvalid, /Dialog field name cannot be duplicated on a dialog: field1/)
end

it "fails with two identical field names on same group" do
dialog.dialog_tabs.first.dialog_groups.first.dialog_fields << FactoryGirl.create(:dialog_field, :label => 'field 3', :name => 'field1')
expect { dialog.save! }
.to raise_error(ActiveRecord::RecordInvalid, /Dialog field name cannot be duplicated on a dialog: field1/)
end
end

it "validates with tab" do
dialog.dialog_tabs << FactoryGirl.create(:dialog_tab, :label => 'tab')
expect_any_instance_of(DialogTab).to receive(:valid?)
Expand Down

0 comments on commit 89ebd84

Please sign in to comment.