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.
-
#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) ⇒ 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.
-
#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
176 177 178 179 |
# File 'app/helpers/muck_engine_helper.rb', line 176 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
27 28 29 30 31 |
# File 'app/helpers/muck_engine_helper.rb', line 27 def country_scripts return if defined?(@@country_scripts_included) @@country_scripts_included = true render :partial => 'scripts/country_scripts' end |
#format_date(date) ⇒ Object
247 248 249 250 |
# File 'app/helpers/muck_engine_helper.rb', line 247 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.
92 93 94 95 96 97 98 |
# File 'app/helpers/muck_engine_helper.rb', line 92 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'
197 198 199 |
# File 'app/helpers/muck_engine_helper.rb', line 197 def html_summarize(text, length = 30, omission = '...') snippet((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
8 9 10 |
# File 'app/helpers/muck_engine_helper.rb', line 8 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/helpers/muck_engine_helper.rb', line 68 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.
17 18 19 20 21 22 23 24 |
# File 'app/helpers/muck_engine_helper.rb', line 17 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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/helpers/muck_engine_helper.rb', line 139 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.
168 169 170 171 172 |
# File 'app/helpers/muck_engine_helper.rb', line 168 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
162 163 164 165 |
# File 'app/helpers/muck_engine_helper.rb', line 162 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
33 34 35 36 37 38 39 40 41 |
# File 'app/helpers/muck_engine_helper.rb', line 33 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
131 132 133 134 135 136 137 |
# File 'app/helpers/muck_engine_helper.rb', line 131 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
238 239 240 |
# File 'app/helpers/muck_engine_helper.rb', line 238 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
185 186 187 188 |
# File 'app/helpers/muck_engine_helper.rb', line 185 def raw_block_to_partial(partial_name, = {}, &block) .merge!(:block => block) render(:partial => partial_name, :locals => ) end |
#round(flt) ⇒ Object
211 212 213 |
# File 'app/helpers/muck_engine_helper.rb', line 211 def round(flt) return (((flt.to_f*100).to_i.round).to_f)/100.0 end |
#safe_id(term) ⇒ Object
242 243 244 245 |
# File 'app/helpers/muck_engine_helper.rb', line 242 def safe_id(term) term = URI.escape(term) term = term.gsub('.', '%2E') end |
#secure_mail_to(email) ⇒ Object
Generates a secure mailto link
125 126 127 |
# File 'app/helpers/muck_engine_helper.rb', line 125 def secure_mail_to(email) mail_to email, nil, :encode => 'javascript' end |
#service_icon(name, size = 24) ⇒ 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.
120 121 122 |
# File 'app/helpers/muck_engine_helper.rb', line 120 def service_icon(name, size = 24) %Q{<img src="/images/service_icons/#{size}/#{name}" />} end |
#service_icon_background(service) ⇒ Object
Generates a css style for the given service
101 102 103 104 105 106 107 108 |
# File 'app/helpers/muck_engine_helper.rb', line 101 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. ie twitter, google, delicious, etc size: Size of the image to get. Valid values are 16, 24, 48 and 60
113 114 115 |
# File 'app/helpers/muck_engine_helper.rb', line 113 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
45 46 47 48 49 50 51 52 |
# File 'app/helpers/muck_engine_helper.rb', line 45 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'
207 208 209 |
# File 'app/helpers/muck_engine_helper.rb', line 207 def snippet(text, wordcount, omission) 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
232 233 234 |
# File 'app/helpers/muck_engine_helper.rb', line 232 def time_scripts(locale) render :partial => 'scripts/time_scripts', :locals => {:locale => locale} end |
#truncate_on_word(text, length = 270, end_string = ' ...') ⇒ Object
215 216 217 218 219 220 221 222 223 |
# File 'app/helpers/muck_engine_helper.rb', line 215 def truncate_on_word(text, length = 270, end_string = ' ...') return '' if text.blank? if text.length > length + end_string.length stop_index = text.rindex(' ', length - end_string.length) text[0,stop_index] + end_string else text + end_string end end |
#truncate_words(text, length = 40, end_string = ' ...') ⇒ Object
225 226 227 228 229 |
# File 'app/helpers/muck_engine_helper.rb', line 225 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 |