<?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:users208</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/208" rel="self"/>
  <title>Etandrib</title>
  <updated>Tue Oct 30 20:41:12 -0700 2007</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor658</id>
    <published>2007-10-30T20:41:12-07:00</published>
    <title>[Ruby] On Three groups, three bits of information, too many partials.</title>
    <content type="html">&lt;p&gt;A problem with the solution Kevin presented is that the partial also includes the model name and if you are sharing the partials with other models then you'll need to dynamically swap out the model - in this case below it is member. I'm not sure how to do this. Any suggestions?&lt;/p&gt;

&lt;pre&gt;## views/shared/_email
&amp;lt;div class=&amp;quot;phone&amp;quot;&amp;gt;
&amp;lt;% fields_for &amp;quot;member[phone_attributes][]&amp;quot;, phone do |p| %&amp;gt;
  &amp;lt;%= p.text_field :number, :index =&amp;gt; nil %&amp;gt;
  &amp;lt;%= p.select :contact_type, Member::Contact_types, {}, :index =&amp;gt; nil %&amp;gt;
&amp;lt;% end %&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/106-three-groups-three-bits-of-information-too-many-partials/refactors/658" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor559</id>
    <published>2007-10-26T20:30:30-07:00</published>
    <title>[Ruby] On Three groups, three bits of information, too many partials.</title>
    <content type="html">&lt;p&gt;I also had to change the partial for adding a new model (location, email, etc.) you have to have a slash before shared or rails thinks that it is just the name of the partial. This is different than what the render =&amp;gt; :partial syntax wants. I found this out by a proposed change in the Rails code. I'm also using Rails 2.0 Preview.&lt;/p&gt;

&lt;pre&gt;## ApplicationHelper

  def add_another(model_name, label = &amp;quot;Add another&amp;quot;)
    link_to_function label do |page|
    	page.insert_html :bottom, model_name.pluralize, 
    	                 :partial =&amp;gt; &amp;quot;/shared/#{model_name}&amp;quot;, 
    	                 :object =&amp;gt; model_name.camelize.constantize.new
    end
  end

&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/106-three-groups-three-bits-of-information-too-many-partials/refactors/559" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor558</id>
    <published>2007-10-26T20:15:02-07:00</published>
    <title>[JavaScript] On ImageFlow - something like CoverFlow in Javascript</title>
    <content type="html">&lt;p&gt;I'm using safari v3 and it works just as it does in Camino. Performance isn't that great but hey it is CoverFlow in the browser. Pretty amazing JS. I don't have any ideas on refactoring.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/108-test/refactors/558" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code106</id>
    <published>2007-10-25T13:42:58-07:00</published>
    <updated>2007-10-30T20:41:15-07:00</updated>
    <title>[Ruby] Three groups, three bits of information, too many partials.</title>
    <content type="html">&lt;p&gt;I have three classes of people: Members, Volunteers, and Sponsors. They each can have multiple phones, emails, and locations. Right now I have a partial for each phone/email/location for each class. This doesn't seem very DRY since nothing changes except the model. How can this be refactored to share partials between these classes? Or is there an even better way I'm overlooking? Code follows&#226;&#8364;&#166;&lt;/p&gt;

&lt;pre&gt;## members/new.html.erb

  &amp;lt;%= f.text_field :first_name, :size =&amp;gt; 15 %&amp;gt; 
  &amp;lt;%= f.text_field :last_name, :size =&amp;gt; 15 %&amp;gt;

&amp;lt;div id=&amp;quot;phones&amp;quot;&amp;gt;Phone Numbers
  &amp;lt;%= render :partial =&amp;gt; 'phone', :collection =&amp;gt; @member.phones %&amp;gt;
&amp;lt;/div&amp;gt;
  &amp;lt;%= add_another_link &amp;quot;phone&amp;quot; %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

&amp;lt;div id=&amp;quot;emails&amp;quot;&amp;gt;Email Addresses
  &amp;lt;%= render :partial =&amp;gt; 'email', :collection =&amp;gt; @ member.emails %&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;%= add_another_link 'email' %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

&amp;lt;div id=&amp;quot;locations&amp;quot;&amp;gt;Locations
  &amp;lt;%= render :partial =&amp;gt; 'location', :collection =&amp;gt; @ member.locations %&amp;gt;
&amp;lt;/div&amp;gt;
  &amp;lt;%= add_another_link 'location' %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

## volunteers/new.html.erb

  &amp;lt;%= f.text_field :first_name, :size =&amp;gt; 15 %&amp;gt; 
  &amp;lt;%= f.text_field :last_name, :size =&amp;gt; 15 %&amp;gt;

