From bd98a2cbb8796039efbffb7039a9bda1cde22dae Mon Sep 17 00:00:00 2001 From: ankonym Date: Thu, 13 Aug 2015 15:29:04 +0200 Subject: Add validation of invite code in user object based on codes in couch db --- app/models/invite_code_validator.rb | 16 ++++++++++++++++ app/models/user.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 app/models/invite_code_validator.rb (limited to 'app/models') diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb new file mode 100644 index 0000000..55b7a74 --- /dev/null +++ b/app/models/invite_code_validator.rb @@ -0,0 +1,16 @@ +class InviteCodeValidator < ActiveModel::Validator + def validate(user) + if not_existent?(user.invite_code) + add_error_to_user("This is not a valid code", user) + end + end + + private + def not_existent?(code) + InviteCode.find_by__id(code) == nil + end + + def add_error_to_user(error, user) + user.errors[:invite_code] << error + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 5510470..06e1a0f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -39,7 +39,7 @@ class User < CouchRest::Model::Base :email => true, :mx_with_fallback => true - validates :invite_code, inclusion: { in: ["testcode"], message: "%{value} is not a valid invite code" } + validates_with InviteCodeValidator timestamps! -- cgit v1.2.3