From 1c9a9e803eaa85cee225175aabbc4ba2cc0d02f9 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 13 Sep 2012 00:47:39 +0400 Subject: [PATCH] #638: some refactoring --- app/controllers/platforms/platforms_controller.rb | 8 +++++--- app/controllers/platforms/repositories_controller.rb | 8 +++++--- app/models/platform.rb | 8 ++++++++ app/models/relation.rb | 12 ++++-------- app/models/repository.rb | 8 ++++++++ 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/app/controllers/platforms/platforms_controller.rb b/app/controllers/platforms/platforms_controller.rb index 0909fbb11..a35f64b5c 100644 --- a/app/controllers/platforms/platforms_controller.rb +++ b/app/controllers/platforms/platforms_controller.rb @@ -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) diff --git a/app/controllers/platforms/repositories_controller.rb b/app/controllers/platforms/repositories_controller.rb index 25084fb2e..7afc34a21 100644 --- a/app/controllers/platforms/repositories_controller.rb +++ b/app/controllers/platforms/repositories_controller.rb @@ -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) diff --git a/app/models/platform.rb b/app/models/platform.rb index 26dcbe5f1..49a84e898 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -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 diff --git a/app/models/relation.rb b/app/models/relation.rb index 09debb77e..76829a849 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -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 diff --git a/app/models/repository.rb b/app/models/repository.rb index a761d4881..cd28fe6ec 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -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