From cee1ee6cb66579e3a99328a6af41b588ce979026 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Sun, 23 Sep 2012 20:22:49 +0400 Subject: [PATCH] #648: small refactoring --- .../projects/projects_controller.rb | 20 ++++++++----------- app/models/project.rb | 5 +++++ .../{_filter.html.haml => _filters.html.haml} | 2 +- app/views/projects/projects/index.html.haml | 2 +- config/locales/models/relation.en.yml | 1 + config/locales/models/relation.ru.yml | 1 + 6 files changed, 17 insertions(+), 14 deletions(-) rename app/views/projects/projects/{_filter.html.haml => _filters.html.haml} (94%) diff --git a/app/controllers/projects/projects_controller.rb b/app/controllers/projects/projects_controller.rb index 601795e08..4ad644e01 100644 --- a/app/controllers/projects/projects_controller.rb +++ b/app/controllers/projects/projects_controller.rb @@ -5,7 +5,7 @@ class Projects::ProjectsController < Projects::BaseController def index @groups = params[:groups] || [] - @user_owner = params[:user_owner] == ['true'] ? true : false + @user_owner = params[:user_owner] ? true : false @projects = Project.accessible_by(current_ability, :membered) respond_to do |format| @@ -107,18 +107,14 @@ class Projects::ProjectsController < Projects::BaseController res[:total_count] = projects.count - if user_owner - project_ids = current_user.own_projects.map(&:id) - if groups.present? - projects = projects.where("groups.id in (?) OR projects.id in (?)", groups, project_ids). - joins(:relations). - joins('RIGHT OUTER JOIN "groups" ON "groups"."id" = "relations"."actor_id"') - else - projects = projects.where(:id => project_ids) - end - else - projects = projects.where(:groups => {:id => groups}).joins(:groups) if groups.present? + if user_owner && groups.present? + projects = projects.by_groups_or_owner(groups, current_user) + elsif groups.present? + projects = projects.where(:groups => {:id => groups}).joins(:groups) + elsif user_owner + projects = projects.where(:owner_id => current_user, :owner_type => 'User') end + projects = projects.search(params[:sSearch]).search_order if params[:sSearch].present? res[:filtered_count] = projects.count diff --git a/app/models/project.rb b/app/models/project.rb index 535409d9a..f47128b52 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -49,6 +49,11 @@ class Project < ActiveRecord::Base WHERE (ptr.repository_id = #{ repository_id }) ) ) } + scope :by_groups_or_owner, lambda { |group_ids, owner_id| + where("(relations.actor_id IN (?) AND relations.actor_type = 'Group') OR (projects.owner_id = ? AND projects.owner_type = 'User')", group_ids, owner_id). + joins(:relations) + } + before_create :set_maintainer after_save :attach_to_personal_repository diff --git a/app/views/projects/projects/_filter.html.haml b/app/views/projects/projects/_filters.html.haml similarity index 94% rename from app/views/projects/projects/_filter.html.haml rename to app/views/projects/projects/_filters.html.haml index 4f79c373d..1fddd37fb 100644 --- a/app/views/projects/projects/_filter.html.haml +++ b/app/views/projects/projects/_filters.html.haml @@ -11,7 +11,7 @@ :color => '00a651', :check_box_name => 'user_owner', :check_box_value => 'true', - :name => t("layout.relations.user_owner"), + :name => t("layout.relations.my"), :count => current_user.own_projects.count .bordered.bpadding20 diff --git a/app/views/projects/projects/index.html.haml b/app/views/projects/projects/index.html.haml index 3cec22fc9..c88a9292b 100644 --- a/app/views/projects/projects/index.html.haml +++ b/app/views/projects/projects/index.html.haml @@ -1,5 +1,5 @@ -set_meta_tags :title => t('layout.projects.list_header') --render 'filter' +-render 'filters' .toolbar = link_to t('layout.projects.new'), new_project_path, :class => 'button' if can?(:create, Project) diff --git a/config/locales/models/relation.en.yml b/config/locales/models/relation.en.yml index 620780bf0..1dc1fb3bb 100644 --- a/config/locales/models/relation.en.yml +++ b/config/locales/models/relation.en.yml @@ -1,6 +1,7 @@ en: layout: relations: + my: My user_owner: I'm owner group_owner: I'm member of owner group user: I'm collaborator diff --git a/config/locales/models/relation.ru.yml b/config/locales/models/relation.ru.yml index 764ede94a..857d6df7c 100644 --- a/config/locales/models/relation.ru.yml +++ b/config/locales/models/relation.ru.yml @@ -1,6 +1,7 @@ ru: layout: relations: + my: Мои user_owner: Я - владелец group_owner: Я состою в группе-владельцев user: Я - участник