<?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 on Rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 18:44:36 GMT</pubDate>
    <description>DZone Snippets: Ruby on Rails code</description>
    <item>
      <title>WillPaginate RemoteLinkRenderer</title>
      <link>http://snippets.dzone.com/posts/show/5736</link>
      <description>// overrides the normal willpaginate link renderer for use with Ajax Requests and the tiny_mce_plus plugin&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class RemoteLinkRenderer &lt; WillPaginate::LinkRenderer&lt;br /&gt;  def page_link_or_span(page, span_class = 'current', text = nil)&lt;br /&gt;    text ||= page.to_s&lt;br /&gt;    # RAILS_DEFAULT_LOGGER.error "DEBUG: page: #{page.inspect}"&lt;br /&gt;    if page and page != current_page&lt;br /&gt;      @template.link_to_remote text, :url =&gt; url_for(page), :method =&gt; :get&lt;br /&gt;    else&lt;br /&gt;      @template.content_tag :span, text, :class =&gt; span_class&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Returns URL params for +page_link_or_span+, taking the current GET params&lt;br /&gt;  # and &lt;tt&gt;:params&lt;/tt&gt; option into account.&lt;br /&gt;  def url_for(page)&lt;br /&gt;    RAILS_DEFAULT_LOGGER.error "DEBUG: page: #{page.inspect}"&lt;br /&gt;    page_one = page == 1&lt;br /&gt;    unless @url_string and !page_one&lt;br /&gt;      @url_params = {}&lt;br /&gt;      # page links should preserve GET parameters&lt;br /&gt;      stringified_merge @url_params, @template.params if @template.request.get?&lt;br /&gt;      stringified_merge @url_params, @options[:params] if @options[:params]&lt;br /&gt;      RAILS_DEFAULT_LOGGER.error "DEBUG: #{param_name.to_s}"&lt;br /&gt;      if complex = param_name.to_s.index(/[^\w-]/)&lt;br /&gt;        page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).&lt;br /&gt;          parse_query_parameters("#{param_name}=#{page}")&lt;br /&gt;        &lt;br /&gt;        stringified_merge @url_params, page_param&lt;br /&gt;      else&lt;br /&gt;        @url_params[param_name] = page_one ? 1 : 2&lt;br /&gt;      end&lt;br /&gt;      RAILS_DEFAULT_LOGGER.error "DEBUG: #{@url_params.inspect}"&lt;br /&gt;      url = @template.url_for(@url_params)&lt;br /&gt;      RAILS_DEFAULT_LOGGER.error "DEBUG: url: #{url.inspect}"&lt;br /&gt;      return url if page_one&lt;br /&gt;      &lt;br /&gt;      if complex&lt;br /&gt;        @url_string = url.sub(%r!((?:\?|&amp;amp;)#{CGI.escape param_name}=)#{page}!, '\1@')&lt;br /&gt;        return url&lt;br /&gt;      else&lt;br /&gt;        @url_string = url&lt;br /&gt;        @url_params[param_name] = 3&lt;br /&gt;        @template.url_for(@url_params).split(//).each_with_index do |char, i|&lt;br /&gt;          if char == '3' and url[i, 1] == '2'&lt;br /&gt;            @url_string[i] = '@'&lt;br /&gt;            break&lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    # finally!&lt;br /&gt;    @url_string.sub '@', page.to_s&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Recursively merge into target hash by using stringified keys from the other one&lt;br /&gt;  def stringified_merge(target, other)&lt;br /&gt;    target = target.stringify_keys&lt;br /&gt;    other = other.stringify_keys&lt;br /&gt;    other.each do |key, value|&lt;br /&gt;      existing = target[key]&lt;br /&gt;&lt;br /&gt;      if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?)&lt;br /&gt;        stringified_merge(existing || (target[key] = {}), value)&lt;br /&gt;      else&lt;br /&gt;        target[key] = value&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Jul 2008 10:21:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5736</guid>
      <author>chielwester (Chiel Wester)</author>
    </item>
    <item>
      <title>ActiveRecord Class Example</title>
      <link>http://snippets.dzone.com/posts/show/5379</link>
      <description>// Trying out the system, here is just a simple ActiveRecord class&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Task &lt; ActiveRecord::Base &lt;br /&gt;  belongs_to :user&lt;br /&gt;  belongs_to :location&lt;br /&gt;  belongs_to :project&lt;br /&gt;end .&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Apr 2008 14:30:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5379</guid>
      <author>curtis (Curtis Hermann)</author>
    </item>
    <item>
      <title>Add a to_conditions method to ActiveRecord::Base for converting models to finder :conditions hash.</title>
      <link>http://snippets.dzone.com/posts/show/3732</link>
      <description>// Mixes in a to_conditions method to ActiveRecord::Base. Converts the attributes of an AR object to a&lt;br /&gt;// ActiveRecord::Base#find :conditions hash. Useful for comparing AR objects, especially when looking for&lt;br /&gt;// duplicates.&lt;br /&gt;// E.g.&lt;br /&gt;//   &lt;br /&gt;//   if not Post.find(:all, :conditions =&gt; my_post.conditions).empty?&lt;br /&gt;//     puts "Duplicate found"&lt;br /&gt;//   end&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Bezurk #:nodoc:&lt;br /&gt;  module ActiveRecord #:nodoc:&lt;br /&gt;    module Extensions&lt;br /&gt;      def to_conditions&lt;br /&gt;        attributes.inject({}) do |hash, (name, value)|&lt;br /&gt;          hash.merge(name.intern =&gt; value)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      alias :to_conditions_hash :to_conditions&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;ActiveRecord::Base.send(:include, Bezurk::ActiveRecord::Extensions)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Mar 2007 10:54:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3732</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>Application version number and cool codename based on subversion number</title>
      <link>http://snippets.dzone.com/posts/show/2370</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#codename generated from the dictionary&lt;br /&gt;REVISION_NUMBER = `svn info`.split("\n")[4][/\d+/].to_i&lt;br /&gt;APP_CODENAME  = IO.readlines("/usr/share/dict/words")[REVISION_NUMBER]&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 05 Aug 2006 10:10:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2370</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Add Subversion Revsions Number to Rails Application.</title>
      <link>http://snippets.dzone.com/posts/show/2317</link>
      <description>// description of your code here&lt;br /&gt;Add this to your environment.rb file for a dead easy way to get your subversion revision into your application.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;APP_VERSION  = IO.popen("svn info").readlines[4]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Jul 2006 04:30:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2317</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>how to escape from AJAX URL</title>
      <link>http://snippets.dzone.com/posts/show/1705</link>
      <description>From Dylan Stamat's post on the Ruby on Rails list 	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Within my "login.rjs" template that get's invoked after my login process, it returns to the same page with an error message on error, or... if the login was successful, it needs to "redirect" to another controller... which is pretty much impossible to do otherwise.  so, my " login.rjs" looks like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if @logged_in_client&lt;br /&gt;  page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;else&lt;br /&gt;  page.redirect_to "whatever you want here"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;------ End Message--------&lt;br /&gt;Here is my two cents:&lt;br /&gt;Technically, you can do this without a RJS template at all, because it is such a simple example. Use this in your controller&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;render :update do |page|&lt;br /&gt;  if @logged_in_client&lt;br /&gt;    page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;  else&lt;br /&gt;    page.redirect_to "whatever you want here"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 16 Mar 2006 08:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1705</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Submitting data to two different tables with two different models  (or how to modify multiple models from one form)</title>
      <link>http://snippets.dzone.com/posts/show/1674</link>
      <description>Taken from Norman Timmler's example on the ruby on rails mailing list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Take two text fields for example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# view&lt;br /&gt;&lt;%= form_tag :controller =&gt; :posts, :action =&gt; create %&gt;&lt;br /&gt; &lt;%= text_field("post", "title", "size" =&gt; 20) %&gt;&lt;br /&gt; &lt;%= text_field("user", "email", "size" =&gt; 20) %&gt;&lt;br /&gt;&lt;%= end_form_tag %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# posts_controller&lt;br /&gt;class PostsController &lt; ApplicationController&lt;br /&gt; def create&lt;br /&gt;   @post = Post.create(params[:post])&lt;br /&gt;   @user = User.create(params[:user])&lt;br /&gt;   @post.users &lt;&lt; @user&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In the controller the params hash has one key for every object (user,&lt;br /&gt;post) in your form. As a values of this key you find a hash having a&lt;br /&gt;key-value pair for every object attribute (title, email).&lt;br /&gt;&lt;br /&gt;This way you can submit multiple objects via one form.</description>
      <pubDate>Thu, 09 Mar 2006 23:20:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1674</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Create a new instance of Magick::Image out of uploaded form data</title>
      <link>http://snippets.dzone.com/posts/show/1673</link>
      <description>Taken from Norman Timmler's example on the Ruby on Rails mailing list:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def file=(new_file)&lt;br /&gt; new_file.rewind&lt;br /&gt; image = Magick::Image::from_blob(new_file.read).first&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Mar 2006 22:41:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1673</guid>
      <author>heavysixer ()</author>
    </item>
  </channel>
</rss>
