forked from refinery/refinerycms
/
application_helper.rb
213 lines (188 loc) · 8.83 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Methods added to this helper will be available to all templates in the application.
module Refinery
module ApplicationHelper
include Refinery::HtmlTruncationHelper
def t(key, options = {})
if (val = super) =~ /class.+?translation_missing/
val = val.to_s.gsub(/<span[^>]*>/, 'i18n: ').gsub('</span>', '').gsub(', ', '.')
end
val
end
# This is used to display the title of the current object (normally a page) in the browser's titlebar.
#
def browser_title(yield_title=nil)
[
yield_title.present? ? yield_title : nil,
@meta.browser_title.present? ? @meta.browser_title : @meta.path,
RefinerySetting.find_or_set(:site_name, "Company Name")
].compact.join(" - ")
end
# replace all system images with a thumbnail version of them (handy for all images inside a page part)
# for example, <%= content_fu(@page[:body], :preview) %> converts all /system/images to their 'preview' thumbnail
def content_fu(content, thumbnail)
raise NotImplementedError # todo: implement for new syntax.
content.scan(/\/system\/images([^\"\ ]*)/).flatten.each do |match|
parts = match.split(".")
extension = parts.pop
content.gsub!(match, "#{parts.join(".")}_#{thumbnail}.#{extension}")
end unless content.blank?
return content
end
# This was extracted from REFINERY_ROOT/vendor/plugins/refinery/app/views/shared/_menu_branch.html.erb
# to remove the complexity of that template by reducing logic in the view.
def css_for_menu_branch(menu_branch, menu_branch_counter, sibling_count = nil)
css = []
css << "selected" if selected_page?(menu_branch) or descendant_page_selected?(menu_branch)
css << "first" if menu_branch_counter == 0
css << "last" if menu_branch_counter == (sibling_count ||= menu_branch.shown_siblings.size)
css
end
# Determines whether any page underneath the supplied page is the current page according to rails.
# Just calls selected_page? for each descendant of the supplied page.
def descendant_page_selected?(page)
page.descendants.any? {|descendant| selected_page?(descendant) }
end
# image_fu is a helper for inserting an image that has been uploaded into a template.
# Say for example that we had a @model.image (@model having a belongs_to :image relationship)
# and we wanted to display a thumbnail cropped to 200x200 then we can use image_fu like this:
# <%= image_fu @model.image, '200x200' %> or with no thumbnail: <%= image_fu @model.image %>
def image_fu(image, geometry = nil, options={})
if image.present?
if geometry.is_a?(Symbol)
if (sizes = RefinerySetting.find_or_set(:image_thumbnails, {})) and sizes.keys.include?(geometry)
geometry = sizes[geometry].presence
end
end
thumbnail = if geometry.present? && !geometry.is_a?(Symbol)
image.image.thumb(geometry)
else
image.image
end
thumbnail.instance_eval %{
def url(*args)
"\#{super}/#{image.image_uid.split('/').pop}"
end
}
# call rails' image tag function with default alt, width and height options.
# if any other options were supplied these are merged in and can replace the defaults.
image_tag(thumbnail.url, {
:alt => image.respond_to?(:title) ? image.title : image.image_name,
:width => thumbnail.width,
:height => thumbnail.height
}.merge(options))
end
end
# This function helps when including both the jquery and jqueryui libraries.
# If you use this function then whenever we update or relocate the version of jquery or jquery ui in use
# we will update the reference here and your existing application starts to use it.
# Use <%= jquery_include_tags %> to include it in your <head> section.
def jquery_include_tags(options={})
# Merge in options
options = {
:caching => RefinerySetting.find_or_set(:use_resource_caching, Rails.root.writable?),
:google => RefinerySetting.find_or_set(:use_google_ajax_libraries, false),
:jquery_ui => true
}.merge(options)
# render the tags normally unless
unless options[:google] and !local_request?
if options[:jquery_ui]
javascript_include_tag "jquery#{"-min" if Rails.env.production?}", "jquery-ui-custom-min",
:cache => (options[:caching] ? "cache/jquery" : nil)
else
javascript_include_tag "jquery#{"-min" if Rails.env.production?}"
end
else
"#{javascript_include_tag("http://www.google.com/jsapi").gsub(".js", "")}
<script type='text/javascript'>
google.load('jquery', '1.4');
#{"google.load('jqueryui', '1.8');" if options[:jquery_ui]}
</script>".html_safe
end
end
# you can override the object used for the title by supplying options[:object]
# this object must support custom_title_type if you want custom titles.
def page_title(options = {})
object = options.fetch(:object, @meta)
options.delete(:object)
options = RefinerySetting.find_or_set(:page_title, {
:chain_page_title => false,
:ancestors => {
:separator => " | ",
:class => 'ancestors',
:tag => 'span'
},
:page_title => {
:class => nil,
:tag => nil,
:wrap_if_not_chained => false
}
}).merge(options)
title = []
objects = (options[:chain_page_title] and object.respond_to?(:ancestors)) ? [object.ancestors, object] : [object]
objects.flatten.compact.each do |obj|
if obj.respond_to?(:custom_title_type)
title << case obj.custom_title_type
when "text"
obj.custom_title
when "image"
image_fu(obj.custom_title_image, nil, {:alt => obj.title}) rescue obj.title
else
obj.title
end
else
title << obj.title
end
end
final_title = title.pop
if (options[:page_title][:wrap_if_not_chained] and title.empty?) and options[:page_title][:tag].present?
css = options[:page_title][:class].present? ? " class='#{options[:page_title][:class]}'" : nil
final_title = "<#{options[:page_title][:tag]}#{css}>#{final_title}</#{options[:page_title][:tag]}>"
end
if title.empty?
return final_title.to_s.html_safe
else
return "<#{options[:ancestors][:tag]} class='#{options[:ancestors][:class]}'>#{title.join options[:ancestors][:separator]}#{options[:ancestors][:separator]}</#{options[:ancestors][:tag]}>#{final_title}".html_safe
end
end
# Returns <span class='help' title='Your Input'>(help)</span>
# Remember to wrap your block with <span class='label_with_help'></span> if you're using a label next to the help tag.
def refinery_help_tag(title='')
"<span class='help' title='#{title}'>(#{t('shared.admin.help')})</span>".html_safe
end
# This is just a quick wrapper to render an image tag that lives inside refinery/icons.
# They are all 16x16 so this is the default but is able to be overriden with supplied options.
def refinery_icon_tag(filename, options = {})
image_tag "refinery/icons/#{filename}", {:width => 16, :height => 16}.merge(options)
end
# Determine whether the supplied page is the currently open page according to Rails.
def selected_page?(page)
# ensure we match the path without the locale.
path = request.path
if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled?
path = path.split("/#{::I18n.locale}").last
end
current_page?(page) or
(path =~ Regexp.new(page.menu_match) if page.menu_match.present?) or
(path == page.link_url) or
(path == page.nested_path)
end
# Generates the link to determine where the site bar switch button returns to.
def site_bar_switch_link
link_to_if(admin?, t('.switch_to_your_website'),
(if session.keys.include?(:website_return_to) and session[:website_return_to].present?
session[:website_return_to]
else
root_url(:only_path => true)
end)) do
link_to t('.switch_to_your_website_editor'),
(if session.keys.include?(:refinery_return_to) and session[:refinery_return_to].present?
session[:refinery_return_to]
elsif defined?(@page) and @page.present? and !@page.home?
edit_admin_page_url(@page, :only_path => true)
else
(request.fullpath.to_s == '/') ? admin_root_url(:only_path => true) : "/admin#{request.request_uri}/edit"
end rescue admin_root_url(:only_path => true))
end
end
end
end