修改了部分model的validate(custon_field.rb之上)

This commit is contained in:
sw 2014-10-15 11:08:41 +08:00
parent 602982a124
commit 4082dbed5b
18 changed files with 68 additions and 73 deletions

View File

@ -2,6 +2,7 @@ class Activity < ActiveRecord::Base
attr_accessible :act_id, :act_type, :user_id attr_accessible :act_id, :act_type, :user_id
belongs_to :act, :polymorphic => true belongs_to :act, :polymorphic => true
belongs_to :user belongs_to :user
validates :act_id, presence: true
validates_presence_of :act_id, :act_type, :user_id validates :act_type, presence: true
validates :user_id, presence: true
end end

View File

@ -28,10 +28,10 @@ class Attachment < ActiveRecord::Base
include UserScoreHelper include UserScoreHelper
validates_presence_of :filename, :author validates :filename, presence: true, length: {maximum: 254}
validates_length_of :filename, :maximum => 254 validates :author, presence: true
validates_length_of :disk_filename, :maximum => 254 validates :disk_filename, length: {maximum: 254}
validates_length_of :description, :maximum => 254 validates :description, length: {maximum: 254}
validate :validate_max_file_size validate :validate_max_file_size

View File

@ -26,9 +26,7 @@ class AuthSource < ActiveRecord::Base
has_many :users has_many :users
validates_presence_of :name validates :name, presence: true, uniqueness: true, length: {maximum: 60}
validates_uniqueness_of :name
validates_length_of :name, :maximum => 60
def authenticate(login, password) def authenticate(login, password)
end end

View File

@ -20,14 +20,20 @@ require 'net/ldap/dn'
require 'timeout' require 'timeout'
class AuthSourceLdap < AuthSource class AuthSourceLdap < AuthSource
validates_presence_of :host, :port, :attr_login validates :host, presence: true, length: {maximum: 60, allow_nil: true}
validates_length_of :name, :host, :maximum => 60, :allow_nil => true validates :port, presence: true, numericality: {only_integer: true}
validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true validates :attr_login, presence: true
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true validates :name, length: {maximum: 60, allow_nil: true}
validates_numericality_of :port, :only_integer => true validates :account, length: {maximum: 255, allow_blank: true}
validates_numericality_of :timeout, :only_integer => true, :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 validate :validate_filter
before_validation :strip_ldap_attributes before_validation :strip_ldap_attributes
def initialize(attributes=nil, *args) def initialize(attributes=nil, *args)

View File

@ -37,16 +37,14 @@ class Bid < ActiveRecord::Base
NAME_LENGTH_LIMIT = 60 NAME_LENGTH_LIMIT = 60
DESCRIPTION_LENGTH_LIMIT = 250 DESCRIPTION_LENGTH_LIMIT = 250
validates :name, length: {maximum: NAME_LENGTH_LIMIT}, presence: true
validates_length_of :name, :maximum => NAME_LENGTH_LIMIT validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT}
validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT validates :author_id, presence: true
validates_presence_of :author_id, :name, :deadline validates :deadline, presence: true, format: {:with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/}
# validates_presence_of :deadline, :message => 'test' validates :name, length: {maximum: NAME_LENGTH_LIMIT}
# validates_format_of :deadline, :with => validates :budget,
validates_format_of :deadline, :with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/ format: {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]{2})|^(\d+).([0-9]{1})$/, format: {with: /^(\d+)$|^(\d+).([0-9]{1})$/, if: Proc.new { |p| p.reward_type == 3 }}
: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 }
validate :validate_user validate :validate_user
validate :validate_reward_type validate :validate_reward_type
after_create :act_as_activity after_create :act_as_activity

View File

