<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Railsflavored - Home</title>
  <id>tag:railsflavored.com,2008:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://railsflavored.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://railsflavored.com/" rel="alternate" type="text/html"/>
  <updated>2008-05-12T20:02:31Z</updated>
  <entry xml:base="http://railsflavored.com/">
    <author>
      <name>mark</name>
    </author>
    <id>tag:railsflavored.com,2008-05-12:11</id>
    <published>2008-05-12T20:00:00Z</published>
    <updated>2008-05-12T20:02:31Z</updated>
    <link href="http://railsflavored.com/2008/5/12/ruby-prof-shoots-down-my-helper-block" rel="alternate" type="text/html"/>
    <title>ruby-prof shoots down my helper block</title>
<content type="html">
            &lt;p&gt;
	Here is the head of my ruby-prof output.  I'm surprised to see that one of
	my fairly simple helper methods (ApplicationHelper#highlight_my) is topping the charts of the expensive procedures list.  
&lt;/p&gt;
		
&lt;pre&gt;

 18.67      0.27     0.27     0.00     0.00      237  Proc#binding
 17.61      0.25     0.25     0.00     0.00    10208  WIN32OLE#method_missing
 13.05      0.19     0.19     0.00     0.00       96  ApplicationHelper#highlight_my

&lt;/pre&gt;

&lt;p&gt;
	All it does is slap a span tag around any task that
	belongs to the logged in user.  
&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
    &amp;lt;% highlight_my(task.assignee) do %&gt;
	   ...
    &amp;lt;% end %&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
	The span tag has a css class that will highlight those tasks.
&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
  def highlight_my(obj, &amp;block)
    if obj != nil
      mine = obj.id == current_user.id
    end
    concat('&amp;lt;span class=&quot;mine&quot;&amp;gt;', block.binding) if mine
    block.call
    concat('&amp;lt;/span&amp;gt;', block.binding) if mine
  end
			
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
	It appears the block binding might be a bit much overhead for such a
	simple operation.  Perhaps a better use for it would be to wrap something
	more significant like a table to add rounded corners.  You may only need to do this a handful
	of times rather than invoking the method 96 times like I did here.  
	I still really like the syntax so i my play around with some different
	kinds of partial caching schemes to see if i can keep this in my bag of tricks.
&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://railsflavored.com/">
    <author>
      <name>mark</name>
    </author>
    <id>tag:railsflavored.com,2008-04-08:10</id>
    <published>2008-04-08T17:02:00Z</published>
    <updated>2008-04-08T17:04:29Z</updated>
    <link href="http://railsflavored.com/2008/4/8/acts_as_ferret-indexing-on-model-reloads" rel="alternate" type="text/html"/>
    <title>Acts_As_Ferret Indexing on Model Reloads</title>
<content type="html">
            &lt;p&gt;
I've been using spec_server to run tests as I develop and noticed that each time
that my tests run it would take just a bit longer run.  Right about the same
time I was alerted by a file system error about a file being locked.  It happened
to be in the ferret indexing play yard where there were gobs of files.&lt;/p&gt;

&lt;p&gt;No prob, I thought.  I put a remove_dir routine in my spec helper and
it cleared out the index files without a hitch.  But my run times were
still increasing.&lt;/p&gt;

&lt;p&gt;So it appears that AAF likes to auto-index when its gets initialized and
during development it gets reloaded often.  There is a class method in
AAF called disable_ferret but by the time I am able to use this method the
indexing has already taken place.&lt;/p&gt;

&lt;p&gt;For a short term hack I decided I wanted my tests to run faster so I
dumped AAF while using spec_server which runs is test mode.  I perform
my testing on ferret during integration testing in a different mode so
I don't need to worry about toggling this line off and on and worry about
whether it will be there when it is released.&lt;/p&gt;

&lt;p&gt;There is already &lt;a href=&quot;http://projects.jkraemer.net/acts_as_ferret/ticket/172&quot;&gt;ticket&lt;/a&gt; in the trac system for this.  Till then here is what I've got.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
  acts_as_ferret :fields =&gt; {...} unless RAILS_ENV == 'test'

&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://railsflavored.com/">
    <author>
      <name>mark</name>
    </author>
    <id>tag:railsflavored.com,2008-04-02:9</id>
    <published>2008-04-02T10:21:00Z</published>
    <updated>2008-04-02T10:21:41Z</updated>
    <link href="http://railsflavored.com/2008/4/2/know-thy-collection-helpers" rel="alternate" type="text/html"/>
    <title>know thy collection helpers</title>
<content type="html">
            &lt;p&gt;Are you tired of seeing blog posts that takes a familiar chunk of code and turns it into a one liner?   Neither am I, how could you possibly be.  This weeks installment comes from a situation where i have task lists and tasks in the usual parent-child relationship.  For reasons not explained here I have a list of tasks that reference their own copy of their task list parent.  So now I am wanting to collect a unique set of the task lists.&lt;/p&gt;

&lt;p&gt;Here is what I came up with&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
  def self.collect_unique_parents(tasks)
    task_lists = []
    tasks.each do |task| 
      task_list = task.task_list
      task_lists &amp;lt;&amp;lt; task_list unless task_lists.include?(task_list)
    end
    task_lists
  end
  
  task_lists = collect_unique_parents(tasks)

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But I decided to consult the available collection helpers and realized that group_by would be a good fit for this case.  Also symbol-to-proc is thrown in for good measure.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
  task_lists = tasks.group_by(&amp;amp; :task_list).keys

&lt;/code&gt;&lt;/pre&gt;

However, beware that this will mess up any sort ordering because the group_by is hashing the results and I'm just pulling them back out with #keys.
          </content>  </entry>
  <entry xml:base="http://railsflavored.com/">
    <author>
      <name>mark</name>
    </author>
    <id>tag:railsflavored.com,2008-03-18:8</id>
    <published>2008-03-18T01:36:00Z</published>
    <updated>2008-10-24T20:09:25Z</updated>
    <link href="http://railsflavored.com/2008/3/18/shrinking-down-has_many_polymorphs" rel="alternate" type="text/html"/>
    <title>a visual tour of some active record associations</title>
<content type="html">
            &lt;p&gt;
I have been sitting in my office with drawings of active record associations hanging on my whiteboard behind me for quite a while.  When I began learning about active record associations it was helpful to have something visual nearby for quick reminder.  I decided to dump them into images using my pathetic Inkscape svg skills. &lt;/p&gt;

&lt;p&gt;
To clarify how these boxes are used you first need to know that the association method is describing what goes in the green box.  I would have put the method inside of it but there wasn't always enough room.  Also the back end dot of the arrow represents where the foreign sits.  So in the case of belogns_to, the foreign key of the other class sits in the declarers own table.  When you see the colored bars that refers to the :type column that stores the name of the target class.  Having the colors match resembles having the types match.
&lt;/p&gt;

&lt;p&gt;
The last one you can ignore as it is mostly a work in progress for how i've been hacking on has_many_polymorphs.  That can be a topic for another day.
&lt;/p&gt;

&amp;lt;style&gt;
.hmp p {
  border: 1px dashed grey;
  padding: 30px;
  font-size: 24px;
  font-weight: bold;
  color: gray;
}
&amp;lt;/style&gt;

&lt;div class=&quot;hmp&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/17/has_one.png&quot;&gt;&lt;/p&gt;
  &lt;p&gt;&lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/17/belongs_to.png&quot;&gt;&lt;/p&gt;
  &lt;p&gt;&lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/18/has_many.png&quot;&gt;&lt;/p&gt;
  &lt;p&gt;&lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/17/has_and_belongs_to_many.png&quot; width=&quot;460px&quot;&gt;&lt;/p&gt;
  &lt;p&gt;
    &lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/17/belongs_to_polymorphic.png&quot;&gt;&lt;br&gt;
    example: case note
  &lt;/p&gt;
  &lt;p&gt;&lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/17/has_many_as.png&quot;&gt;&lt;br&gt;
    example: something having case notes
  &lt;/p&gt;
  &lt;p&gt;&lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/17/has_many_polymorphs.png&quot; width=&quot;460px&quot;&gt;&lt;br&gt;
    example: tagging multiple types
  &lt;/p&gt;
  &lt;p&gt;&lt;img src=&quot;http://www.railsflavored.com/assets/2008/3/17/has_many_polymorphs_and_has_one.png&quot; width=&quot;460px&quot;&gt;&lt;br&gt;
    example: milestone
  &lt;/p&gt;
&lt;/div&gt;
          </content>  </entry>
  <entry xml:base="http://railsflavored.com/">
    <author>
      <name>mark</name>
    </author>
    <id>tag:railsflavored.com,2008-02-06:7</id>
    <published>2008-02-06T10:44:00Z</published>
    <updated>2008-04-02T09:49:18Z</updated>
    <link href="http://railsflavored.com/2008/2/6/observe-this-task-closely" rel="alternate" type="text/html"/>
    <title>observe this task closely</title>
<content type="html">
            &lt;p&gt;I have a number of task lists that each contains a number of tasks [one to many] that may be active or complete.  If all of the tasks are complete then I want to mark the task list as complete.  I would like to know which task lists are complete without having to perform a join on the task table and count them each time I load the index page.  &lt;/p&gt;

&lt;p&gt;Instead I will keep track of each time a task is created, deleted, changed from active to complete or changed from complete back to active state.  Then I can just look for task lists that have an ‘active task count’ of 0.&lt;/p&gt;

&lt;p&gt;It sounded tough at first and I tried a number of different ways but eventually came up with a way to do it in 26 lines of elegant code and touching only one file.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;amazing&lt;/em&gt; part (to me) is that code below is 100% complete and requires no changes to the to the TaskList class or the Task class which means that there is no worrying that someone will forget to update the task list count.  &lt;/p&gt;

&lt;p&gt;Also the entire thing can be turned off by commenting out the very last line “TaskObserver.instance” and nothing will break.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
class TaskObserver &amp;lt; ActiveRecord::Observer
  def after_initialize(task)
    task.orig_complete = task.complete
  end

  def after_update(task)
    if task.orig_complete != task.complete
      if task.complete == true
        task.task_list.decrement(:active_tasks_count).save
      else
        task.task_list.increment(:active_tasks_count).save
      end
    end
  end

  def after_create(task)
    task.task_list.increment(:active_tasks_count).save
  end

  def after_destroy(task)
    task.task_list.decrement(:active_tasks_count).save unless task.complete?
  end

end

TaskObserver.instance
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://railsflavored.com/">
    <author>
      <name>mark</name>
    </author>
    <id>tag:railsflavored.com,2008-02-04:6</id>
    <published>2008-02-04T11:05:00Z</published>
    <updated>2008-02-08T03:21:52Z</updated>
    <link href="http://railsflavored.com/2008/2/4/blank" rel="alternate" type="text/html"/>
    <title>blank?</title>
<content type="html">
            &lt;p&gt;I have confused myself in the past when trying to use this method in the wrong places.&lt;/p&gt;

&lt;p&gt;Using the method #blank? is useful but confusion can come from not knowing when it can be applied.  For example rails adds the method for you so if you write a standalone ruby program it won&#8217;t be there unless you add it yourself.  Also you may confuse having a nil object with not having an object. Example&#8230;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
puts @a 
puts b
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This little script will choke on the second line since it is not defined.  So while you can call methods on a nil class you cannot call methods on an undefined variable.  Using the @ symbol will implicitly scope the variable as an object and give you a nil class if you don&#8217;t define it.  So now when we do this.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
puts @a.blank?
puts b.blank?
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first variable is a Nil object that supports the #blank? method but the second is undefined and will choke.&lt;/p&gt;
          </content>  </entry>
</feed>
