From 5e7a5b57de0f46b6e2655ca6f802b3ed616d0b17 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 9 Sep 2015 18:32:36 +0200 Subject: [PATCH] Adds max lengths and validations in models --- app/models/comment.rb | 4 ++++ app/models/debate.rb | 9 +++++++-- app/models/organization.rb | 4 ++++ app/models/user.rb | 7 +++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index efa9ec7a0..657994d8e 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,4 +1,6 @@ class Comment < ActiveRecord::Base + BODY_LENGTH = {minimum: 10, maximum: 2000} + acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases acts_as_votable @@ -10,6 +12,8 @@ class Comment < ActiveRecord::Base validates :user, presence: true validates_inclusion_of :commentable_type, in: ["Debate"] + validates :body, length: BODY_LENGTH + belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true belongs_to :user, -> { with_hidden } diff --git a/app/models/debate.rb b/app/models/debate.rb index f3443dc2e..e976bcef9 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,7 +1,9 @@ require 'numeric' class Debate < ActiveRecord::Base + TITLE_LENGTH = {minimum: 10, maximum: Debate.columns.find { |c| c.name == 'title' }.limit || 80 } + DESCRIPTION_LENGTH = {minimum: 10, maximum: 6000} + apply_simple_captcha - TITLE_LENGTH = Debate.columns.find { |c| c.name == 'title' }.limit acts_as_votable acts_as_taggable @@ -16,6 +18,9 @@ class Debate < ActiveRecord::Base validates :description, presence: true validates :author, presence: true + validates :title, length: TITLE_LENGTH + validates :description, length: DESCRIPTION_LENGTH + validates :terms_of_service, acceptance: { allow_nil: false }, on: :create before_validation :sanitize_description @@ -148,6 +153,6 @@ class Debate < ActiveRecord::Base end def sanitize_tag_list - self.tag_list = TagSanitizer.new.sanitize_tag_list(self.tag_list) + #self.tag_list = TagSanitizer.new.sanitize_tag_list(self.tag_list) end end diff --git a/app/models/organization.rb b/app/models/organization.rb index 2962724a6..4c068cc4a 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -1,7 +1,11 @@ class Organization < ActiveRecord::Base + NAME_LENGTH = {maximum: Organization.columns.find { |c| c.name == 'name' }.limit || 60} + belongs_to :user, touch: true validates :name, presence: true + validates :name, length: NAME_LENGTH + validates :name, uniqueness: true delegate :email, :phone_number, to: :user diff --git a/app/models/user.rb b/app/models/user.rb index a806de7c3..68274caa5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,9 +1,11 @@ class User < ActiveRecord::Base - include Verification - OMNIAUTH_EMAIL_PREFIX = 'omniauth@participacion' OMNIAUTH_EMAIL_REGEX = /\A#{OMNIAUTH_EMAIL_PREFIX}/ + USERNAME_LENGTH = {maximum: User.columns.find { |c| c.name == 'username' }.limit || 60} + + include Verification + apply_simple_captcha devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :async @@ -23,6 +25,7 @@ class User < ActiveRecord::Base validates :username, presence: true, unless: :organization? validates :username, uniqueness: true, unless: :organization? + validates :username, length: USERNAME_LENGTH validates :official_level, inclusion: {in: 0..5} validates_format_of :email, without: OMNIAUTH_EMAIL_REGEX, on: :update validates :terms_of_service, acceptance: { allow_nil: false }, on: :create