<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Janos Mucsi&#039;s Got Ruby?</title>
	<atom:link href="http://janosmucsi.thruhere.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://janosmucsi.thruhere.net</link>
	<description>BetteR software</description>
	<lastBuildDate>Thu, 11 Mar 2010 19:58:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rails memory usage testing</title>
		<link>http://janosmucsi.thruhere.net/2010/03/rails-memory-usage-testing/</link>
		<comments>http://janosmucsi.thruhere.net/2010/03/rails-memory-usage-testing/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 19:26:52 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=259</guid>
		<description><![CDATA[We need to write a test to detect possible memory leaks in some Rails code. The test itself is written in Ruby.
Testing GETs
I simply put the URLs in an array:

uris = %w(/admin
/users)

Then I make GET calls using the net API that comes with Ruby:

uris.each do &#124;uri&#124;
  Net::HTTP.get(uri)
end

To determine the virtual memory used by Rails, [...]]]></description>
			<content:encoded><![CDATA[<p>We need to write a test to detect possible memory leaks in some Rails code. The test itself is written in Ruby.</p>
<h3>Testing GETs</h3>
<p>I simply put the URLs in an array:</p>
<pre class="brush: ruby">
uris = %w(/admin
/users)
</pre>
<p>Then I make GET calls using the net API that comes with Ruby:</p>
<pre class="brush: ruby">
uris.each do |uri|
  Net::HTTP.get(uri)
end
</pre>
<p>To determine the virtual memory used by Rails, execute:</p>
<pre class="brush: ruby">
memory_check = `ps -eo vsz,args | grep script/server`
m = memory_check.match(/(\d+)/)
virtual = m[0].to_i
</pre>
<p>If you don&#8217;t manually start the server, replace the string after grep with<br />
the name of your process.</p>
<h3>Testing POSTs</h3>
<p>Operations that modify data are usually more involved to set up because the POST<br />
data is sent in the body of the request and also because there may be constraints<br />
in the database. We, therefore, modify the datastructure to include the POST data<br />
separately from the URI:</p>
<pre class="brush: ruby">
uris = [&quot;/admin&quot;,
        [&quot;/admin/users&quot;, {&quot;client_id&quot; =&gt; &quot;387&quot;, &quot;email&quot;=&gt;&quot;test@mycompany.com&quot;, &quot;permissions_select&quot;=&gt;&quot;standard&quot; },
        Proc.new { |hash| hash[&quot;email&quot;] = &quot;test#{Time.now.to_i.to_s}@mycompany.com&quot;} ]]
</pre>
<p>We have an Array now. The first element will be still the URI. The second element contains<br />
the submit data &#8211; Rails style. Finally, we specify a Proc object to modify the data<br />
in a way that lets us send the request multiple times without getting constraint or<br />
validation errors. The example Proc above ensures that each email is unique by using<br />
the current time to build it.<br />
Then in the code we inspect the first element of each URI to detect whether it&#8217;s a GET<br />
or a POST:</p>
<pre class="brush: ruby">
uris.each do |uri|
  post = false
  case uri
    when String
      full_uri = uri
    when Array
      # POST with a hash of values
      full_uri = uri[0]
      post = true
  end
  # test code here
end
</pre>
<p>Before submitting, we check the post flag and act accordingly. We invoke the supplied<br />
Proc object passing in the Hash containing the POST data for modification:</p>
<pre class="brush: ruby">
url = URI.parse full_uri
  Net::HTTP.get(url) if !post
  if post
    # let&#039;s customize
    uri[2].call(uri[1])
    Net::HTTP.post_form(url, uri[1])
  end
end
</pre>
<h3>Hitting each URL multiple times</h3>
<p>The memory test is only realistic with many hits. For this, we introduce a variable<br />
called @how_many into our mini-framework that the caller can set.</p>
<pre class="brush: ruby">
uris = [ &quot;/admin&quot; ]
module Net
  class HTTP
    class &lt;&lt; self

      attr_accessor :prefix
      attr_accessor :how_many

      def fetch(uris)
        data = []

        uris.each do |uri|
          post = false
          case uri
            when String
              full_uri = prefix + uri
            when Array
              # POST with a hash of values
              full_uri = prefix + uri[0]
              post = true
          end

          url = URI.parse full_uri
          @how_many.times do
            Net::HTTP.get(url) if !post
            if post
              # let&#039;s customize
              uri[2].call(uri[1])
              Net::HTTP.post_form(url, uri[1])
            end

            memory_check = `ps -eo vsz,args | grep script/server`
            m = memory_check.match(/(\d+)/)
            virtual = m[0].to_i
          end
        end
      end
    end
  end
end
Net::HTTP.how_many = 50
Net::HTTP.prefix = &#039;http://localhost:3000&#039;
Net::HTTP.fetch(uris)
</pre>
<p>We&#8217;ve also added a prefix to the URI so that we can test against any host/port<br />
combination.</p>
<h3>Data collection and graph</h3>
<p>We use an array to store the memory used after each hit.</p>
<pre class="brush: ruby">
def fetch(uris)
  data = []
  # processing goes here
  memory_check = `ps -eo vsz,args | grep script/server`
  m = memory_check.match(/(\d+)/)
  virtual = m[0].to_i
  data &lt;&lt; virtual
  # more processing
end
</pre>
<p>Once we looped through all URIs and hit them as many times as specified, we have an array that we<br />
can use the draw a graph. gruff is well-suited for this purpose. First we need to<br />
install ImageMagick,  then we implement a module to encapsulate graphing code:</p>
<pre class="brush: ruby">
require &#039;gruff&#039;

module Net
  module Grapher

    def draw_graph(title, url_count, times, data)
      puts &#039;drawing graph!&#039;
      g = Gruff::Line.new
      g.title = title

      g.data(&quot;#{url_count.to_s}/#{times.to_s}&quot;, data)

      g.labels = {0 =&gt; &#039;0&#039;, 40 =&gt; &#039;40&#039;, data.size =&gt; &quot;#{data.size}&quot;}

      file_name = &quot;#{title.gsub(/\//, &#039;&#039;)}_#{url_count.to_s}_#{times.to_s}.png&quot;
      File.delete(file_name) if File.exists? file_name
      g.write(file_name)
    end
  end
  class HTTP
    extend Grapher
    # more code here
  end
end
</pre>
<p>The draw_grap method is available now. It takes a title, and some statistical information.<br />
The last argument is the Array itself containing the data. Since the number of hits<br />
and the number of URIs is important to evaluate the graph, we use that information<br />
to construct the name of the file name:</p>
<pre class="brush: ruby">
file_name = &quot;#{title.gsub(/\//, &#039;&#039;)}_#{url_count.to_s}_#{times.to_s}.png&quot;
</pre>
<h3>Running the test</h3>
<p>Let&#8217;s repeat the code in its entirety:</p>
<pre class="brush: ruby">
#!/usr/local/bin/ruby
require &#039;gruff&#039;

uris = [&quot;/admin&quot;,
        [&quot;/admin/projects&quot;, {&quot;admin_project[change_id]&quot;=&gt;&quot;0&quot;,
                              &quot;admin_project[new_project_support_contact_attributes][][work_phone]&quot;=&gt;&quot;&quot;,
                              &quot;admin_project[new_project_support_contact_attributes][][last_name]&quot;=&gt;&quot;&quot;,
                              &quot;admin_project[new_project_support_contact_attributes][][first_name]&quot;=&gt;&quot;&quot;,
                              &quot;admin_project[new_project_support_contact_attributes][][mobile_phone]&quot;=&gt;&quot;209-346-4294&quot;,
                              &quot;admin_project[client_id]&quot;=&gt;&quot;387&quot;,
                              &quot;admin_project[description]&quot;=&gt;&quot;&quot;,
                              &quot;admin_project[project_name]&quot;=&gt;&quot;This is test4448&quot;,
                              &quot;commit&quot;=&gt;&quot;Save&quot;,
                              &quot;client_id&quot;=&gt;&quot;387&quot;}, Proc.new { |hash| hash[&quot;admin_project[project_name]&quot;] = &quot;This is test#{Time.now.to_i.to_s}&quot; } ]
]

module Net
  module Grapher

    def draw_graph(title, url_count, times, data)
      puts &#039;drawing graph!&#039;
      g = Gruff::Line.new
      g.title = title

      g.data(&quot;#{url_count.to_s}/#{times.to_s}&quot;, data)

      g.labels = {0 =&gt; &#039;0&#039;, 40 =&gt; &#039;40&#039;, data.size =&gt; &quot;#{data.size}&quot;}

      file_name = &quot;#{title.gsub(/\//, &#039;&#039;)}_#{url_count.to_s}_#{times.to_s}.png&quot;
      File.delete(file_name) if File.exists? file_name
      g.write(file_name)
    end
  end
  class HTTP
    extend Grapher
    class &lt;&lt; self

      attr_accessor :prefix
      attr_accessor :how_many

      def fetch(uris)
        data = []

        uris.each do |uri|
          post = false
          case uri
            when String
              full_uri = prefix + uri
            when Array
              # POST with a hash of values
              full_uri = prefix + uri[0]
              post = true
          end

          url = URI.parse full_uri
          @how_many.times do
            Net::HTTP.get(url) if !post
            if post
              # let&#039;s customize
              uri[2].call(uri[1])
              Net::HTTP.post_form(url, uri[1])
            end

            memory_check = `ps -eo vsz,args | grep script/server`
            m = memory_check.match(/(\d+)/)
            virtual = m[0].to_i
            data &lt;&lt; virtual
          end
        end

        draw_graph(uris[0], uris.size, @how_many, data)
      end
    end
  end
end

Net::HTTP.how_many = 50
Net::HTTP.prefix = &#039;http://localhost:3000&#039;
Net::HTTP.fetch(uris)
</pre>
<p>Make sure the file has the x flag set so it&#8217;s executable. If we save the code to<br />
a file called test_me.rb and run ./test_me.rb, we get a nice graph like this:<br />
</ br><br />
<a href="http://janosmucsi.thruhere.net/wp-content/uploads/2010/03/admin_5_100_put_second.png"><img src="http://janosmucsi.thruhere.net/wp-content/uploads/2010/03/admin_5_100_put_second.png" alt="" title="Virtual memory usage" width="800" height="600" class="alignnone size-full wp-image-261" /></a></p>
<h3>Evaluation</h3>
<p>This test was run on a Linux machine with 4G of memory. We hit 5 URLs, each a 100 times. The difference between the highest<br />
and lowest memory usage is less than 4K so we see a nice garbage collection pattern.<br />
No memory leak so far!</p>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/03/rails-memory-usage-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to write a test for a Rails filter</title>
		<link>http://janosmucsi.thruhere.net/2010/03/how-to-write-a-test-for-a-rails-filter/</link>
		<comments>http://janosmucsi.thruhere.net/2010/03/how-to-write-a-test-for-a-rails-filter/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 02:04:04 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=237</guid>
		<description><![CDATA[How do we test a filter in ApplicationController? We want to bypass all the logic in the controllers that subclass it,
but we still want a realistic test with a request and a response. Such tests are
called functional tests in the Rails world.
Setting up the test
Our ApplicationController that has the filter is typical, with a before_filter:

class [...]]]></description>
			<content:encoded><![CDATA[<p>How do we test a filter in ApplicationController? We want to bypass all the logic in the controllers that subclass it,<br />
but we still want a realistic test with a request and a response. Such tests are<br />
called functional tests in the Rails world.</p>
<h3>Setting up the test</h3>
<p>Our ApplicationController that has the filter is typical, with a before_filter:</p>
<pre class="brush: ruby">
class ApplicationController &lt; ActionController::Base
  before_filter :check_logged_on
..
end
</pre>
<p>We create a test in test/functional/application_controller_test.rb:</p>
<pre class="brush: ruby">
require File.dirname(__FILE__) + &#039;/../test_helper&#039;

class FooControllerTest &lt; ActionController::TestCase
  def setup
  end

  def test_before_filter
  end
end
</pre>
<p>We do need a real controller for a real test, but it can not be any of the existing ones. So let&#8217;s create a controller on the fly:</p>
<pre class="brush: ruby">
class FooController &lt; ApplicationController
    def index
    render :text =&gt; &quot;index called&quot;
  end
end
</pre>
<p>For simplicity, we just render text, so we don&#8217;t have to create a view.<br />
Now we can write the setup method:</p>
<pre class="brush: ruby">
class FooControllerTest &lt; ActionController::TestCase
  def setup
    @controller = FooController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    ActionController::Routing::Routes.draw do |map|
      map.resources :foo
    end
  end
end
</pre>
<p>We needed to add the routing code, so we can hit the controller. Now we are ready to test something.<br />
Let&#8217;s say, the filter renders an error if the user is not logged in:</p>
<pre class="brush: ruby">
class ApplicationController &lt; ActionController::Base
  def check_logged_on
      unless @logged_on
        render :status =&gt; 500
      end
  end
  # more code here
end
</pre>
<p>Of course, in the real world, this method would be more complicated, but this example illustrates the point. Our first test looks like this:</p>
<pre class="brush: ruby">
class FooControllerTest &lt; ActionController::TestCase
  def setup
    @controller = FooController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    ActionController::Routing::Routes.draw do |map|
      map.resources :foo
    end
  end

  def test_bypass
    @controller.logged_on = true
    get :index
    assert_response :success
  end
end
</pre>
<p>For this to work, we need an accessor in FooController:</p>
<pre class="brush: ruby">
class FooController &lt; ApplicationController
  prepend_before_filter :check_logged_on
  attr_accessor :logged_on

  def index
    render :text =&gt; &quot;index called&quot;
  end
end
</pre>
<p>If you run this, you will get success because FooController#index has been called and by default the status code returned is 200 which is :success.<br />
The second test, will run the full code in the filter:</p>
<pre class="brush: ruby">
def test_render_error
  @controller.logged_on = false
  get :index
  assert_response :error
end
</pre>
<p>In this case the code under unless will execute and FooController#index will not be called. The returned status code will correspond to :error.</p>
<h3>Running the test</h3>
<p>To run the test, type the following from command line and you should get no errors:</p>
<pre class="brush: sh">
janos@janos-laptop:/work/myapp$ rake test:functionals TEST=test/functional/application_controller_test.rb
(in /work/myapp)
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
..
Finished in 0.23019 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
</pre>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/03/how-to-write-a-test-for-a-rails-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More on vSafe</title>
		<link>http://janosmucsi.thruhere.net/2010/02/more-on-vsafe/</link>
		<comments>http://janosmucsi.thruhere.net/2010/02/more-on-vsafe/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 01:11:15 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=229</guid>
		<description><![CDATA[Finally they put up a video&#8230;
&#8230;and a test test drive
]]></description>
			<content:encoded><![CDATA[<p>Finally they put up a <a href="https://www.wellsfargo.com/wfonline/wellsfargovsafe/education/video">video</a>&#8230;</p>
<p>&#8230;and a test <a href="https://labs.wellsfargo.com/vsafetestdrive/guest/testDrive.do">test drive</a></p>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/02/more-on-vsafe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>myfitv live</title>
		<link>http://janosmucsi.thruhere.net/2010/02/myfitv-live/</link>
		<comments>http://janosmucsi.thruhere.net/2010/02/myfitv-live/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 16:39:43 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=210</guid>
		<description><![CDATA[I am pleased to announce that after six months of development our successful project myfitv.com is live. You can see recommendations:



Find out what&#8217;s on:



&#8230;and of course, you can watch a show, a movie or just a clip:


]]></description>
			<content:encoded><![CDATA[<p>I am pleased to announce that after six months of development our successful project <a href="http://www.myfitv.com">myfitv.com</a> is live. You can see recommendations:</p>
<div></div>
<p><a href="http://janosmucsi.thruhere.net/wp-content/uploads/2010/02/home_page_fitv60.png"><img src="http://janosmucsi.thruhere.net/wp-content/uploads/2010/02/home_page_fitv60.png" alt="" title="home_page_fitv60" width="609" height="569" class="alignnone size-full wp-image-217" /></a></p>
<div></div>
<p>Find out what&#8217;s on:</p>
<div></div>
<p><a href="http://janosmucsi.thruhere.net/wp-content/uploads/2010/02/channel_guide_myfitv60.png"><img src="http://janosmucsi.thruhere.net/wp-content/uploads/2010/02/channel_guide_myfitv60.png" alt="" title="channel_guide_myfitv60" width="609" height="569" class="alignnone size-full wp-image-219" /></a></p>
<div></div>
<p>&#8230;and of course, you can watch a show, a movie or just a clip:</p>
<div></div>
<p><a href="http://janosmucsi.thruhere.net/wp-content/uploads/2010/02/watch_myfitv60.png"><img src="http://janosmucsi.thruhere.net/wp-content/uploads/2010/02/watch_myfitv60.png" alt="" title="watch_myfitv60" width="609" height="569" class="alignnone size-full wp-image-220" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/02/myfitv-live/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple caching in Rails</title>
		<link>http://janosmucsi.thruhere.net/2010/02/simple-caching-in-rails/</link>
		<comments>http://janosmucsi.thruhere.net/2010/02/simple-caching-in-rails/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 04:12:12 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=197</guid>
		<description><![CDATA[We have seen the documentation on Rails caching. So the real question about caching, then, is where to put the code? Caching is a crosscutting concern, it can occur anywhere in the code, because anything can be cached. It does not go well with other modules. Caching code stays separate. 
Almost all calls end up [...]]]></description>
			<content:encoded><![CDATA[<p>We have seen the documentation on Rails caching. So the real question about caching, then, is where to put the code? Caching is a crosscutting concern, it can occur anywhere in the code, because anything can be cached. It does not go well with other modules. Caching code stays separate. </p>
<p>Almost all calls end up in ActiveRecord::Base.  There is also first, last, but let&#8217;s focus on find for now. All queries go through Base.find &#8211; a class method.<br />
We need to alias it so we can do more. The new method will be called find, but not before the old method is renamed to &#8216;old_find&#8217;.</p>
<pre>
alias old_find find
</pre>
<p>Nobody will know about the new find method, and that&#8217;s the right thing to do. When it&#8217;s called, it will check the cache to see whether the requested thing is there. If it&#8217;s there, it will return the value and never call the original cache. If there is a miss, the requested item is not there, the find method will call old_find. If old_find returns a value, it will cache that value.</p>
<pre>
value=Rails.cache.read(key)
 if !value
    value=old_find(*args)
     Rails.cache.write(key, value, :expires_in => 1.hours) if value
end
</pre>
<p>How do I generate the key? If we assume that all queries return different results, then one way to go is to use the method arguments. I will write a method like this:</p>
<pre>
def generate_key(*args)
    key="#{self.name}#{args.join}"
    key
end
</pre>
<p>I made it two lines for illustration so that you can put a breakpoint there and inspect the value in the debugger. I also use the name of the class since it&#8217;s possible to have a Video with id=94 and the Customer with id=94.</p>
<h3>Preloading</h3>
<p>You might be out of luck when referencing a cached object&#8217;s collections. This is because the connection to the database may be closed since the &#8216;parent&#8217; object was cached. So if your videos have actors, for example, you might just want to call actors on your video to force the proxy to load the actors. Since finders either return a single object or multiple objects in an Array, I need to handle that:</p>
<pre>
def preload(result)
   if result.class==Video
      result.actors
      result.images
   elsif result.class==Array
      result.each do |video|
         video.actors
         video.images
     end
   end
end
</pre>
<p>Notice how you can do this for multiple collections.<br />
Let&#8217;s put this all together:</p>
<pre class="brush: ruby">
if ENV[&#039;RAILS_ENV&#039;]==&#039;development&#039;
  RAILS_DEFAULT_LOGGER.debug &quot;environment.rb: Enabling finder interceptor for Videos.&quot;
  class ActiveRecord::Base
     class &lt;&lt; self
        alias old_find find
        def find(*args)
          begin
            #if defined?(@cache_finds) and @cache_finds
            if self.name==&#039;Video&#039;
              key=generate_key(*args)
              value=Rails.cache.read(key)
              if value
                logger.debug &quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Returning &quot;&lt;&lt;key&lt;&lt;&quot; from cache.&quot;
              end
              if !value
                value=old_find(*args)
                preload value
                Rails.cache.write(key, value, :expires_in =&gt; 1.hours) if value
              end
            else
              value=old_find(*args)
            end
            value
          rescue Exception =&gt; e
              logger.error e
          end
        end

        def generate_key(*args)
          key=&quot;#{self.name}#{args.join}&quot;
          key
        end

        def preload(result)
          if result.class==Video
            result.series
            result.images
          elsif result.class==Array
            result.each do |video|
              video.actors
              video.images
            end
          end
        end
      end
  end
end
</pre>
<p>Put this code in an appropriate location in Rails.<br />
The beauty of the solution is that it keeps all other parts of the code clean &#8211; it is non-invasive. You can remove it, without affecting other places in the code. You can use any caching technology such as Redis, if you like. Or just use the MemoryStore that comes with Rails.</p>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/02/simple-caching-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting and stopping mySQL on Snow Leopard</title>
		<link>http://janosmucsi.thruhere.net/2010/02/starting-and-stopping-mysql-on-snow-leopard/</link>
		<comments>http://janosmucsi.thruhere.net/2010/02/starting-and-stopping-mysql-on-snow-leopard/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 02:44:44 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=194</guid>
		<description><![CDATA[If you installed mySQL with ports, do the following:

cd /Library/LaunchDaemons
sudo launchctl unload -w org.macports.mysql5.plist
sudo launchctl load -w org.macports.mysql5.plist

]]></description>
			<content:encoded><![CDATA[<p>If you installed mySQL with ports, do the following:</p>
<pre>
cd /Library/LaunchDaemons
sudo launchctl unload -w org.macports.mysql5.plist
sudo launchctl load -w org.macports.mysql5.plist
</pre>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/02/starting-and-stopping-mysql-on-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax Pagination</title>
		<link>http://janosmucsi.thruhere.net/2010/01/ajax-pagination/</link>
		<comments>http://janosmucsi.thruhere.net/2010/01/ajax-pagination/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 04:56:59 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=160</guid>
		<description><![CDATA[It&#8217;s often necessary to paginate in Ajax applications. For example, let&#8217;s say we have a tool for editors that lets them search for videos. The search is performed in one area and in an other area the user selects the page he wants to populate. Then he can just drag the chosen videos over the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s often necessary to paginate in Ajax applications. For example, let&#8217;s say we have a tool for editors that lets them search for videos. The search is performed in one area and in an other area the user selects the page he wants to populate. Then he can just drag the chosen videos over the selected page. Full-page reload on pagination would be a problem, because the user would have to find the page he wants to edit again. We need to to able to paginate within the search results while leaving the rest of the page intact.</p>
<p>I did not want to implement pagination on the back end myself, so I took advantage of the mislaw-will_paginate gem.<br />
The first task was to capture the pagination links. We render the links into a local variable like this:</p>
<pre>
@videos=Video.paginate :page=>params[:page]
pagination=ActionView::InlineTemplate.new("<%= will_paginate @videos %>", nil).render(@template, {})
</pre>
<p>The code to query the database would typically be more complicated, there would be chained named scopes, for example, and the call to &#8216;paginate&#8217; would come last:</p>
<pre>
@videos=Video.most_popular.paginate :page=>params[:page]
</pre>
<p>We use JSON to communicate between the browser and the server. The video results are in an array generated by ActiveRecord. Let&#8217;s add the pagination string in the first position in the array:</p>
<pre>
@videos.unshift pagination # let's stick the pagination information in the first position
render({ :json => @videos.to_json(..) })
</pre>
<p>This does not disrupt the json encoding, the links stay a string while the rest of the array&#8217;s members get transformed according to the rules given in the to_json method.</p>
<p>On the JavaScript side, assuming that I have a div somewhere that looks like this:</p>
<pre>
&lt;div id="pagination"&gt;
&lt;/div&gt;
</pre>
<p>I can take resolve the pagination information and stick it to the div:</p>
<pre>
function onSearchResultsReady(results) {
  var pagination=results.shift();
  //processing here
  // ..
  $('#pagination').append(pagination);
  ajaxifyLinks();
}
</pre>
<p>The final step is to prevent the browser from reloading the page so that it instead makes an Ajax call when the user clicks on a page link. Notice the call to ajaxifyLinks. That function overrides the links&#8217; default behavior:</p>
<pre>
function ajaxifyLinks() {
    $('#pagination a').click (function(){
        $.getJSON(this.href,
          {},
          onSearchResultsReady);
        return false; // don't follow the link
    });
  }
</pre>
<p>With minimal amount of work, we were able to make the gem do all the heavy-lifting, and even got our ajaxified pagination as well:</p>
<p>
<img src="http://janosmucsi.thruhere.net/wp-content/uploads/2010/01/video_editor.png" alt="video_editor" title="video_editor"/></p>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/01/ajax-pagination/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autotest</title>
		<link>http://janosmucsi.thruhere.net/2010/01/autotest/</link>
		<comments>http://janosmucsi.thruhere.net/2010/01/autotest/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 13:21:15 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=155</guid>
		<description><![CDATA[Autotest is a convenient way to keep your tests passing without having to continuously issue the rake rspec commands.
To use autotest, create a Rails project if you have not done so already:

rails huhu

cd into the new directory and enable RSpec:

./script/generate rspec

Now generate two RSpec-enabled models:

./script/generate rspec_model book title:string
./script/generate rspec_model author full_name:string

Notice how now you have [...]]]></description>
			<content:encoded><![CDATA[<p>Autotest is a convenient way to keep your tests passing without having to continuously issue the rake rspec commands.<br />
To use autotest, create a Rails project if you have not done so already:</p>
<pre>
rails huhu
</pre>
<p>cd into the new directory and enable RSpec:</p>
<pre>
./script/generate rspec
</pre>
<p>Now generate two RSpec-enabled models:</p>
<pre>
./script/generate rspec_model book title:string
./script/generate rspec_model author full_name:string
</pre>
<p>Notice how now you have two tests in spec/models.<br />
Before running the tests, make sure your test database is ready:</p>
<pre>
rake db:migrate RAILS_ENV=test
</pre>
<p>Now from the root directory of the Rails application, execute:</p>
<pre>
./script/autospec
</pre>
<p>You should see all tests passing. Observe that the script does not exit.<br />
Open a second shell and add a test like the following to spec/models/author_spec.rb:</p>
<pre>
it "should be stuff" do
    false.should== true
  end
</pre>
<p>Obviously, you would write a meaningful test but it illustrates the point. As soon as you save the file, you will see autotest execute in the first shell window. Here is the relevant output:</p>
<pre>
'Author should be stuff' FAILED
expected: true,
     got: false (using ==)
./spec/models/author_spec.rb:15:

Finished in 0.087639 seconds

2 examples, 1 failure
</pre>
<p>Now correct the test so that the offending line reads:</p>
<pre>
false.should==false
</pre>
<p>You&#8217;ll see how all the tests are rerun and there are no failures.<br />
Edit the other test file, book_spec.rb, and add a passing test. You will see autotest execute only the tests in book_spec.rb.<br />
However, if you add a failing test to book_spec.rb, then you correct it, you will see that this time autotest first executes the tests in book_spec.rb to check whether they all pass, then it goes on to execute <i>all</i> tests in all spec files.</p>
<p>It takes a little time to get used to the work style autotest offers, but it&#8217;s well worth the peace of mind to know that all your tests are passing.</p>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2010/01/autotest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passenger on Ubuntu</title>
		<link>http://janosmucsi.thruhere.net/2009/11/passenger-on-ubuntu/</link>
		<comments>http://janosmucsi.thruhere.net/2009/11/passenger-on-ubuntu/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 22:57:01 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=24</guid>
		<description><![CDATA[These notes describe how to install Passenger in a typical environment.
First get the gem:

sudo gem install  passenger

Run the installer following its intructions:

sudo apt-get install apache2-prefork-dev
sudo passenger-install-apache2-module

The installer now will build and install the Apache module.
Once, that&#8217;s done, add the snippet given by the installer to /etc/apache2/apache2.conf. For example, it might look like this:

LoadModule passenger_module [...]]]></description>
			<content:encoded><![CDATA[<p>These notes describe how to install Passenger in a typical environment.<br />
First get the gem:</p>
<pre>
sudo gem install  passenger
</pre>
<p>Run the installer following its intructions:</p>
<pre>
sudo apt-get install apache2-prefork-dev
sudo passenger-install-apache2-module
</pre>
<p>The installer now will build and install the Apache module.</p>
<p>Once, that&#8217;s done, add the snippet given by the installer to /etc/apache2/apache2.conf. For example, it might look like this:</p>
<pre>
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.7/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.7
PassengerRuby /usr/bin/ruby1.8
</pre>
<p>Let&#8217;s say we want to make the application available on port 3000. Add the port to /etc/apache2/ports.conf:</p>
<pre>
NameVirtualHost *:3000
Listen 3000
</pre>
<p>Create a file in /etc/apache2/sites-available choosing any name for it. In this example, let&#8217;s call it rails:</p>
<pre>
&lt;VirtualHost *:3000&gt;
  ServerName myserver.thruhere.net:3000
  CustomLog /var/www/ruby/log/access_log common
  ErrorLog /var/www/ruby/log/error_log
  DocumentRoot /somewhere/myrailsapp/public
&lt;/VirtualHost&gt;
</pre>
<p>Notice how the port matches the entry in ports.conf.<br />
Now go to /somewhere/ and type rails myrailsapp. Once the application is generated, restart Apache:</p>
<pre>
sudo /etc/init.d/apache2 restart
</pre>
<p>Now if you go to the url specified in the file rails, you should see the Rails welcome page:</p>
<pre>

http://myserver.thruhere.net:3000
</pre>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2009/11/passenger-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enable mod rewrite in Apache</title>
		<link>http://janosmucsi.thruhere.net/2009/11/enable-mod-rewrite-in-apache/</link>
		<comments>http://janosmucsi.thruhere.net/2009/11/enable-mod-rewrite-in-apache/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 21:59:40 +0000</pubDate>
		<dc:creator>Janos</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://janosmucsi.thruhere.net/?p=6</guid>
		<description><![CDATA[Enable url rewriting like this:

sudo a2enmod rewrite

Then restart Apache:

sudo /etc/init.d/apache2 restart

In the file /etc/apache2/sites-available/default, in the section &#60;Directory /var/www&#62; make sure that &#8216;AllowOverride All&#8217; appears:

&#60;Directory /var/www/&#62;
          Options Indexes FollowSymLinks MultiViews
           AllowOverride All
     [...]]]></description>
			<content:encoded><![CDATA[<p>Enable url rewriting like this:</p>
<pre>
sudo a2enmod rewrite
</pre>
<p>Then restart Apache:</p>
<pre>
sudo /etc/init.d/apache2 restart
</pre>
<p>In the file /etc/apache2/sites-available/default, in the section &lt;Directory /var/www&gt; make sure that &#8216;AllowOverride All&#8217; appears:</p>
<pre>
&lt;Directory /var/www/&gt;
          Options Indexes FollowSymLinks MultiViews
           AllowOverride All
           Order allow,deny
           allow from all
&lt;/Directory&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://janosmucsi.thruhere.net/2009/11/enable-mod-rewrite-in-apache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
