Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for files in forum messages #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions app/controllers/project_attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ProjectAttachmentsController < ApplicationController
before_filter :filter
menu_item :all_files

@@module_names_to_container_types = { :issue_tracking => 'issues', :news => 'news', :documents => 'documents', :wiki => 'wiki_pages', :files => 'projects' }
@@module_names_to_container_types = { :issue_tracking => 'issues', :news => 'news', :documents => 'documents', :wiki => 'wiki_pages', :files => 'projects', :boards => 'messages' }

def index
params[:project_id].present? ? get_attachment_from_project(params[:project_id]) : get_all_attachments
Expand Down Expand Up @@ -85,12 +85,15 @@ def get_all_attachments
# user select container types from available
scope = scope.select { |t| @scope.include? t }

cond << Attachment.search_attachments_for_projects_bis(projects,
@tokens,
:scope => scope,
:all_words => @all_words,
:titles_only => @titles_only)
cond << ' OR '
scope_cond = Attachment.search_attachments_for_projects_bis(projects,
@tokens,
:scope => scope,
:all_words => @all_words,
:titles_only => @titles_only)
unless scope_cond.empty?
cond << scope_cond
cond << ' OR '
end
end
cond << '(1 = 2) '

Expand Down
8 changes: 8 additions & 0 deletions app/helpers/project_attachments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def group_by(attachments = [], *criterias)
# File: File: attachment.filename => project/project_id/file/id
# Version: Version: name => projects/project_id/versions/id
# Wiki_page: Wiki page: title => projects/project_id/wiki_pages/id
# Message: Message: subject => projects/project_id/messages/id
def link_to_container_for(attachment)
link = case attachment.container_type
when 'Issue' then
Expand All @@ -60,6 +61,13 @@ def link_to_container_for(attachment)
"#{t('to_document')} #{link_to(attachment.document_title, document_path(attachment.container_id))}"
when 'News' then
"#{t('to_news')} #{link_to(attachment.new_title, news_path(attachment.container_id))}"
when 'Message' then
message_link = if attachment.message_topic_id
url_for(:controller => 'messages', :action => 'show', :board_id => attachment.message_board_id, :id => attachment.message_topic_id, :r => attachment.container_id, :anchor => "message-#{attachment.container_id}")
else
board_message_path(attachment.message_board_id, attachment.container_id)
end
"#{t('to_message')} #{link_to(attachment.message_subject, message_link)}"
else
raise ArgumentError
end
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ en:
to_news: news
to_version: version
to_wiki_page: wiki page
to_message: message
of_the_project: of the project

