<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1056</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1056" rel="self"/>
  <title>chovy.myopenid.com</title>
  <updated>Mon Aug 03 06:50:01 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code980</id>
    <published>2009-08-03T06:50:01-07:00</published>
    <updated>2009-08-03T18:02:29-07:00</updated>
    <title>[Ruby] dual object creating in Rails</title>
    <content type="html">&lt;p&gt;This code works, but could use some more love.&lt;/p&gt;

&lt;p&gt;The idea is when a user creates a new &amp;quot;Topic&amp;quot; -- they automatically create a new &amp;quot;Post&amp;quot; as well ( handled by the textarea in the &amp;quot;new topic&amp;quot; form).&lt;/p&gt;

&lt;p&gt;The code from the book I'm reviewing is outdated and you can no longer pass more than one argument to Foo.new() in rails 2.3&lt;/p&gt;

&lt;p&gt;The commented out params[:post].merge(...) command didn't to work.&lt;/p&gt;

&lt;pre&gt;  # POST /topics
  # POST /topics.xml
  def create
    @topic = Topic.new(params[:topic])
    @topic.forum_id = params[:forum_id]
    @topic.user_id = logged_in_user.id

    #params[:post].merge({:topic_id =&amp;gt; @topic.id, :user_id =&amp;gt; logged_in_user.id})
    @post = Post.new(params[:post])
    @post.topic = @topic
    @post.user_id = logged_in_user.id

    respond_to do |format|
      if @topic.save &amp;amp;&amp;amp; @post.save
        flash[:notice] = 'Topic was successfully created.'
        format.html { redirect_to forum_topic_posts_path(:topic_id =&amp;gt; @topic, :forum_id =&amp;gt; @topic.forum.id) }
        format.xml  { render :xml =&amp;gt; @topic, :status =&amp;gt; :created, :location =&amp;gt; forum_topic_posts_path(:id =&amp;gt; @topic, :form_id =&amp;gt; @topic.forum.id) }
      else
        format.html { render :action =&amp;gt; &amp;quot;new&amp;quot; }
        format.xml  { render :xml =&amp;gt; @topic.errors, :status =&amp;gt; :unprocessable_entity }
      end
    end
  end&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/980-rector-dual-object-creating-in-rails" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code577</id>
    <published>2008-11-05T07:13:41-08:00</published>
    <updated>2008-11-05T16:12:09-08:00</updated>
    <title>[Ruby] xml rss feed</title>
    <content type="html">&lt;p&gt;Looking for a better way to define the xml.description node...which needs html embedded as CDATA, but I think it is ugly concating this way with &amp;quot;+&amp;quot;.&lt;/p&gt;

&lt;p&gt;Generates a description and a link at the bottom.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;    for directory in @directories
      xml.item do
        xml.title &amp;quot;#{directory.name} (PR #{directory.pagerank})&amp;quot;

        //cleaner proc here?
        xml.description { xml.cdata! simple_format(directory.description) + simple_format(link_to directory.software.name, software_url(directory.software)) }

        xml.pubDate directory.created_at.to_s(:rfc822)
        xml.link directory_url(directory)
        xml.guid directory_url(directory)
      end
    end
&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/577-xml-rss-feed" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor54431</id>
    <published>2008-10-08T18:41:57-07:00</published>
    <title>[PHP] On Getter and Setter</title>
    <content type="html">&lt;p&gt;I tend to do special handling when I need special handling. Like data validation being different depending on the variable in question.&lt;/p&gt;

&lt;p&gt;$foo-&amp;gt;email_addr = 'http://www.'; &lt;/p&gt;

&lt;p&gt;You would want different validation there then say:&lt;/p&gt;

&lt;p&gt;$foo-&amp;gt;web_addr = '&lt;a href="mailto:foo@bar.com"&gt;foo@bar.com&lt;/a&gt;';&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/425-getter-and-setter/refactors/54431" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code520</id>
    <published>2008-10-06T20:16:20-07:00</published>
    <updated>2011-09-05T06:52:59-07:00</updated>
    <title>[JavaScript] Toggle "Check all / Uncheck all" with jQuery</title>
    <content type="html">&lt;p&gt;I have a simple toggler to mass select (unselect) all input checkboxes with the &amp;quot;id&amp;quot; class in a form. It should be triggered from an anchor tag and have its text updated.&lt;/p&gt;

&lt;p&gt;The things I don't like are the hard codings of the &amp;quot;mode&amp;quot; and the anchor text labels.&lt;/p&gt;

&lt;pre&gt;Q = jQuery.noConflict();

