project.rb
1096 lines
| 38.2 KiB
| text/x-ruby
|
RubyLexer
|
r5020 | # Redmine - project management software | ||
|
r14856 | # Copyright (C) 2006-2016 Jean-Philippe Lang | ||
|
r330 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6397 | # | ||
|
r330 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6397 | # | ||
|
r330 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
class Project < ActiveRecord::Base | ||||
|
r4378 | include Redmine::SafeAttributes | ||
|
r13459 | include Redmine::NestedSet::ProjectNestedSet | ||
|
r6397 | |||
|
r546 | # Project statuses | ||
STATUS_ACTIVE = 1 | ||||
|
r9700 | STATUS_CLOSED = 5 | ||
|
r546 | STATUS_ARCHIVED = 9 | ||
|
r6397 | |||
|
r4288 | # Maximum length for project identifiers | ||
IDENTIFIER_MAX_LENGTH = 100 | ||||
|
r6397 | |||
|
r12800 | # Specific overridden Activities | ||
|
r2969 | has_many :time_entry_activities | ||
|
r13950 | has_many :memberships, :class_name => 'Member', :inverse_of => :project | ||
# Memberships of active users only | ||||
|
r13100 | has_many :members, | ||
|
r13950 | lambda { joins(:principal).where(:users => {:type => 'User', :status => Principal::STATUS_ACTIVE}) } | ||
|
r714 | has_many :enabled_modules, :dependent => :delete_all | ||
|
r13145 | has_and_belongs_to_many :trackers, lambda {order(:position)} | ||
|
r13100 | has_many :issues, :dependent => :destroy | ||
|
r640 | has_many :issue_changes, :through => :issues, :source => :journals | ||
|
r15937 | has_many :versions, :dependent => :destroy | ||
|
r14404 | belongs_to :default_version, :class_name => 'Version' | ||
|
r12146 | has_many :time_entries, :dependent => :destroy | ||
|
r15938 | has_many :queries, :dependent => :delete_all | ||
|
r330 | has_many :documents, :dependent => :destroy | ||
|
r13100 | has_many :news, lambda {includes(:author)}, :dependent => :destroy | ||
has_many :issue_categories, lambda {order("#{IssueCategory.table_name}.name")}, :dependent => :delete_all | ||||
has_many :boards, lambda {order("position ASC")}, :dependent => :destroy | ||||
has_one :repository, lambda {where(["is_default = ?", true])} | ||||
|
r8530 | has_many :repositories, :dependent => :destroy | ||
|
r703 | has_many :changesets, :through => :repository | ||
|
r330 | has_one :wiki, :dependent => :destroy | ||
|
r888 | # Custom field for the project issues | ||
|
r6397 | has_and_belongs_to_many :issue_custom_fields, | ||
|
r13100 | lambda {order("#{CustomField.table_name}.position")}, | ||
|
r888 | :class_name => 'IssueCustomField', | ||
:join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", | ||||
:association_foreign_key => 'custom_field_id' | ||||
|
r6397 | |||
|
r2115 | acts_as_attachable :view_permission => :view_files, | ||
|
r13283 | :edit_permission => :manage_files, | ||
|
r2115 | :delete_permission => :manage_files | ||
|
r755 | |||
|
r1578 | acts_as_customizable | ||
|
r13474 | acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => "#{Project.table_name}.id", :permission => nil | ||
|
r755 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, | ||
|
r3368 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}}, | ||
|
r1639 | :author => nil | ||
|
r755 | |||
|
r4525 | attr_protected :status | ||
|
r6397 | |||
|
r1074 | validates_presence_of :name, :identifier | ||
|
r13341 | validates_uniqueness_of :identifier, :if => Proc.new {|p| p.identifier_changed?} | ||
|
r4288 | validates_length_of :name, :maximum => 255 | ||
|
r1443 | validates_length_of :homepage, :maximum => 255 | ||
|
r15605 | validates_length_of :identifier, :maximum => IDENTIFIER_MAX_LENGTH | ||
|
r12800 | # downcase letters, digits, dashes but not digits only | ||
|
r10733 | validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :if => Proc.new { |p| p.identifier_changed? } | ||
|
r2716 | # reserved words | ||
validates_exclusion_of :identifier, :in => %w( new ) | ||||
|
r13465 | validate :validate_parent | ||
|
r2716 | |||
|
r11068 | after_save :update_inherited_members, :if => Proc.new {|project| project.inherit_members_changed?} | ||
|
r13459 | after_save :remove_inherited_member_roles, :add_inherited_member_roles, :if => Proc.new {|project| project.parent_id_changed?} | ||
|
r13465 | after_update :update_versions_from_hierarchy_change, :if => Proc.new {|project| project.parent_id_changed?} | ||
|
r5051 | before_destroy :delete_all_members | ||
|
r1812 | |||
|
r10723 | scope :has_module, lambda {|mod| | ||
where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s) | ||||
} | ||||
scope :active, lambda { where(:status => STATUS_ACTIVE) } | ||||
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } | ||||
scope :all_public, lambda { where(:is_public => true) } | ||||
scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) } | ||||
|
r11506 | scope :allowed_to, lambda {|*args| | ||
|
r8571 | user = User.current | ||
permission = nil | ||||
if args.first.is_a?(Symbol) | ||||
permission = args.shift | ||||
else | ||||
user = args.shift | ||||
permission = args.shift | ||||
end | ||||
|
r10723 | where(Project.allowed_to_condition(user, permission, *args)) | ||
|
r8571 | } | ||
|
r9355 | scope :like, lambda {|arg| | ||
|
r7962 | if arg.blank? | ||
|
r10723 | where(nil) | ||
|
r7962 | else | ||
pattern = "%#{arg.to_s.strip.downcase}%" | ||||
|
r10723 | where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern) | ||
|
r7962 | end | ||
} | ||||
|
r13159 | scope :sorted, lambda {order(:lft)} | ||
|
r14231 | scope :having_trackers, lambda { | ||
where("#{Project.table_name}.id IN (SELECT DISTINCT project_id FROM #{table_name_prefix}projects_trackers#{table_name_suffix})") | ||||
} | ||||
|
r6397 | |||
|
r8167 | def initialize(attributes=nil, *args) | ||
|
r4346 | super | ||
|
r6397 | |||
|
r4346 | initialized = (attributes || {}).stringify_keys | ||
|
r6397 | if !initialized.key?('identifier') && Setting.sequential_project_identifiers? | ||
|
r4346 | self.identifier = Project.next_identifier | ||
end | ||||
if !initialized.key?('is_public') | ||||
self.is_public = Setting.default_projects_public? | ||||
end | ||||
if !initialized.key?('enabled_module_names') | ||||
self.enabled_module_names = Setting.default_projects_modules | ||||
end | ||||
if !initialized.key?('trackers') && !initialized.key?('tracker_ids') | ||||
|
r11164 | default = Setting.default_projects_tracker_ids | ||
if default.is_a?(Array) | ||||
|
r13100 | self.trackers = Tracker.where(:id => default.map(&:to_i)).sorted.to_a | ||
|
r11164 | else | ||
|
r13100 | self.trackers = Tracker.sorted.to_a | ||
|
r11164 | end | ||
|
r4346 | end | ||
end | ||||
|
r6397 | |||
|
r393 | def identifier=(identifier) | ||
super unless identifier_frozen? | ||||
end | ||||
|
r6397 | |||
|
r393 | def identifier_frozen? | ||
|
r9527 | errors[:identifier].blank? && !(new_record? || identifier.blank?) | ||
|
r393 | end | ||
|
r879 | |||
|
r330 | # returns latest created projects | ||
# non public projects will be returned only if user is a member of those | ||||
def self.latest(user=nil, count=5) | ||||
|
r14472 | visible(user).limit(count). | ||
order(:created_on => :desc). | ||||
where("#{table_name}.created_on >= ?", 30.days.ago). | ||||
to_a | ||||
|
r11185 | end | ||
|
r330 | |||
|
r6084 | # Returns true if the project is visible to +user+ or to the current user. | ||
def visible?(user=User.current) | ||||
user.allowed_to?(:view_project, self) | ||||
end | ||||
|
r6397 | |||
|
r5204 | # Returns a SQL conditions string used to find all projects visible by the specified user. | ||
|
r2536 | # | ||
# Examples: | ||||
|
r5204 | # Project.visible_condition(admin) => "projects.status = 1" | ||
# Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))" | ||||
# Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))" | ||||
def self.visible_condition(user, options={}) | ||||
allowed_to_condition(user, :view_project, options) | ||||
|
r546 | end | ||
|
r6397 | |||
|
r5204 | # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+ | ||
# | ||||
# Valid options: | ||||
|
r15846 | # * :skip_pre_condition => true don't check that the module is enabled (eg. when the condition is already set elsewhere in the query) | ||
# * :project => project limit the condition to project | ||||
# * :with_subprojects => true limit the condition to project and its subprojects | ||||
# * :member => true limit the condition to the user projects | ||||
|
r1213 | def self.allowed_to_condition(user, permission, options={}) | ||
|
r9700 | perm = Redmine::AccessControl.permission(permission) | ||
base_statement = (perm && perm.read? ? "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED}" : "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}") | ||||
|
r15846 | if !options[:skip_pre_condition] && perm && perm.project_module | ||
|
r9700 | # If the permission belongs to a project module, make sure the module is enabled | ||
|
r15860 | base_statement << " AND EXISTS (SELECT 1 AS one FROM #{EnabledModule.table_name} em WHERE em.project_id = #{Project.table_name}.id AND em.name='#{perm.project_module}')" | ||
|
r1905 | end | ||
|
r13115 | if project = options[:project] | ||
project_statement = project.project_condition(options[:with_subprojects]) | ||||
|
r1213 | base_statement = "(#{project_statement}) AND (#{base_statement})" | ||
end | ||||
|
r6397 | |||
|
r1162 | if user.admin? | ||
|
r5020 | base_statement | ||
|
r1162 | else | ||
|
r5020 | statement_by_role = {} | ||
|
r5339 | unless options[:member] | ||
|
r11735 | role = user.builtin_role | ||
|
r5339 | if role.allowed_to?(permission) | ||
|
r14068 | s = "#{Project.table_name}.is_public = #{connection.quoted_true}" | ||
if user.id | ||||
|
r15368 | group = role.anonymous? ? Group.anonymous : Group.non_member | ||
principal_ids = [user.id, group.id].compact | ||||
s = "(#{s} AND #{Project.table_name}.id NOT IN (SELECT project_id FROM #{Member.table_name} WHERE user_id IN (#{principal_ids.join(',')})))" | ||||
|
r14068 | end | ||
statement_by_role[role] = s | ||||
|
r5020 | end | ||
|
r5339 | end | ||
|
r15742 | user.project_ids_by_role.each do |role, project_ids| | ||
if role.allowed_to?(permission) && project_ids.any? | ||||
statement_by_role[role] = "#{Project.table_name}.id IN (#{project_ids.join(',')})" | ||||
|
r2945 | end | ||
|
r1635 | end | ||
|
r5020 | if statement_by_role.empty? | ||
"1=0" | ||||
else | ||||
|
r5296 | if block_given? | ||
statement_by_role.each do |role, statement| | ||||
if s = yield(role, user) | ||||
statement_by_role[role] = "(#{statement} AND (#{s}))" | ||||
end | ||||
end | ||||
end | ||||
|
r5020 | "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))" | ||
end | ||||
|
r1162 | end | ||
end | ||||
|
r2536 | |||
|
r13053 | def override_roles(role) | ||
|
r13950 | @override_members ||= memberships. | ||
joins(:principal). | ||||
where(:users => {:type => ['GroupAnonymous', 'GroupNonMember']}).to_a | ||||
|
r13552 | |||
|
r13100 | group_class = role.anonymous? ? GroupAnonymous : GroupNonMember | ||
|
r13552 | member = @override_members.detect {|m| m.principal.is_a? group_class} | ||
|
r13100 | member ? member.roles.to_a : [role] | ||
|
r13053 | end | ||
|
r11378 | def principals | ||
|
r15272 | @principals ||= Principal.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).distinct | ||
|
r11378 | end | ||
|
r11379 | def users | ||
|
r15272 | @users ||= User.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).distinct | ||
|
r11379 | end | ||
|
r2835 | # Returns the Systemwide and project specific activities | ||
def activities(include_inactive=false) | ||||
|
r13910 | t = TimeEntryActivity.table_name | ||
scope = TimeEntryActivity.where("#{t}.project_id IS NULL OR #{t}.project_id = ?", id) | ||||
overridden_activity_ids = self.time_entry_activities.pluck(:parent_id).compact | ||||
if overridden_activity_ids.any? | ||||
scope = scope.where("#{t}.id NOT IN (?)", overridden_activity_ids) | ||||
end | ||||
unless include_inactive | ||||
scope = scope.active | ||||
|
r2835 | end | ||
|
r13910 | scope | ||
|
r2835 | end | ||
|
r2834 | |||
|
r2836 | # Will create a new Project specific Activity or update an existing one | ||
# | ||||
# This will raise a ActiveRecord::Rollback if the TimeEntryActivity | ||||
# does not successfully save. | ||||
def update_or_create_time_entry_activity(id, activity_hash) | ||||
|
r2835 | if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id') | ||
|
r2836 | self.create_time_entry_activity_if_needed(activity_hash) | ||
|
r2834 | else | ||
|
r2835 | activity = project.time_entry_activities.find_by_id(id.to_i) | ||
activity.update_attributes(activity_hash) if activity | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r2836 | # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity | ||
# | ||||
# This will raise a ActiveRecord::Rollback if the TimeEntryActivity | ||||
# does not successfully save. | ||||
def create_time_entry_activity_if_needed(activity) | ||||
|
r2835 | if activity['parent_id'] | ||
parent_activity = TimeEntryActivity.find(activity['parent_id']) | ||||
activity['name'] = parent_activity.name | ||||
activity['position'] = parent_activity.position | ||||
|
r12781 | if Enumeration.overriding_change?(activity, parent_activity) | ||
|
r2836 | project_activity = self.time_entry_activities.create(activity) | ||
if project_activity.new_record? | ||||
|
r12781 | raise ActiveRecord::Rollback, "Overriding TimeEntryActivity was not successfully saved" | ||
|
r2836 | else | ||
|
r12339 | self.time_entries. | ||
|
r14310 | where(:activity_id => parent_activity.id). | ||
update_all(:activity_id => project_activity.id) | ||||
|
r2836 | end | ||
|
r2835 | end | ||
|
r2834 | end | ||
end | ||||
|
r2536 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. | ||
# | ||||
# Examples: | ||||
# project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" | ||||
# project.project_condition(false) => "projects.id = 1" | ||||
|
r1283 | def project_condition(with_subprojects) | ||
cond = "#{Project.table_name}.id = #{id}" | ||||
|
r2302 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects | ||
|
r1283 | cond | ||
end | ||||
|
r6397 | |||
|
r994 | def self.find(*args) | ||
if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) | ||||
project = find_by_identifier(*args) | ||||
raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? | ||||
project | ||||
else | ||||
super | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r9346 | def self.find_by_param(*args) | ||
self.find(*args) | ||||
end | ||||
|
r11267 | alias :base_reload :reload | ||
|
r8859 | def reload(*args) | ||
|
r11378 | @principals = nil | ||
|
r11379 | @users = nil | ||
|
r8859 | @shared_versions = nil | ||
@rolled_up_versions = nil | ||||
@rolled_up_trackers = nil | ||||
@all_issue_custom_fields = nil | ||||
@all_time_entry_custom_fields = nil | ||||
@to_param = nil | ||||
@allowed_parents = nil | ||||
@allowed_permissions = nil | ||||
@actions_allowed = nil | ||||
|
r10905 | @start_date = nil | ||
@due_date = nil | ||||
|
r13053 | @override_members = nil | ||
|
r13128 | @assignable_users = nil | ||
|
r11267 | base_reload(*args) | ||
|
r8859 | end | ||
|
r994 | def to_param | ||
|
r14232 | if new_record? | ||
nil | ||||
else | ||||
# id is used for projects with a numeric identifier (compatibility) | ||||
@to_param ||= (identifier.to_s =~ %r{^\d*$} ? id.to_s : identifier) | ||||
end | ||||
|
r994 | end | ||
|
r6397 | |||
|
r546 | def active? | ||
self.status == STATUS_ACTIVE | ||||
end | ||||
|
r6397 | |||
|
r15689 | def closed? | ||
self.status == STATUS_CLOSED | ||||
end | ||||
|
r4171 | def archived? | ||
self.status == STATUS_ARCHIVED | ||||
end | ||||
|
r6397 | |||
|
r3009 | # Archives the project and its descendants | ||
|
r546 | def archive | ||
|
r3009 | # Check that there is no issue of a non descendant project that is assigned | ||
# to one of the project or descendant versions | ||||
|
r13312 | version_ids = self_and_descendants.joins(:versions).pluck("#{Version.table_name}.id") | ||
if version_ids.any? && | ||||
|
r10701 | Issue. | ||
includes(:project). | ||||
where("#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?", lft, rgt). | ||||
|
r13312 | where(:fixed_version_id => version_ids). | ||
|
r10701 | exists? | ||
|
r3009 | return false | ||
|
r330 | end | ||
|
r3009 | Project.transaction do | ||
archive! | ||||
end | ||||
true | ||||
|
r546 | end | ||
|
r6397 | |||
|
r2302 | # Unarchives the project | ||
# All its ancestors must be active | ||||
|
r546 | def unarchive | ||
|
r15689 | return false if ancestors.detect {|a| a.archived?} | ||
new_status = STATUS_ACTIVE | ||||
if parent | ||||
new_status = parent.status | ||||
end | ||||
update_attribute :status, new_status | ||||
|
r546 | end | ||
|
r6397 | |||
|
r9700 | def close | ||
self_and_descendants.status(STATUS_ACTIVE).update_all :status => STATUS_CLOSED | ||||
end | ||||
def reopen | ||||
self_and_descendants.status(STATUS_CLOSED).update_all :status => STATUS_ACTIVE | ||||
end | ||||
|
r2302 | # Returns an array of projects the project can be moved to | ||
|
r2945 | # by the current user | ||
|
r13465 | def allowed_parents(user=User.current) | ||
|
r2945 | return @allowed_parents if @allowed_parents | ||
|
r13465 | @allowed_parents = Project.allowed_to(user, :add_subprojects).to_a | ||
|
r3124 | @allowed_parents = @allowed_parents - self_and_descendants | ||
|
r13465 | if user.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?) | ||
|
r3124 | @allowed_parents << nil | ||
end | ||||
|
r2945 | unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent) | ||
@allowed_parents << parent | ||||
end | ||||
@allowed_parents | ||||
end | ||||
|
r6397 | |||
|
r2945 | # Sets the parent of the project with authorization check | ||
def set_allowed_parent!(p) | ||||
|
r13467 | ActiveSupport::Deprecation.warn "Project#set_allowed_parent! is deprecated and will be removed in Redmine 4, use #safe_attributes= instead." | ||
|
r13465 | p = p.id if p.is_a?(Project) | ||
send :safe_attributes, {:project_id => p} | ||||
save | ||||
|
r2302 | end | ||
|
r6397 | |||
|
r13467 | # Sets the parent of the project and saves the project | ||
|
r2302 | # Argument can be either a Project, a String, a Fixnum or nil | ||
def set_parent!(p) | ||||
|
r13465 | if p.is_a?(Project) | ||
|
r13459 | self.parent = p | ||
|
r2302 | else | ||
|
r13465 | self.parent_id = p | ||
|
r2302 | end | ||
|
r13465 | save | ||
|
r330 | end | ||
|
r6397 | |||
|
r15158 | # Returns a scope of the trackers used by the project and its active sub projects | ||
def rolled_up_trackers(include_subprojects=true) | ||||
if include_subprojects | ||||
@rolled_up_trackers ||= rolled_up_trackers_base_scope. | ||||
where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ?", lft, rgt) | ||||
else | ||||
rolled_up_trackers_base_scope. | ||||
where(:projects => {:id => id}) | ||||
end | ||||
end | ||||
def rolled_up_trackers_base_scope | ||||
Tracker. | ||||
joins(projects: :enabled_modules). | ||||
where("#{Project.table_name}.status <> ?", STATUS_ARCHIVED). | ||||
where(:enabled_modules => {:name => 'issue_tracking'}). | ||||
|
r15272 | distinct. | ||
|
r15158 | sorted | ||
|
r1057 | end | ||
|
r6397 | |||
|
r2909 | # Closes open and locked project versions that are completed | ||
def close_completed_versions | ||||
Version.transaction do | ||||
|
r12454 | versions.where(:status => %w(open locked)).each do |version| | ||
|
r2909 | if version.completed? | ||
version.update_attribute(:status, 'closed') | ||||
end | ||||
end | ||||
end | ||||
end | ||||
|
r3646 | |||
# Returns a scope of the Versions on subprojects | ||||
def rolled_up_versions | ||||
@rolled_up_versions ||= | ||||
|
r11643 | Version. | ||
|
r13100 | joins(:project). | ||
|
r11643 | where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED) | ||
|
r3646 | end | ||
|
r6397 | |||
|
r3009 | # Returns a scope of the Versions used by the project | ||
def shared_versions | ||||
|
r8745 | if new_record? | ||
|
r11643 | Version. | ||
|
r13100 | joins(:project). | ||
preload(:project). | ||||
|
r11643 | where("#{Project.table_name}.status <> ? AND #{Version.table_name}.sharing = 'system'", STATUS_ARCHIVED) | ||
|
r8745 | else | ||
@shared_versions ||= begin | ||||
r = root? ? self : root | ||||
|
r11643 | Version. | ||
|
r13100 | joins(:project). | ||
preload(:project). | ||||
|
r11643 | where("#{Project.table_name}.id = #{id}" + | ||
" OR (#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND (" + | ||||
" #{Version.table_name}.sharing = 'system'" + | ||||
" OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" + | ||||
" OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" + | ||||
" OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" + | ||||
"))") | ||||
|
r8745 | end | ||
|
r5123 | end | ||
|
r3009 | end | ||
|
r2635 | # Returns a hash of project users grouped by role | ||
def users_by_role | ||||
|
r12454 | members.includes(:user, :roles).inject({}) do |h, m| | ||
|
r2635 | m.roles.each do |r| | ||
h[r] ||= [] | ||||
h[r] << m.user | ||||
end | ||||
h | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r13160 | # Adds user as a project member with the default role | ||
# Used for when a non-admin user creates a project | ||||
def add_default_member(user) | ||||
|
r15370 | role = self.class.default_member_role | ||
|
r13160 | member = Member.new(:project => self, :principal => user, :roles => [role]) | ||
self.members << member | ||||
member | ||||
end | ||||
|
r15370 | # Default role that is given to non-admin users that | ||
# create a project | ||||
def self.default_member_role | ||||
Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first | ||||
end | ||||
|
r962 | # Deletes all project's members | ||
def delete_all_members | ||||
|
r2627 | me, mr = Member.table_name, MemberRole.table_name | ||
|
r13100 | self.class.connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") | ||
|
r15293 | Member.where(:project_id => id).delete_all | ||
|
r962 | end | ||
|
r6397 | |||
|
r13127 | # Return a Principal scope of users/groups issues can be assigned to | ||
|
r15204 | def assignable_users(tracker=nil) | ||
return @assignable_users[tracker] if @assignable_users && @assignable_users[tracker] | ||||
|
r13053 | types = ['User'] | ||
types << 'Group' if Setting.issue_group_assignment? | ||||
|
r15204 | scope = Principal. | ||
|
r13127 | active. | ||
joins(:members => :roles). | ||||
where(:type => types, :members => {:project_id => id}, :roles => {:assignable => true}). | ||||
|
r15272 | distinct. | ||
|
r13127 | sorted | ||
|
r15204 | |||
if tracker | ||||
# Rejects users that cannot the view the tracker | ||||
roles = Role.where(:assignable => true).select {|role| role.permissions_tracker?(:view_issues, tracker)} | ||||
scope = scope.where(:roles => {:id => roles.map(&:id)}) | ||||
end | ||||
@assignable_users ||= {} | ||||
@assignable_users[tracker] = scope | ||||
|
r806 | end | ||
|
r6397 | |||
|
r12782 | # Returns the mail addresses of users that should be always notified on project events | ||
|
r842 | def recipients | ||
|
r4133 | notified_users.collect {|user| user.mail} | ||
|
r842 | end | ||
|
r6397 | |||
|
r3007 | # Returns the users that should be notified on project events | ||
def notified_users | ||||
|
r4133 | # TODO: User part should be extracted to User#notify_about? | ||
|
r15136 | members.preload(:principal).select {|m| m.principal.present? && (m.mail_notification? || m.principal.mail_notification == 'all')}.collect {|m| m.principal} | ||
|
r3007 | end | ||
|
r6397 | |||
|
r11686 | # Returns a scope of all custom fields enabled for project issues | ||
|
r12782 | # (explicitly associated custom fields and custom fields enabled for all projects) | ||
|
r1578 | def all_issue_custom_fields | ||
|
r14236 | if new_record? | ||
@all_issue_custom_fields ||= IssueCustomField. | ||||
sorted. | ||||
where("is_for_all = ? OR id IN (?)", true, issue_custom_field_ids) | ||||
else | ||||
@all_issue_custom_fields ||= IssueCustomField. | ||||
sorted. | ||||
where("is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id" + | ||||
" FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} cfp" + | ||||
" WHERE cfp.project_id = ?)", true, id) | ||||
end | ||||
|
r330 | end | ||
|
r5193 | |||
|
r1420 | def project | ||
self | ||||
end | ||||
|
r6397 | |||
|
r692 | def <=>(project) | ||
|
r14102 | name.casecmp(project.name) | ||
|
r692 | end | ||
|
r6397 | |||
|
r1074 | def to_s | ||
name | ||||
end | ||||
|
r6397 | |||
|
r1074 | # Returns a short description of the projects (first lines) | ||
def short_description(length = 255) | ||||
|
r2302 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description | ||
|
r1074 | end | ||
|
r3958 | |||
|
r3966 | def css_classes | ||
s = 'project' | ||||
s << ' root' if root? | ||||
s << ' child' if child? | ||||
s << (leaf? ? ' leaf' : ' parent') | ||||
|
r9700 | unless active? | ||
if archived? | ||||
s << ' archived' | ||||
else | ||||
s << ' closed' | ||||
end | ||||
end | ||||
|
r3966 | s | ||
end | ||||
|
r3958 | # The earliest start date of a project, based on it's issues and versions | ||
def start_date | ||||
|
r10905 | @start_date ||= [ | ||
|
r4363 | issues.minimum('start_date'), | ||
|
r10905 | shared_versions.minimum('effective_date'), | ||
Issue.fixed_version(shared_versions).minimum('start_date') | ||||
].compact.min | ||||
|
r3958 | end | ||
|
r4070 | # The latest due date of an issue or version | ||
|
r3958 | def due_date | ||
|
r10905 | @due_date ||= [ | ||
|
r4363 | issues.maximum('due_date'), | ||
|
r10905 | shared_versions.maximum('effective_date'), | ||
Issue.fixed_version(shared_versions).maximum('due_date') | ||||
].compact.max | ||||
|
r3958 | end | ||
def overdue? | ||||
|
r14997 | active? && !due_date.nil? && (due_date < User.current.today) | ||
|
r3958 | end | ||
# Returns the percent completed for this project, based on the | ||||
# progress on it's versions. | ||||
def completed_percent(options={:include_subprojects => false}) | ||||
if options.delete(:include_subprojects) | ||||
total = self_and_descendants.collect(&:completed_percent).sum | ||||
total / self_and_descendants.count | ||||
else | ||||
if versions.count > 0 | ||||
|
r10883 | total = versions.collect(&:completed_percent).sum | ||
|
r3958 | |||
total / versions.count | ||||
else | ||||
100 | ||||
end | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r9703 | # Return true if this project allows to do the specified action. | ||
|
r2536 | # action can be: | ||
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') | ||||
# * a permission Symbol (eg. :edit_project) | ||||
|
r714 | def allows_to?(action) | ||
|
r9703 | if archived? | ||
# No action allowed on archived projects | ||||
return false | ||||
end | ||||
unless active? || Redmine::AccessControl.read_action?(action) | ||||
# No write action allowed on closed projects | ||||
return false | ||||
end | ||||
# No action allowed on disabled modules | ||||
|
r714 | if action.is_a? Hash | ||
allowed_actions.include? "#{action[:controller]}/#{action[:action]}" | ||||
else | ||||
allowed_permissions.include? action | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r12592 | # Return the enabled module with the given name | ||
# or nil if the module is not enabled for the project | ||||
|
r12591 | def enabled_module(name) | ||
name = name.to_s | ||||
enabled_modules.detect {|m| m.name == name} | ||||
end | ||||
|
r12592 | # Return true if the module with the given name is enabled | ||
|
r12591 | def module_enabled?(name) | ||
enabled_module(name).present? | ||||
|
r714 | end | ||
|
r6397 | |||
|
r714 | def enabled_module_names=(module_names) | ||
|
r2412 | if module_names && module_names.is_a?(Array) | ||
|
r4346 | module_names = module_names.collect(&:to_s).reject(&:blank?) | ||
|
r5145 | self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)} | ||
|
r2412 | else | ||
enabled_modules.clear | ||||
|
r714 | end | ||
end | ||||
|
r6397 | |||
|
r4346 | # Returns an array of the enabled modules names | ||
def enabled_module_names | ||||
enabled_modules.collect(&:name) | ||||
end | ||||
|
r5978 | |||
# Enable a specific module | ||||
# | ||||
# Examples: | ||||
# project.enable_module!(:issue_tracking) | ||||
# project.enable_module!("issue_tracking") | ||||
def enable_module!(name) | ||||
enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name) | ||||
end | ||||
# Disable a module if it exists | ||||
# | ||||
# Examples: | ||||
# project.disable_module!(:issue_tracking) | ||||
# project.disable_module!("issue_tracking") | ||||
# project.disable_module!(project.enabled_modules.first) | ||||
def disable_module!(target) | ||||
target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target) | ||||
target.destroy unless target.blank? | ||||
end | ||||
|
r4378 | safe_attributes 'name', | ||
'description', | ||||
'homepage', | ||||
'is_public', | ||||
'identifier', | ||||
'custom_field_values', | ||||
'custom_fields', | ||||
|
r4415 | 'tracker_ids', | ||
|
r13465 | 'issue_custom_field_ids', | ||
|
r14404 | 'parent_id', | ||
'default_version_id' | ||||
|
r3973 | |||
|
r4525 | safe_attributes 'enabled_module_names', | ||
|
r15370 | :if => lambda {|project, user| | ||
if project.new_record? | ||||
if user.admin? | ||||
true | ||||
else | ||||
default_member_role.has_permission?(:select_project_modules) | ||||
end | ||||
else | ||||
user.allowed_to?(:select_project_modules, project) | ||||
end | ||||
} | ||||
|
r6397 | |||
|
r11068 | safe_attributes 'inherit_members', | ||
|
r11071 | :if => lambda {|project, user| project.parent.nil? || project.parent.visible?(user)} | ||
|
r11068 | |||
|
r13465 | def safe_attributes=(attrs, user=User.current) | ||
return unless attrs.is_a?(Hash) | ||||
attrs = attrs.deep_dup | ||||
@unallowed_parent_id = nil | ||||
|
r14237 | if new_record? || attrs.key?('parent_id') | ||
parent_id_param = attrs['parent_id'].to_s | ||||
if new_record? || parent_id_param != parent_id.to_s | ||||
p = parent_id_param.present? ? Project.find_by_id(parent_id_param) : nil | ||||
unless allowed_parents(user).include?(p) | ||||
attrs.delete('parent_id') | ||||
@unallowed_parent_id = true | ||||
end | ||||
|
r13465 | end | ||
end | ||||
super(attrs, user) | ||||
end | ||||
|
r1776 | # Returns an auto-generated project identifier based on the last identifier used | ||
def self.next_identifier | ||||
|
r11712 | p = Project.order('id DESC').first | ||
|
r1776 | p.nil? ? nil : p.identifier.to_s.succ | ||
end | ||||
|
r330 | |||
|
r2608 | # Copies and saves the Project instance based on the +project+. | ||
|
r2852 | # Duplicates the source project's: | ||
# * Wiki | ||||
# * Versions | ||||
# * Categories | ||||
|
r2608 | # * Issues | ||
# * Members | ||||
# * Queries | ||||
|
r2852 | # | ||
# Accepts an +options+ argument to specify what to copy | ||||
# | ||||
# Examples: | ||||
# project.copy(1) # => copies everything | ||||
# project.copy(1, :only => 'members') # => copies members only | ||||
# project.copy(1, :only => ['members', 'versions']) # => copies members and versions | ||||
def copy(project, options={}) | ||||
|
r2608 | project = project.is_a?(Project) ? project : Project.find(project) | ||
|
r6397 | |||
|
r15673 | to_be_copied = %w(members wiki versions issue_categories issues queries boards) | ||
|
r13100 | to_be_copied = to_be_copied & Array.wrap(options[:only]) unless options[:only].nil? | ||
|
r6397 | |||
|
r2852 | Project.transaction do | ||
|
r2855 | if save | ||
reload | ||||
to_be_copied.each do |name| | ||||
send "copy_#{name}", project | ||||
end | ||||
Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) | ||||
save | ||||
|
r13466 | else | ||
false | ||||
|
r2608 | end | ||
end | ||||
end | ||||
|
r13950 | def member_principals | ||
ActiveSupport::Deprecation.warn "Project#member_principals is deprecated and will be removed in Redmine 4.0. Use #memberships.active instead." | ||||
memberships.active | ||||
end | ||||
|
r10712 | # Returns a new unsaved Project instance with attributes copied from +project+ | ||
|
r2608 | def self.copy_from(project) | ||
|
r10712 | project = project.is_a?(Project) ? project : Project.find(project) | ||
# clear unique attributes | ||||
attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt') | ||||
copy = Project.new(attributes) | ||||
|
r14126 | copy.enabled_module_names = project.enabled_module_names | ||
|
r10712 | copy.trackers = project.trackers | ||
copy.custom_values = project.custom_values.collect {|v| v.clone} | ||||
copy.issue_custom_fields = project.issue_custom_fields | ||||
copy | ||||
|
r2608 | end | ||
|
r4168 | |||
# Yields the given block for each project with its level in the tree | ||||
|
r15373 | def self.project_tree(projects, options={}, &block) | ||
|
r4168 | ancestors = [] | ||
|
r15373 | if options[:init_level] && projects.first | ||
ancestors = projects.first.ancestors.to_a | ||||
end | ||||
|
r4168 | projects.sort_by(&:lft).each do |project| | ||
|
r6397 | while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) | ||
|
r4168 | ancestors.pop | ||
end | ||||
yield project, ancestors.size | ||||
ancestors << project | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r2852 | private | ||
|
r6397 | |||
|
r11068 | def update_inherited_members | ||
if parent | ||||
if inherit_members? && !inherit_members_was | ||||
remove_inherited_member_roles | ||||
add_inherited_member_roles | ||||
elsif !inherit_members? && inherit_members_was | ||||
remove_inherited_member_roles | ||||
end | ||||
end | ||||
end | ||||
def remove_inherited_member_roles | ||||
|
r15501 | member_roles = MemberRole.where(:member_id => membership_ids).to_a | ||
|
r11068 | member_role_ids = member_roles.map(&:id) | ||
member_roles.each do |member_role| | ||||
if member_role.inherited_from && !member_role_ids.include?(member_role.inherited_from) | ||||
member_role.destroy | ||||
end | ||||
end | ||||
end | ||||
def add_inherited_member_roles | ||||
if inherit_members? && parent | ||||
parent.memberships.each do |parent_member| | ||||
member = Member.find_or_new(self.id, parent_member.user_id) | ||||
parent_member.member_roles.each do |parent_member_role| | ||||
member.member_roles << MemberRole.new(:role => parent_member_role.role, :inherited_from => parent_member_role.id) | ||||
end | ||||
member.save! | ||||
end | ||||
|
r13459 | memberships.reset | ||
|
r11068 | end | ||
end | ||||
|
r13465 | def update_versions_from_hierarchy_change | ||
Issue.update_versions_from_hierarchy_change(self) | ||||
end | ||||
def validate_parent | ||||
if @unallowed_parent_id | ||||
errors.add(:parent_id, :invalid) | ||||
elsif parent_id_changed? | ||||
unless parent.nil? || (parent.active? && move_possible?(parent)) | ||||
errors.add(:parent_id, :invalid) | ||||
end | ||||
end | ||||
end | ||||
|
r2852 | # Copies wiki from +project+ | ||
def copy_wiki(project) | ||||
|
r2855 | # Check that the source project has a wiki first | ||
unless project.wiki.nil? | ||||
|
r11102 | wiki = self.wiki || Wiki.new | ||
|
r2857 | wiki.attributes = project.wiki.attributes.dup.except("id", "project_id") | ||
|
r3298 | wiki_pages_map = {} | ||
|
r2855 | project.wiki.pages.each do |page| | ||
|
r3298 | # Skip pages without content | ||
next if page.content.nil? | ||||
|
r2858 | new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on")) | ||
new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id")) | ||||
|
r2855 | new_wiki_page.content = new_wiki_content | ||
wiki.pages << new_wiki_page | ||||
|
r3298 | wiki_pages_map[page.id] = new_wiki_page | ||
end | ||||
|
r11102 | |||
self.wiki = wiki | ||||
|
r3298 | wiki.save | ||
# Reproduce page hierarchy | ||||
project.wiki.pages.each do |page| | ||||
if page.parent_id && wiki_pages_map[page.id] | ||||
wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id] | ||||
wiki_pages_map[page.id].save | ||||
end | ||||
|
r2855 | end | ||
|
r2852 | end | ||
end | ||||
# Copies versions from +project+ | ||||
def copy_versions(project) | ||||
project.versions.each do |version| | ||||
new_version = Version.new | ||||
|
r2858 | new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on") | ||
|
r2852 | self.versions << new_version | ||
end | ||||
end | ||||
# Copies issue categories from +project+ | ||||
def copy_issue_categories(project) | ||||
project.issue_categories.each do |issue_category| | ||||
new_issue_category = IssueCategory.new | ||||
|
r2857 | new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id") | ||
|
r2852 | self.issue_categories << new_issue_category | ||
end | ||||
end | ||||
|
r6397 | |||
|
r2852 | # Copies issues from +project+ | ||
def copy_issues(project) | ||||
|
r3050 | # Stores the source issue id as a key and the copied issues as the | ||
|
r12800 | # value. Used to map the two together for issue relations. | ||
|
r3050 | issues_map = {} | ||
|
r6397 | |||
|
r10151 | # Store status and reopen locked/closed versions | ||
version_statuses = versions.reject(&:open?).map {|version| [version, version.status]} | ||||
version_statuses.each do |version, status| | ||||
version.update_attribute :status, 'open' | ||||
end | ||||
|
r3459 | # Get issues sorted by root_id, lft so that parent issues | ||
# get copied before their children | ||||
|
r12266 | project.issues.reorder('root_id, lft').each do |issue| | ||
|
r2852 | new_issue = Issue.new | ||
|
r10286 | new_issue.copy_from(issue, :subtasks => false, :link => false) | ||
|
r3459 | new_issue.project = self | ||
|
r11613 | # Changing project resets the custom field values | ||
# TODO: handle this in Issue#project= | ||||
new_issue.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} | ||||
|
r10149 | # Reassign fixed_versions by name, since names are unique per project | ||
|
r10150 | if issue.fixed_version && issue.fixed_version.project == project | ||
|
r10149 | new_issue.fixed_version = self.versions.detect {|v| v.name == issue.fixed_version.name} | ||
|
r2852 | end | ||
|
r14233 | # Reassign version custom field values | ||
new_issue.custom_field_values.each do |custom_value| | ||||
if custom_value.custom_field.field_format == 'version' && custom_value.value.present? | ||||
versions = Version.where(:id => custom_value.value).to_a | ||||
new_value = versions.map do |version| | ||||
if version.project == project | ||||
self.versions.detect {|v| v.name == version.name}.try(:id) | ||||
else | ||||
version.id | ||||
end | ||||
end | ||||
new_value.compact! | ||||
new_value = new_value.first unless custom_value.custom_field.multiple? | ||||
custom_value.value = new_value | ||||
end | ||||
end | ||||
|
r10149 | # Reassign the category by name, since names are unique per project | ||
|
r2852 | if issue.category | ||
|
r10149 | new_issue.category = self.issue_categories.detect {|c| c.name == issue.category.name} | ||
|
r2852 | end | ||
|
r3459 | # Parent issue | ||
if issue.parent_id | ||||
if copied_parent = issues_map[issue.parent_id] | ||||
new_issue.parent_issue_id = copied_parent.id | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r2852 | self.issues << new_issue | ||
|
r4370 | if new_issue.new_record? | ||
|
r13384 | logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info? | ||
|
r4370 | else | ||
issues_map[issue.id] = new_issue unless new_issue.new_record? | ||||
end | ||||
|
r3050 | end | ||
|
r10151 | # Restore locked/closed version statuses | ||
version_statuses.each do |version, status| | ||||
version.update_attribute :status, status | ||||
end | ||||
|
r3050 | # Relations after in case issues related each other | ||
project.issues.each do |issue| | ||||
new_issue = issues_map[issue.id] | ||||
|
r4370 | unless new_issue | ||
# Issue was not copied | ||||
next | ||||
end | ||||
|
r6397 | |||
|
r3050 | # Relations | ||
issue.relations_from.each do |source_relation| | ||||
new_issue_relation = IssueRelation.new | ||||
new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") | ||||
new_issue_relation.issue_to = issues_map[source_relation.issue_to_id] | ||||
if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations? | ||||
new_issue_relation.issue_to = source_relation.issue_to | ||||
end | ||||
new_issue.relations_from << new_issue_relation | ||||
end | ||||
|
r6397 | |||
|
r3050 | issue.relations_to.each do |source_relation| | ||
new_issue_relation = IssueRelation.new | ||||
new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id") | ||||
new_issue_relation.issue_from = issues_map[source_relation.issue_from_id] | ||||
if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations? | ||||
new_issue_relation.issue_from = source_relation.issue_from | ||||
end | ||||
new_issue.relations_to << new_issue_relation | ||||
end | ||||
|
r2852 | end | ||
end | ||||
# Copies members from +project+ | ||||
def copy_members(project) | ||||
|
r4495 | # Copy users first, then groups to handle members with inherited and given roles | ||
members_to_copy = [] | ||||
members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)} | ||||
members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)} | ||||
|
r6397 | |||
|
r4495 | members_to_copy.each do |member| | ||
|
r2852 | new_member = Member.new | ||
|
r2858 | new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on") | ||
|
r3136 | # only copy non inherited roles | ||
# inherited roles will be added when copying the group membership | ||||
role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id) | ||||
next if role_ids.empty? | ||||
new_member.role_ids = role_ids | ||||
|
r2852 | new_member.project = self | ||
self.members << new_member | ||||
end | ||||
end | ||||
# Copies queries from +project+ | ||||
def copy_queries(project) | ||||
project.queries.each do |query| | ||||
|
r15938 | new_query = query.class.new | ||
|
r13122 | new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria", "user_id", "type") | ||
|
r2852 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria | ||
new_query.project = self | ||||
|
r7662 | new_query.user_id = query.user_id | ||
|
r15938 | new_query.role_ids = query.role_ids if query.visibility == ::Query::VISIBILITY_ROLES | ||
|
r2852 | self.queries << new_query | ||
end | ||||
end | ||||
|
r2862 | |||
# Copies boards from +project+ | ||||
def copy_boards(project) | ||||
project.boards.each do |board| | ||||
new_board = Board.new | ||||
new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id") | ||||
new_board.project = self | ||||
self.boards << new_board | ||||
end | ||||
end | ||||
|
r6397 | |||
|
r714 | def allowed_permissions | ||
@allowed_permissions ||= begin | ||||
|
r11998 | module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name) | ||
|
r714 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} | ||
end | ||||
end | ||||
def allowed_actions | ||||
@actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten | ||||
end | ||||
|
r2834 | |||
|
r3009 | # Archives subprojects recursively | ||
def archive! | ||||
children.each do |subproject| | ||||
subproject.send :archive! | ||||
end | ||||
update_attribute :status, STATUS_ARCHIVED | ||||
end | ||||
|
r2 | end | ||