/
application_helper.rb
138 lines (118 loc) · 5.08 KB
/
application_helper.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module ApplicationHelper
HW_LINK = "<span class='hw-link%s'>%s</span>"
NO_ENTRY_TEXT = "[No entry for <i>%s</i> ×%d]"
# lang_for: Builds a string with lang= and xml:lang= attributes usable in an
# XHTML element. Takes as argument an element or an array.
#
# Will return:
# * 'und' (ISO 639: 'undetermined') if no languages are found for
# any of the content
# * 'mul' (ISO 639: 'multiple languages') if more than one
# language is found in the array's content
# * the language value of the content, if they are all of one language
#
# May also take a hash of subtags. At the moment the only one specifically
# handled is 'variant'.
def lang_for content, subtags = {}
langtag = Language.code_for content, subtags
"xml:lang=\"#{h langtag}\" lang=\"#{h langtag}\"".html_safe
end
# Link to the first lexeme whose headword matches a given parse's parsed form.
# Passes to #new_headword_link if no such lexeme.
def headword_link (parse)
is_wanted = parse == @wantedparse
head = @headwords ? @headwords[parse.parsed_form] : Lexeme.lookup_by_headword(parse.parsed_form)
head = head.respond_to?(:lexeme) ? head.lexeme : head
if head
link_to((HW_LINK % [(" wanted" if is_wanted), html_escape(parse.parsed_form)]).html_safe, head )
else
new_headword_link parse, is_wanted
end
end
# Verbose link to create a new lexeme based on a headword, which lists how many
# times that headword is attested in the loci
def new_headword_link (parse, is_wanted = false)
count = parse.respond_to?(:count_all) ? parse.count_all : parse.count
no_entry = NO_ENTRY_TEXT % [html_escape(parse.parsed_form), count]
link_to((HW_LINK % [(" wanted" if is_wanted), no_entry]).html_safe,
exact_lexeme_path(:headword => parse.parsed_form))
end
def sentence_case str
str.dup.tap do |x|
x[0,1] = x[0,1].upcase
end
end
def nested_attributes_for(form_builder, *args)
@nests ||= {}
content_for :javascript do
content = ""
args.each do |association|
association = Array(association)
assoc_key = association.join('$')
content << "\nvar #{assoc_key}_template='#{generate_template(form_builder, association[0].to_sym)}';" unless @nests[assoc_key]
@nests[assoc_key] = true
end
content.html_safe
end
end
def generate_html(form_builder, method, options = {})
options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
options[:partial] ||= method.to_s.singularize
options[:form_builder_local] ||= :f
form_builder.fields_for(method, options[:object], :child_index => 'NEW_RECORD') do |f|
render(:partial => options[:partial], :locals => { options[:form_builder_local] => f })
end
end
def generate_template(form_builder, method, options = {})
escape_javascript generate_html(form_builder, method, options)
end
def remove_link_unless_new_record(form_builder)
unless form_builder.object.try(:new_record?)
"<span class=\"type-check\">%s</span>" % [form_builder.check_box(:_destroy) + " " + form_builder.label(:_destroy, 'Delete', :class => 'delete_label')]
else
form_builder.hidden_field(:_destroy) + " " + link_to("(remove)", "##{form_builder.object.class.name.underscore}", :class => 'remove delete_label')
end
end
def label_with_remove_option_for label_name = "Section", form_builder
label = "%s %s" % [sanitize(label_name), remove_link_unless_new_record(form_builder)]
label.html_safe
end
def spaced_render(options = {})
coll = options[:collection].collect do |item|
render :partial => options[:partial], :object => item, :locals => options[:locals]
end
sanitize(coll.to_sentence({
:words_connector => options[:spacer] || ", ",
:last_word_connector => options[:last_spacer] || options[:spacer] || ", ",
:two_words_connector => options[:dual_spacer] || options[:spacer] || ", "
}))
end
# Create a <li> link suitable for the navbar, unless we are already on the page.
# Modified form of #link_to_unless.
def navlink_unless_current name, options = {}, html_options = {}, &block
output = if current_page?(options)
"<li class=\"active\"><strong>%s</strong></li>" % [
if block_given?
block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options_ & block)
else
name
end
]
else
"<li>%s</li>" % [link_to(name, options, html_options)]
end
sanitize(output)
end
# Return @page_title, or a default of "Controller - action".
def page_title
html_escape (@page_title || [params[:controller].camelize, params[:action]].join(" - "))
end
# Translate a string in wiki format into HTML
def wh text
output = html_escape(text)
output.gsub!(/'''(.+?)'''/, '<b>\1</b>')
output.gsub!(/''(.+?)''/, '<i>\1</i>')
output.gsub!(/\[\[([^|]+?)\]\](\w*)/, '<a href="/html/\1" title="\1">\1\2</a>')
output.gsub(/\[\[(.+?)\|(.+?)\]\](\w*)/, '<a href="/html/\1" title="\1">\2\3</a>')
end
end