extracted boolean validation logic into stand-alone validator

This commit is contained in:
Hauke Klement
2015-03-18 09:10:58 +01:00
parent 9be0e12990
commit d109663cb9
9 changed files with 40 additions and 7 deletions

View File

@ -1,4 +1,5 @@
require File.expand_path('../../../uploaders/file_uploader', __FILE__)
require File.expand_path('../../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
module CodeOcean
class File < ActiveRecord::Base
@ -35,9 +36,9 @@ module CodeOcean
validates :feedback_message, absence: true, unless: :teacher_defined_test?
validates :file_type_id, 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 :read_only, inclusion: {in: [true, false]}
validates :read_only, boolean_presence: true
validates :role, inclusion: {in: ROLES}
validates :weight, if: :teacher_defined_test?, numericality: true, presence: true
validates :weight, absence: true, unless: :teacher_defined_test?

View File

@ -1,3 +1,5 @@
require File.expand_path('../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
class ExecutionEnvironment < ActiveRecord::Base
include Creation
include DefaultValues
@ -16,7 +18,7 @@ class ExecutionEnvironment < ActiveRecord::Base
validate :working_docker_image?, if: :validate_docker_image?
validates :docker_image, 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 :permitted_execution_time, numericality: {only_integer: true}, presence: true
validates :pool_size, numericality: {only_integer: true}, presence: true

View File

@ -1,3 +1,5 @@
require File.expand_path('../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
class Exercise < ActiveRecord::Base
include Context
include Creation
@ -16,7 +18,7 @@ class Exercise < ActiveRecord::Base
validate :valid_main_file?
validates :description, presence: true
validates :execution_environment_id, presence: true
validates :public, inclusion: {in: [true, false]}
validates :public, boolean_presence: true
validates :title, presence: true
validates :token, presence: true, uniqueness: true

View File

@ -1,3 +1,5 @@
require File.expand_path('../../../lib/active_model/validations/boolean_presence_validator', __FILE__)
class FileType < ActiveRecord::Base
include Creation
include DefaultValues
@ -11,12 +13,12 @@ class FileType < ActiveRecord::Base
has_many :execution_environments
has_many :files
validates :binary, inclusion: {in: [true, false]}
validates :binary, boolean_presence: true
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 :name, presence: true
validates :renderable, inclusion: {in: [true, false]}
validates :renderable, boolean_presence: true
[:audio, :image, :video].each do |type|
define_method("#{type}?") do

View 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

View File

@ -9,6 +9,8 @@ describe CodeOcean::File do
it 'validates the presence of the hidden flag' do
expect(file.errors[:hidden]).to be_present
file.update(hidden: false)
expect(file.errors[:hidden]).to be_blank
end
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
expect(file.errors[:read_only]).to be_present
file.update(read_only: false)
expect(file.errors[:read_only]).to be_blank
end
context 'as a teacher-defined test' do

View File

@ -34,6 +34,8 @@ describe ExecutionEnvironment do
it 'validates the presence of the network enabled flag' do
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
it 'validates the numericality of the permitted run time' do

View File

@ -28,6 +28,8 @@ describe Exercise do
it 'validates the presence of the public flag' do
expect(exercise.errors[:public]).to be_present
exercise.update(public: false)
expect(exercise.errors[:public]).to be_blank
end
it 'validates the presence of a title' do

View File

@ -5,6 +5,8 @@ describe FileType do
it 'validates the presence of the binary flag' do
expect(file_type.errors[:binary]).to be_present
file_type.update(binary: false)
expect(file_type.errors[:binary]).to be_blank
end
context 'when binary' do
@ -33,6 +35,8 @@ describe FileType do
it 'validates the presence of the executable flag' do
expect(file_type.errors[:executable]).to be_present
file_type.update(executable: false)
expect(file_type.errors[:executable]).to be_blank
end
it 'validates the presence of a name' do
@ -41,6 +45,8 @@ describe FileType do
it 'validates the presence of the renderable flag' do
expect(file_type.errors[:renderable]).to be_present
file_type.update(renderable: false)
expect(file_type.errors[:renderable]).to be_blank
end
it 'validates the presence of a user' do