Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

automate linking to Twitter users

this method searches for @some_user in a string (txt) and links found matches to the twitter-page of some_user

def link_twitter_user(txt)
	if match = txt.match(/.*?(@)((?:[a-z][a-z]+))(:|\s)/i)
		user = match[2]
		txt.gsub!(user, '<a href="http://twitter.com/' + user + '">' + user + '</a>')
	end
	txt
end

Don't display the delete link unless no service events exist

// This is the link I'll use when I get to adding the delete methods in the controllers.

<% if @client.service_events.empty? -%> | <%= link_to "delete", :controller => "admin", :action => "destroy_client", :id => @client %><% end -%>

link_to_remote_unless_current

// Full discussion about this available at:
// http://6brand.com/articles/2006/06/07/link_to_remote_unless_current

# throw this in one of your controller helpers.
# it works just like a combination of link_to_unless_current and link_to_remote
def link_to_remote_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &block)
  if current_page?(options[:url])
    if block_given?
      block.arity <= 1 ? yield(name) : yield(name, remote_function(options), html_options, *parameters_for_method_reference)
    else
      name
    end
  else
    link_to_function(name, remote_function(options), html_options)
  end
end

Javascript Link Helper

Ruby on Rails helper to create a link tag to 'name' wich execute the 'javascript' statement onclick event. The last both parameters are the same as for link_to.

Helper para o Ruby on Rails para criar um tag de link associado ao 'name' que executa o código 'javascript' no evento onclick. Os últimos dois parâmetros são os mesmos utilizados no link_to

	def javascript_link_to(name, javascript, html_options = nil, *parameters_for_method_reference)
		options = [ :action => @params["action"] ]
		html_options = { :onclick => javascript+"return false" }
		link_to(name, options, html_options, *parameters_for_method_reference)
	end

A javascript hyperlink

// method 1
<a href='#' onclick='alert("hello"); return false'>hello</a>

// method 2
<a href='javascript:alert("hello")'>hello</a>

showing attributes content after element (like link hreflang)

You can use pseudo-elements to insert content via CSS, and you can retrieve elements attributes to show them as new pseudo-elements.

Example : show links hreflang attribute.

XHTML :
<a href="" hreflang="fr">a link to a french page</a>

CSS :
a:after {
	content: attr(hreflang);
	color: #999;
	margin-left: 0.2em;
}

Make an html link update several dom elements at the same time.

    <a href="#" onClick="<%= remote_function(:update => 'fashion1', :url => { :action => :get_fashion_for_color, :color_name => color_name, :number => 1 })%>;
                         <%= remote_function(:update => 'fashion2', :url => { :action => :get_fashion_for_color, :color_name => color_name, :number => 2 })%>;
                         <%= remote_function(:update => 'fashion3', :url => { :action => :get_fashion_for_color, :color_name => color_name, :number => 3 })%>;
                         return false;">
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS