ensl.org/app/helpers/application_helper.rb

188 lines
4.1 KiB
Ruby
Raw Normal View History

module ApplicationHelper
2014-04-06 14:14:03 +00:00
def full_title(page_title)
2014-05-16 21:24:49 +00:00
base_title = "NSL"
2014-04-06 14:14:03 +00:00
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
2014-05-11 10:27:59 +00:00
def active_theme
if cuser && cuser.profile
cuser.current_layout
else
'default'
end
2014-05-11 10:27:59 +00:00
end
def theme_stylesheet_link_tag
stylesheet_link_tag "themes/#{active_theme}/theme"
end
def namelink model, length = nil
return if model.nil?
model = case model.class.to_s
when "DataFile"
model.movie ? model.movie : model
when "Comment"
model.commentable
when "Post"
model.topic
else
model
end
str = model.to_s
if length and str.length > length
link_to raw(str.to_s[0, length] + "..."), model, class: model.class.to_s.downcase
else
link_to raw(str), model, class: model.class.to_s.downcase
end
end
def shorten str, length
if length and str and str.to_s.length > length
str = str.to_s[0, length] + "..."
end
str
end
def longtime time
printtime time, "%d %B %y %H:%M"
end
def longertime time
printtime time, "%e %B %Y - %H:%M %Z"
end
def shorttime time
printtime time, "%d/%b/%y %H:%M"
end
def shortdate time
printtime time, "%d %b %y"
end
def longdate time
printtime time, "%e %B %Y"
end
def printtime time, format
return unless time
2014-04-16 08:44:36 +00:00
content_tag(:span, style: 'font-style: italic') do
Time.use_zone(timezone_offset) { time.strftime(format) }
end
end
def cascade model, list
2014-04-16 08:44:36 +00:00
out = list.map do |element|
name = key = element
2014-04-16 08:44:36 +00:00
item = ""
result = ""
if element.instance_of?(Array)
name = element[0]
key = element[1]
end
if m = key.to_s.match(/^(.*)_b$/)
name = m[1]
key = m[1]
end
str = eval("model.#{key}")
next if str == "" or str.nil?
if model[key].instance_of?(Time)
result << shorttime(str)
elsif element.instance_of?(Symbol)
result << namelink(str)
elsif key.to_s.match(/^(.*)_b$/)
result << str.bbcode_to_html
else
result << h(str)
end
2014-04-16 08:44:36 +00:00
item << content_tag(:dt) do
"#{name.to_s.capitalize.gsub(/_s/, '').gsub(/_/, ' ')}".html_safe
2014-04-16 08:44:36 +00:00
end
item << content_tag(:dd) do
result.html_safe
2014-04-16 08:44:36 +00:00
end
item
end
content_tag(:dl) do
out.join.html_safe
end
end
def abslink text, url
raw link_to text, url
end
def flag country
if country and country.to_s.size > 0
image_tag "flags/#{country}.png", class: 'flag'
else
image_tag 'flags/EU.png', class: 'flag'
end
end
2014-04-06 14:14:03 +00:00
def add_comments(object)
@comment = Comment.new(commentable: object)
@comments = object.comments.ordered.with_userteam
2014-04-06 14:14:03 +00:00
return_here
2014-04-06 14:14:03 +00:00
render partial: "comments/index"
end
def bbcode
2014-04-06 14:14:03 +00:00
link_to "(BBCode)", article_url(id: 536)
end
def sortable(column, title = nil)
title ||= column.titleize
css_class = (column == sort_column) ? "current #{sort_direction}" : nil
direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc"
2014-04-06 14:14:03 +00:00
link_to title, { sort: column, direction: direction }, { class: css_class }
end
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
2014-04-06 14:14:03 +00:00
fields = f.fields_for(association, new_object, child_index: "new_#{association}") do |builder|
render(association.to_s.singularize, f: builder)
end
link_to_function(name, ("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
end
def timezone_offset
if @cuser
@cuser.time_zone
else
Time.zone.name
end
end
def upcoming_matches
GoogleCalendar.new(ENV['GOOGLE_CALENDAR_ID'], timezone_offset).upcoming.sort_by do |event|
event.start
end
end
def latest_rules
if Contest.last
2014-07-01 11:39:40 +00:00
article_path(Contest.last.rules)
else
article_path(Article::RULES)
end
end
end