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

About this user

Nick nickhammond.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 21 total  RSS 

Default merb datamapper database.yml file


   1  
   2  # This is a sample database file for the DataMapper ORM
   3  :development: &defaults
   4    :adapter: mysql
   5    :database: merb-blog
   6    :username: root
   7    :password: 
   8    :host: localhost
   9  
  10  :test:
  11    <<: *defaults
  12    :database: sample_test
  13  
  14  :production:
  15    <<: *defaults
  16    :database: sample_production
  17  

Ruby button helper

I use this for the admin section of my sites. It's good for your basic buttons. I use the tango icon library as well (http://tango.freedesktop.org/Tango_Icon_Gallery).

   1  
   2    def button(options = {})
   3      image_source = case options[:type]
   4        when 'edit':          "edit-find-replace.png"
   5        when 'preview':       "document-print-preview.png"
   6        when 'delete':        "edit-delete.png"
   7        else "list-add.png"
   8      end
   9      
  10      button = "<img src='/images/icons/#{image_source}' />"
  11      
  12      if options[:link]
  13        target = params[:target].blank? ? ">" : " target='#{options[:target]}'>"
  14        "<a href='#{options[:link]}'" + target + button + "</a>"
  15      else
  16        button
  17      end
  18    end


Example usage:
   1  
   2  <%= button %>
   3  # <img src='/images/icons/list-add.png' />


   1  
   2  <%= button :type => "edit", :link => "/edit/5" %>

YM4R configuration with multiple domains

Need to put domains in config/gmaps_api_key.yml
   1  
   2  production:
   3   host.com: longapikeythatmakesnosense
   4   anotherhost.com: jsdkfljskfljsgklsjgkl


When printing out the header information you need to pass the param :host. request.host should do the job.
   1  
   2  <%= GMap.header(:host => request.host) %>

Ruby string proper case



   1  
   2  str = "how are you? are you feeling good?"  
   3  puts str.split(/\s+/).each{ |word| word.capitalize! }.join(' ')  
   4  => "How Are You? Are You Feeling Good?

Display special characters.

Display special characters.

   1  
   2  header("Content-type: text/html; charset=iso-8859-1 ");

Ruby simple SMTP email

By Ian Purton and found at http://jiploo.com/blog/simple-email-send-function-in-ruby/
   1  
   2  def send_email(from, from_alias, to, to_alias, subject, message)
   3  	msg = <<END_OF_MESSAGE
   4  From: #{from_alias} <#{from}>
   5  To: #{to_alias} <#{to}>
   6  Subject: #{subject}
   7  	
   8  #{message}
   9  END_OF_MESSAGE
  10  	
  11  	Net::SMTP.start('localhost') do |smtp|
  12  		smtp.send_message msg, from, to
  13  	end
  14  end

Javascript open new window with all params

// description of your code here

   1  
   2  function pop_window()
   3  {
   4  window.open ("http://url.com","Window Title","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1, scrollbars=0,height=0,width=0");
   5  }


Rails link to helper
   1  
   2  <%= link_to "link text", {:action => "help"}, :popup => ['Terms and Conditions','width=400,height=400'] %>

Lighttpd add/remove www from domain name

Add following configuration directive if you want to redirect www.domain.com to domain.com (no www)

   1  
   2  $HTTP["host"] =~ "^www\.(.*)" { url.redirect = ( "^/(.*)" => "http://%1/$1" ) }


Add following configuration directive if you want to redirect domain.com to www.domain.com (force www)
   1  
   2  $HTTP["host"] =~ "^domain\.com$" { url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" ) }


Credit goes to Vivek Gite from Nixcraft (http://www.cyberciti.biz/tips/lighttpd-redirect-www-domaincom-to-domain-com.html)

PHP get current file name

   1  
   2  
   3      $currentFile = $_SERVER["SCRIPT_NAME"];
   4      $parts = Explode('/', $currentFile);
   5      $currentFile = $parts[count($parts) - 1];
   6  

Credit goes to:http://php.snippetdb.com/view.php?ID=42

Radiant freeze gems

   1  
   2  rake radiant:freeze:gems
« Newer Snippets
Older Snippets »
Showing 1-10 of 21 total  RSS