extracted boolean validation logic into stand-alone validator
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
require File.expand_path('../../../uploaders/file_uploader', __FILE__)
|
require File.expand_path('../../../uploaders/file_uploader', __FILE__)
|
||||||
|
require File.expand_path('../../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
|
||||||
|
|
||||||
module CodeOcean
|
module CodeOcean
|
||||||
class File < ActiveRecord::Base
|
class File < ActiveRecord::Base
|
||||||
@ -35,9 +36,9 @@ module CodeOcean
|
|||||||
validates :feedback_message, absence: true, unless: :teacher_defined_test?
|
validates :feedback_message, absence: true, unless: :teacher_defined_test?
|
||||||
validates :file_type_id, presence: true
|
validates :file_type_id, presence: true
|
||||||
validates :hashed_content, if: :content_present?, presence: true
|
validates :hashed_content, if: :content_present?, presence: true
|
||||||
validates :hidden, inclusion: {in: [true, false]}
|
validates :hidden, boolean_presence: true
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
validates :read_only, inclusion: {in: [true, false]}
|
validates :read_only, boolean_presence: true
|
||||||
validates :role, inclusion: {in: ROLES}
|
validates :role, inclusion: {in: ROLES}
|
||||||
validates :weight, if: :teacher_defined_test?, numericality: true, presence: true
|
validates :weight, if: :teacher_defined_test?, numericality: true, presence: true
|
||||||
validates :weight, absence: true, unless: :teacher_defined_test?
|
validates :weight, absence: true, unless: :teacher_defined_test?
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
require File.expand_path('../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
|
||||||
|
|
||||||
class ExecutionEnvironment < ActiveRecord::Base
|
class ExecutionEnvironment < ActiveRecord::Base
|
||||||
include Creation
|
include Creation
|
||||||
include DefaultValues
|
include DefaultValues
|
||||||
@ -16,7 +18,7 @@ class ExecutionEnvironment < ActiveRecord::Base
|
|||||||
validate :working_docker_image?, if: :validate_docker_image?
|
validate :working_docker_image?, if: :validate_docker_image?
|
||||||
validates :docker_image, presence: true
|
validates :docker_image, presence: true
|
||||||
validates :memory_limit, numericality: {greater_than_or_equal_to: DockerClient::MINIMUM_MEMORY_LIMIT, only_integer: true}, presence: true
|
validates :memory_limit, numericality: {greater_than_or_equal_to: DockerClient::MINIMUM_MEMORY_LIMIT, only_integer: true}, presence: true
|
||||||
validates :network_enabled, inclusion: {in: [true, false]}
|
validates :network_enabled, boolean_presence: true
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
validates :permitted_execution_time, numericality: {only_integer: true}, presence: true
|
validates :permitted_execution_time, numericality: {only_integer: true}, presence: true
|
||||||
validates :pool_size, numericality: {only_integer: true}, presence: true
|
validates :pool_size, numericality: {only_integer: true}, presence: true
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
require File.expand_path('../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
|
||||||
|
|
||||||
class Exercise < ActiveRecord::Base
|
class Exercise < ActiveRecord::Base
|
||||||
include Context
|
include Context
|
||||||
include Creation
|
include Creation
|
||||||
@ -16,7 +18,7 @@ class Exercise < ActiveRecord::Base
|
|||||||
validate :valid_main_file?
|
validate :valid_main_file?
|
||||||
validates :description, presence: true
|
validates :description, presence: true
|
||||||
validates :execution_environment_id, presence: true
|
validates :execution_environment_id, presence: true
|
||||||
validates :public, inclusion: {in: [true, false]}
|
validates :public, boolean_presence: true
|
||||||
validates :title, presence: true
|
validates :title, presence: true
|
||||||
validates :token, presence: true, uniqueness: true
|
validates :token, presence: true, uniqueness: true
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
require File.expand_path('../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
|
||||||
|
|
||||||
class FileType < ActiveRecord::Base
|
class FileType < ActiveRecord::Base
|
||||||
include Creation
|
include Creation
|
||||||
include DefaultValues
|
include DefaultValues
|
||||||
@ -11,12 +13,12 @@ class FileType < ActiveRecord::Base
|
|||||||
has_many :execution_environments
|
has_many :execution_environments
|
||||||
has_many :files
|
has_many :files
|
||||||
|
|
||||||
validates :binary, inclusion: {in: [true, false]}
|
validates :binary, boolean_presence: true
|
||||||
validates :editor_mode, presence: true, unless: :binary?
|
validates :editor_mode, presence: true, unless: :binary?
|
||||||
validates :executable, inclusion: {in: [true, false]}
|
validates :executable, boolean_presence: true
|
||||||
validates :indent_size, presence: true, unless: :binary?
|
validates :indent_size, presence: true, unless: :binary?
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
validates :renderable, inclusion: {in: [true, false]}
|
validates :renderable, boolean_presence: true
|
||||||
|
|
||||||
[:audio, :image, :video].each do |type|
|
[:audio, :image, :video].each do |type|
|
||||||
define_method("#{type}?") do
|
define_method("#{type}?") do
|
||||||
|
12
lib/active_model/validations/boolean_presence_validator.rb
Normal file
12
lib/active_model/validations/boolean_presence_validator.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module ActiveModel
|
||||||
|
module Validations
|
||||||
|
class BooleanPresenceValidator < EachValidator
|
||||||
|
def validate(record)
|
||||||
|
[attributes].flatten.each do |attribute|
|
||||||
|
value = record.send(:read_attribute_for_validation, attribute)
|
||||||
|
record.errors.add(attribute, nil, options) unless [false, true].include?(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -9,6 +9,8 @@ describe CodeOcean::File do
|
|||||||
|
|
||||||
it 'validates the presence of the hidden flag' do
|
it 'validates the presence of the hidden flag' do
|
||||||
expect(file.errors[:hidden]).to be_present
|
expect(file.errors[:hidden]).to be_present
|
||||||
|
file.update(hidden: false)
|
||||||
|
expect(file.errors[:hidden]).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'validates the presence of a name' do
|
it 'validates the presence of a name' do
|
||||||
@ -17,6 +19,8 @@ describe CodeOcean::File do
|
|||||||
|
|
||||||
it 'validates the presence of the read-only flag' do
|
it 'validates the presence of the read-only flag' do
|
||||||
expect(file.errors[:read_only]).to be_present
|
expect(file.errors[:read_only]).to be_present
|
||||||
|
file.update(read_only: false)
|
||||||
|
expect(file.errors[:read_only]).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'as a teacher-defined test' do
|
context 'as a teacher-defined test' do
|
||||||
|
@ -34,6 +34,8 @@ describe ExecutionEnvironment do
|
|||||||
|
|
||||||
it 'validates the presence of the network enabled flag' do
|
it 'validates the presence of the network enabled flag' do
|
||||||
expect(execution_environment.errors[:network_enabled]).to be_present
|
expect(execution_environment.errors[:network_enabled]).to be_present
|
||||||
|
execution_environment.update(network_enabled: false)
|
||||||
|
expect(execution_environment.errors[:network_enabled]).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'validates the numericality of the permitted run time' do
|
it 'validates the numericality of the permitted run time' do
|
||||||
|
@ -28,6 +28,8 @@ describe Exercise do
|
|||||||
|
|
||||||
it 'validates the presence of the public flag' do
|
it 'validates the presence of the public flag' do
|
||||||
expect(exercise.errors[:public]).to be_present
|
expect(exercise.errors[:public]).to be_present
|
||||||
|
exercise.update(public: false)
|
||||||
|
expect(exercise.errors[:public]).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'validates the presence of a title' do
|
it 'validates the presence of a title' do
|
||||||
|
@ -5,6 +5,8 @@ describe FileType do
|
|||||||
|
|
||||||
it 'validates the presence of the binary flag' do
|
it 'validates the presence of the binary flag' do
|
||||||
expect(file_type.errors[:binary]).to be_present
|
expect(file_type.errors[:binary]).to be_present
|
||||||
|
file_type.update(binary: false)
|
||||||
|
expect(file_type.errors[:binary]).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when binary' do
|
context 'when binary' do
|
||||||
@ -33,6 +35,8 @@ describe FileType do
|
|||||||
|
|
||||||
it 'validates the presence of the executable flag' do
|
it 'validates the presence of the executable flag' do
|
||||||
expect(file_type.errors[:executable]).to be_present
|
expect(file_type.errors[:executable]).to be_present
|
||||||
|
file_type.update(executable: false)
|
||||||
|
expect(file_type.errors[:executable]).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'validates the presence of a name' do
|
it 'validates the presence of a name' do
|
||||||
@ -41,6 +45,8 @@ describe FileType do
|
|||||||
|
|
||||||
it 'validates the presence of the renderable flag' do
|
it 'validates the presence of the renderable flag' do
|
||||||
expect(file_type.errors[:renderable]).to be_present
|
expect(file_type.errors[:renderable]).to be_present
|
||||||
|
file_type.update(renderable: false)
|
||||||
|
expect(file_type.errors[:renderable]).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'validates the presence of a user' do
|
it 'validates the presence of a user' do
|
||||||
|
Reference in New Issue
Block a user