<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ruby code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 00:42:10 GMT</pubDate>
    <description>DZone Snippets: ruby code</description>
    <item>
      <title>Getting the _session_id from SWFUpload (Flash 8 multiple file uploader)</title>
      <link>http://snippets.dzone.com/posts/show/3118</link>
      <description>It appears that Ruby's CGI::Session class will not use the _session_id in the query string when the Request is a POST.&lt;br /&gt;&lt;br /&gt;Normally, a POST-type request occurs when a form is submitted to the server (e.g. a Rails application).  In this scenario, there is an easy workaround since we can send the _session_id as a hidden field.&lt;br /&gt;&lt;br /&gt;With Flash 8, however, there is no way to add a 'hidden field' to the multi-part form data, thus Rails will fail to recognize the _session_id in the query string portion of our request.&lt;br /&gt;&lt;br /&gt;Here is a hackish work-around:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# The following code is a work-around for the&lt;br /&gt;# Flash 8 bug that prevents our multiple file uploader&lt;br /&gt;# from sending the _session_id.  Here, we hack the&lt;br /&gt;# Session#initialize method and force the session_id&lt;br /&gt;# to load from the query string via the request uri. &lt;br /&gt;# (Tested on Lighttpd)&lt;br /&gt;&lt;br /&gt;class CGI::Session&lt;br /&gt;  alias original_initialize initialize&lt;br /&gt;  def initialize(request, option = {})&lt;br /&gt;    session_key = option['session_key'] || '_session_id'&lt;br /&gt;    option['session_id'] =&lt;br /&gt;      request.env_table["REQUEST_URI"][0..-1].&lt;br /&gt;      scan(/#{session_key}=(.*?)(&amp;.*?)*$/).&lt;br /&gt;      flatten.first&lt;br /&gt;    original_initialize(request, option)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Dec 2006 03:14:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3118</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>ActiveRecord::Errors#to_xml</title>
      <link>http://snippets.dzone.com/posts/show/1960</link>
      <description>Here&#8217;s a method that will allow you to call to_xml on an ActiveRecord::Errors object. We&#8217;re using this to pass errors between web apps via a web service api.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ActiveRecord::Errors&lt;br /&gt;  def to_xml(options = {})&lt;br /&gt;    options[:indent] ||= 2&lt;br /&gt;    options.reverse_merge!({ :builder =&gt;&lt;br /&gt;      Builder::XmlMarkup.new(:indent =&gt;&lt;br /&gt;        options[:indent]), :root =&gt; "errors" })&lt;br /&gt;    options[:builder].instruct! unless options.delete(:skip_instruct)&lt;br /&gt;&lt;br /&gt;    options[:builder].__send__(options[:root].to_s.dasherize) do |xml|&lt;br /&gt;      @errors.each do |key, value|&lt;br /&gt;        xml.__send__(key.to_s.dasherize) do |xm|&lt;br /&gt;          for message in value&lt;br /&gt;            xm.message(message)&lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 26 Apr 2006 00:09:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1960</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Validate URIs by Pinging the Server</title>
      <link>http://snippets.dzone.com/posts/show/1913</link>
      <description>&lt;code&gt;&lt;br /&gt;require 'open-uri'&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;  def self.validates_uri_existence_of(*attr_names)&lt;br /&gt;    configuration = { :message =&gt; "is not a valid web address" }&lt;br /&gt;    configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;    validates_each attr_names do |m, a, v|&lt;br /&gt;      begin&lt;br /&gt;        # Try to open the URI&lt;br /&gt;        open v&lt;br /&gt;      rescue&lt;br /&gt;        # Report the error if it throws an exception&lt;br /&gt;        m.errors.add(a, configuration[:message])&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Details on my blog.</description>
      <pubDate>Fri, 14 Apr 2006 02:12:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1913</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Use background color to show error fields instead of wrapping them in a div</title>
      <link>http://snippets.dzone.com/posts/show/1912</link>
      <description>Using the default rails field_error_proc may lead to some layout headaches--your form looks perfect until, uh-oh, someone entered an invalid email address and Rails adds a fieldWithError-styled div that wraps around the problem field.&lt;br /&gt;&lt;br /&gt;While this works in many cases, some pixel-perfect layouts may not be able to tolerate the 2-pixel padding around the text_field caused by the red border.  An alternative is to change the background color of the offending field.&lt;br /&gt;&lt;br /&gt;Include the following code in environment.rb (or use a "require" like I do):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|&lt;br /&gt;  error_style = "background-color: #ffff80"&lt;br /&gt;  if html_tag =~ /&lt;(input|textarea|select)[^&gt;]+style=/&lt;br /&gt;    style_attribute = html_tag =~ /style=['"]/&lt;br /&gt;    html_tag.insert(style_attribute + 7, "#{error_style}; ")&lt;br /&gt;  elsif html_tag =~ /&lt;(input|textarea|select)/&lt;br /&gt;    first_whitespace = html_tag =~ /\s/&lt;br /&gt;    html_tag[first_whitespace] = " style='#{error_style}' "&lt;br /&gt;  end&lt;br /&gt;  html_tag&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;See my blog.inquirylabs.com for more Rails stuff.</description>
      <pubDate>Thu, 13 Apr 2006 22:21:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1912</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>content_tag that accepts a block = block_tag</title>
      <link>http://snippets.dzone.com/posts/show/1910</link>
      <description>Sometimes I want to do something like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% content_tag("div", :class =&gt; "section_with_error" do %&gt;&lt;br /&gt;  HTML GOES HERE&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;instead of this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%= content_tag("div", "HTML GOES HERE", :class =&gt; "section_with_error") %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Put the following code in ApplicationHelper, and you can do just that (replacing "content_tag" in the example above with "block_tag", of course):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def block_tag(tag, options = {}, &amp;block)&lt;br /&gt;    concat(content_tag(tag, capture(&amp;block), options), block.binding)&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Apr 2006 02:08:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1910</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>String#to_id</title>
      <link>http://snippets.dzone.com/posts/show/1844</link>
      <description>Convert any (English) text to a programming-language friendly identifier.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  def to_id&lt;br /&gt;    downcase.gsub(/\W+/, ' ').strip.gsub(' ', '_')&lt;br /&gt;  end&lt;br /&gt; &lt;br /&gt;  def to_id!&lt;br /&gt;    replace to_id&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 05 Apr 2006 03:04:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1844</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Parse Technorati-style Tags from HTML</title>
      <link>http://snippets.dzone.com/posts/show/1758</link>
      <description>An enhancement to the String class that parses out Technorati-style tags.  Uses Typo's strip_html method to remove img tags and other markup.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  # Strips any html markup from a string&lt;br /&gt;  TYPO_TAG_KEY = TYPO_ATTRIBUTE_KEY = /[\w:_-]+/&lt;br /&gt;  TYPO_ATTRIBUTE_VALUE = /(?:[A-Za-z0-9]+|(?:'[^']*?'|"[^"]*?"))/&lt;br /&gt;  TYPO_ATTRIBUTE = /(?:#{TYPO_ATTRIBUTE_KEY}(?:\s*=\s*#{TYPO_ATTRIBUTE_VALUE})?)/&lt;br /&gt;  TYPO_ATTRIBUTES = /(?:#{TYPO_ATTRIBUTE}(?:\s+#{TYPO_ATTRIBUTE})*)/&lt;br /&gt;  TAG = %r{&lt;[!/?\[]?(?:#{TYPO_TAG_KEY}|--)(?:\s+#{TYPO_ATTRIBUTES})?\s*(?:[!/?\]]+|--)?&gt;}&lt;br /&gt;  def strip_html&lt;br /&gt;    self.gsub(TAG, '').gsub(/\s+/, ' ').strip&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def tags&lt;br /&gt;    scan(/&lt;a\s+[^&gt;]*\s*rel=\s*(.?)tag\1[^&gt;]*&gt;(.+?)&lt;\/a&gt;/i).&lt;br /&gt;    map { |match| match.last.strip_html rescue nil }.&lt;br /&gt;    compact.select { |s| !s.strip.empty? }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# Example usage&lt;br /&gt;&lt;br /&gt;s = %{&lt;a href="http://www.docstrangelove.com/tag/civil-war" rel="tag"&gt;civil war&lt;/a&gt; &lt;a href="http://www.technorati.com/tag/civil+war" rel="tag"&gt;&lt;img src="http://www.docstrangelove.com/wp-content/plugins/UltimateTagWarrior/technoratiicon.jpg" alt="Technorati tag page for civil war"/&gt;&lt;/a&gt; &lt;a href="http://www.docstrangelovecom/tag/iraq" rel="tag"&gt;iraq&lt;/a&gt; &lt;a href="http://www.technorati.com/tag/iraq" rel="tag"&gt;&lt;img src="http://www.docstrangelove.com/wp-content/plugins/UltimateTagWarrior/technoratiicon.jpg" alt="Technorati tag page for iraq"/&gt;&lt;/a&gt;}&lt;br /&gt;&lt;br /&gt;s.tags&lt;br /&gt;# =&gt; ["civil war", "iraq"]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Mar 2006 02:02:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1758</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Array#randomly_pick( n )</title>
      <link>http://snippets.dzone.com/posts/show/1167</link>
      <description>Adds a handy function that lets you randomly pick &lt;i&gt;n&lt;/i&gt; elements from an array.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  # If +number+ is greater than the size of the array, the method&lt;br /&gt;  # will simply return the array itself sorted randomly&lt;br /&gt;  def randomly_pick(number)&lt;br /&gt;    sort_by{ rand }.slice(0...number)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 17 Jan 2006 05:17:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1167</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Firing a controller's action from the console</title>
      <link>http://snippets.dzone.com/posts/show/600</link>
      <description>It seems like a simple task, but here's how you can simulate the calling of a controller's action:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/console&lt;br /&gt;&lt;br /&gt;irb&gt; require 'action_controller/test_process'&lt;br /&gt;irb&gt; require 'application'&lt;br /&gt;irb&gt; require 'site_controller'&lt;br /&gt;irb&gt; request = ActionController::TestRequest.new&lt;br /&gt;irb&gt; response = ActionController::TestResponse.new&lt;br /&gt;irb&gt; request.env['REQUEST_METHOD'] = 'GET'&lt;br /&gt;irb&gt; request.action = "late_employee"&lt;br /&gt;irb&gt; InfoController.process(request,response)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Basically, it's like getting inside of a TestUnit method, but you have to do the dirty work yourself.</description>
      <pubDate>Fri, 26 Aug 2005 02:44:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/600</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Make an html link update several dom elements at the same time.</title>
      <link>http://snippets.dzone.com/posts/show/526</link>
      <description>&lt;code&gt;&lt;br /&gt;    &lt;a href="#" onClick="&lt;%= remote_function(:update =&gt; 'fashion1', :url =&gt; { :action =&gt; :get_fashion_for_color, :color_name =&gt; color_name, :number =&gt; 1 })%&gt;;&lt;br /&gt;                         &lt;%= remote_function(:update =&gt; 'fashion2', :url =&gt; { :action =&gt; :get_fashion_for_color, :color_name =&gt; color_name, :number =&gt; 2 })%&gt;;&lt;br /&gt;                         &lt;%= remote_function(:update =&gt; 'fashion3', :url =&gt; { :action =&gt; :get_fashion_for_color, :color_name =&gt; color_name, :number =&gt; 3 })%&gt;;&lt;br /&gt;                         return false;"&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 30 Jul 2005 02:46:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/526</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
  </channel>
</rss>