@ -7,13 +7,13 @@ class BidingProject < ActiveRecord::Base
belongs_to :user belongs_to :user
DESCRIPTION_LENGTH_LIMIT = 500 DESCRIPTION_LENGTH_LIMIT = 500
validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT}
validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT validates :user_id, presence: true
validates_presence_of :user_id, :bid_id, :project_id validates :bid_id, presence: true, :uniqueness => { :scope => :project_id}
validates :project_id, presence: true
validate :validate_user validate :validate_user
validate :validate_bid validate :validate_bid
validate :validate_project validate :validate_project
validates_uniqueness_of :bid_id, :scope => :project_id
def self.cerate_bidding(bid_id, project_id, description = nil) def self.cerate_bidding(bid_id, project_id, description = nil)
self.create(:user_id => User.current.id, :bid_id => bid_id, self.create(:user_id => User.current.id, :bid_id => bid_id,

View File

@ -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_list :scope => '(project_id = #{project_id} AND parent_id #{parent_id ? "= #{parent_id}" : "IS NULL"})'
acts_as_watchable acts_as_watchable
validates_presence_of :name, :description validates :name, presence: true, length: {maximum: 30}
validates_length_of :name, :maximum => 30 validates :description, presence: true, length: {maximum: 255}
validates_length_of :description, :maximum => 255
validate :validate_board validate :validate_board
scope :visible, lambda {|*args| scope :visible, lambda {|*args|

View File

@ -2,11 +2,10 @@ class BugToOsp < ActiveRecord::Base
# attr_accessible :title, :body # attr_accessible :title, :body
belongs_to :open_source_project, :foreign_key => "osp_id" belongs_to :open_source_project, :foreign_key => "osp_id"
belongs_to :bug, :class_name => 'RelativeMemo', :foreign_key => "relative_memo_id" belongs_to :bug, :class_name => 'RelativeMemo', :foreign_key => "relative_memo_id"
validates :osp_id, presence: true
validates_presence_of :osp_id, :relative_memo_id validates :relative_memo_id, presence: true
scope :visible, lambda {|*args| scope :visible, lambda {|*args|
nil nil
} }
end end

View File

@ -18,7 +18,9 @@
class Change < ActiveRecord::Base class Change < ActiveRecord::Base
belongs_to :changeset 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_save :init_path
before_validation :replace_invalid_utf8_of_path before_validation :replace_invalid_utf8_of_path

View File

@ -54,10 +54,11 @@ class Changeset < ActiveRecord::Base
acts_as_activity_provider :timestamp => "#{table_name}.committed_on", acts_as_activity_provider :timestamp => "#{table_name}.committed_on",
:author_key => :user_id, :author_key => :user_id,
:find_options => {:include => [:user, {:repository => :project}]} :find_options => {:include => [:user, {:repository => :project}]}
validates :repository_id, presence: true
validates_presence_of :repository_id, :revision, :committed_on, :commit_date validates :revision, presence: true, uniqueness: {scope: :repository_id}
validates_uniqueness_of :revision, :scope => :repository_id validates :committed_on, presence: true
validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true validates :commit_date, presence: true
validates :scmid, uniqueness: {scope: :repository_id, allow_nil: true}
scope :visible, lambda {|*args| scope :visible, lambda {|*args|
includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args)) includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args))

View File

@ -19,8 +19,6 @@ class Comment < ActiveRecord::Base
include Redmine::SafeAttributes include Redmine::SafeAttributes
belongs_to :commented, :polymorphic => true, :counter_cache => true belongs_to :commented, :polymorphic => true, :counter_cache => true
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
validates_presence_of :commented, :author, :comments validates_presence_of :commented, :author, :comments
safe_attributes 'comments' safe_attributes 'comments'
end end

View File

@ -21,13 +21,11 @@ class Contest < ActiveRecord::Base
NAME_LENGTH_LIMIT = 60 NAME_LENGTH_LIMIT = 60
DESCRIPTION_LENGTH_LIMIT = 250 DESCRIPTION_LENGTH_LIMIT = 250
validates :name, length: {maximum: NAME_LENGTH_LIMIT}, presence: true
validates_length_of :name, :maximum => NAME_LENGTH_LIMIT validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT}
validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT validates :author_id, presence: true
validates_presence_of :author_id, :name, :budget validates :budget, presence: true
#validates_format_of :deadline, :with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/ validates :deadline, format: {:with =>/^[1-9][0-9]{3}\-0?[1-9]|1[12]\-0?[1-9]|[12]\d|3[01]$/}
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})$/
validate :validate_user validate :validate_user
after_create :act_as_activity after_create :act_as_activity

View File

@ -1,4 +1,4 @@
class ContestNotification < ActiveRecord::Base class ContestNotification < ActiveRecord::Base
attr_accessible :content, :title attr_accessible :content, :title
validates_length_of :title, maximum: 30 validates :title, length: {maximum: 30}
end end

View File

