2012-04-09 18:11:39 +01:00
|
|
|
class Collaborator
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
include ActiveModel::Validations
|
|
|
|
include ActiveModel::Serializers::JSON
|
|
|
|
include ActiveModel::MassAssignmentSecurity
|
2012-05-04 18:12:51 +01:00
|
|
|
extend ActiveModel::Naming
|
2012-04-09 18:11:39 +01:00
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
attr_accessor :role, :actor, :project, :relation
|
|
|
|
attr_reader :id, :actor_id, :actor_type, :actor_name, :project_id
|
2012-04-09 18:11:39 +01:00
|
|
|
|
|
|
|
attr_accessible :role
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
delegate :new_record?, to: :relation
|
2012-04-09 18:11:39 +01:00
|
|
|
|
|
|
|
class << self
|
2012-04-12 22:44:53 +01:00
|
|
|
def find_by_project(project)
|
|
|
|
res = []
|
|
|
|
project.relations.each do |r|
|
2012-04-26 02:38:33 +01:00
|
|
|
res << from_relation(r) unless project.owner_id == r.actor_id and project.owner_type == r.actor_type
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
def find(id)
|
|
|
|
return self.from_relation(Relation.find(id)) || nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(args)
|
2012-04-24 15:59:50 +01:00
|
|
|
c = self.new(args)
|
|
|
|
return c.save ? c : false
|
2012-04-12 22:44:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create!(args)
|
2012-04-24 15:59:50 +01:00
|
|
|
c = self.new(args)
|
|
|
|
c.save!
|
|
|
|
return c
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(args = {})
|
|
|
|
args.to_options!
|
2012-04-12 22:44:53 +01:00
|
|
|
acc_options = args.select{ |(k, v)| k.in? [:actor, :project, :relation] }
|
2012-04-09 18:11:39 +01:00
|
|
|
acc_options.each_pair do |name, value|
|
|
|
|
send("#{name}=", value)
|
|
|
|
end
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
if args[:project_id].present?
|
2012-04-09 18:11:39 +01:00
|
|
|
@project = Project.find(args[:project_id])
|
|
|
|
end
|
2012-04-12 22:44:53 +01:00
|
|
|
if args[:actor_id].present? and args[:actor_type].present?
|
|
|
|
@actor = args[:actor_type].classify.constantize.find(args[:actor_id])
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
2012-04-24 15:59:50 +01:00
|
|
|
relation.role = args[:role] if args[:role].present? #if @relation.present? and args[:role].present?
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_attributes(attributes, options = {})
|
|
|
|
sanitize_for_mass_assignment(attributes, options[:as]).each_pair do |k, v|
|
|
|
|
send("#{k}=", v)
|
|
|
|
end
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
def relation=(model)
|
|
|
|
@relation = model
|
2012-04-26 02:38:33 +01:00
|
|
|
@actor = @relation.actor
|
2012-04-12 22:44:53 +01:00
|
|
|
@project = @relation.target
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
def id
|
2012-04-24 15:59:50 +01:00
|
|
|
relation.try(:id)
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
def actor_id
|
2012-04-09 18:11:39 +01:00
|
|
|
@actor.try(:id)
|
|
|
|
end
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
def actor_type
|
2012-04-09 18:11:39 +01:00
|
|
|
@actor.class.to_s.underscore
|
|
|
|
end
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
def actor_name
|
2012-04-09 18:11:39 +01:00
|
|
|
if @actor.present?
|
2012-04-16 20:22:08 +01:00
|
|
|
@actor.instance_of?(User) ? "#{@actor.uname}#{ @actor.try(:name) and !@actor.name.empty? ? " (#{@actor.name})" : ''}" : @actor.uname
|
2012-04-09 18:11:39 +01:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-14 20:31:45 +00:00
|
|
|
def actor_uname
|
|
|
|
@actor.uname
|
|
|
|
end
|
|
|
|
|
2012-04-09 18:11:39 +01:00
|
|
|
def project_id
|
|
|
|
@project.try(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def role
|
2012-04-24 15:59:50 +01:00
|
|
|
relation.try(:role)
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def role=(arg)
|
2012-04-24 15:59:50 +01:00
|
|
|
relation.role = arg
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2012-04-24 15:59:50 +01:00
|
|
|
relation.try(:save)
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def save!
|
2012-04-24 15:59:50 +01:00
|
|
|
relation.try(:save!)
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2012-04-24 15:59:50 +01:00
|
|
|
relation.try(:destroy)
|
2013-04-17 21:53:17 +01:00
|
|
|
@actor.check_assigned_issues @project
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
2012-04-12 22:44:53 +01:00
|
|
|
%w{ id actor_id actor_type actor_name project_id role}.inject({}) do |h, e|
|
2012-04-09 18:11:39 +01:00
|
|
|
h.merge(e => send(e))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2012-04-12 22:44:53 +01:00
|
|
|
class << self
|
|
|
|
|
|
|
|
def from_relation(relation)
|
|
|
|
return nil unless relation.present?
|
2014-01-21 04:51:49 +00:00
|
|
|
return self.new(relation: relation)
|
2012-04-12 22:44:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-04-09 18:11:39 +01:00
|
|
|
def relation
|
2012-04-26 02:38:33 +01:00
|
|
|
return @relation if @relation.present? and @relation.actor == @actor and @relation.target == @project
|
2012-04-12 22:44:53 +01:00
|
|
|
|
2012-04-09 18:11:39 +01:00
|
|
|
if @actor.present? and @project.present?
|
2012-04-26 02:38:33 +01:00
|
|
|
@relation = Relation.by_actor(@actor).by_target(@project).limit(1).first
|
|
|
|
@relation ||= Relation.new(:actor_id => @actor.id, :actor_type => @actor.class.to_s,
|
2014-01-21 04:51:49 +00:00
|
|
|
target_id: @project.id, target_type: 'Project')
|
2012-04-09 18:11:39 +01:00
|
|
|
else
|
|
|
|
@relation = Relation.new
|
2012-04-26 02:38:33 +01:00
|
|
|
@relation.actor = @actor
|
2012-04-09 18:11:39 +01:00
|
|
|
@relation.target = @project
|
|
|
|
end
|
2012-04-19 16:27:16 +01:00
|
|
|
@relation
|
2012-04-09 18:11:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
Collaborator.include_root_in_json = false
|