BigW Consortium Gitlab

Commit dc33f71b by Valeriy Sizov

Merge pull request #1656 from zzet/refactoring

Refactoring
parents c348284f a4cd7386
...@@ -17,7 +17,7 @@ module CommitsHelper ...@@ -17,7 +17,7 @@ module CommitsHelper
line_old = 1 line_old = 1
line_new = 1 line_new = 1
type = nil type = nil
lines_arr = ::Gitlab::InlineDiff.processing diff_arr lines_arr = ::Gitlab::InlineDiff.processing diff_arr
lines_arr.each do |line| lines_arr.each do |line|
next if line.match(/^\-\-\- \/dev\/null/) next if line.match(/^\-\-\- \/dev\/null/)
......
...@@ -24,7 +24,7 @@ module MergeRequestsHelper ...@@ -24,7 +24,7 @@ module MergeRequestsHelper
def new_mr_path_from_push_event(event) def new_mr_path_from_push_event(event)
new_project_merge_request_path( new_project_merge_request_path(
event.project, event.project,
merge_request: { merge_request: {
source_branch: event.branch_name, source_branch: event.branch_name,
target_branch: event.project.root_ref, target_branch: event.project.root_ref,
title: event.branch_name.titleize title: event.branch_name.titleize
......
class Ability class Ability
def self.allowed(object, subject) class << self
case subject.class.name def allowed(object, subject)
when "Project" then project_abilities(object, subject) case subject.class.name
when "Issue" then issue_abilities(object, subject) when "Project" then project_abilities(object, subject)
when "Note" then note_abilities(object, subject) when "Issue" then issue_abilities(object, subject)
when "Snippet" then snippet_abilities(object, subject) when "Note" then note_abilities(object, subject)
when "MergeRequest" then merge_request_abilities(object, subject) when "Snippet" then snippet_abilities(object, subject)
else [] when "MergeRequest" then merge_request_abilities(object, subject)
else []
end
end end
end
def self.project_abilities(user, project) def project_abilities(user, project)
rules = [] rules = []
rules << [ rules << [
:read_project, :read_project,
:read_wiki, :read_wiki,
:read_issue, :read_issue,
:read_milestone, :read_milestone,
:read_snippet, :read_snippet,
:read_team_member, :read_team_member,
:read_merge_request, :read_merge_request,
:read_note, :read_note,
:write_project, :write_project,
:write_issue, :write_issue,
:write_note :write_note
] if project.guest_access_for?(user) ] if project.guest_access_for?(user)
rules << [ rules << [
:download_code, :download_code,
:write_merge_request, :write_merge_request,
:write_snippet :write_snippet
] if project.report_access_for?(user) ] if project.report_access_for?(user)
rules << [ rules << [
:write_wiki :write_wiki
] if project.dev_access_for?(user) ] if project.dev_access_for?(user)
rules << [ rules << [
:modify_issue, :modify_issue,
:modify_snippet, :modify_snippet,
:modify_merge_request, :modify_merge_request,
:admin_project, :admin_project,
:admin_issue, :admin_issue,
:admin_milestone, :admin_milestone,
:admin_snippet, :admin_snippet,
:admin_team_member, :admin_team_member,
:admin_merge_request, :admin_merge_request,
:admin_note, :admin_note,
:accept_mr, :accept_mr,
:admin_wiki :admin_wiki
] if project.master_access_for?(user) || project.owner == user ] if project.master_access_for?(user) || project.owner == user
rules.flatten rules.flatten
end end
class << self
[:issue, :note, :snippet, :merge_request].each do |name| [:issue, :note, :snippet, :merge_request].each do |name|
define_method "#{name}_abilities" do |user, subject| define_method "#{name}_abilities" do |user, subject|
if subject.author == user if subject.author == user
...@@ -72,8 +72,7 @@ class Ability ...@@ -72,8 +72,7 @@ class Ability
:"modify_#{name}", :"modify_#{name}",
] ]
else else
subject.respond_to?(:project) ? subject.respond_to?(:project) ? project_abilities(user, subject.project) : []
project_abilities(user, subject.project) : []
end end
end end
end end
......
...@@ -27,19 +27,22 @@ class Event < ActiveRecord::Base ...@@ -27,19 +27,22 @@ class Event < ActiveRecord::Base
# For Hash only # For Hash only
serialize :data serialize :data
# Scopes
scope :recent, order("created_at DESC") scope :recent, order("created_at DESC")
scope :code_push, where(action: Pushed) scope :code_push, where(action: Pushed)
def self.determine_action(record) class << self
if [Issue, MergeRequest].include? record.class def determine_action(record)
Event::Created if [Issue, MergeRequest].include? record.class
elsif record.kind_of? Note Event::Created
Event::Commented elsif record.kind_of? Note
Event::Commented
end
end end
end
def self.recent_for_user user def recent_for_user user
where(project_id: user.projects.map(&:id)).recent where(project_id: user.projects.map(&:id)).recent
end
end end
# Next events currently enabled for system # Next events currently enabled for system
......
# == Schema Information
#
# Table name: groups
#
# id :integer not null, primary key
# name :string(255) not null
# code :string(255) not null
# owner_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Group < ActiveRecord::Base class Group < ActiveRecord::Base
attr_accessible :code, :name, :owner_id attr_accessible :code, :name, :owner_id
...@@ -18,7 +6,7 @@ class Group < ActiveRecord::Base ...@@ -18,7 +6,7 @@ class Group < ActiveRecord::Base
validates :name, presence: true, uniqueness: true validates :name, presence: true, uniqueness: true
validates :code, presence: true, uniqueness: true validates :code, presence: true, uniqueness: true
validates :owner_id, presence: true validates :owner, presence: true
delegate :name, to: :owner, allow_nil: true, prefix: true delegate :name, to: :owner, allow_nil: true, prefix: true
...@@ -31,6 +19,18 @@ class Group < ActiveRecord::Base ...@@ -31,6 +19,18 @@ class Group < ActiveRecord::Base
end end
def users def users
User.joins(:users_projects).where('users_projects.project_id' => project_ids).uniq User.joins(:users_projects).where(users_projects: {project_id: project_ids}).uniq
end end
end end
# == Schema Information
#
# Table name: groups
#
# id :integer not null, primary key
# name :string(255) not null
# code :string(255) not null
# owner_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
#
...@@ -6,15 +6,14 @@ class Key < ActiveRecord::Base ...@@ -6,15 +6,14 @@ class Key < ActiveRecord::Base
attr_accessible :key, :title attr_accessible :key, :title
before_validation :strip_white_space
before_save :set_identifier
validates :title, presence: true, length: { within: 0..255 } validates :title, presence: true, length: { within: 0..255 }
validates :key, presence: true, validates :key, presence: true, length: { within: 0..5000 }, format: { :with => /ssh-.{3} / }
length: { within: 0..5000 }, validate :unique_key, :fingerprintable_key
format: { :with => /ssh-.{3} / }
before_save :set_identifier
before_validation :strip_white_space
delegate :name, :email, to: :user, prefix: true delegate :name, :email, to: :user, prefix: true
validate :unique_key, :fingerprintable_key
def strip_white_space def strip_white_space
self.key = self.key.strip unless self.key.blank? self.key = self.key.strip unless self.key.blank?
......
...@@ -7,6 +7,8 @@ class MergeRequest < ActiveRecord::Base ...@@ -7,6 +7,8 @@ class MergeRequest < ActiveRecord::Base
attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch, attr_accessible :title, :assignee_id, :closed, :target_branch, :source_branch,
:author_id_of_changes :author_id_of_changes
attr_accessor :should_remove_source_branch
BROKEN_DIFF = "--broken-diff" BROKEN_DIFF = "--broken-diff"
UNCHECKED = 1 UNCHECKED = 1
...@@ -16,9 +18,8 @@ class MergeRequest < ActiveRecord::Base ...@@ -16,9 +18,8 @@ class MergeRequest < ActiveRecord::Base
serialize :st_commits serialize :st_commits
serialize :st_diffs serialize :st_diffs
attr_accessor :should_remove_source_branch validates :source_branch, presence: true
validates :target_branch, presence: true
validates_presence_of :source_branch, :target_branch
validate :validate_branches validate :validate_branches
def self.find_all_by_branch(branch_name) def self.find_all_by_branch(branch_name)
......
...@@ -4,7 +4,8 @@ class Milestone < ActiveRecord::Base ...@@ -4,7 +4,8 @@ class Milestone < ActiveRecord::Base
belongs_to :project belongs_to :project
has_many :issues has_many :issues
validates_presence_of :title, :project_id validates :title, presence: true
validates :project, presence: true
def self.active def self.active
where("due_date > ? OR due_date IS NULL", Date.today) where("due_date > ? OR due_date IS NULL", Date.today)
......
...@@ -2,10 +2,13 @@ require 'carrierwave/orm/activerecord' ...@@ -2,10 +2,13 @@ require 'carrierwave/orm/activerecord'
require 'file_size_validator' require 'file_size_validator'
class Note < ActiveRecord::Base class Note < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id, attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id,
:attachment, :line_code :attachment, :line_code
attr_accessor :notify
attr_accessor :notify_author
belongs_to :project belongs_to :project
belongs_to :noteable, polymorphic: true belongs_to :noteable, polymorphic: true
belongs_to :author, class_name: "User" belongs_to :author, class_name: "User"
...@@ -13,18 +16,17 @@ class Note < ActiveRecord::Base ...@@ -13,18 +16,17 @@ class Note < ActiveRecord::Base
delegate :name, to: :project, prefix: true delegate :name, to: :project, prefix: true
delegate :name, :email, to: :author, prefix: true delegate :name, :email, to: :author, prefix: true
attr_accessor :notify validates :project, presence: true
attr_accessor :notify_author
validates_presence_of :project
validates :note, presence: true, length: { within: 0..5000 } validates :note, presence: true, length: { within: 0..5000 }
validates :attachment, file_size: { maximum: 10.megabytes.to_i } validates :attachment, file_size: { maximum: 10.megabytes.to_i }
mount_uploader :attachment, AttachmentUploader
# Scopes
scope :common, where(noteable_id: nil) scope :common, where(noteable_id: nil)
scope :today, where("created_at >= :date", date: Date.today) scope :today, where("created_at >= :date", date: Date.today)
scope :last_week, where("created_at >= :date", date: (Date.today - 7.days)) scope :last_week, where("created_at >= :date", date: (Date.today - 7.days))
scope :since, lambda { |day| where("created_at >= :date", date: (day)) } scope :since, ->(day) { where("created_at >= :date", date: (day)) }
scope :fresh, order("created_at ASC, id ASC") scope :fresh, order("created_at ASC, id ASC")
scope :inc_author_project, includes(:project, :author) scope :inc_author_project, includes(:project, :author)
scope :inc_author, includes(:author) scope :inc_author, includes(:author)
......
...@@ -28,42 +28,62 @@ class Project < ActiveRecord::Base ...@@ -28,42 +28,62 @@ class Project < ActiveRecord::Base
delegate :name, to: :owner, allow_nil: true, prefix: true delegate :name, to: :owner, allow_nil: true, prefix: true
# Validations
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[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[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] }
validate :check_limit, :repo_name
# Scopes # Scopes
scope :public_only, where(private_flag: false) scope :public_only, where(private_flag: false)
scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) } scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.projects.map(&:id) ) }
scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) } scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }
def self.active class << self
joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC") def active
end joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
end
def self.search query
where("name LIKE :query OR code LIKE :query OR path LIKE :query", query: "%#{query}%")
end
def self.create_by_user(params, user)
project = Project.new params
Project.transaction do
project.owner = user
project.save!
# Add user as project master def search query
project.users_projects.create!(project_access: UsersProject::MASTER, user: user) where("name LIKE :query OR code LIKE :query OR path LIKE :query", query: "%#{query}%")
end
# when project saved no team member exist so def create_by_user(params, user)
# project repository should be updated after first user add project = Project.new params
project.update_repository
Project.transaction do
project.owner = user
project.save!
# Add user as project master
project.users_projects.create!(project_access: UsersProject::MASTER, user: user)
# when project saved no team member exist so
# project repository should be updated after first user add
project.update_repository
end
project
rescue Gitlab::Gitolite::AccessDenied => ex
project.error_code = :gitolite
project
rescue => ex
project.error_code = :db
project.errors.add(:base, "Can't save project. Please try again later")
project
end end
project def access_options
rescue Gitlab::Gitolite::AccessDenied => ex UsersProject.access_roles
project.error_code = :gitolite end
project
rescue => ex
project.error_code = :db
project.errors.add(:base, "Can't save project. Please try again later")
project
end end
def git_error? def git_error?
...@@ -74,20 +94,6 @@ class Project < ActiveRecord::Base ...@@ -74,20 +94,6 @@ class Project < ActiveRecord::Base
id && valid? id && valid?
end end
# Validations
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[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[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] }
validate :check_limit, :repo_name
def check_limit def check_limit
unless owner.can_create_project? unless owner.can_create_project?
errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it")
...@@ -102,10 +108,6 @@ class Project < ActiveRecord::Base ...@@ -102,10 +108,6 @@ class Project < ActiveRecord::Base
end end
end end
def self.access_options
UsersProject.access_roles
end
def to_param def to_param
code code
end end
......
...@@ -4,7 +4,8 @@ class ProtectedBranch < ActiveRecord::Base ...@@ -4,7 +4,8 @@ class ProtectedBranch < ActiveRecord::Base
attr_accessible :name attr_accessible :name
belongs_to :project belongs_to :project
validates_presence_of :name, :project_id validates :name, presence: true
validates :project, presence: true
after_save :update_repository after_save :update_repository
after_destroy :update_repository after_destroy :update_repository
......
...@@ -9,11 +9,13 @@ class Snippet < ActiveRecord::Base ...@@ -9,11 +9,13 @@ class Snippet < ActiveRecord::Base
delegate :name, :email, to: :author, prefix: true delegate :name, :email, to: :author, prefix: true
validates_presence_of :author_id, :project_id validates :author, presence: true
validates :project, presence: true
validates :title, presence: true, length: { within: 0..255 } validates :title, presence: true, length: { within: 0..255 }
validates :file_name, presence: true, length: { within: 0..255 } validates :file_name, presence: true, length: { within: 0..255 }
validates :content, presence: true, length: { within: 0..10000 } validates :content, presence: true, length: { within: 0..10000 }
# Scopes
scope :fresh, order("created_at DESC") scope :fresh, order("created_at DESC")
scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current]) scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current])
scope :expired, where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) scope :expired, where(["expires_at IS NOT NULL AND expires_at < ?", Time.current])
......
class SystemHook < WebHook class SystemHook < WebHook
def async_execute(data)
Resque.enqueue(SystemHookWorker, id, data)
end
def self.all_hooks_fire(data) def self.all_hooks_fire(data)
SystemHook.all.each do |sh| SystemHook.all.each do |sh|
sh.async_execute data sh.async_execute data
end end
end end
def async_execute(data)
Resque.enqueue(SystemHookWorker, id, data)
end
end end
# == Schema Information # == Schema Information
......
...@@ -27,53 +27,56 @@ class User < ActiveRecord::Base ...@@ -27,53 +27,56 @@ class User < ActiveRecord::Base
validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider} validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider}
validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0} 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)
scope :blocked, where(blocked: true)
scope :active, where(blocked: false)
before_validation :generate_password, on: :create before_validation :generate_password, on: :create
before_save :ensure_authentication_token before_save :ensure_authentication_token
alias_attribute :private_token, :authentication_token alias_attribute :private_token, :authentication_token
def generate_password # Scopes
if self.force_random_password scope :not_in_project, ->(project) { where("id not in (:ids)", ids: project.users.map(&:id) ) }
self.password = self.password_confirmation = Devise.friendly_token.first(8) scope :admins, where(admin: true)
scope :blocked, where(blocked: true)
scope :active, where(blocked: false)
class << self
def filter filter_name
case filter_name
when "admins"; self.admins
when "blocked"; self.blocked
when "wop"; self.without_projects
else
self.active
end
end end
end
def self.filter filter_name def without_projects
case filter_name where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')
when "admins"; self.admins
when "blocked"; self.blocked
when "wop"; self.without_projects
else
self.active
end end
end
def self.without_projects def create_from_omniauth(auth, ldap = false)
where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)') gitlab_auth.create_from_omniauth(auth, ldap)
end end
def self.create_from_omniauth(auth, ldap = false) def find_or_new_for_omniauth(auth)
gitlab_auth.create_from_omniauth(auth, ldap) gitlab_auth.find_or_new_for_omniauth(auth)
end end
def self.find_or_new_for_omniauth(auth) def find_for_ldap_auth(auth, signed_in_resource = nil)
gitlab_auth.find_or_new_for_omniauth(auth) gitlab_auth.find_for_ldap_auth(auth, signed_in_resource)
end end
def self.find_for_ldap_auth(auth, signed_in_resource = nil) def gitlab_auth
gitlab_auth.find_for_ldap_auth(auth, signed_in_resource) Gitlab::Auth.new
end end
def self.gitlab_auth def search query
Gitlab::Auth.new where("name LIKE :query or email LIKE :query", query: "%#{query}%")
end
end end
def self.search query def generate_password
where("name LIKE :query OR email LIKE :query", query: "%#{query}%") if self.force_random_password
self.password = self.password_confirmation = Devise.friendly_token.first(8)
end
end end
end end
......
...@@ -14,62 +14,64 @@ class UsersProject < ActiveRecord::Base ...@@ -14,62 +14,64 @@ class UsersProject < ActiveRecord::Base
after_save :update_repository after_save :update_repository
after_destroy :update_repository after_destroy :update_repository
validates_uniqueness_of :user_id, scope: [:project_id], message: "already exists in project" validates :user, presence: true
validates_presence_of :user_id validates :user_id, uniqueness: { :scope => [:project_id], message: "already exists in project" }
validates_presence_of :project_id validates :project, presence: true
delegate :name, :email, to: :user, prefix: true delegate :name, :email, to: :user, prefix: true
def self.bulk_delete(project, user_ids) class << self
UsersProject.transaction do def bulk_delete(project, user_ids)
UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project| UsersProject.transaction do
users_project.destroy UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
users_project.destroy
end
end end
end end
end
def self.bulk_update(project, user_ids, project_access) def bulk_update(project, user_ids, project_access)
UsersProject.transaction do UsersProject.transaction do
UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project| UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
users_project.project_access = project_access users_project.project_access = project_access
users_project.save users_project.save
end
end end
end end
end
def self.bulk_import(project, user_ids, project_access) def bulk_import(project, user_ids, project_access)
UsersProject.transaction do UsersProject.transaction do
user_ids.each do |user_id| user_ids.each do |user_id|
users_project = UsersProject.new( users_project = UsersProject.new(
project_access: project_access, project_access: project_access,
user_id: user_id user_id: user_id
) )
users_project.project = project users_project.project = project
users_project.save users_project.save
end
end end
end end
end
def self.user_bulk_import(user, project_ids, project_access) def user_bulk_import(user, project_ids, project_access)
UsersProject.transaction do UsersProject.transaction do
project_ids.each do |project_id| project_ids.each do |project_id|
users_project = UsersProject.new( users_project = UsersProject.new(
project_access: project_access, project_access: project_access,
) )
users_project.project_id = project_id users_project.project_id = project_id
users_project.user_id = user.id users_project.user_id = user.id
users_project.save users_project.save
end
end end
end end
end
def self.access_roles def access_roles
{ {
"Guest" => GUEST, "Guest" => GUEST,
"Reporter" => REPORTER, "Reporter" => REPORTER,
"Developer" => DEVELOPER, "Developer" => DEVELOPER,
"Master" => MASTER "Master" => MASTER
} }
end
end end
def role_access def role_access
......
...@@ -7,7 +7,7 @@ class WebHook < ActiveRecord::Base ...@@ -7,7 +7,7 @@ class WebHook < ActiveRecord::Base
default_timeout 10 default_timeout 10
validates :url, presence: true, validates :url, presence: true,
format: { with: URI::regexp(%w(http https)), message: "should be a valid url" } format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
def execute(data) def execute(data)
parsed_url = URI.parse(url) parsed_url = URI.parse(url)
......
...@@ -5,8 +5,9 @@ class Wiki < ActiveRecord::Base ...@@ -5,8 +5,9 @@ class Wiki < ActiveRecord::Base
belongs_to :user belongs_to :user
has_many :notes, as: :noteable, dependent: :destroy has_many :notes, as: :noteable, dependent: :destroy
validates :content, :title, :user_id, presence: true validates :content, presence: true
validates :title, length: 1..250 validates :user, presence: true
validates :title, presence: true, length: 1..250
before_update :set_slug before_update :set_slug
...@@ -16,21 +17,20 @@ class Wiki < ActiveRecord::Base ...@@ -16,21 +17,20 @@ class Wiki < ActiveRecord::Base
protected protected
def self.regenerate_from wiki
regenerated_field = [:slug, :content, :title]
new_wiki = Wiki.new
regenerated_field.each do |field|
new_wiki.send("#{field}=", wiki.send(field))
end
new_wiki
end
def set_slug def set_slug
self.slug = self.title.parameterize self.slug = self.title.parameterize
end end
class << self
def regenerate_from wiki
regenerated_field = [:slug, :content, :title]
new_wiki = Wiki.new
regenerated_field.each do |field|
new_wiki.send("#{field}=", wiki.send(field))
end
new_wiki
end
end
end end
# == Schema Information # == Schema Information
......
...@@ -45,7 +45,7 @@ module Account ...@@ -45,7 +45,7 @@ module Account
# Remove user from all projects and # Remove user from all projects and
# set blocked attribute to true # set blocked attribute to true
def block def block
users_projects.all.each do |membership| users_projects.find_each do |membership|
return false unless membership.destroy return false unless membership.destroy
end end
......
...@@ -2,12 +2,12 @@ module Authority ...@@ -2,12 +2,12 @@ module Authority
# Compatible with all access rights # Compatible with all access rights
# Should be rewrited for new access rights # Should be rewrited for new access rights
def add_access(user, *access) def add_access(user, *access)
access = if access.include?(:admin) access = if access.include?(:admin)
{ project_access: UsersProject::MASTER } { project_access: UsersProject::MASTER }
elsif access.include?(:write) elsif access.include?(:write)
{ project_access: UsersProject::DEVELOPER } { project_access: UsersProject::DEVELOPER }
else else
{ project_access: UsersProject::REPORTER } { project_access: UsersProject::REPORTER }
end end
opts = { user: user } opts = { user: user }
opts.merge!(access) opts.merge!(access)
......
...@@ -8,12 +8,9 @@ module IssueCommonality ...@@ -8,12 +8,9 @@ module IssueCommonality
belongs_to :assignee, class_name: "User" belongs_to :assignee, class_name: "User"
has_many :notes, as: :noteable, dependent: :destroy has_many :notes, as: :noteable, dependent: :destroy
validates_presence_of :project_id validates :project, presence: true
validates_presence_of :author_id validates :author, presence: true
validates :title, presence: true, length: { within: 0..255 }
validates :title,
presence: true,
length: { within: 0..255 }
validates :closed, inclusion: { in: [true, false] } validates :closed, inclusion: { in: [true, false] }
scope :opened, where(closed: false) scope :opened, where(closed: false)
......
...@@ -5,11 +5,11 @@ module PushEvent ...@@ -5,11 +5,11 @@ module PushEvent
false false
end end
def tag? def tag?
data[:ref]["refs/tags"] data[:ref]["refs/tags"]
end end
def branch? def branch?
data[:ref]["refs/heads"] data[:ref]["refs/heads"]
end end
...@@ -25,7 +25,7 @@ module PushEvent ...@@ -25,7 +25,7 @@ module PushEvent
commit_to =~ /^00000/ commit_to =~ /^00000/
end end
def md_ref? def md_ref?
!(rm_ref? || new_ref?) !(rm_ref? || new_ref?)
end end
...@@ -37,7 +37,7 @@ module PushEvent ...@@ -37,7 +37,7 @@ module PushEvent
data[:after] data[:after]
end end
def ref_name def ref_name
if tag? if tag?
tag_name tag_name
else else
......
...@@ -20,5 +20,5 @@ describe Group do ...@@ -20,5 +20,5 @@ describe Group do
it { should validate_uniqueness_of(:name) } it { should validate_uniqueness_of(:name) }
it { should validate_presence_of :code } it { should validate_presence_of :code }
it { should validate_uniqueness_of(:code) } it { should validate_uniqueness_of(:code) }
it { should validate_presence_of :owner_id } it { should validate_presence_of :owner }
end end
...@@ -12,7 +12,7 @@ describe Milestone do ...@@ -12,7 +12,7 @@ describe Milestone do
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:title) } it { should validate_presence_of(:title) }
it { should validate_presence_of(:project_id) } it { should validate_presence_of(:project) }
it { should ensure_inclusion_of(:closed).in_array([true, false]) } it { should ensure_inclusion_of(:closed).in_array([true, false]) }
end end
......
...@@ -10,7 +10,7 @@ describe ProtectedBranch do ...@@ -10,7 +10,7 @@ describe ProtectedBranch do
end end
describe 'Validation' do describe 'Validation' do
it { should validate_presence_of(:project_id) } it { should validate_presence_of(:project) }
it { should validate_presence_of(:name) } it { should validate_presence_of(:name) }
end end
......
...@@ -13,8 +13,8 @@ describe Snippet do ...@@ -13,8 +13,8 @@ describe Snippet do
end end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:author_id) } it { should validate_presence_of(:author) }
it { should validate_presence_of(:project_id) } it { should validate_presence_of(:project) }
it { should validate_presence_of(:title) } it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_within(0..255) } it { should ensure_length_of(:title).is_within(0..255) }
......
...@@ -13,10 +13,10 @@ describe UsersProject do ...@@ -13,10 +13,10 @@ describe UsersProject do
describe "Validation" do describe "Validation" do
let!(:users_project) { create(:users_project) } let!(:users_project) { create(:users_project) }
it { should validate_presence_of(:user_id) } it { should validate_presence_of(:user) }
it { should validate_uniqueness_of(:user_id).scoped_to(:project_id).with_message(/already exists/) } it { should validate_uniqueness_of(:user_id).scoped_to(:project_id).with_message(/already exists/) }
it { should validate_presence_of(:project_id) } it { should validate_presence_of(:project) }
end end
describe "Delegate methods" do describe "Delegate methods" do
......
...@@ -16,6 +16,6 @@ describe Wiki do ...@@ -16,6 +16,6 @@ describe Wiki do
it { should validate_presence_of(:title) } it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_within(1..250) } it { should ensure_length_of(:title).is_within(1..250) }
it { should validate_presence_of(:content) } it { should validate_presence_of(:content) }
it { should validate_presence_of(:user_id) } it { should validate_presence_of(:user) }
end end
end end
...@@ -11,8 +11,8 @@ describe Issue, "IssueCommonality" do ...@@ -11,8 +11,8 @@ describe Issue, "IssueCommonality" do
end end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:project_id) } it { should validate_presence_of(:project) }
it { should validate_presence_of(:author_id) } it { should validate_presence_of(:author) }
it { should validate_presence_of(:title) } it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_at_least(0).is_at_most(255) } it { should ensure_length_of(:title).is_at_least(0).is_at_most(255) }
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment