Module: MuckEngineHelper
- Defined in:
- app/helpers/muck_engine_helper.rb
Instance Method Summary collapse
-
#block_to_partial(partial_name, options = {}, &block) ⇒ Object
Take a block and renders that block within the context of a partial.
-
#country_scripts ⇒ Object
Outputs scripts that manipulate the country and state select controls.
- #format_date(date) ⇒ Object
-
#gravatar(email, gravatar_default, size = 40, rating = 'g') ⇒ Object
Gets a gravatar for a given email.
-
#html_summarize(text, length = 30, omission = '...') ⇒ Object
Summarize html content by removing html tags and truncating at a given number of words.
-
#html_summarize_by_chars(text, length = 270, omission = '...') ⇒ Object
Summarize html content by removing html tags and truncating on a word at a given number of characters.
-
#http_protocol(use_ssl = request.ssl?) ⇒ Object
Outputs the appropriate http protocol based on the request type.
-
#icon(object, size = :icon, default_image = '/images/profile_default.jpg', use_only_gravatar = false, gravatar_size = 50, rating = 'g', gravatar_default = nil) ⇒ Object
Render a photo for the given object.
-
#jquery_json_message(message_dom_id = nil) ⇒ Object
Outputs a small bit of javascript that will enable message output to jgrowl or to the given message dom id.
- #locale_link(name, locale) ⇒ Object
-
#make_muck_parent_fields(parent) ⇒ Object
Generate hidden input fields that refer to a given object as parent.
-
#make_muck_parent_params(parent) ⇒ Object
Generate parameters for a url that refer to a given object as parent.
- #muck_form_for(record_or_name_or_array, *args, &proc) ⇒ Object
-
#page_alert(message, title = '') ⇒ Object
Used inside of format.js to return a message to the client.
-
#parse_uri_script ⇒ Object
Outputs a snippet of javascript that can parse uris blog.stevenlevithan.com/archives/parseuri.
-
#raw_block_to_partial(partial_name, options = {}, &block) ⇒ Object
Take a block and renders that block within the context of a partial.
- #round(flt) ⇒ Object
- #safe_id(term) ⇒ Object
-
#secure_mail_to(email) ⇒ Object
Generates a secure mailto link.
-
#service_icon(name, size = 24, alt = nil) ⇒ Object
Renders an icon for the given service Name is the name of the image file associated with the service Size can be one of 16, 24, 48 or 60.
-
#service_icon_background(service) ⇒ Object
Generates a css style for the given service.
-
#service_image(name, size = 24) ⇒ Object
Builds a link to an image representing the service specified by name name: Name of a service image.
-
#show_hide_on_click(id_to_show, id_to_hide) ⇒ Object
generates javascript that will hide a link or button when click and then show a different dom object.
-
#snippet(text, wordcount, omission) ⇒ Object
Truncates text at a word boundry and provides a parameter for a ‘more link’ Parameters: text - The text to truncate wordcount - The number of words omission - Text to add when the text is truncated ie ‘read more’.
-
#time_scripts(locale) ⇒ Object
Outputs a javascript include localized for specific time actions.
- #truncate_on_word(text, length = 270, end_string = ' ...') ⇒ Object
- #truncate_words(text, length = 40, end_string = ' ...') ⇒ Object
Instance Method Details
#block_to_partial(partial_name, options = {}, &block) ⇒ Object
Take a block and renders that block within the context of a partial. from snippets.dzone.com/posts/show/2483
181 182 183 184 |
# File 'app/helpers/muck_engine_helper.rb', line 181 def block_to_partial(partial_name, = {}, &block) .merge!(:body => capture(&block)) render(:partial => partial_name, :locals => ) end |
#country_scripts ⇒ Object
Outputs scripts that manipulate the country and state select controls
31 32 33 34 35 |
# File 'app/helpers/muck_engine_helper.rb', line 31 def country_scripts return if defined?(@@country_scripts_included) @@country_scripts_included = true render :partial => 'scripts/country_scripts' end |
#format_date(date) ⇒ Object
268 269 270 271 |
# File 'app/helpers/muck_engine_helper.rb', line 268 def format_date(date) return '' if date.nil? date.to_date.to_s(:long) end |
#gravatar(email, gravatar_default, size = 40, rating = 'g') ⇒ Object
Gets a gravatar for a given email
gravatar_default: If a gravatar is used, but no image is found several defaults are available. Leaving
this value nil will result in the 'default_image' being used. Other wise one of the following can be set:
identicon, monsterid, wavatar, 404
size: Size in pixels for the gravatar. Can be from 1 to 512. rating: Default gravatar rating - g, pg, r, x.
96 97 98 99 100 101 102 |
# File 'app/helpers/muck_engine_helper.rb', line 96 def gravatar(email, gravatar_default, size = 40, = 'g') hash = MD5::md5(email) image_url = "http://www.gravatar.com/avatar/#{hash}" image_url << "?d=#{CGI::escape(gravatar_default)}" image_url << "&s=#{size}" image_url << "&r=#{}" end |
#html_summarize(text, length = 30, omission = '...') ⇒ Object
Summarize html content by removing html tags and truncating at a given number of words. Truncation will occur at word boundries Parameters:
text - The text to truncate
length - The desired number of words
omission - Text to add when the text is truncated ie 'read more'
202 203 204 205 |
# File 'app/helpers/muck_engine_helper.rb', line 202 def html_summarize(text, length = 30, omission = '...') return '' if text.blank? snippet((text), length, omission) end |
#html_summarize_by_chars(text, length = 270, omission = '...') ⇒ Object
Summarize html content by removing html tags and truncating on a word at a given number of characters. Truncation will occur at word boundries Parameters:
text - The text to truncate
length - The desired number of words
omission - Text to add when the text is truncated ie 'read more'
214 215 216 217 |
# File 'app/helpers/muck_engine_helper.rb', line 214 def html_summarize_by_chars(text, length = 270, omission = '...') return '' if text.blank? truncate_on_word((text), length, omission) end |
#http_protocol(use_ssl = request.ssl?) ⇒ Object
Outputs the appropriate http protocol based on the request type. if https is desired then pass true to override default behavior
12 13 14 |
# File 'app/helpers/muck_engine_helper.rb', line 12 def http_protocol(use_ssl = request.ssl?) (use_ssl ? "https://" : "http://") end |
#icon(object, size = :icon, default_image = '/images/profile_default.jpg', use_only_gravatar = false, gravatar_size = 50, rating = 'g', gravatar_default = nil) ⇒ Object
Render a photo for the given object. Note that the object will need a ‘photo’ method provided by paperclip. If the object does not provide an image, but does have an ‘email’ method this method will attempt to use gravatar.com to find a matching image.
object: Object to get icon for. size: Size to get. size is commonly one of:
:medium, :thumb, :icon or :tiny but can be any value provided by the photo object
default_image: A default image should the photo not be found. gravatar_size: Size in pixels for the gravatar. Can be from 1 to 512. For reference the sizes from muck-profiles are:
medium: 300, thumb: 100, icon: 50, tiny: 24. The default is set to 50 to match the default 'size' setting which is icon.
rating: Default gravatar rating - g, pg, r, x. gravatar_default: If a gravatar is used, but no image is found several defaults are available. Leaving
this value nil will result in the 'default_image' being used. Other wise one of the following can be set:
identicon, monsterid, wavatar, 404
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/helpers/muck_engine_helper.rb', line 72 def icon(object, size = :icon, default_image = '/images/profile_default.jpg', use_only_gravatar = false, gravatar_size = 50, = 'g', gravatar_default = nil) return "" if object.blank? if object.photo.original_filename && !use_only_gravatar image_url = object.photo.url(size) rescue nil end if image_url.blank? && object.respond_to?(:email) && object.email.present? gravatar_default = File.join(root_url, default_image) if gravatar_default.blank? image_url = gravatar(object.email, gravatar_default, gravatar_size, ) else image_url ||= default_image end link_to(image_tag(image_url, :class => size), object, { :title => object.full_name }) end |
#jquery_json_message(message_dom_id = nil) ⇒ Object
Outputs a small bit of javascript that will enable message output to jgrowl or to the given message dom id
message_dom_id: The dom id of the element that will hold messages.
This element can have display:none by default.
21 22 23 24 25 26 27 28 |
# File 'app/helpers/muck_engine_helper.rb', line 21 def ( = nil) if MuckEngine.configuration.growl_enabled "jQuery.jGrowl.info(json.message);" else "jQuery('##{}').html(json.message);" + "jQuery('##{}').show();" end end |
#locale_link(name, locale) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/helpers/muck_engine_helper.rb', line 144 def locale_link(name, locale) parts = request.host.split('.') first_subdomain = parts.first fullpath = request.fullpath if first_subdomain == 'www' or Language.supported_locale?(first_subdomain) link_to name, request.protocol + (locale == I18n.default_locale.to_s ? 'www' : locale) + '.' + parts[1..-1].join('.') + request.port_string + fullpath elsif /^localhost/.match( request.host ) or /^(\d{1,3}\.){3}\d{1,3}$/.match( request.host ) if fullpath.include?('?') if fullpath.include?('locale') link_to name, request.protocol + request.host_with_port + fullpath.sub(/locale=.*/, 'locale=' + locale) else link_to name, request.protocol + request.host_with_port + fullpath + '&locale=' + locale end else link_to name, request.protocol + request.host_with_port + fullpath + '?locale=' + locale end else link_to name, request.protocol + (locale == I18n.default_locale.to_s ? 'www' : locale) + '.' + request.host_with_port + fullpath end end |
#make_muck_parent_fields(parent) ⇒ Object
Generate hidden input fields that refer to a given object as parent.
173 174 175 176 177 |
# File 'app/helpers/muck_engine_helper.rb', line 173 def make_muck_parent_fields(parent) return if parent.blank? %Q{<input id="parent_id" type="hidden" value="#{parent.id}"> <input id="parent_type" type="hidden" value="#{parent.class.to_s}">} end |
#make_muck_parent_params(parent) ⇒ Object
Generate parameters for a url that refer to a given object as parent. Useful for comments, shares, etc
167 168 169 170 |
# File 'app/helpers/muck_engine_helper.rb', line 167 def make_muck_parent_params(parent) return if parent.blank? { :parent_id => parent.id, :parent_type => parent.class.to_s } end |
#muck_form_for(record_or_name_or_array, *args, &proc) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'app/helpers/muck_engine_helper.rb', line 37 def muck_form_for(record_or_name_or_array, *args, &proc) = args.detect { |argument| argument.is_a?(Hash) } if .nil? = {:builder => MuckEngine::FormBuilder} args << end [:builder] = MuckEngine::FormBuilder unless .nil? form_for(record_or_name_or_array, *args, &proc) end |
#page_alert(message, title = '') ⇒ Object
Used inside of format.js to return a message to the client. If jGrowl is enabled the message will show up as a growl instead of a popup
136 137 138 139 140 141 142 |
# File 'app/helpers/muck_engine_helper.rb', line 136 def page_alert(, title = '') if MuckEngine.configuration.growl_enabled "jQuery.jGrowl.error('" + + "', {header:'" + title + "'});" else "alert(#{});" end end |
#parse_uri_script ⇒ Object
Outputs a snippet of javascript that can parse uris blog.stevenlevithan.com/archives/parseuri
259 260 261 |
# File 'app/helpers/muck_engine_helper.rb', line 259 def parse_uri_script render :partial => 'scripts/parse_uri' end |
#raw_block_to_partial(partial_name, options = {}, &block) ⇒ Object
Take a block and renders that block within the context of a partial. Passes the block to the partial. The partial is then responsible for capturing and rendering the block. from snippets.dzone.com/posts/show/2483
190 191 192 193 |
# File 'app/helpers/muck_engine_helper.rb', line 190 def raw_block_to_partial(partial_name, = {}, &block) .merge!(:block => block) render(:partial => partial_name, :locals => ) end |
#round(flt) ⇒ Object
230 231 232 |
# File 'app/helpers/muck_engine_helper.rb', line 230 def round(flt) return (((flt.to_f*100).to_i.round).to_f)/100.0 end |
#safe_id(term) ⇒ Object
263 264 265 266 |
# File 'app/helpers/muck_engine_helper.rb', line 263 def safe_id(term) term = URI.escape(term) term = term.gsub('.', '%2E') end |
#secure_mail_to(email) ⇒ Object
Generates a secure mailto link
130 131 132 |
# File 'app/helpers/muck_engine_helper.rb', line 130 def secure_mail_to(email) mail_to email, nil, :encode => 'javascript' end |
#service_icon(name, size = 24, alt = nil) ⇒ Object
Renders an icon for the given service Name is the name of the image file associated with the service Size can be one of 16, 24, 48 or 60.
124 125 126 127 |
# File 'app/helpers/muck_engine_helper.rb', line 124 def service_icon(name, size = 24, alt = nil) alt ||= name %Q{<img src="#{service_image(name, size)}" alt="#{alt}" />} end |
#service_icon_background(service) ⇒ Object
Generates a css style for the given service
105 106 107 108 109 110 111 112 |
# File 'app/helpers/muck_engine_helper.rb', line 105 def service_icon_background(service) if service.respond_to?(:icon) service_name = service.icon else service_name = "#{service}.png" end %Q{style="background: transparent url('#{service_image(service_name, 24)}') no-repeat scroll left top;"} end |
#service_image(name, size = 24) ⇒ Object
Builds a link to an image representing the service specified by name name: Name of a service image. ie twitter.png, google.png, delicious.png, etc size: Size of the image to get. Valid values are 16, 24, 48 and 60
117 118 119 |
# File 'app/helpers/muck_engine_helper.rb', line 117 def service_image(name, size = 24) %Q{/images/service_icons/#{size}/#{name}} end |
#show_hide_on_click(id_to_show, id_to_hide) ⇒ Object
generates javascript that will hide a link or button when click and then show a different dom object
49 50 51 52 53 54 55 56 |
# File 'app/helpers/muck_engine_helper.rb', line 49 def show_hide_on_click(id_to_show, id_to_hide) %Q{jQuery(document).ready(function() { jQuery('##{id_to_hide}').click(function(){ jQuery(this).hide(); jQuery('##{id_to_show}').show(); }); });} end |
#snippet(text, wordcount, omission) ⇒ Object
Truncates text at a word boundry and provides a parameter for a ‘more link’ Parameters:
text - The text to truncate
wordcount - The number of words
omission - Text to add when the text is truncated ie 'read more'
225 226 227 228 |
# File 'app/helpers/muck_engine_helper.rb', line 225 def snippet(text, wordcount, omission) return '' if text.blank? text.split[0..(wordcount-1)].join(" ") + (text.split.size > wordcount ? " " + omission : "") end |
#time_scripts(locale) ⇒ Object
Outputs a javascript include localized for specific time actions
253 254 255 |
# File 'app/helpers/muck_engine_helper.rb', line 253 def time_scripts(locale) render :partial => 'scripts/time_scripts', :locals => {:locale => locale} end |
#truncate_on_word(text, length = 270, end_string = ' ...') ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 |
# File 'app/helpers/muck_engine_helper.rb', line 234 def truncate_on_word(text, length = 270, end_string = ' ...') return '' if text.blank? if text.length > length + end_string.length no_html_end_string = (end_string) stop_index = text.rindex(' ', length - no_html_end_string.length) raise "End string (omission text) was longer than the allowed length for the entire string. Please use a shorter omission string - ie '...'" if stop_index.blank? text[0,stop_index] + end_string else text + end_string end end |
#truncate_words(text, length = 40, end_string = ' ...') ⇒ Object
246 247 248 249 250 |
# File 'app/helpers/muck_engine_helper.rb', line 246 def truncate_words(text, length = 40, end_string = ' ...') return '' if text.blank? words = text.split() words[0..(length-1)].join(' ') + (words.length > length ? end_string : '') end |