jump to navigation

Run a single cookbook recipe with chef-solo November 25, 2009

Posted by John Dewey in Chef, Code, Example, Rake.
1 comment so far

I generally build my systems with chef-solo cookbooks. I do not want to maintain a central server for recipes. However, I’m getting to the point where I’d like to share my cookbooks from one solo project to another. Anyways, I
wanted to run an individual recipe of a given cookbook. The following gist allows for this. Although, it may not be necessary, since cookbooks are generally idempotent. Seemed like a good idea earlier in the evening, meh.


Changing the default error text in the Rails reCAPTCHA plugin July 15, 2009

Posted by John Dewey in Configuration, Example, Plugins & Gems, Rails.
add a comment

I couldn’t find information on changing the default “Captcha response is incorrect, please try again.” text returned by the reCAPTCHA Rails plugin.

This gist uses Rails 2.2’s internationalization (I18n) to change the message.

Sinatra utf-8 Content-Type before filter April 22, 2009

Posted by John Dewey in Code, Example, Sinatra.
add a comment

I wanted to learn Sinatra, so I started converting my personal Rails site over to Sinatra. I scrape my Twitterholic rating, and redisplay it on my site. I need the css and html to have a Content-Type of utf-8.

I came up with the following before filter. There is probably a better way to do this, I just wanted my controllers to be less “wet”.

I also use the following to render all css.

using geminstaller with vlad the deployer February 28, 2009

Posted by John Dewey in Configuration, Deployment, Example, Rake, Vlad.
1 comment so far

I really really really like vlad the deployer. I recently switched my apps over to geminstaller vs Rails’ config.gem.

This gist adds in geminstaller functionality to vlad’s update task.

icanhasrspec – lolspek sneak peek February 17, 2009

Posted by John Dewey in Example, RSpec.

mah frend reid addd lolspeak 2 rspec. i am bloggin dis as wai 2 persuade him 2 post hims gist. 🙂

updat0rz: teh gist addded – kthxbye

requiring javascript_helper outside rails December 30, 2008

Posted by John Dewey in Code, Example, TestUnit.
add a comment

I wanted to fix the tests for css_browser_selector, when updating it to support a rails 2.2.0 change with concat.

However, I would get the following error.

>> require 'action_view/helpers/javascript_helper'
TypeError: wrong argument type Symbol (expected Proc)

As it turns out you must require ActiveSupport first, since the JavaScriptHelper requires Sym#to_proc.

Also, ActionView::Helpers::JavascriptHelper alias was removed – yay tests!

Am I rspec-tarded? December 29, 2008

Posted by John Dewey in Code, Example, RSpec, Ruby.
1 comment so far

So I want to test the xml declaration and xhtml doctype of my application layout.

I ended up with this pastie.
Does anyone know of a way to accomplish this using rspec‘s have_tag/with_tag? I’m not too fond of the regexp solution in the pastie.

Rails log rotations via rsyslog December 25, 2008

Posted by John Dewey in Code, Configuration, Deployment, Example, Rails.

I have a client that logs their rails logs via syslog, for consumption by pl_analyze. I am not a fan of this, and will probably change it at some point. When using Apache, I’ll typically configure rails to use a rotatelogs pipe.

However, in this case, to avoid changing legacy infrastructure around the logs, I setup rsyslog to do the rotations for me. Hopefully this will be useful to someone else.

This assumes your rails app already logs to syslog.

Added the following to /etc/rsyslog.conf.

selectedIndex is your friend on IE6 November 5, 2008

Posted by reidmix in Code, Example, Javascript.
Tags: , , , , , , ,

While working on getting ChainedSelects to work on IE6, I realized there are multiple ways to set an option as selected.  Since ChainedSelects creates all the options for a pre-existing select, I first used the defaultSelected argument passed in the Option constructor:

option = new Option("name", "value", true);

