-
Notifications
You must be signed in to change notification settings - Fork 13
/
init.rb
executable file
·58 lines (49 loc) · 1.58 KB
/
init.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'redmine'
require 'open-uri'
require 'issue'
Redmine::Plugin.register :redmine_wiki_sql do
name 'Redmine Wiki SQL'
author 'Rodrigo Ramalho'
author_url 'http://www.rodrigoramalho.com/'
description 'Allows you to run SQL queries and have them shown on your wiki in table format'
version '0.0.1'
Redmine::WikiFormatting::Macros.register do
desc "Run SQL query"
macro :sql do |obj, args, text|
_sentence = args.join(",")
_sentence = _sentence.gsub("\\(", "(")
_sentence = _sentence.gsub("\\)", ")")
_sentence = _sentence.gsub("\\*", "*")
result = ActiveRecord::Base.connection.execute(_sentence)
unless result.nil?
unless result.num_rows() == 0
column_names = []
for columns in result.fetch_fields.each do
column_names += columns.name.to_a
end
_thead = '<tr>'
column_names.each do |column_name|
_thead << '<th>' + column_name.to_s + '</th>'
end
_thead << '</tr>'
_tbody = ''
result.each_hash do |record|
unless record.nil?
_tbody << '<tr>'
column_names.each do |column_name|
_tbody << '<td>' + record[column_name].to_s + '</td>'
end
_tbody << '</tr>'
end
end
text = '<table>' << _thead << _tbody << '</table>'
text.html_safe
else
''.html_safe
end
else
''.html_safe
end
end
end
end