#638: some refactoring

This commit is contained in:
Vokhmin Alexey V 2012-09-13 00:47:39 +04:00
parent 1a75a5a394
commit 1c9a9e803e
5 changed files with 30 additions and 14 deletions

View File

@ -86,18 +86,20 @@ class Platforms::PlatformsController < Platforms::BaseController
end
def remove_members
Relation.remove_members(params[:user_remove], @platform)
user_ids = params[:user_remove] ?
params[:user_remove].map{ |k, v| k if v.first == '1' }.compact : []
User.where(:id => user_ids).each{ |user| @platform.remove_member(user) }
redirect_to members_platform_path(@platform)
end
def remove_member
Relation.remove_members(params[:member_id], @platform)
User.where(:id => params[:member_id]).each{ |user| @platform.remove_member(user) }
redirect_to members_platform_path(@platform)
end
def add_member
if member = User.where(:id => params[:member_id]).first
if Relation.add_member(member, @platform)
if @platform.add_member(member)
flash[:notice] = t('flash.platform.members.successfully_added', :name => member.uname)
else
flash[:error] = t('flash.platform.members.error_in_adding', :name => member.uname)

View File

@ -34,18 +34,20 @@ class Platforms::RepositoriesController < Platforms::BaseController
end
def remove_members
Relation.remove_members(params[:user_remove], @repository)
user_ids = params[:user_remove] ?
params[:user_remove].map{ |k, v| k if v.first == '1' }.compact : []
User.where(:id => user_ids).each{ |user| @repository.remove_member(user) }
redirect_to edit_platform_repository_path(@platform, @repository)
end
def remove_member
Relation.remove_members(params[:member_id], @repository)
User.where(:id => params[:member_id]).each{ |user| @repository.remove_member(user) }
redirect_to edit_platform_repository_path(@platform, @repository)
end
def add_member
if member = User.where(:id => params[:member_id]).first
if Relation.add_member(member, @repository)
if @repository.add_member(member)
flash[:notice] = t('flash.repository.members.successfully_added', :name => member.uname)
else
flash[:error] = t('flash.repository.members.error_in_adding', :name => member.uname)

View File

@ -73,6 +73,14 @@ class Platform < ActiveRecord::Base
build_path(name)
end
def add_member(member, role = 'admin')
Relation.add_member(member, self, role)
end
def remove_member(member)
Relation.remove_member(member, self)
end
def symlink_path
Rails.root.join("public", "downloads", name)
end

View File

@ -22,22 +22,18 @@ class Relation < ActiveRecord::Base
r.save
end
def self.add_member(member, target)
def self.add_member(member, target, role)
if target.relations.exists?(:actor_id => member.id, :actor_type => member.class.to_s) || @platform.try(:owner) == member
true
else
rel = target.relations.build(:role => 'admin')
rel = target.relations.build(:role => role)
rel.actor = member
rel.save
end
end
# @param [String, Hash] user_remove
# Hash looks like {"9"=>["1"], "32"=>["1"]}
def self.remove_members(user_remove, target)
member_ids = user_remove.is_a?(Hash) ?
user_remove.map{ |k, v| k if v.first == '1' }.compact : user_remove
Relation.by_target(target).where(:actor_id => member_ids, :actor_type => 'User').each{|r| r.destroy}
def self.remove_member(member, target)
Relation.by_actor(member).by_target(target).each{|r| r.destroy}
end
protected

View File

@ -43,6 +43,14 @@ class Repository < ActiveRecord::Base
end
end
def add_member(member, role = 'admin')
Relation.add_member(member, self, role)
end
def remove_member(member)
Relation.remove_member(member, self)
end
class << self
def build_stub(platform)
rep = Repository.new