diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 2b4a320..012af17 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -61,3 +61,4 @@ fr: field_top_department_in_ldap: "Département racine dans le LDAP" field_updated_by_organization: Mise à jour par un membre de permission_share_issues_with_organizations: "Partager des demandes avec des organisations" + field_related_organizations: "Organisations concernées" diff --git a/lib/redmine_organizations/patches/issue_patch.rb b/lib/redmine_organizations/patches/issue_patch.rb index ff82915..b2dc55b 100644 --- a/lib/redmine_organizations/patches/issue_patch.rb +++ b/lib/redmine_organizations/patches/issue_patch.rb @@ -51,6 +51,10 @@ def shared_with?(user = User.current) related_organizations_members.include?(user) end + def related_organizations + organizations.to_a + end + end module RedmineOrganizations::Patches::IssuePatch diff --git a/lib/redmine_organizations/patches/issue_query_patch.rb b/lib/redmine_organizations/patches/issue_query_patch.rb index 7bc6ad7..41e3639 100644 --- a/lib/redmine_organizations/patches/issue_query_patch.rb +++ b/lib/redmine_organizations/patches/issue_query_patch.rb @@ -2,6 +2,7 @@ class IssueQuery < Query self.available_columns << QueryColumn.new(:author_organization, :groupable => false) if self.available_columns.select { |c| c.name == :author_organization }.empty? + self.available_columns << QueryColumn.new(:related_organizations, :groupable => false, sortable: false) if self.available_columns.select { |c| c.name == :related_organizations }.empty? end module RedmineOrganizations::Patches @@ -28,6 +29,10 @@ def initialize_available_filters :type => :list, :values => lambda { organization_values }, :label => :field_updated_by_organization + add_available_filter "related_organizations", + :type => :list_optional, + :values => lambda { organization_values }, + :label => :field_related_organizations end def sql_for_author_organization_field(field, operator, value) @@ -50,6 +55,25 @@ def sql_for_author_organization_field(field, operator, value) end end + def sql_for_related_organizations_field(field, operator, value) + value.push User.current&.organization&.id&.to_s if value.delete('mine') + + case operator + when "*", "!*" # All / None + sql_not = operator == "!*" ? 'NOT' : '' + "(#{sql_not} EXISTS (SELECT 1 FROM #{IssuesOrganization.table_name}" + + " WHERE #{Issue.table_name}.id = #{IssuesOrganization.table_name}.issue_id))" + when "=", "!" + cond = value.any? ? + "issues_orgas.organization_id IN (" + value.collect { |val| "'#{self.class.connection.quote_string(val)}'" }.join(",") + ")" : + "1=0" + sql_not = operator == "!" ? 'NOT' : '' + "(#{sql_not} EXISTS (SELECT 1 FROM #{IssuesOrganization.table_name} issues_orgas" + + " WHERE #{Issue.table_name}.id = issues_orgas.issue_id" + + " AND #{cond}))" + end + end + def sql_for_updated_by_organization_field(field, operator, value) if value.delete('mine') diff --git a/spec/models/issue_query_patch_spec.rb b/spec/models/issue_query_patch_spec.rb index 8d0a560..7c3f097 100644 --- a/spec/models/issue_query_patch_spec.rb +++ b/spec/models/issue_query_patch_spec.rb @@ -29,7 +29,7 @@ def find_issues_with_query(query) query = IssueQuery.new(:name => '_') filter_name = "updated_by_organization" - query.filters = { filter_name => {:operator => '=', :values => [@org.id.to_s] }} + query.filters = { filter_name => { :operator => '=', :values => [@org.id.to_s] } } expect(find_issues_with_query(query).map(&:id).sort).to include(2, 3) end @@ -41,7 +41,7 @@ def find_issues_with_query(query) query = IssueQuery.new(:name => '_') filter_name = "updated_by_organization" - query.filters = { filter_name => {:operator => '!', :values => [@org.id.to_s] }} + query.filters = { filter_name => { :operator => '!', :values => [@org.id.to_s] } } expect(find_issues_with_query(query).map(&:id).sort).to_not include(2, 3) end @@ -57,7 +57,7 @@ def find_issues_with_query(query) query = IssueQuery.new(:name => '_') filter_name = "updated_by_organization" - query.filters = { filter_name => {:operator => '=', :values => [@org.id.to_s, org_test.id.to_s] }} + query.filters = { filter_name => { :operator => '=', :values => [@org.id.to_s, org_test.id.to_s] } } expect(find_issues_with_query(query).map(&:id).sort).to include(2, 3, 4) end @@ -73,7 +73,7 @@ def find_issues_with_query(query) query = IssueQuery.new(:name => '_') filter_name = "updated_by_organization" - query.filters = { filter_name => {:operator => '=', :values => [@org.id.to_s, org_test.id.to_s] }} + query.filters = { filter_name => { :operator => '=', :values => [@org.id.to_s, org_test.id.to_s] } } expect(find_issues_with_query(query).map(&:id).sort).to_not include([2, 3, 4]) end @@ -84,14 +84,20 @@ def find_issues_with_query(query) query = IssueQuery.new(:name => '_') filter_name = "updated_by_organization" - query.filters = {filter_name => {:operator => '=', :values => [@org.id.to_s]}} + query.filters = { filter_name => { :operator => '=', :values => [@org.id.to_s] } } expect(find_issues_with_query(query).map(&:id).sort).to eq([2, 3]) - User.current = User.anonymous - query.filters = {filter_name => {:operator => '=', :values => [@org.id.to_s]}} + User.current = User.anonymous + query.filters = { filter_name => { :operator => '=', :values => [@org.id.to_s] } } expect(find_issues_with_query(query).map(&:id).sort).to eq([3]) end end + + context "filter issues by related organizations" do + it "adds a relate organization column" do + expect(IssueQuery.available_columns.find { |column| column.name == :related_organizations }).to be_present + end + end end