From 4082dbed5bcb1908f544dd28299ecbf06eba44b9 Mon Sep 17 00:00:00 2001 From: sw <939547590@qq.com> Date: Wed, 15 Oct 2014 11:08:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E9=83=A8=E5=88=86mo?= =?UTF-8?q?del=E7=9A=84validate(custon=5Ffield.rb=E4=B9=8B=E4=B8=8A)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/activity.rb | 5 +++-- app/models/attachment.rb | 8 ++++---- app/models/auth_source.rb | 4 +--- app/models/auth_source_ldap.rb | 20 +++++++++++++------- app/models/bid.rb | 18 ++++++++---------- app/models/biding_project.rb | 8 ++++---- app/models/board.rb | 5 ++--- app/models/bug_to_osp.rb | 5 ++--- app/models/change.rb | 4 +++- app/models/changeset.rb | 9 +++++---- app/models/comment.rb | 2 -- app/models/contest.rb | 12 +++++------- app/models/contest_notification.rb | 2 +- app/models/contesting_project.rb | 8 ++++---- app/models/contestnotification.rb | 10 +++------- app/models/course.rb | 9 ++++----- app/models/course_status.rb | 4 +--- app/models/web_footer_company.rb | 8 +++++--- 18 files changed, 68 insertions(+), 73 deletions(-) diff --git a/app/models/activity.rb b/app/models/activity.rb index 635171653..4676ca7b8 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -2,6 +2,7 @@ class Activity < ActiveRecord::Base attr_accessible :act_id, :act_type, :user_id belongs_to :act, :polymorphic => true belongs_to :user - - validates_presence_of :act_id, :act_type, :user_id + validates :act_id, presence: true + validates :act_type, presence: true + validates :user_id, presence: true end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 90cfed429..891ea269a 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -28,10 +28,10 @@ class Attachment < ActiveRecord::Base include UserScoreHelper - validates_presence_of :filename, :author - validates_length_of :filename, :maximum => 254 - validates_length_of :disk_filename, :maximum => 254 - validates_length_of :description, :maximum => 254 + validates :filename, presence: true, length: {maximum: 254} + validates :author, presence: true + validates :disk_filename, length: {maximum: 254} + validates :description, length: {maximum: 254} validate :validate_max_file_size diff --git a/app/models/auth_source.rb b/app/models/auth_source.rb index 0b4db9bcc..fdaf622b3 100644 --- a/app/models/auth_source.rb +++ b/app/models/auth_source.rb @@ -26,9 +26,7 @@ class AuthSource < ActiveRecord::Base has_many :users - validates_presence_of :name - validates_uniqueness_of :name - validates_length_of :name, :maximum => 60 + validates :name, presence: true, uniqueness: true, length: {maximum: 60} def authenticate(login, password) end diff --git a/app/models/auth_source_ldap.rb b/app/models/auth_source_ldap.rb index 25cdc6f1b..8eae422e5 100644 --- a/app/models/auth_source_ldap.rb +++ b/app/models/auth_source_ldap.rb @@ -20,14 +20,20 @@ require 'net/ldap/dn' require 'timeout' class AuthSourceLdap < AuthSource - validates_presence_of :host, :port, :attr_login - validates_length_of :name, :host, :maximum => 60, :allow_nil => true - validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true - validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true - validates_numericality_of :port, :only_integer => true - validates_numericality_of :timeout, :only_integer => true, :allow_blank => true + validates :host, presence: true, length: {maximum: 60, allow_nil: true} + validates :port, presence: true, numericality: {only_integer: true} + validates :attr_login, presence: true + validates :name, length: {maximum: 60, allow_nil: true} + validates :account, length: {maximum: 255, allow_blank: true} + validates :account_password, length: {maximum: 255, allow_blank: true} + validates :base_dn, length: {maximum: 255, allow_blank: true} + validates :filter, length: {maximum: 255, allow_blank: true} + validates :attr_login, length: {maximum: 30, allow_nil: true} + validates :attr_firstname, length: {maximum: 30, allow_nil: true} + validates :attr_lastname, length: {maximum: 30, allow_nil: true} + validates :attr_mail, length: {maximum: 30, allow_nil: true} + validates :timeout, numericality: { only_integer: true, allow_blank: true} validate :validate_filter - before_validation :strip_ldap_attributes def initialize(attributes=nil, *args) diff --git a/app/models/bid.rb b/app/models/bid.rb index f65d71fa8..038c45cd5 100644 --- a/app/models/bid.rb +++ b/app/models/bid.rb @@ -37,16 +37,14 @@ class Bid < ActiveRecord::Base NAME_LENGTH_LIMIT = 60 DESCRIPTION_LENGTH_LIMIT = 250 - - validates_length_of :name, :maximum => NAME_LENGTH_LIMIT - validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT - validates_presence_of :author_id, :name, :deadline - # validates_presence_of :deadline, :message => 'test' - # validates_format_of :deadline, :with => - validates_format_of :deadline, :with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/ - validates_format_of :budget, :with => /^(\d+)$|^(\d+).([0-9]{2})|^(\d+).([0-9]{1})$/, - :if => Proc.new { |p| p.reward_type == 1 } - validates_format_of :budget, :with => /^(\d+)$|^(\d+).([0-9]{1})$/, :if => Proc.new { |p| p.reward_type == 3 } + validates :name, length: {maximum: NAME_LENGTH_LIMIT}, presence: true + validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT} + validates :author_id, presence: true + validates :deadline, presence: true, format: {:with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/} + validates :name, length: {maximum: NAME_LENGTH_LIMIT} + validates :budget, + format: {with : /^(\d+)$|^(\d+).([0-9]{2})|^(\d+).([0-9]{1})$/, if: Proc.new { |p| p.reward_type == 1 }}, + format: {with: /^(\d+)$|^(\d+).([0-9]{1})$/, if: Proc.new { |p| p.reward_type == 3 }} validate :validate_user validate :validate_reward_type after_create :act_as_activity diff --git a/app/models/biding_project.rb b/app/models/biding_project.rb index 0b7b3718e..9ebd1b73c 100644 --- a/app/models/biding_project.rb +++ b/app/models/biding_project.rb @@ -7,13 +7,13 @@ class BidingProject < ActiveRecord::Base belongs_to :user DESCRIPTION_LENGTH_LIMIT = 500 - - validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT - validates_presence_of :user_id, :bid_id, :project_id + validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT} + validates :user_id, presence: true + validates :bid_id, presence: true, :uniqueness => { :scope => :project_id} + validates :project_id, presence: true validate :validate_user validate :validate_bid validate :validate_project - validates_uniqueness_of :bid_id, :scope => :project_id def self.cerate_bidding(bid_id, project_id, description = nil) self.create(:user_id => User.current.id, :bid_id => bid_id, diff --git a/app/models/board.rb b/app/models/board.rb index b51790e19..0d8c21ab2 100644 --- a/app/models/board.rb +++ b/app/models/board.rb @@ -26,9 +26,8 @@ class Board < ActiveRecord::Base acts_as_list :scope => '(project_id = #{project_id} AND parent_id #{parent_id ? "= #{parent_id}" : "IS NULL"})' acts_as_watchable - validates_presence_of :name, :description - validates_length_of :name, :maximum => 30 - validates_length_of :description, :maximum => 255 + validates :name, presence: true, length: {maximum: 30} + validates :description, presence: true, length: {maximum: 255} validate :validate_board scope :visible, lambda {|*args| diff --git a/app/models/bug_to_osp.rb b/app/models/bug_to_osp.rb index 9cd1d7359..3d93957cf 100644 --- a/app/models/bug_to_osp.rb +++ b/app/models/bug_to_osp.rb @@ -2,11 +2,10 @@ class BugToOsp < ActiveRecord::Base # attr_accessible :title, :body belongs_to :open_source_project, :foreign_key => "osp_id" belongs_to :bug, :class_name => 'RelativeMemo', :foreign_key => "relative_memo_id" - - validates_presence_of :osp_id, :relative_memo_id + validates :osp_id, presence: true + validates :relative_memo_id, presence: true scope :visible, lambda {|*args| nil } - end diff --git a/app/models/change.rb b/app/models/change.rb index b80b96eb9..304a2fb59 100644 --- a/app/models/change.rb +++ b/app/models/change.rb @@ -18,7 +18,9 @@ class Change < ActiveRecord::Base belongs_to :changeset - validates_presence_of :changeset_id, :action, :path + validates :changeset_id, presence: true + validates :action, presence: true + validates :path, presence: true before_save :init_path before_validation :replace_invalid_utf8_of_path diff --git a/app/models/changeset.rb b/app/models/changeset.rb index b9ce4acf5..5a617b790 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -54,10 +54,11 @@ class Changeset < ActiveRecord::Base acts_as_activity_provider :timestamp => "#{table_name}.committed_on", :author_key => :user_id, :find_options => {:include => [:user, {:repository => :project}]} - - validates_presence_of :repository_id, :revision, :committed_on, :commit_date - validates_uniqueness_of :revision, :scope => :repository_id - validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true + validates :repository_id, presence: true + validates :revision, presence: true, uniqueness: {scope: :repository_id} + validates :committed_on, presence: true + validates :commit_date, presence: true + validates :scmid, uniqueness: {scope: :repository_id, allow_nil: true} scope :visible, lambda {|*args| includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args)) diff --git a/app/models/comment.rb b/app/models/comment.rb index 577afe942..f261fcb9e 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -19,8 +19,6 @@ class Comment < ActiveRecord::Base include Redmine::SafeAttributes belongs_to :commented, :polymorphic => true, :counter_cache => true belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' - validates_presence_of :commented, :author, :comments - safe_attributes 'comments' end diff --git a/app/models/contest.rb b/app/models/contest.rb index b2c11ceac..59f8ee470 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -21,13 +21,11 @@ class Contest < ActiveRecord::Base NAME_LENGTH_LIMIT = 60 DESCRIPTION_LENGTH_LIMIT = 250 - - validates_length_of :name, :maximum => NAME_LENGTH_LIMIT - validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT - validates_presence_of :author_id, :name, :budget - #validates_format_of :deadline, :with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/ - validates_format_of :deadline, :with =>/^[1-9][0-9]{3}\-0?[1-9]|1[12]\-0?[1-9]|[12]\d|3[01]$/ - # validates_format_of :budget, :with => /^(\d+)$|^(\d+).([0-9]{2})|^(\d+).([0-9]{1})$/ + validates :name, length: {maximum: NAME_LENGTH_LIMIT}, presence: true + validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT} + validates :author_id, presence: true + validates :budget, presence: true + validates :deadline, format: {:with =>/^[1-9][0-9]{3}\-0?[1-9]|1[12]\-0?[1-9]|[12]\d|3[01]$/} validate :validate_user after_create :act_as_activity diff --git a/app/models/contest_notification.rb b/app/models/contest_notification.rb index d525b4b3a..79d8bc254 100644 --- a/app/models/contest_notification.rb +++ b/app/models/contest_notification.rb @@ -1,4 +1,4 @@ class ContestNotification < ActiveRecord::Base attr_accessible :content, :title - validates_length_of :title, maximum: 30 + validates :title, length: {maximum: 30} end diff --git a/app/models/contesting_project.rb b/app/models/contesting_project.rb index 641990b8a..3db6d6f86 100644 --- a/app/models/contesting_project.rb +++ b/app/models/contesting_project.rb @@ -6,13 +6,13 @@ class ContestingProject < ActiveRecord::Base belongs_to :user DESCRIPTION_LENGTH_LIMIT = 500 - - validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT - validates_presence_of :user_id, :contest_id, :project_id + validates :description, length: {maximum:DESCRIPTION_LENGTH_LIMIT } + validates :user_id, presence: true + validates :contest_id, presence: true, uniqueness: {:scope => :project_id} + validates :project_id, presence: true validate :validate_user validate :validate_contest validate :validate_project - validates_uniqueness_of :contest_id, :scope => :project_id def self.create_contesting(contest_id, project_id, description = nil) self.create(:user_id => User.current.id, :contest_id => contest_id, diff --git a/app/models/contestnotification.rb b/app/models/contestnotification.rb index 0bda49548..fff861ab4 100644 --- a/app/models/contestnotification.rb +++ b/app/models/contestnotification.rb @@ -8,10 +8,9 @@ class Contestnotification < ActiveRecord::Base has_many :notificationcomments, as: :notificationcommented, :dependent => :delete_all, :order => "created_at" # fq has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy - - validates_presence_of :title, :description - validates_length_of :title, :maximum => 60 - validates_length_of :summary, :maximum => 255 + validates :title, length: {maximum: 60}, presence: true + validates :description, presence: true + validates :summary, length: {maximum: 255} acts_as_attachable :delete_permission => :manage_contestnotifications acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :contest @@ -19,11 +18,8 @@ class Contestnotification < ActiveRecord::Base acts_as_activity_provider :find_options => {:include => [:contest, :author]}, :author_key => :author_id acts_as_watchable - after_create :add_author_as_watcher - after_create :act_as_activity - scope :visible, lambda {|*args| nil diff --git a/app/models/course.rb b/app/models/course.rb index 1c2828adc..36329ccda 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -10,7 +10,6 @@ class Course < ActiveRecord::Base belongs_to :teacher, :class_name => 'User', :foreign_key => :tea_id # 定义一个方法teacher,该方法通过tea_id来调用User表 belongs_to :school, :class_name => 'School', :foreign_key => :school_id #定义一个方法school,该方法通过school_id来调用School表 has_many :bid - has_many :members, :include => [:principal, :roles], :conditions => "#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}" has_many :memberships, :class_name => 'Member' has_many :member_principals, :class_name => 'Member', @@ -34,10 +33,10 @@ class Course < ActiveRecord::Base acts_as_attachable :view_permission => :view_files, :delete_permission => :manage_files - validates_presence_of :password, :term,:name ,:class_period - validates_format_of :class_period, :with =>/^\d*$/ - #validates_format_of :name,:with =>/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/ - + validates :password, presence: true + validates :term, presence: true + validates :name, presence: true + validates :class_period, presence: true,format: {:with =>/^\d*$/} before_save :self_validate after_create :create_board_sync before_destroy :delete_all_members diff --git a/app/models/course_status.rb b/app/models/course_status.rb index b36661ebe..85969e55e 100644 --- a/app/models/course_status.rb +++ b/app/models/course_status.rb @@ -1,7 +1,5 @@ class CourseStatus < ActiveRecord::Base attr_accessible :changesets_count, :course_ac_para, :course_id, :grade, :watchers_count - belongs_to :course - validates_presence_of :course_id - validates_uniqueness_of :course_id + validates :course_id, presence: true,uniqueness: true end diff --git a/app/models/web_footer_company.rb b/app/models/web_footer_company.rb index 0e5f37976..bca8dfb5d 100644 --- a/app/models/web_footer_company.rb +++ b/app/models/web_footer_company.rb @@ -1,6 +1,8 @@ class WebFooterCompany < ActiveRecord::Base attr_accessible :logo_size, :name, :url - validates_presence_of :name,:url - validates_length_of :name,:url, :maximum => 255 - validates_format_of :url,:with => /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/,:message => l(:is_not_url_error) + validates :name, presence: true, length: { maximum: 500 } + validates :url, length: { maximum: 500 }, + format: { with: /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/, + message: l(:is_not_url_error) + } end