From 2a4359a572c123d117988487f9bb29f4ae176ceb Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 26 Sep 2012 22:36:31 -0700 Subject: [PATCH 1/5] annotate models --- app/models/event.rb | 10 +++++----- app/models/issue.rb | 15 +++++++-------- app/models/key.rb | 6 +++--- app/models/merge_request.rb | 14 +++++++------- app/models/milestone.rb | 6 +++--- app/models/note.rb | 6 +++--- app/models/project.rb | 14 +++++++------- app/models/project_hook.rb | 12 ++++++++++++ app/models/protected_branch.rb | 4 ++-- app/models/snippet.rb | 6 +++--- app/models/system_hook.rb | 12 ++++++++++++ app/models/user.rb | 19 ++++++++++++------- app/models/users_project.rb | 8 ++++---- app/models/web_hook.rb | 5 +++-- app/models/wiki.rb | 6 +++--- 15 files changed, 86 insertions(+), 57 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index 76e428ad..e77afb98 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -144,15 +144,15 @@ end # # Table name: events # -# id :integer(4) not null, primary key +# id :integer not null, primary key # target_type :string(255) -# target_id :integer(4) +# target_id :integer # title :string(255) # data :text -# project_id :integer(4) +# project_id :integer # created_at :datetime not null # updated_at :datetime not null -# action :integer(4) -# author_id :integer(4) +# action :integer +# author_id :integer # diff --git a/app/models/issue.rb b/app/models/issue.rb index e1181b97..5e7616d6 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -36,18 +36,17 @@ end # # Table name: issues # -# id :integer(4) not null, primary key +# id :integer not null, primary key # title :string(255) -# assignee_id :integer(4) -# author_id :integer(4) -# project_id :integer(4) +# assignee_id :integer +# author_id :integer +# project_id :integer # created_at :datetime not null # updated_at :datetime not null -# closed :boolean(1) default(FALSE), not null -# position :integer(4) default(0) -# critical :boolean(1) default(FALSE), not null +# closed :boolean default(FALSE), not null +# position :integer default(0) # branch_name :string(255) # description :text -# milestone_id :integer(4) +# milestone_id :integer # diff --git a/app/models/key.rb b/app/models/key.rb index e23447e6..eadfcd85 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -61,13 +61,13 @@ end # # Table name: keys # -# id :integer(4) not null, primary key -# user_id :integer(4) +# id :integer not null, primary key +# user_id :integer # created_at :datetime not null # updated_at :datetime not null # key :text # title :string(255) # identifier :string(255) -# project_id :integer(4) +# project_id :integer # diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 717fe296..1e73276e 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -191,19 +191,19 @@ end # # Table name: merge_requests # -# id :integer(4) not null, primary key +# id :integer not null, primary key # target_branch :string(255) not null # source_branch :string(255) not null -# project_id :integer(4) not null -# author_id :integer(4) -# assignee_id :integer(4) +# project_id :integer not null +# author_id :integer +# assignee_id :integer # title :string(255) -# closed :boolean(1) default(FALSE), not null +# closed :boolean default(FALSE), not null # created_at :datetime not null # updated_at :datetime not null # st_commits :text(2147483647 # st_diffs :text(2147483647 -# merged :boolean(1) default(FALSE), not null -# state :integer(4) default(1), not null +# merged :boolean default(FALSE), not null +# state :integer default(1), not null # diff --git a/app/models/milestone.rb b/app/models/milestone.rb index 65fa461f..9014647c 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -2,12 +2,12 @@ # # Table name: milestones # -# id :integer(4) not null, primary key +# id :integer not null, primary key # title :string(255) not null -# project_id :integer(4) not null +# project_id :integer not null # description :text # due_date :date -# closed :boolean(1) default(FALSE), not null +# closed :boolean default(FALSE), not null # created_at :datetime not null # updated_at :datetime not null # diff --git a/app/models/note.rb b/app/models/note.rb index 9ac77ef7..84913970 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -118,14 +118,14 @@ end # # Table name: notes # -# id :integer(4) not null, primary key +# id :integer not null, primary key # note :text # noteable_id :string(255) # noteable_type :string(255) -# author_id :integer(4) +# author_id :integer # created_at :datetime not null # updated_at :datetime not null -# project_id :integer(4) +# project_id :integer # attachment :string(255) # line_code :string(255) # diff --git a/app/models/project.rb b/app/models/project.rb index 7470bd95..39e2bb25 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -182,19 +182,19 @@ end # # Table name: projects # -# id :integer(4) not null, primary key +# id :integer not null, primary key # name :string(255) # path :string(255) # description :text # created_at :datetime not null # updated_at :datetime not null -# private_flag :boolean(1) default(TRUE), not null +# private_flag :boolean default(TRUE), not null # code :string(255) -# owner_id :integer(4) +# owner_id :integer # default_branch :string(255) -# issues_enabled :boolean(1) default(TRUE), not null -# wall_enabled :boolean(1) default(TRUE), not null -# merge_requests_enabled :boolean(1) default(TRUE), not null -# wiki_enabled :boolean(1) default(TRUE), not null +# issues_enabled :boolean default(TRUE), not null +# wall_enabled :boolean default(TRUE), not null +# merge_requests_enabled :boolean default(TRUE), not null +# wiki_enabled :boolean default(TRUE), not null # diff --git a/app/models/project_hook.rb b/app/models/project_hook.rb index 06388aae..e7a05d57 100644 --- a/app/models/project_hook.rb +++ b/app/models/project_hook.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: web_hooks +# +# id :integer not null, primary key +# url :string(255) +# project_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# type :string(255) default("ProjectHook") +# + class ProjectHook < WebHook belongs_to :project end diff --git a/app/models/protected_branch.rb b/app/models/protected_branch.rb index 4ea083c1..42182864 100644 --- a/app/models/protected_branch.rb +++ b/app/models/protected_branch.rb @@ -22,8 +22,8 @@ end # # Table name: protected_branches # -# id :integer(4) not null, primary key -# project_id :integer(4) not null +# id :integer not null, primary key +# project_id :integer not null # name :string(255) not null # created_at :datetime not null # updated_at :datetime not null diff --git a/app/models/snippet.rb b/app/models/snippet.rb index bfd28684..10e37645 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -63,11 +63,11 @@ end # # Table name: snippets # -# id :integer(4) not null, primary key +# id :integer not null, primary key # title :string(255) # content :text -# author_id :integer(4) not null -# project_id :integer(4) not null +# author_id :integer not null +# project_id :integer not null # created_at :datetime not null # updated_at :datetime not null # file_name :string(255) diff --git a/app/models/system_hook.rb b/app/models/system_hook.rb index 8517d43a..89c69fce 100644 --- a/app/models/system_hook.rb +++ b/app/models/system_hook.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: web_hooks +# +# id :integer not null, primary key +# url :string(255) +# project_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# type :string(255) default("ProjectHook") +# + class SystemHook < WebHook def async_execute(data) diff --git a/app/models/user.rb b/app/models/user.rb index a8626cc1..f33f01fd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -111,13 +111,13 @@ end # # Table name: users # -# id :integer(4) not null, primary key +# id :integer not null, primary key # email :string(255) default(""), not null # encrypted_password :string(128) default(""), not null # reset_password_token :string(255) # reset_password_sent_at :datetime # remember_created_at :datetime -# sign_in_count :integer(4) default(0) +# sign_in_count :integer default(0) # current_sign_in_at :datetime # last_sign_in_at :datetime # current_sign_in_ip :string(255) @@ -125,14 +125,19 @@ end # created_at :datetime not null # updated_at :datetime not null # name :string(255) -# admin :boolean(1) default(FALSE), not null -# projects_limit :integer(4) default(10) +# admin :boolean default(FALSE), not null +# projects_limit :integer default(10) # skype :string(255) default(""), not null # linkedin :string(255) default(""), not null # twitter :string(255) default(""), not null # authentication_token :string(255) -# dark_scheme :boolean(1) default(FALSE), not null -# theme_id :integer(4) default(1), not null +# dark_scheme :boolean default(FALSE), not null +# theme_id :integer default(1), not null # bio :string(255) -# blocked :boolean(1) default(FALSE), not null +# blocked :boolean default(FALSE), not null +# failed_attempts :integer default(0) +# locked_at :datetime +# extern_uid :string(255) +# provider :string(255) # + diff --git a/app/models/users_project.rb b/app/models/users_project.rb index c42cc865..171bf2bb 100644 --- a/app/models/users_project.rb +++ b/app/models/users_project.rb @@ -92,11 +92,11 @@ end # # Table name: users_projects # -# id :integer(4) not null, primary key -# user_id :integer(4) not null -# project_id :integer(4) not null +# id :integer not null, primary key +# user_id :integer not null +# project_id :integer not null # created_at :datetime not null # updated_at :datetime not null -# project_access :integer(4) default(0), not null +# project_access :integer default(0), not null # diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb index 5d826d2f..b2e481e6 100644 --- a/app/models/web_hook.rb +++ b/app/models/web_hook.rb @@ -30,10 +30,11 @@ end # # Table name: web_hooks # -# id :integer(4) not null, primary key +# id :integer not null, primary key # url :string(255) -# project_id :integer(4) +# project_id :integer # created_at :datetime not null # updated_at :datetime not null +# type :string(255) default("ProjectHook") # diff --git a/app/models/wiki.rb b/app/models/wiki.rb index b053f5ad..a81a151b 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -36,13 +36,13 @@ end # # Table name: wikis # -# id :integer(4) not null, primary key +# id :integer not null, primary key # title :string(255) # content :text -# project_id :integer(4) +# project_id :integer # created_at :datetime not null # updated_at :datetime not null # slug :string(255) -# user_id :integer(4) +# user_id :integer # From 841e4fbd08d2d642c127506001a76a973ea3d536 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 26 Sep 2012 23:20:36 -0700 Subject: [PATCH 2/5] cosmetical cleanup of models --- app/models/ability.rb | 1 - app/models/commit.rb | 22 +++--------- app/models/event.rb | 11 +++--- app/models/issue.rb | 5 ++- app/models/key.rb | 16 ++++----- app/models/merge_request.rb | 5 ++- app/models/milestone.rb | 31 ++++++++-------- app/models/note.rb | 40 +++++++-------------- app/models/project.rb | 44 ++++++----------------- app/models/project_hook.rb | 8 ++--- app/models/protected_branch.rb | 5 ++- app/models/snippet.rb | 25 ++++--------- app/models/system_hook.rb | 26 +++++++------- app/models/tree.rb | 12 ++----- app/models/user.rb | 65 +++++++++------------------------- app/models/users_project.rb | 2 +- app/models/web_hook.rb | 14 +++----- app/models/wiki.rb | 2 +- 18 files changed, 108 insertions(+), 226 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index d65695a2..20ded59b 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -52,7 +52,6 @@ class Ability :admin_wiki ] if project.master_access_for?(user) || project.owner == user - rules.flatten end diff --git a/app/models/commit.rb b/app/models/commit.rb index 73583e9e..aba21762 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -4,24 +4,11 @@ class Commit include StaticModel extend ActiveModel::Naming - attr_accessor :commit - attr_accessor :head - attr_accessor :refs + attr_accessor :commit, :head, :refs - delegate :message, - :authored_date, - :committed_date, - :parents, - :sha, - :date, - :committer, - :author, - :message, - :diffs, - :tree, - :id, - :to_patch, - to: :commit + delegate :message, :authored_date, :committed_date, :parents, :sha, + :date, :committer, :author, :message, :diffs, :tree, :id, + :to_patch, to: :commit class << self def find_or_first(repo, commit_id = nil, root_ref) @@ -30,6 +17,7 @@ class Commit else repo.commits(root_ref).first end + Commit.new(commit) if commit end diff --git a/app/models/event.rb b/app/models/event.rb index e77afb98..43131ad6 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -16,6 +16,10 @@ class Event < ActiveRecord::Base Joined = 8 # User joined project Left = 9 # User left project + delegate :name, :email, to: :author, prefix: true, allow_nil: true + delegate :title, to: :issue, prefix: true, allow_nil: true + delegate :title, to: :merge_request, prefix: true, allow_nil: true + belongs_to :project belongs_to :target, polymorphic: true @@ -134,12 +138,8 @@ class Event < ActiveRecord::Base "opened" end end - - - delegate :name, :email, to: :author, prefix: true, allow_nil: true - delegate :title, to: :issue, prefix: true, allow_nil: true - delegate :title, to: :merge_request, prefix: true, allow_nil: true end + # == Schema Information # # Table name: events @@ -155,4 +155,3 @@ end # action :integer # author_id :integer # - diff --git a/app/models/issue.rb b/app/models/issue.rb index 5e7616d6..9acee1e5 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -9,8 +9,7 @@ class Issue < ActiveRecord::Base belongs_to :milestone - validates :description, - length: { within: 0..2000 } + validates :description, length: { within: 0..2000 } def self.open_for(user) opened.assigned(user) @@ -32,6 +31,7 @@ class Issue < ActiveRecord::Base closed_changed? && !closed end end + # == Schema Information # # Table name: issues @@ -49,4 +49,3 @@ end # description :text # milestone_id :integer # - diff --git a/app/models/key.rb b/app/models/key.rb index eadfcd85..3ef21811 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -6,14 +6,10 @@ class Key < ActiveRecord::Base attr_accessible :key, :title - validates :title, - presence: true, - length: { within: 0..255 } - - validates :key, - presence: true, - format: { :with => /ssh-.{3} / }, - length: { within: 0..5000 } + validates :title, presence: true, length: { within: 0..255 } + validates :key, presence: true, + length: { within: 0..5000 }, + format: { :with => /ssh-.{3} / } before_save :set_identifier before_validation :strip_white_space @@ -34,7 +30,7 @@ class Key < ActiveRecord::Base def set_identifier if is_deploy_key - self.identifier = "deploy_" + Digest::MD5.hexdigest(key) + self.identifier = "deploy_#{Digest::MD5.hexdigest(key)}" else self.identifier = "#{user.identifier}_#{Time.now.to_i}" end @@ -57,6 +53,7 @@ class Key < ActiveRecord::Base Key.where(identifier: identifier).count == 0 end end + # == Schema Information # # Table name: keys @@ -70,4 +67,3 @@ end # identifier :string(255) # project_id :integer # - diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 1e73276e..8428c7c4 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -18,8 +18,7 @@ class MergeRequest < ActiveRecord::Base attr_accessor :should_remove_source_branch - validates_presence_of :source_branch - validates_presence_of :target_branch + validates_presence_of :source_branch, :target_branch validate :validate_branches def self.find_all_by_branch(branch_name) @@ -187,6 +186,7 @@ class MergeRequest < ActiveRecord::Base patch_path end end + # == Schema Information # # Table name: merge_requests @@ -206,4 +206,3 @@ end # merged :boolean default(FALSE), not null # state :integer default(1), not null # - diff --git a/app/models/milestone.rb b/app/models/milestone.rb index 9014647c..da54ac40 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -1,25 +1,10 @@ -# == Schema Information -# -# Table name: milestones -# -# id :integer not null, primary key -# title :string(255) not null -# project_id :integer not null -# description :text -# due_date :date -# closed :boolean default(FALSE), not null -# created_at :datetime not null -# updated_at :datetime not null -# - class Milestone < ActiveRecord::Base attr_accessible :title, :description, :due_date, :closed belongs_to :project has_many :issues - validates_presence_of :project_id - validates_presence_of :title + validates_presence_of :title, :project_id def self.active where("due_date > ? OR due_date IS NULL", Date.today) @@ -39,3 +24,17 @@ class Milestone < ActiveRecord::Base "expires at #{due_date.stamp("Aug 21, 2011")}" if due_date end end + +# == Schema Information +# +# Table name: milestones +# +# id :integer not null, primary key +# title :string(255) not null +# project_id :integer not null +# description :text +# due_date :date +# closed :boolean default(FALSE), not null +# created_at :datetime not null +# updated_at :datetime not null +# diff --git a/app/models/note.rb b/app/models/note.rb index 84913970..23ced536 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -2,39 +2,26 @@ require 'carrierwave/orm/activerecord' require 'file_size_validator' class Note < ActiveRecord::Base + mount_uploader :attachment, AttachmentUploader attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id, :attachment, :line_code belongs_to :project belongs_to :noteable, polymorphic: true - belongs_to :author, - class_name: "User" + belongs_to :author, class_name: "User" - delegate :name, - to: :project, - prefix: true - - delegate :name, - :email, - to: :author, - prefix: true + delegate :name, to: :project, prefix: true + delegate :name, :email, to: :author, prefix: true attr_accessor :notify attr_accessor :notify_author validates_presence_of :project - validates :note, - presence: true, - length: { within: 0..5000 } - - validates :attachment, - file_size: { - maximum: 10.megabytes.to_i - } + validates :note, presence: true, length: { within: 0..5000 } + validates :attachment, file_size: { maximum: 10.megabytes.to_i } scope :common, where(noteable_id: nil) - scope :today, where("created_at >= :date", date: Date.today) scope :last_week, where("created_at >= :date", date: (Date.today - 7.days)) scope :since, lambda { |day| where("created_at >= :date", date: (day)) } @@ -42,14 +29,13 @@ class Note < ActiveRecord::Base scope :inc_author_project, includes(:project, :author) scope :inc_author, includes(:author) - mount_uploader :attachment, AttachmentUploader - def self.create_status_change_note(noteable, author, status) - create({ noteable: noteable, - project: noteable.project, - author: author, - note: "_Status changed to #{status}_" }, - without_protection: true) + create({ + noteable: noteable, + project: noteable.project, + author: author, + note: "_Status changed to #{status}_" + }, without_protection: true) end def notify @@ -114,6 +100,7 @@ class Note < ActiveRecord::Base note.start_with?('-1') || note.start_with?(':-1:') end end + # == Schema Information # # Table name: notes @@ -129,4 +116,3 @@ end # attachment :string(255) # line_code :string(255) # - diff --git a/app/models/project.rb b/app/models/project.rb index 39e2bb25..0652d187 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -8,10 +8,9 @@ class Project < ActiveRecord::Base attr_accessible :name, :path, :description, :code, :default_branch, :issues_enabled, :wall_enabled, :merge_requests_enabled, :wiki_enabled + attr_accessor :error_code - # # Relations - # belongs_to :owner, class_name: "User" has_many :users, through: :users_projects has_many :events, dependent: :destroy @@ -26,11 +25,7 @@ class Project < ActiveRecord::Base has_many :wikis, dependent: :destroy has_many :protected_branches, dependent: :destroy - attr_accessor :error_code - - # # Scopes - # scope :public_only, where(private_flag: false) scope :without_user, lambda { |user| where("id not in (:ids)", ids: user.projects.map(&:id) ) } @@ -47,7 +42,6 @@ class Project < ActiveRecord::Base Project.transaction do project.owner = user - project.save! # Add user as project master @@ -76,36 +70,19 @@ class Project < ActiveRecord::Base id && valid? end - # # Validations - # - validates :name, - uniqueness: true, - presence: true, - length: { within: 0..255 } - - validates :path, - uniqueness: true, - presence: true, - format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/, - message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }, - length: { within: 0..255 } - - validates :description, - length: { within: 0..2000 } - - validates :code, - presence: true, - uniqueness: true, - format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/, - message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }, - length: { within: 1..255 } - validates :owner, presence: true + validates :description, length: { within: 0..2000 } + validates :name, uniqueness: true, presence: true, length: { within: 0..255 } + validates :path, uniqueness: true, presence: true, length: { within: 0..255 }, + format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/, + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } + validates :code, presence: true, uniqueness: true, length: { within: 1..255 }, + format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/, + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } validates :issues_enabled, :wall_enabled, :merge_requests_enabled, :wiki_enabled, inclusion: { in: [true, false] } - validate :check_limit - validate :repo_name + validate :check_limit, :repo_name def check_limit unless owner.can_create_project? @@ -197,4 +174,3 @@ end # merge_requests_enabled :boolean default(TRUE), not null # wiki_enabled :boolean default(TRUE), not null # - diff --git a/app/models/project_hook.rb b/app/models/project_hook.rb index e7a05d57..5a59ebe9 100644 --- a/app/models/project_hook.rb +++ b/app/models/project_hook.rb @@ -1,3 +1,7 @@ +class ProjectHook < WebHook + belongs_to :project +end + # == Schema Information # # Table name: web_hooks @@ -9,7 +13,3 @@ # updated_at :datetime not null # type :string(255) default("ProjectHook") # - -class ProjectHook < WebHook - belongs_to :project -end diff --git a/app/models/protected_branch.rb b/app/models/protected_branch.rb index 42182864..c9e88d6b 100644 --- a/app/models/protected_branch.rb +++ b/app/models/protected_branch.rb @@ -4,8 +4,7 @@ class ProtectedBranch < ActiveRecord::Base attr_accessible :name belongs_to :project - validates_presence_of :project_id - validates_presence_of :name + validates_presence_of :name, :project_id after_save :update_repository after_destroy :update_repository @@ -18,6 +17,7 @@ class ProtectedBranch < ActiveRecord::Base project.commit(self.name) end end + # == Schema Information # # Table name: protected_branches @@ -28,4 +28,3 @@ end # created_at :datetime not null # updated_at :datetime not null # - diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 10e37645..b37d6bef 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -7,25 +7,12 @@ class Snippet < ActiveRecord::Base belongs_to :author, class_name: "User" has_many :notes, as: :noteable, dependent: :destroy - delegate :name, - :email, - to: :author, - prefix: true + delegate :name, :email, to: :author, prefix: true - validates_presence_of :project_id - validates_presence_of :author_id - - validates :title, - presence: true, - length: { within: 0..255 } - - validates :file_name, - presence: true, - length: { within: 0..255 } - - validates :content, - presence: true, - length: { within: 0..10000 } + validates_presence_of :author_id, :project_id + validates :title, presence: true, length: { within: 0..255 } + validates :file_name, presence: true, length: { within: 0..255 } + validates :content, presence: true, length: { within: 0..10000 } scope :fresh, order("created_at DESC") scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current]) @@ -59,6 +46,7 @@ class Snippet < ActiveRecord::Base expires_at && expires_at < Time.current end end + # == Schema Information # # Table name: snippets @@ -73,4 +61,3 @@ end # file_name :string(255) # expires_at :datetime # - diff --git a/app/models/system_hook.rb b/app/models/system_hook.rb index 89c69fce..376f336d 100644 --- a/app/models/system_hook.rb +++ b/app/models/system_hook.rb @@ -1,3 +1,15 @@ +class SystemHook < WebHook + def async_execute(data) + Resque.enqueue(SystemHookWorker, id, data) + end + + def self.all_hooks_fire(data) + SystemHook.all.each do |sh| + sh.async_execute data + end + end +end + # == Schema Information # # Table name: web_hooks @@ -9,17 +21,3 @@ # updated_at :datetime not null # type :string(255) default("ProjectHook") # - -class SystemHook < WebHook - - def async_execute(data) - Resque.enqueue(SystemHookWorker, id, data) - end - - def self.all_hooks_fire(data) - SystemHook.all.each do |sh| - sh.async_execute data - end - end - -end diff --git a/app/models/tree.rb b/app/models/tree.rb index 88e8f2f4..e4297a71 100644 --- a/app/models/tree.rb +++ b/app/models/tree.rb @@ -2,16 +2,8 @@ class Tree include Linguist::BlobHelper attr_accessor :path, :tree, :project, :ref - delegate :contents, - :basename, - :name, - :data, - :mime_type, - :mode, - :size, - :text?, - :colorize, - to: :tree + delegate :contents, :basename, :name, :data, :mime_type, + :mode, :size, :text?, :colorize, to: :tree def initialize(raw_tree, project, ref = nil, path = nil) @project, @ref, @path = project, ref, path diff --git a/app/models/user.rb b/app/models/user.rb index f33f01fd..bed568ef 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,64 +1,31 @@ class User < ActiveRecord::Base - include Account devise :database_authenticatable, :token_authenticatable, :lockable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable - attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, - :name, :skype, :linkedin, :twitter, :dark_scheme, - :theme_id, :force_random_password, :extern_uid, :provider, :as => [:default, :admin] + attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, :name, + :skype, :linkedin, :twitter, :dark_scheme, :theme_id, :force_random_password, + :extern_uid, :provider, :as => [:default, :admin] attr_accessible :projects_limit, :as => :admin attr_accessor :force_random_password - has_many :users_projects, dependent: :destroy - has_many :projects, through: :users_projects - has_many :my_own_projects, class_name: "Project", foreign_key: :owner_id has_many :keys, dependent: :destroy - - has_many :events, - class_name: "Event", - foreign_key: :author_id, - dependent: :destroy - - has_many :recent_events, - class_name: "Event", - foreign_key: :author_id, - order: "id DESC" - - has_many :issues, - foreign_key: :author_id, - dependent: :destroy - - has_many :notes, - foreign_key: :author_id, - dependent: :destroy - - has_many :assigned_issues, - class_name: "Issue", - foreign_key: :assignee_id, - dependent: :destroy - - has_many :merge_requests, - foreign_key: :author_id, - dependent: :destroy - - has_many :assigned_merge_requests, - class_name: "MergeRequest", - foreign_key: :assignee_id, - dependent: :destroy - - validates :projects_limit, - presence: true, - numericality: {greater_than_or_equal_to: 0} + has_many :projects, through: :users_projects + has_many :users_projects, dependent: :destroy + has_many :issues, foreign_key: :author_id, dependent: :destroy + has_many :notes, foreign_key: :author_id, dependent: :destroy + has_many :merge_requests, foreign_key: :author_id, dependent: :destroy + has_many :my_own_projects, class_name: "Project", foreign_key: :owner_id + has_many :events, class_name: "Event", foreign_key: :author_id, dependent: :destroy + has_many :recent_events, class_name: "Event", foreign_key: :author_id, order: "id DESC" + has_many :assigned_issues, class_name: "Issue", foreign_key: :assignee_id, dependent: :destroy + has_many :assigned_merge_requests, class_name: "MergeRequest", foreign_key: :assignee_id, dependent: :destroy validates :bio, length: { within: 0..255 } - validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider} - - before_save :ensure_authentication_token - alias_attribute :private_token, :authentication_token + validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0} scope :not_in_project, lambda { |project| where("id not in (:ids)", ids: project.users.map(&:id) ) } scope :admins, where(admin: true) @@ -66,6 +33,8 @@ class User < ActiveRecord::Base scope :active, where(blocked: false) before_validation :generate_password, on: :create + before_save :ensure_authentication_token + alias_attribute :private_token, :authentication_token def generate_password if self.force_random_password @@ -107,6 +76,7 @@ class User < ActiveRecord::Base where("name like :query or email like :query", query: "%#{query}%") end end + # == Schema Information # # Table name: users @@ -140,4 +110,3 @@ end # extern_uid :string(255) # provider :string(255) # - diff --git a/app/models/users_project.rb b/app/models/users_project.rb index 171bf2bb..510b90cb 100644 --- a/app/models/users_project.rb +++ b/app/models/users_project.rb @@ -88,6 +88,7 @@ class UsersProject < ActiveRecord::Base self.class.access_roles.invert[self.project_access] end end + # == Schema Information # # Table name: users_projects @@ -99,4 +100,3 @@ end # updated_at :datetime not null # project_access :integer default(0), not null # - diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb index b2e481e6..edfab886 100644 --- a/app/models/web_hook.rb +++ b/app/models/web_hook.rb @@ -6,26 +6,23 @@ class WebHook < ActiveRecord::Base # HTTParty timeout default_timeout 10 - validates :url, - presence: true, - format: { - with: URI::regexp(%w(http https)), - message: "should be a valid url" } + validates :url, presence: true, + format: { with: URI::regexp(%w(http https)), message: "should be a valid url" } def execute(data) parsed_url = URI.parse(url) if parsed_url.userinfo.blank? WebHook.post(url, body: data.to_json, headers: { "Content-Type" => "application/json" }) else - post_url = url.gsub(parsed_url.userinfo+"@", "") + post_url = url.gsub("#{parsed_url.userinfo}@", "") WebHook.post(post_url, body: data.to_json, - headers: { "Content-Type" => "application/json" }, + headers: {"Content-Type" => "application/json"}, basic_auth: {username: parsed_url.user, password: parsed_url.password}) end end - end + # == Schema Information # # Table name: web_hooks @@ -37,4 +34,3 @@ end # updated_at :datetime not null # type :string(255) default("ProjectHook") # - diff --git a/app/models/wiki.rb b/app/models/wiki.rb index a81a151b..c87ac89e 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -32,6 +32,7 @@ class Wiki < ActiveRecord::Base end end end + # == Schema Information # # Table name: wikis @@ -45,4 +46,3 @@ end # slug :string(255) # user_id :integer # - From c7a1779b2215b1815d43c5635e026afeed8da7f4 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 26 Sep 2012 23:28:10 -0700 Subject: [PATCH 3/5] .map -> .pluck --- app/models/milestone.rb | 2 +- app/models/project.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/milestone.rb b/app/models/milestone.rb index da54ac40..a8619954 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -11,7 +11,7 @@ class Milestone < ActiveRecord::Base end def participants - User.where(id: issues.map(&:assignee_id)) + User.where(id: issues.pluck(:assignee_id)) end def percent_complete diff --git a/app/models/project.rb b/app/models/project.rb index 0652d187..5c82b700 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -147,7 +147,7 @@ class Project < ActiveRecord::Base end def wiki_notes - Note.where(noteable_id: wikis.map(&:id), noteable_type: 'Wiki', project_id: self.id) + Note.where(noteable_id: wikis.pluck(:id), noteable_type: 'Wiki', project_id: self.id) end def project_id From 2088d2eba86878dafaa69e8b908a0db2730451b4 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 26 Sep 2012 23:32:04 -0700 Subject: [PATCH 4/5] replace ^ and $ anchors in regexp with \A and \z respectively http://guides.rubyonrails.org/security.html#regular-expressions --- app/models/project.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 5c82b700..9b13de62 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -75,10 +75,10 @@ class Project < ActiveRecord::Base validates :description, length: { within: 0..2000 } validates :name, uniqueness: true, presence: true, length: { within: 0..255 } validates :path, uniqueness: true, presence: true, length: { within: 0..255 }, - format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/, + format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/, message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } validates :code, presence: true, uniqueness: true, length: { within: 1..255 }, - format: { with: /^[a-zA-Z][a-zA-Z0-9_\-\.]*$/, + format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/, message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" } validates :issues_enabled, :wall_enabled, :merge_requests_enabled, :wiki_enabled, inclusion: { in: [true, false] } From a030e4b6645263ae409b37b569eff2600eb98aa8 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 26 Sep 2012 23:36:55 -0700 Subject: [PATCH 5/5] cleanup database.yml files --- config/database.yml.example | 8 ++------ config/database.yml.mysql | 8 ++------ config/database.yml.sqlite | 8 +------- 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/config/database.yml.example b/config/database.yml.example index 5b5c3f0b..c5a2b8d6 100644 --- a/config/database.yml.example +++ b/config/database.yml.example @@ -9,12 +9,11 @@ production: pool: 5 username: root password: "secure password" + # host: localhost # socket: /tmp/mysql.sock - -# -# Development specific # +# Development specific # development: adapter: mysql2 @@ -38,6 +37,3 @@ test: &test username: root password: "secure password" # socket: /tmp/mysql.sock - -cucumber: - <<: *test diff --git a/config/database.yml.mysql b/config/database.yml.mysql index 99cd7eaa..436bea77 100644 --- a/config/database.yml.mysql +++ b/config/database.yml.mysql @@ -9,12 +9,11 @@ production: pool: 5 username: root password: "secure password" + # host: localhost # socket: /tmp/mysql.sock - -# -# Development specific # +# Development specific # development: adapter: mysql2 @@ -38,6 +37,3 @@ test: &test username: root password: # socket: /tmp/mysql.sock - -cucumber: - <<: *test diff --git a/config/database.yml.sqlite b/config/database.yml.sqlite index 077a17ba..591448f6 100644 --- a/config/database.yml.sqlite +++ b/config/database.yml.sqlite @@ -12,12 +12,9 @@ production: pool: 5 timeout: 5000 - -# +# # Development specific # -# -# development: adapter: sqlite3 database: db/development.sqlite3 @@ -32,6 +29,3 @@ test: &test database: db/test.sqlite3 pool: 5 timeout: 5000 - -cucumber: - <<: *test