&amp;lt;div id=&amp;quot;phones&amp;quot;&amp;gt;Phone Numbers
  &amp;lt;%= render :partial =&amp;gt; 'phone', :collection =&amp;gt; @ volunteer.phones %&amp;gt;
&amp;lt;/div&amp;gt;
  &amp;lt;%= add_another_link &amp;quot;phone&amp;quot; %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

&amp;lt;div id=&amp;quot;emails&amp;quot;&amp;gt;Email Addresses
  &amp;lt;%= render :partial =&amp;gt; 'email', :collection =&amp;gt; @ volunteer.emails %&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;%= add_another_link 'email' %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

&amp;lt;div id=&amp;quot;locations&amp;quot;&amp;gt;Locations
  &amp;lt;%= render :partial =&amp;gt; 'location', :collection =&amp;gt; @ volunteer.locations %&amp;gt;
&amp;lt;/div&amp;gt;
  &amp;lt;%= add_another_link 'location' %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

## sponsors/new.html.erb

  &amp;lt;%= f.text_field :first_name, :size =&amp;gt; 15 %&amp;gt; 
  &amp;lt;%= f.text_field :last_name, :size =&amp;gt; 15 %&amp;gt;

&amp;lt;div id=&amp;quot;phones&amp;quot;&amp;gt;Phone Numbers
  &amp;lt;%= render :partial =&amp;gt; 'phone', :collection =&amp;gt; @sponsor.phones %&amp;gt;
&amp;lt;/div&amp;gt;
  &amp;lt;%= add_another_link &amp;quot;phone&amp;quot; %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

&amp;lt;div id=&amp;quot;emails&amp;quot;&amp;gt;Email Addresses
  &amp;lt;%= render :partial =&amp;gt; 'email', :collection =&amp;gt; @ sponsor.emails %&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;%= add_another_link 'email' %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

&amp;lt;div id=&amp;quot;locations&amp;quot;&amp;gt;Locations
  &amp;lt;%= render :partial =&amp;gt; 'location', :collection =&amp;gt; @ sponsor.locations %&amp;gt;
&amp;lt;/div&amp;gt;
  &amp;lt;%= add_another_link 'location' %&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;

## _phone.html.erb
&amp;lt;div class=&amp;quot;phone&amp;quot;&amp;gt;
&amp;lt;% fields_for &amp;quot;member[phone_attributes][]&amp;quot;, phone do |p| %&amp;gt;
  &amp;lt;%= p.text_field :number, :index =&amp;gt; nil %&amp;gt;
  &amp;lt;%= p.select :contact_type, Member::Contact_types, {}, :index =&amp;gt; nil %&amp;gt;
&amp;lt;% end %&amp;gt;
&amp;lt;/div&amp;gt;

## _email.html.erb
&amp;lt;div class=&amp;quot;email&amp;quot;&amp;gt;
&amp;lt;% fields_for &amp;quot;member[email_attributes][]&amp;quot;, email do |e| %&amp;gt;
  &amp;lt;%= e.text_field :address, :index =&amp;gt; nil %&amp;gt;
  &amp;lt;%= e.select :contact_type, Member::Contact_types, {}, :index =&amp;gt; nil %&amp;gt;
&amp;lt;% end %&amp;gt;
&amp;lt;/div&amp;gt;

## _location.html.erb
&amp;lt;div class=&amp;quot;location&amp;quot;&amp;gt;
&amp;lt;% fields_for &amp;quot;member[location_attributes][]&amp;quot;, location do |l| %&amp;gt;
  &amp;lt;%= l.text_field :address, :index =&amp;gt; nil %&amp;gt;
  &amp;lt;%= l.select :contact_type, Member::Contact_types, {}, :index =&amp;gt; nil %&amp;gt;
&amp;lt;% end %&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/106-three-groups-three-bits-of-information-too-many-partials" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor470</id>
    <published>2007-10-18T15:34:43-07:00</published>
    <title>[JavaScript] On Combining javascript for 2+ models</title>
    <content type="html">&lt;p&gt;This works great. For other people following the call needs to look something similar to what I have below.&lt;/p&gt;

&lt;pre&gt;## _email.rhtml
&amp;lt;%= link_to_function &amp;quot;Remove&amp;quot;, &amp;quot;mark_for_destroy(this,'.email')&amp;quot; %&amp;gt;

&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/92-combining-javascript-for-2-models/refactors/470" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code92</id>
    <published>2007-10-17T21:32:28-07:00</published>
    <updated>2010-01-06T14:23:24-08:00</updated>
    <title>[JavaScript] Combining javascript for 2+ models</title>
    <content type="html">&lt;p&gt;This is some javascript which flags an element (and hides it from the current view) to be deleted when a form is submitted. Thanks for the help! I've learned a lot from my previous posts as well. This is a great community!&lt;/p&gt;

&lt;pre&gt;## application.js

