<?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:users591</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/591" rel="self"/>
  <title>mitch</title>
  <updated>Thu Jul 10 23:47:07 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12864</id>
    <published>2008-07-10T23:47:07-07:00</published>
    <title>[Ruby] On Recreating a list with acts_as_list</title>
    <content type="html">&lt;p&gt;Michael and zoopzopp:
&lt;br /&gt;Thanks for the refactorings, I think the final result is more readable.  &lt;/p&gt;

&lt;p&gt;Anthony:
&lt;br /&gt;I don't like your refactoring because it made me thing about my design too much! :)&lt;/p&gt;

&lt;p&gt;I'm really wrestling about your refactoring in my mind.  I see the advantages of having an intermediate model for assignments which would create a list.  On one hand I don't want that extra model, especially if it just handles the &amp;quot;listing&amp;quot; aspect of widgets.  I think a many-to-one association is what I was going for here rather than the many-to-many association that you created. &lt;/p&gt;

&lt;p&gt;On the other hand if I ever wanted to work with the assignments more or have the same widget on multiple pages I would definitely do that.  In the future a simple list of widgets on a page is probably not going to be enough as a widget may have to be assigned to a column for instance.  &lt;/p&gt;

&lt;p&gt;&amp;gt;I don't know why you'd want to clear the widgets and instantiate them just to assign them to the page object.&lt;/p&gt;

&lt;p&gt;As it stands now I DON'T think that I'm instantiating Widgets here and you ARE instantiating Assignments.  Maybe by instantiating you mean looking up the widgets in the database? (Maybe this is a correct use of the term).  Still instantiating Assignments (your technique) is probably less expensive than looking up Widgets.  &lt;/p&gt;

&lt;p&gt;Thanks for the help!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>mitch</name>
      <email>mitch.lloyd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/357-recreating-a-list-with-acts_as_list/refactors/12864" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code357</id>
    <published>2008-07-10T03:27:56-07:00</published>
    <updated>2009-05-11T21:15:57-07:00</updated>
    <title>[Ruby] Recreating a list with acts_as_list</title>
    <content type="html">&lt;p&gt;I tried playing with this a bit but had trouble even making it work.  The method gets a list of widgets in a string that looks like &amp;quot;4,3,6,1,12&amp;quot; from jQuery's sortable plugin.  &amp;quot;3,1,2&amp;quot; would mean that widget with id==3 is first, id==1 is second and id==2 is last.  &lt;/p&gt;

&lt;p&gt;The list must be recreated (I think).  If an empty string came in that would mean that the page should now have no widgets.  &lt;/p&gt;

&lt;p&gt;This code looks cryptic to me and may be slow.  Thanks for any help!&lt;/p&gt;

&lt;pre&gt;class Page &amp;lt; ActiveRecord::Base

def assigned_widgets=(string)
  array = string.split(',')
  self.widgets.clear
    
  1.upto(array.size) do |i|
    w = Widget.find(array[i-1])
    w.position = i
    self.widgets &amp;lt;&amp;lt; w
  end
end&lt;/pre&gt;</content>
    <author>
      <name>mitch</name>
      <email>mitch.lloyd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/357-recreating-a-list-with-acts_as_list" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor9801</id>
    <published>2008-06-06T01:15:13-07:00</published>
    <title>[Ruby] On ActiveRecord Validations with a lot of conditions</title>
    <content type="html">&lt;p&gt;One little change.  This wasn't working for me and I found a small change from the documentation.  Just passing something in the block.  &lt;/p&gt;

&lt;pre&gt;with_options :if =&amp;gt; :password_validations_required? do |p|
  p.validates_presence_of     :password
  p.validates_presence_of     :password_confirmation
  p.validates_length_of       :password, :within =&amp;gt; 4..40
  p.validates_confirmation_of :password
end
&lt;/pre&gt;</content>
    <author>
      <name>mitch</name>
      <email>mitch.lloyd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/307-activerecord-validations-with-a-lot-of-conditions/refactors/9801" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor9055</id>
    <published>2008-06-03T00:42:58-07:00</published>
    <title>[Ruby] On ActiveRecord Validations with a lot of conditions</title>
    <content type="html">&lt;p&gt;Thanks so much for the help!  I knew that rails must have a way to deal with this but I didn't see it in the documentation.  &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>mitch</name>
      <email>mitch.lloyd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/307-activerecord-validations-with-a-lot-of-conditions/refactors/9055" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code307</id>
    <published>2008-05-31T19:49:06-07:00</published>
    <updated>2008-06-06T01:15:16-07:00</updated>
    <title>[Ruby] ActiveRecord Validations with a lot of conditions</title>
    <content type="html">&lt;p&gt;I've never run into a situation where I wanted to use a lot of conditions on my validations.  The way I'm doing it seems a little ridiculous.  &lt;/p&gt;

&lt;pre&gt;in models/user.rb
...  

  validates_presence_of     :email,                      :if =&amp;gt; :email_validations_required?
  validates_presence_of     :password,                   :if =&amp;gt; :password_validations_required?
  validates_presence_of     :password_confirmation,      :if =&amp;gt; :password_validations_required?
  validates_length_of       :password, :within =&amp;gt; 4..40, :if =&amp;gt; :password_validations_required?
  validates_confirmation_of :password,                   :if =&amp;gt; :password_validations_required?
  validates_length_of       :nick,    :maximum =&amp;gt; 40,    :if =&amp;gt; :nick_validations_required?
  validates_length_of       :email,    :within =&amp;gt; 3..100,:if =&amp;gt; :email_validations_required?
  validates_format_of       :email, :with =&amp;gt; /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, 
    :on =&amp;gt; :create, :message =&amp;gt; &amp;quot;doesn't seem to be valid.&amp;quot;,
                                                         :if =&amp;gt; :email_validations_required?
  validates_format_of       :nick, :with =&amp;gt; /\A[^@]+\Z/i,:if =&amp;gt; :nick_validations_required?,
    :message =&amp;gt; &amp;quot;can't have a @ in it.  (Sorry, 1337 |-|@&amp;gt;&amp;lt;0R5!)&amp;quot;

  validates_uniqueness_of   :login,                      :if =&amp;gt; :nick_validations_required?
  validates_uniqueness_of   :email, :case_sensitive =&amp;gt; false, :if =&amp;gt; :email_validations_required?
...


def password_validations_required?
    (crypted_password.blank? &amp;amp;&amp;amp; !using_openid?) || !password.blank?
  end

  def email_validations_required?
    !email.blank? || !using_openid?
  end
  
  def nick_validations_required?
    !nick.blank?
  end
&lt;/pre&gt;</content>
    <author>
      <name>mitch</name>
      <email>mitch.lloyd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/307-activerecord-validations-with-a-lot-of-conditions" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor7172</id>
    <published>2008-05-17T14:22:17-07:00</published>
    <title>[Ruby] On Better Nest Ifs</title>
    <content type="html">&lt;p&gt;I know that this is an old thread but I was just confused by all the &amp;quot;!(something.nil?)&amp;quot; refactorings.  It think the logic is: If (the key is there) and (the invitation_details_count is less than the total) then it's valid.  &lt;/p&gt;

&lt;pre&gt;def self.valid?(key)
  (h = find_by_key(key)) &amp;amp;&amp;amp; (h.invitation_details_count &amp;lt; h.total)
end&lt;/pre&gt;</content>
    <author>
      <name>mitch</name>
      <email>mitch.lloyd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/282-better-nest-ifs/refactors/7172" rel="alternate"/>
  </entry>
</feed>

