<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ajax code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 06:31:05 GMT</pubDate>
    <description>DZone Snippets: ajax code</description>
    <item>
      <title>Rails ajax star rater</title>
      <link>http://snippets.dzone.com/posts/show/5675</link>
      <description>Here's some Rails code I created to try out the &lt;a href="http://www.komodomedia.com/blog/2007/01/css-star-rating-redux/"&gt;CSS Star Rating Redux&lt;/a&gt; from Komodo Media.&lt;br /&gt;&lt;br /&gt;I set up my &lt;b&gt;routes.rb&lt;/b&gt; to use REST.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;map.resources :titles, :member =&gt; { :rate =&gt; :any }&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I use the &lt;a href="http://www.deveiate.org/projects/Linguistics/"&gt;Ruby Linguistics Framework&lt;/a&gt; to make this implementation easier. Install it using RubyGems (&lt;b&gt;sudo gem install linguistics&lt;/b&gt;). And then put the following code at the top of your &lt;b&gt;application_helper.rb&lt;/b&gt; file.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;require 'linguistics'&lt;br /&gt;Linguistics::use(:en)  # extends Array, String, and Numeric&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This code goes in the page you want the rater to appear in, in this example, &lt;b&gt;show.html.erb&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;ul class="star-rating" id="&lt;%= dom_id(@title) -%&gt;_rating"&gt;&lt;%= render :partial =&gt; '/partials/star_rating', :locals =&gt; { :record =&gt; @title } %&gt;&lt;/ul&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Create a partial called &lt;b&gt;_star_rating.html.erb&lt;/b&gt; in your &lt;b&gt;RAILS_ROOT/views/partials&lt;/b&gt; directory.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;li class="current-rating" style="width: &lt;%= number_to_percentage((record.rating.to_f * 25) / (5 * 25) * 100) -%&gt;;"&gt;Currently &lt;%= record.rating -%&gt;/5 Stars.&lt;/li&gt;&lt;br /&gt;&lt;% (1..5).each do |i| -%&gt;&lt;br /&gt;    &lt;li&gt;&lt;%= link_to_remote pluralize(i, 'Star'), {&lt;br /&gt;        :update =&gt; "#{dom_id(record)}_rating",&lt;br /&gt;        :url =&gt; eval("rate_#{record.class.name.downcase}_url(:rating =&gt; #{i})")&lt;br /&gt;    }, {&lt;br /&gt;        :class =&gt; "#{i.en.numwords}-#{i.abs == 1 ? 'star' : 'stars'}",&lt;br /&gt;        :title =&gt; "#{pluralize(i, 'star')} out of 5"&lt;br /&gt;    } -%&gt;&lt;/li&gt;&lt;br /&gt;&lt;% end -%&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 20 Jun 2008 04:19:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5675</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>FirstLast (real classes for Ajaxability)</title>
      <link>http://snippets.dzone.com/posts/show/2707</link>
      <description>I know CSS has the :first-child and :last-child pseudo classes.  But :first-child is a part of CSS 2 and has poor browsers support and :last-child is a part of CSS 3 and that's not worth thinking about using yet.  The idea is sound though, so I worked up this JavaScript method of getting the same effect.&lt;br /&gt;&lt;br /&gt;One advantage of doing this in JavaScript rather than using CSS is that the classes will change if you reorder the child nodes with more JavaScript.  In my case I'm using &lt;a href="http://wiki.script.aculo.us/scriptaculous/show/Sortables"&gt;Scriptaculous sortables&lt;/a&gt;.  Or to be more specific, I'm using a &lt;a href="http://api.rubyonrails.com/"&gt;Ruby on Rails&lt;/a&gt; helper method to make something sortable through Scriptaculous:&lt;br /&gt;&lt;code&gt;&lt;%= sortable_element 'image-list',&lt;br /&gt;    :constraint =&gt; false,&lt;br /&gt;    :url =&gt; { :action =&gt; 'sort', :issue_id =&gt; params[:issue_id] }&lt;br /&gt;-%&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I've chosen to not make this script run unobtrusively.  Most of it can go into a file and be included in the header or added to your own common JavaScript include file.  To get it to run on a page you will need to include something like the below in each page.&lt;br /&gt;&lt;code&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;//&lt;![CDATA[&lt;br /&gt;    Event.onDOMReady(function(){FirstLast.go("image-list")});&lt;br /&gt;    Ajax.Responders.register({&lt;br /&gt;        onComplete: function(){FirstLast.go("image-list");}&lt;br /&gt;   });&lt;br /&gt;//]]&gt;&lt;br /&gt;&lt;/script&gt;&lt;/code&gt;&lt;br /&gt;I use the &lt;a href="http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype"&gt;DOMReady extension&lt;/a&gt; for Prototype's Event object, but you can use any loader you want.  The Ajax.Responders.register part reruns the script after a drag-and-drop operation (or any Ajax operation really).&lt;br /&gt;&lt;br /&gt;&lt;a href="http://script.aculo.us/downloads"&gt;Download Scriptaculous and Prototype.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;var FirstLast = {&lt;br /&gt;    go: function(el) {&lt;br /&gt;        el = $(el);&lt;br /&gt;&lt;br /&gt;        // Whitespace nodes need to be cleaned to get the intended effect&lt;br /&gt;        var children = Element.cleanWhitespace(el).childNodes;&lt;br /&gt;&lt;br /&gt;        // Return if there are not any children&lt;br /&gt;        if (0 == children.length) return&lt;br /&gt;        &lt;br /&gt;        if (1 == children.length) {&lt;br /&gt;            // Cheap shortcut if there is only 1 child node&lt;br /&gt;            children[0].addClassName(this._firstChildClassName);&lt;br /&gt;            children[0].addClassName(this._lastChildClassName);&lt;br /&gt;        } else {&lt;br /&gt;            for (var i = 0; i &lt; children.length; i++) {&lt;br /&gt;                switch (i) {&lt;br /&gt;                    // First child&lt;br /&gt;                    case 0:&lt;br /&gt;                        children[i].addClassName(this._firstChildClassName);&lt;br /&gt;                        children[i].removeClassName(this._lastChildClassName);&lt;br /&gt;                        break;&lt;br /&gt;                    // Last child&lt;br /&gt;                    case children.length - 1:&lt;br /&gt;                        children[i].removeClassName(this._firstChildClassName);&lt;br /&gt;                        children[i].addClassName(this._lastChildClassName);&lt;br /&gt;                        break;&lt;br /&gt;                    // Every child other than the first or last&lt;br /&gt;                    default:&lt;br /&gt;                        children[i].removeClassName(this._firstChildClassName);&lt;br /&gt;                        children[i].removeClassName(this._lastChildClassName);&lt;br /&gt;                        break;  // I know it is unnecessary&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    },&lt;br /&gt;    // Pseudo Private methods and attributes&lt;br /&gt;    _firstChildClassName: "first",&lt;br /&gt;    _lastChildClassName: "last"&lt;br /&gt;};&lt;/code&gt;</description>
      <pubDate>Sun, 24 Sep 2006 19:16:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2707</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>PrototypeHelper :with helper</title>
      <link>http://snippets.dzone.com/posts/show/2216</link>
      <description>This method is for use in conjuction with the Ruby on Rails Module &lt;a href="http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html"&gt;ActionView::Helpers::PrototypeHelper&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The options hash takes any number of key/value pairs the key becomes the name of the parameter passed in the with value and the value becomes a javascript expression&lt;br /&gt;&lt;br /&gt;key &#8212; name of the parameter&lt;br /&gt;value &#8212; a fragment of javascript that will be the value of the parameter&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;pre&gt;&gt;&gt; params_for_with(:clean_range =&gt; 'clean_range', :raw_range =&gt; 'raw_range', :total_count =&gt; 'total_count')&lt;br /&gt;=&gt; 'total_count=' + total_count + '&amp;' + 'clean_range=' + clean_range + '&amp;' + 'raw_range=' + raw_range&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Don&#8216;t forget you won&#8217;t necessarily get the parameters out in the same order you put them in, such is the nature of hashes&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def params_for_with(options = {})&lt;br /&gt;    options.collect { |param_name, js_fragment| "'#{param_name}='+#{js_fragment}" }.join("+'&amp;'+")&lt;br /&gt;    # or this one&lt;br /&gt;    #options.stringify_keys.collect { |param_name, js_fragment| '"' &lt;&lt; param_name &lt;&lt; '="+' &lt;&lt; js_fragment }.join('+"&amp;"+')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 18 Jun 2006 20:52:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2216</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
  </channel>
</rss>