Here, the third argument specifies whether the option is defaultSelected, when the select is first created or the form is reset. This worked fine in Firefox but IE6 blythely ignored it.  So I tried passing a fourth argument that specifies whether the option is currently selected.

option = new Option("name", "value", true, true);

Again, Firefox does the right thing but IE6 gets wacky and incosistently selects the correct option or sometimes does nothing at all.  It seems as if IE6 misses the “selected” message on the event queue, because it works only when i put some alert()s in there.

So I tried another way, by using the selected attribute after creating the option:

option = new Option("name", "value", true, true);
option.selected = true

Again, inconsistent behavior on IE6.  I went to the great oracle and asked google and tried setting the selected attribute to ‘selected’, ‘true’, and ‘yes’ all with no luck.  I wonder if this occurs because the select is pre-existing and is not newly created.  This led me to try out the selectedIndex attribute on the select element.

select = $('myselect')
select.selectedIndex = 3

Bingo!  This works in both Firefox and IE.  The index is 0-based as you would guess (0 is the first option is select.options).

Subselector, Moneypenny November 1, 2008

Posted by reidmix in ActiveRecord, Code, Database, Example, Monkey Patch, Plugins & Gems, Rails.
Tags: , , , , ,

Building on Josh and Damon‘s idea of Hacking a Subselect in ActiveRecord, I wondered if you could bake this kind of functionality into ActiveRecord.  So Doug and I went digging into the rails code, and came up with a plugin that adds subselects to ActiveRecord which we call Subselector.

So far, it only works on the Hash version of conditions.

On a column you wish to perform a subselect, pass a hash with :in, :not_in, :equals, or :not_equals as the only key.  The value is any of the options you normally would pass to ActiveRecord find.  Notice that we make sure to select a single column with the :select option:

Critic.find(:all, :conditions => { :id => {:in => {:select => :id, :conditions => {:active => true} } } })

Although the example may be contrived, here, we are looking for a critics that are in a set of active critics. The SQL:

select * from critics where id in (select id from critics where active = false)

You can see by default it runs the subselect on the table of outer select.  It gets more interesting you want to run a query on another ActiveRecord model:

Critic.find(:all, :conditions => { :id => {:in => {:model => :rankings, :select => :critic_id, :conditions =>
  {:week => 39} } } })

Here we set :model to :rankings.  Rankings is the ActiveRecord model to perform the find, notice we select the :critic_id column, the SQL is:

select * from critics where id in (select critic_id from rankings where week = 39)

And of course you can always just pass a string as a value to the subselect:

Critic.find(:all, :conditions => { :id => {:not_in => 'select id from critics where active = true' } })

Here’s how subselector can be used with the original example:

Post.find(:all, :conditions => :id => {:in => { :select => :post_id, :conditions => {:blog_id => self.id}, :order => "published_at DESC", :limit => options[:limit] || 10, :offset => options[:offset])} }, :order => "published_at DESC")

UPDATE: Subselector now likes Condition Arrays and Named Bind Variables.

Just pass the hash as a bind variable and specify the type (in/equals) of subselect in the string, make sure to enclose your ‘?’ inside parentheses:

Critic.find(:all, :conditions => ['id in (?)', {:select => :id, :conditions => 'active = true' }])
Critic.find(:all, :conditions => ['id not in (?)', {:select => :id, :conditions => {:active => false} }])
Critic.find(:all, :conditions => ['id in (?)', {:model => :rankings, :select => :critic_id, :conditions => {:week => 39} }])

As you can see, you can format the subselect hash just as above and can specify another model to run the subselect on. If you prefer to use named bind variable hashes, they still work (yay) as you would expect. And you can assign the subselect using them:

Critic.find(:all, :conditions => ['id in (:subselect)', {:subselect => {:select => :id, :conditions => {:active => false} } }])

UPDATE 2: Now with no ActiveRecord breakage

We’ve run the rails ActiveRecord tests without any problems. Let me know if you find any problems.