date:
Expand Down
14 changes: 10 additions & 4 deletions lib/redmine_all_files/patches/attachments_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def search_attachments_for_projects projects, tokens = [], options = {}
when 'n', 'd' then str += " OR (LOWER(#{val}.title) LIKE %{token} #{ "OR LOWER(#{val}.description) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
when 'p' then str += " OR (LOWER(#{val}.name) LIKE %{token} OR LOWER(#{val}.identifier) LIKE %{token} #{ "OR LOWER(#{val}.description) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
when 'w' then str += " OR (LOWER(#{val}.title) LIKE %{token})" % { :token => sanitize("%#{token.downcase}%") }
when 'm' then str += " OR (LOWER(#{val}.subject) LIKE %{token} #{ "OR LOWER(#{val}.content) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
when 'v' then str += " OR (LOWER(#{val}.name) LIKE %{token} #{ "OR LOWER(#{val}.description) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
else
end
Expand All @@ -41,6 +42,7 @@ def search_attachments_for_projects projects, tokens = [], options = {}
find_by_sql <<-SQL
SELECT d.title AS document_title, i.subject AS issue_subject, t.name AS issue_tracker_name,
n.title AS new_title, v.name AS version_name, w.title AS wiki_page_title, ww.project_id AS wiki_project_id,
m.subject AS message_subject, m.board_id AS message_board_id, m.parent_id AS message_topic_id,
p.name AS attachment_project_name, p.id AS attachment_project_id, a.*
FROM attachments a

Expand All @@ -49,7 +51,8 @@ def search_attachments_for_projects projects, tokens = [], options = {}
LEFT JOIN news n ON n.id = a.container_id AND a.container_type = 'News'
LEFT JOIN versions v ON v.id = a.container_id AND a.container_type = 'Version'
LEFT JOIN wiki_pages w ON w.id = a.container_id AND a.container_type = 'WikiPage' LEFT JOIN wikis ww ON ww.id = w.wiki_id
LEFT JOIN projects p ON p.id = d.project_id OR p.id = i.project_id OR p.id = n.project_id OR p.id = v.project_id OR p.id = ww.project_id OR p.id = a.container_id AND a.container_type = 'Project'
LEFT JOIN messages m ON m.id = a.container_id AND a.container_type = 'Message' LEFT JOIN boards b ON b.id = m.board_id
LEFT JOIN projects p ON p.id = d.project_id OR p.id = i.project_id OR p.id = n.project_id OR p.id = v.project_id OR p.id = ww.project_id OR p.id = b.project_id OR p.id = a.container_id AND a.container_type = 'Project'


WHERE (d.project_id IN #{sql_project_ids} OR
Expand All @@ -67,7 +70,7 @@ def search_attachments_for_projects projects, tokens = [], options = {}
end
def search_attachments_for_projects_bis projects, tokens = [], options = {}
project_ids = projects
return [] if options[:scope].blank? || project_ids.blank?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This led to Array to String conversion error.

return '' if options[:scope].blank? || project_ids.blank?
sql_project_ids = "(#{ project_ids.join(', ') })"

containers = options[:scope].map { |container| container.singularize.camelize }
Expand All @@ -83,6 +86,7 @@ def search_attachments_for_projects_bis projects, tokens = [], options = {}
when 'n', 'd' then str += " OR (LOWER(#{val}.title) LIKE %{token} #{ "OR LOWER(#{val}.description) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
when 'p' then str += " OR (LOWER(#{val}.name) LIKE %{token} OR LOWER(#{val}.identifier) LIKE %{token} #{ "OR LOWER(#{val}.description) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
when 'w' then str += " OR (LOWER(#{val}.title) LIKE %{token})" % { :token => sanitize("%#{token.downcase}%") }
when 'm' then str += " OR (LOWER(#{val}.subject) LIKE %{token} #{ "OR LOWER(#{val}.content) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
when 'v' then str += " OR (LOWER(#{val}.name) LIKE %{token} #{ "OR LOWER(#{val}.description) LIKE %{token}" unless options[:titles_only] })" % { :token => sanitize("%#{token.downcase}%") }
else
end
Expand All @@ -102,6 +106,7 @@ def search_attachments_for_projects_bis projects, tokens = [], options = {}
n.project_id IN #{sql_project_ids} OR
v.project_id IN #{sql_project_ids} OR
ww.project_id IN #{sql_project_ids} OR
b.project_id IN #{sql_project_ids} OR
p.id IN #{sql_project_ids}
) AND (
#{ statement }
Expand All @@ -116,13 +121,14 @@ def order_by_created_on
end

def selection
select(" d.title AS document_title, i.subject AS issue_subject, t.name AS issue_tracker_name, n.title AS new_title, v.name AS version_name, w.title AS wiki_page_title, ww.project_id AS wiki_project_id, p.name AS attachment_project_name, p.id AS attachment_project_id, attachments.* ").
select(" d.title AS document_title, i.subject AS issue_subject, t.name AS issue_tracker_name, n.title AS new_title, v.name AS version_name, w.title AS wiki_page_title, ww.project_id AS wiki_project_id, m.subject AS message_subject, m.board_id AS message_board_id, m.parent_id AS message_topic_id, p.name AS attachment_project_name, p.id AS attachment_project_id, attachments.* ").
joins(" LEFT JOIN documents d ON d.id = attachments.container_id AND attachments.container_type = 'Document' ").
joins("LEFT JOIN issues i ON i.id = attachments.container_id AND attachments.container_type = 'Issue' LEFT JOIN trackers t ON t.id = i.tracker_id ").
joins("LEFT JOIN news n ON n.id = attachments.container_id AND attachments.container_type = 'News'").
joins("LEFT JOIN versions v ON v.id = attachments.container_id AND attachments.container_type = 'Version'").
joins(" LEFT JOIN wiki_pages w ON w.id = attachments.container_id AND attachments.container_type = 'WikiPage' LEFT JOIN wikis ww ON ww.id = w.wiki_id").
joins("LEFT JOIN projects p ON p.id = d.project_id OR p.id = i.project_id OR p.id = n.project_id OR p.id = v.project_id OR p.id = ww.project_id OR p.id = attachments.container_id AND attachments.container_type = 'Project'")
joins("LEFT JOIN messages m ON m.id = attachments.container_id AND attachments.container_type = 'Message' LEFT JOIN boards b ON b.id = m.board_id").
joins("LEFT JOIN projects p ON p.id = d.project_id OR p.id = i.project_id OR p.id = n.project_id OR p.id = v.project_id OR p.id = ww.project_id OR p.id = b.project_id OR p.id = attachments.container_id AND attachments.container_type = 'Project'")
end
end
end
Expand Down