Skip to content

Commit

Permalink
Add new filter and column for issue_organizations association
Browse files Browse the repository at this point in the history
  • Loading branch information
nanego committed Oct 22, 2024
1 parent 2f561ee commit b7a7505
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions lib/redmine_organizations/patches/issue_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions lib/redmine_organizations/patches/issue_query_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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')
Expand Down
20 changes: 13 additions & 7 deletions spec/models/issue_query_patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit b7a7505

Please sign in to comment.