-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ActiveModel types should only cast and not validate
- Loading branch information
1 parent
de5a832
commit 1a2303e
Showing
11 changed files
with
36 additions
and
119 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 was deleted.
Oops, something went wrong.
17 changes: 2 additions & 15 deletions
17
...ce_tasks/parameters/integer_array_type.rb → ...ntenance_tasks/task/integer_array_type.rb
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 |
---|---|---|
@@ -1,31 +1,18 @@ | ||
# frozen_string_literal: true | ||
module MaintenanceTasks | ||
module Parameters | ||
class Task | ||
# Type class representing an array of integers. Tasks using the Attributes | ||
# API for parameter support can use this class to turn input from the UI | ||
# into an array of integers within their Task. | ||
class IntegerArrayType < ActiveModel::Type::Value | ||
# Casts string from form input field to an array of integers after | ||
# validating that the input is correct. | ||
# Casts string from form input field to an array of integers. | ||
# | ||
# @param input [String] the value to cast to an array of integers. | ||
# @return [Array<Integer>] the data cast as an array of integers. | ||
def cast(input) | ||
return unless input.present? | ||
|
||
validate_input(input) | ||
input.split(",").map(&:to_i) | ||
end | ||
|
||
private | ||
|
||
def validate_input(input) | ||
unless /\A(\s?\d+\s?(,\s?\d+\s?)*\s?)\z/.match?(input) | ||
error_message = "#{self.class} expects a " \ | ||
"comma-delimited string of integers. Input received: #{input}" | ||
raise TypeError, error_message | ||
end | ||
end | ||
end | ||
end | ||
end |
17 changes: 2 additions & 15 deletions
17
...nce_tasks/parameters/string_array_type.rb → ...intenance_tasks/task/string_array_type.rb
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 |
---|---|---|
@@ -1,31 +1,18 @@ | ||
# frozen_string_literal: true | ||
module MaintenanceTasks | ||
module Parameters | ||
class Task | ||
# Type class representing an array of strings. Tasks using the Attributes | ||
# API for parameter support can use this class to turn input from the UI | ||
# into an array of strings within their Task. | ||
class StringArrayType < ActiveModel::Type::Value | ||
# Casts string from form input field to an array of strings after | ||
# validating that the input is correct. | ||
# Casts string from form input field to an array of strings. | ||
# | ||
# @param input [String] the value to cast to an array of strings. | ||
# @return [Array<Integer>] the data cast as an array of strings. | ||
def cast(input) | ||
return unless input.present? | ||
|
||
validate_input(input) | ||
input.split(",") | ||
end | ||
|
||
private | ||
|
||
def validate_input(input) | ||
unless /\A(\s?\w+\s?(,\s?\w+\s?)*)\z/.match?(input) | ||
error_message = "#{self.class} expects alphanumeric, "\ | ||
"comma-delimited string. Input received: #{input}" | ||
raise TypeError, error_message | ||
end | ||
end | ||
end | ||
end | ||
end |
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
34 changes: 0 additions & 34 deletions
34
test/models/maintenance_tasks/parameters/string_array_type_test.rb
This file was deleted.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
test/models/maintenance_tasks/task/string_array_type_test.rb
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,16 @@ | ||
# frozen_string_literal: true | ||
require "test_helper" | ||
|
||
module MaintenanceTasks | ||
class Task | ||
class StringArrayTypeTest < ActiveSupport::TestCase | ||
test "#cast returns nil if input not present" do | ||
assert_nil StringArrayType.new.cast("") | ||
end | ||
|
||
test "#cast converts string to array of strings" do | ||
assert_equal ["abc", "def"], StringArrayType.new.cast("abc,def") | ||
end | ||
end | ||
end | ||
end |
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