function mark_for_destroy(element) {
  $(element).next('.should_destroy').value = 1;
  $(element).up('.phone').hide();
}

function e_mark_for_destroy(element) {
  $(element).next('.should_destroy').value = 1;
  $(element).up('.email').hide();
}&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/92-combining-javascript-for-2-models" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor460</id>
    <published>2007-10-17T21:06:19-07:00</published>
    <title>[Ruby] On Combining add/remove links</title>
    <content type="html">&lt;p&gt;Ah, ok. I see now. Thanks!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/89-combining-add-remove-links/refactors/460" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor458</id>
    <published>2007-10-17T20:50:56-07:00</published>
    <title>[Ruby] On Combining add/remove links</title>
    <content type="html">&lt;p&gt;Here is where I call the links. For some reason I get an error with the new code saying the following:
&lt;br /&gt;Showing app/views/people/_fields.rhtml where line #11 raised: &amp;quot;Add another&amp;quot; is not a valid constant name!&amp;quot;
&lt;/p&gt;

&lt;pre&gt;##_fields

&amp;lt;h3&amp;gt;Phone Numbers&amp;lt;/h3&amp;gt;
  &amp;lt;%= render :partial =&amp;gt; 'phone', :collection =&amp;gt; @person.phones %&amp;gt;
&amp;lt;%= add_associated_link &amp;quot;Add another&amp;quot; %&amp;gt;

&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/89-combining-add-remove-links/refactors/458" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor441</id>
    <published>2007-10-16T21:56:05-07:00</published>
    <title>[Ruby] On Combine contact attributes in model</title>
    <content type="html">&lt;p&gt;Thanks for this. I think I understand it a little better. I need to look into the class_eval stuff you've done here. I haven't seen that before.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/87-combine-contact-attributes-in-model/refactors/441" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code89</id>
    <published>2007-10-16T21:41:57-07:00</published>
    <updated>2010-06-22T19:42:47-07:00</updated>
    <title>[Ruby] Combining add/remove links</title>
    <content type="html">&lt;p&gt;I've got a bunch of add and remove functions that would be nice to refactor without all the duplication. Lets see what you can do! Thanks!&lt;/p&gt;

&lt;pre&gt;  ## Helper

  def add_phone_link(name)
    link_to_function &amp;quot;Add another&amp;quot; do |page|
    	page.insert_html :bottom, :phones, :partial =&amp;gt; 'phone', :object =&amp;gt; Phone.new
    end
  end
  
  def remove_phone_link(name)
    link_to_function &amp;quot;Remove&amp;quot; do |page|
      page[&amp;quot;phone_#{params[:index]}&amp;quot;].remove
    end
  end
  
  def add_email_link(name)
    link_to_function &amp;quot;Add another&amp;quot; do |page|
    	page.insert_html :bottom, :emails, :partial =&amp;gt; 'email', :object =&amp;gt; Email.new
    end
  end
  
  def remove_email_link(name)
    link_to_function &amp;quot;Remove&amp;quot; do |page|
      page[&amp;quot;email_#{params[:index]}&amp;quot;].remove
    end
  end&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/89-combining-add-remove-links" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code87</id>
    <published>2007-10-16T20:34:29-07:00</published>
    <updated>2007-12-04T02:13:48-08:00</updated>
    <title>[Ruby] Combine contact attributes in model</title>
    <content type="html">&lt;p&gt;We have a model where a person has many phones, emails, addresses, etc. It would be nice to refactor these into something more compact. I'm basically replicating the &amp;quot;model_attributes&amp;quot; and &amp;quot;save_model&amp;quot; functions over and over for each piece of contact information I add to a user.&lt;/p&gt;

&lt;pre&gt;## Model
  def phone_attributes=(phone_attributes)
    phone_attributes.each do |attributes|
      if attributes[:id].blank?
        phones.build(attributes)
      else
        phone = phones.detect { |t| t.id == attributes[:id].to_i }
        phone.attributes = attributes
      end
    end
  end
  
  def save_phones
    phones.each do |t|
      if t.should_destroy?
        t.destroy
      else
        t.save(false)
      end
    end
  end
  
  def email_attributes=(email_attributes)
    email_attributes.each do |attributes|
      if attributes[:id].blank?
        emails.build(attributes)
      else
        email = emails.detect { |e| e.id == attributes[:id].to_i }
        email.attributes = attributes
      end
    end
  end
  
  def save_emails
    emails.each do |e|
      if e.should_destroy?
        e.destroy
      else
        e.save(false)
      end
    end
  end&lt;/pre&gt;</content>
    <author>
      <name>Etandrib</name>
      <email>etandrib@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/87-combine-contact-attributes-in-model" rel="alternate"/>
  </entry>
</feed>

