Refactoring

This commit is contained in:
2015-09-04 17:47:25 -04:00
parent c686ee1d7c
commit ae098bd45c
27 changed files with 409 additions and 7709 deletions

View File

@@ -22,76 +22,75 @@
require 'digest/md5'
module ApplicationHelper
include CalendarHelper
include CalendarHelper
# Converte para o timezone local
def tz(time_at)
#FIXME
#TzTime.zone.utc_to_local(time_at.utc)
time_at
end
# Converte para o timezone local
def tz(time_at)
# FIXME
# TzTime.zone.utc_to_local(time_at.utc)
time_at
end
FLASH_NAMES = [:notice, :warning, :message]
FLASH_NAMES = [:notice, :warning, :message]
def flash_div
output = ""
for name in FLASH_NAMES
if flash[name]
output << "<div id='validation' class='validation #{name}' style='display: none'>#{flash[name]}"
output << ". " + link_to(t(:undo).capitalize + "?", flash[:undo], :method => 'post', :accesskey => 'u') if flash[:undo]
output << "</div>"
end
end
return output
end
def logged_in?
session[:user_id]
end
def current_user
User.find(session[:user_id]) if logged_in?
end
def admin?
logged_in? and current_user.admin?
end
def formatted(text)
h(text).gsub("\n", "<br/>")
end
def highlight(name)
return {:class => 'highlight'} if (flash[:highlight] == name)
return {}
end
def spinner(name)
return image_tag("loading.gif", :id => "spinner_#{name}", :style => "display:none")
end
def markup_enabled_field
return "<p class='grey'>Este campo aceita as linguagens Markdown, Latex e HTML. " +
link_to('Saiba mais.', '#', :id => 'show_markup_help') + spinner('help') + "</p>"
def flash_div
output = ""
for name in FLASH_NAMES
if flash[name]
output << "<div id='validation' class='validation #{name}' style='display: none'>#{flash[name]}"
output << ". " + link_to(t(:undo).capitalize + "?", flash[:undo], method: 'post', accesskey: 'u') if flash[:undo]
output << "</div>"
end
end
output
end
def markup_help
return "<div id='markup_help' style='display: none'>" +
File.read("#{Rails.root}/public/static/markup_help.mkd").format_wiki +
"</div>"
end
def logged_in?
session[:user_id]
end
def gravatar_url_for(email, size=80)
"http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5.hexdigest(email)}&size=#{size}&default=#{u(App.default_avatar)}_#{size}.png"
end
def current_user
User.find(session[:user_id]) if logged_in?
end
def action_icon(action_name, description, options = {}, html_options = {})
html_options.merge!({:class => 'icon', :alt => description, :title => description})
link_to(image_tag("action/#{action_name}.gif", :title => description), options, html_options)
end
def admin?
logged_in? && current_user.admin?
end
def format_period(period)
return "20#{period[0..1]}.#{period[2..2]}"
end
def formatted(text)
h(text).gsub("\n", "<br/>")
end
def highlight(name)
return { class: 'highlight' } if (flash[:highlight] == name)
{}
end
def spinner(name)
image_tag("loading.gif", id: "spinner_#{name}", style: "display:none")
end
def markup_enabled_field
"<p class='grey'>Este campo aceita as linguagens Markdown, Latex e HTML. " +
link_to('Saiba mais.', '#', id: 'show_markup_help') + spinner('help') + "</p>"
end
def markup_help
"<div id='markup_help' style='display: none'>" +
File.read("#{Rails.root}/public/static/markup_help.mkd").format_wiki +
"</div>"
end
def gravatar_url_for(email, size = 80)
"http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5.hexdigest(email)}&size=#{size}&default=#{u(App.default_avatar)}_#{size}.png"
end
def action_icon(action_name, description, options = {}, html_options = {})
html_options.merge!(class: 'icon', alt: description, title: description)
link_to(image_tag("action/#{action_name}.gif", title: description), options, html_options)
end
def format_period(period)
"20#{period[0..1]}.#{period[2..2]}"
end
end

View File

@@ -20,46 +20,41 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class String
include ActionView::Helpers::SanitizeHelper
def format_wiki
text = BlueCloth.new(self).to_html
text = Hpricot(text, :xhtml_strict => true).to_s
return sanitize text
end
include ActionView::Helpers::SanitizeHelper
def format_wiki
text = BlueCloth.new(self).to_html
text = Hpricot(text, xhtml_strict: true).to_s
sanitize text
end
end
module WikiHelper
def format_diff(text)
last = 0
result = ""
text << "\n"
style = { '+' => 'add', '-' => 'del', ' ' => 'line' }
def format_diff(text)
last = 0
result = ""
text << "\n"
style = { '+' => 'add', '-' => 'del', ' ' => 'line' }
text.each_line do |line|
next if line.match(/^---/)
next if line.match(/^\+\+\+/)
text.each_line do |line|
# Ignora o cabecalho
next if line.match(/^---/)
next if line.match(/^\+\+\+/)
if line[0].chr == '@'
result << "<tr><td class='diff_space diff_border_#{style[last.chr]}' ></td></tr>"
last = 0
else
if line[0] != last
last = line[0] if last == 0 || line[0].chr == '+' || line[0].chr == '-'
result << "<tr><td class='diff_border_#{style[last.chr]} "
last = line[0]
else
result << "<tr><td class='"
end
result << "diff_#{style[line[0].chr]}'>" \
" #{line[1..-1]}&nbsp;</td></td>"
end
end
# Divisao entre pedacos
if line[0].chr == '@'
result << "<tr><td class='diff_space diff_border_#{style[last.chr]}' ></td></tr>"
last = 0
else
# Verifica se mudou de contexto (add para del, linha normal para add, etc)
if line[0] != last
last = line[0] if last == 0 or line[0].chr == '+' or line[0].chr == '-'
result << "<tr><td class='diff_border_#{style[last.chr]} "
last = line[0]
else
result << "<tr><td class='"
end
result << "diff_#{style[line[0].chr]}'>" +
" #{line[1..-1]}&nbsp;</td></td>"
end
end
return "<table class='diff'>#{result}</table>"
end
"<table class='diff'>#{result}</table>"
end
end