@ -6,13 +6,13 @@ class ContestingProject < ActiveRecord::Base
belongs_to :user belongs_to :user
DESCRIPTION_LENGTH_LIMIT = 500 DESCRIPTION_LENGTH_LIMIT = 500
validates :description, length: {maximum:DESCRIPTION_LENGTH_LIMIT }
validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT validates :user_id, presence: true
validates_presence_of :user_id, :contest_id, :project_id validates :contest_id, presence: true, uniqueness: {:scope => :project_id}
validates :project_id, presence: true
validate :validate_user validate :validate_user
validate :validate_contest validate :validate_contest
validate :validate_project validate :validate_project
validates_uniqueness_of :contest_id, :scope => :project_id
def self.create_contesting(contest_id, project_id, description = nil) def self.create_contesting(contest_id, project_id, description = nil)
self.create(:user_id => User.current.id, :contest_id => contest_id, self.create(:user_id => User.current.id, :contest_id => contest_id,

View File

@ -8,10 +8,9 @@ class Contestnotification < ActiveRecord::Base
has_many :notificationcomments, as: :notificationcommented, :dependent => :delete_all, :order => "created_at" has_many :notificationcomments, as: :notificationcommented, :dependent => :delete_all, :order => "created_at"
# fq # fq
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
validates :title, length: {maximum: 60}, presence: true
validates_presence_of :title, :description validates :description, presence: true
validates_length_of :title, :maximum => 60 validates :summary, length: {maximum: 255}
validates_length_of :summary, :maximum => 255
acts_as_attachable :delete_permission => :manage_contestnotifications acts_as_attachable :delete_permission => :manage_contestnotifications
acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :contest acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :contest
@ -19,12 +18,9 @@ class Contestnotification < ActiveRecord::Base
acts_as_activity_provider :find_options => {:include => [:contest, :author]}, acts_as_activity_provider :find_options => {:include => [:contest, :author]},
:author_key => :author_id :author_key => :author_id
acts_as_watchable acts_as_watchable
after_create :add_author_as_watcher after_create :add_author_as_watcher
after_create :act_as_activity after_create :act_as_activity
scope :visible, lambda {|*args| scope :visible, lambda {|*args|
nil nil
#includes(:contest).where(Contest.allowed_to_condition(args.shift || User.current, :view_contestnotifications, *args)) #includes(:contest).where(Contest.allowed_to_condition(args.shift || User.current, :view_contestnotifications, *args))

View File

@ -10,7 +10,6 @@ class Course < ActiveRecord::Base
belongs_to :teacher, :class_name => 'User', :foreign_key => :tea_id # 定义一个方法teacher该方法通过tea_id来调用User表 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表 belongs_to :school, :class_name => 'School', :foreign_key => :school_id #定义一个方法school该方法通过school_id来调用School表
has_many :bid 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 :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 :memberships, :class_name => 'Member'
has_many :member_principals, :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, acts_as_attachable :view_permission => :view_files,
:delete_permission => :manage_files :delete_permission => :manage_files
validates_presence_of :password, :term,:name ,:class_period validates :password, presence: true
validates_format_of :class_period, :with =>/^\d*$/ validates :term, presence: true
#validates_format_of :name,:with =>/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/ validates :name, presence: true
validates :class_period, presence: true,format: {:with =>/^\d*$/}
before_save :self_validate before_save :self_validate
after_create :create_board_sync after_create :create_board_sync
before_destroy :delete_all_members before_destroy :delete_all_members

View File

@ -1,7 +1,5 @@
class CourseStatus < ActiveRecord::Base class CourseStatus < ActiveRecord::Base
attr_accessible :changesets_count, :course_ac_para, :course_id, :grade, :watchers_count attr_accessible :changesets_count, :course_ac_para, :course_id, :grade, :watchers_count
belongs_to :course belongs_to :course
validates_presence_of :course_id validates :course_id, presence: true,uniqueness: true
validates_uniqueness_of :course_id
end end

View File

@ -1,6 +1,8 @@
class WebFooterCompany < ActiveRecord::Base class WebFooterCompany < ActiveRecord::Base
attr_accessible :logo_size, :name, :url attr_accessible :logo_size, :name, :url
validates_presence_of :name,:url validates :name, presence: true, length: { maximum: 500 }
validates_length_of :name,:url, :maximum => 255 validates :url, length: { maximum: 500 },
validates_format_of :url,:with => /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/,:message => l(:is_not_url_error) format: { with: /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/,
message: l(:is_not_url_error)
}
end end