-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct "required" asterisk when using validation option
:on
(#1872)
We show a "required" asterisk when fields are required. However there are some edge cases, like when the requirement has an if/unless. We don't consider those actually required and we don't show the asterisk there. But there are other edge cases. Issue #1860 shows how the validation option :on can also create cases where an apparent requirement is not actually a requirement. Normally this option has the values :create or :update, but there are other possibilities. This PR tries to solve the issue. There are three commits, each an incremental improvement: 1. Consider :on to be the same as :if and :unless, and hide the asterisk. 2. Refactor specs to avoid the mystery guests involved in the examples for :if, :unless, and :on. This also allows for more scalable specs, should we decide to add more fine-grained handling of these options. 3. Consider the special (and most common) cases of on: :create and on: :update, and hide the asterisk for any other :on value. Fixes #1860
- Loading branch information
Showing
5 changed files
with
194 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
require "administrate/field/url" | ||
require "active_record" | ||
|
||
describe Administrate::Field::Base do | ||
let(:field_class) { Class.new(Administrate::Field::Base) } | ||
|
||
describe "required?" do | ||
it "is false by default" do | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(false) | ||
end | ||
|
||
it "is true on an unconditional requirement for a value" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
options: {}, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(true) | ||
end | ||
|
||
it "is false on a conditional requirement for a value (with :if)" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
if: -> { true }, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(false) | ||
end | ||
|
||
it "is false on a conditional requirement for a value (with :unless)" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
unless: -> { true }, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(false) | ||
end | ||
|
||
it "is true for an unpersisted record in only required on create" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
on: :create, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
persisted?: false, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(true) | ||
end | ||
|
||
it "is false for a persisted record if only required on create" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
on: :create, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
persisted?: true, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(false) | ||
end | ||
|
||
it "is true for a persisted record in only required on update" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
on: :update, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
persisted?: true, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(true) | ||
end | ||
|
||
it "is false for a persisted record in only required on update" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
on: :update, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
persisted?: false, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(false) | ||
end | ||
|
||
it "is false when required only on unstandard situations" do | ||
validator = ActiveRecord::Validations::PresenceValidator.new( | ||
attributes: [:foo], | ||
on: :some_situation_or_the_other, | ||
) | ||
resource_class = class_double( | ||
"ActiveRecord::Base", | ||
validators_on: [validator], | ||
) | ||
resource = instance_double( | ||
"ActiveRecord::Base", | ||
class: resource_class, | ||
) | ||
field = field_class.new(:attribute, :date, :page, resource: resource) | ||
|
||
expect(field.required?).to eq(false) | ||
end | ||
end | ||
end |