Q(document).ready(function(){
    Q(&amp;quot;#toggler&amp;quot;).click(function() {
        var mode = this.mode == 'off' ? 'on' : 'off';
        this.mode = mode;

        Q(&amp;quot;form#the_form input.id&amp;quot;).each(function(){
            mode == 'off' ? this.checked = true : this.checked = false;
        }); 

        Q(this).text() == 'check all' ? Q(this).text('uncheck all') : Q(this).text('check all');
        return false;
    });
};

// called from a link...
// &amp;lt;a href=&amp;quot;#&amp;quot; id=&amp;quot;toggler&amp;quot;&amp;gt;check all&amp;lt;/a&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/520-toggle-check-all-uncheck-all-with-jquery" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor52165</id>
    <published>2008-10-06T19:46:44-07:00</published>
    <title>[JavaScript] On Small Javascript Code</title>
    <content type="html">&lt;p&gt;Some would say you should only have one return point from a function. And there's no need to pass the location object each invocation.&lt;/p&gt;

&lt;pre&gt;var getAnchor = function (default) {
  default ||= 'news';
  return document.location.toString().split( '#' )[1] || default;
};

loadURL(getAnchor()); // default is used if no anchor
loadURL(getAnchor(&amp;quot;help&amp;quot;)); // default is set to 'help' and used if no anchor
&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/518-small-javascript-code/refactors/52165" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor50370</id>
    <published>2008-10-06T02:05:25-07:00</published>
    <title>[JavaScript] On Small Javascript Code</title>
    <content type="html">&lt;p&gt;Get position of &amp;quot;#&amp;quot; in the location.
&lt;br /&gt;Check if we have a &amp;quot;#&amp;quot; at all (-1 is &amp;quot;none&amp;quot;)
&lt;br /&gt;Get substring from starting position to end of location length.&lt;/p&gt;

&lt;p&gt;Uses a two ternary statements to set has_hash boolean, and defining the page.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;/*

var biz;
if ( some_test ) { biz = 'foo' } else { biz = 'bar' }

can be written as a one-line ternary:

var biz = some_test ? 'foo' : 'bar';

*/

var pos_hash = document.location.indexOf(&amp;quot;#&amp;quot;);
var has_hash = pos_hash &amp;gt; 0 ? true : false;
var page = has_hash ? document.location.substring(pos_hash + 1, document.location.length) : 'news';

loadURL(page);
&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/518-small-javascript-code/refactors/50370" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor48214</id>
    <published>2008-10-05T02:18:11-07:00</published>
    <title>[Ruby] On render partial layout</title>
    <content type="html">&lt;p&gt;I think you just need to call it as a function call, instead of a symbol.&lt;/p&gt;

&lt;p&gt;Try this...&lt;/p&gt;

&lt;pre&gt;layout :set_layout # might be just set_layout



render partial 'new_alert',  :layout =&amp;gt; set_layout


def set_layout
     logged_in? ? &amp;quot;application&amp;quot; : &amp;quot;home&amp;quot;
end&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/410-render-partial-layout/refactors/48214" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor48177</id>
    <published>2008-10-05T01:58:51-07:00</published>
    <title>[Ruby] On link_to with an image_tag and text</title>
    <content type="html">&lt;p&gt;The image itself would be a 32px x 96px image with the 3 states (link, active, hover) shifted up 32px for each state. This allows the image to preload and there will be no delay when doing rollovers.&lt;/p&gt;

&lt;p&gt;The text is indented offscreen -9999px so it is not visable when the browser supports CSS. If the browser has CSS disabled, and/or images disabled, the user would simply see the text.&lt;/p&gt;

&lt;p&gt;The title attribute is added to the link tag to give users a tooltip and assistive devices.
&lt;/p&gt;

&lt;pre&gt;## view

&amp;lt;%= link_to &amp;quot;Go back&amp;quot;, path_to_go_back, { :class =&amp;gt; &amp;quot;prev&amp;quot;, :title =&amp;gt; &amp;quot;Previous page&amp;quot; } %&amp;gt;

## css
&amp;lt;style type=&amp;quot;text/css&amp;quot; media=&amp;quot;screen&amp;quot;&amp;gt;
// normal button icon
a.prev:link, a.prev:visited, a.prev:hover, a.prev:active {
    display: block; width: 32px; height: 32px; text-decoration: none; text-indent: -9999px;
    background: transparent url('/images/icons_prev.png') no-repeat 0 0;
}

// highlighted button
a.prev:hover {
    background-position: 0 -32px;
}

// depressed button icon
a.prev:active {
    background-position: 0 -64px;
}
&amp;lt;/style&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>chovy.myopenid.com</name>
      <email>spam@chovy.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/419-link_to-with-an-image_tag-and-text/refactors/48177" rel="alternate"/>
  </entry>
</feed>

