Tuesday, October 28, 2008

Installing restful_authentication and acts_as_state_machine (aasm) as plugins

Here are the steps I took to install restful_authentication with acts_as_state_machine (using the new aasm gem) as plugins.

Started in the rails home directory of the app.

Installed aasm gem as a plugin:

script/plugin install git://github.com/rubyist/aasm.git

(thanks to Erik on Rails for the above)

Installed restful authentication:
git submodule add \
git://github.com/technoweenie/restful-authentication.git \
vendor/plugins/restful_authentication

I use capistrano so I also added the following to my config/deploy.rb file
set :git_enable_submodules, 1

Created a lib directory in the app home directory:
mkdir lib

Called the restful authentication generator. Fyi, instead of user, I use person as the name of my model for people:
./script/generate authenticated person sessions --include-activation --aasm

Then I did the post-generate steps, following some of the instructions at github. Some of the steps enumerated in the docs seem to be taken care of as part of the generate call now.

In config/routes.rb, I did a find/replace on peoples (incorrect plural) replacing all with people (correct plural) and added the activate route. In the end my routes.rb looked like this:
map.logout '/logout', :controller => 'sessions', :action => 'destroy'
map.login '/login', :controller => 'sessions', :action => 'new'
map.register '/register', :controller => 'people', :action => 'create'
map.signup '/signup', :controller => 'people', :action => 'new'
map.activate '/activate/:activation_code', :controller => 'people',
:action => 'activate', :activation_code => nil
map.resources :people, :member => { :suspend => :put,
:unsuspend => :put,
:purge => :delete }

In config/environment.rb, I added:
config.active_record.observers = :person_observer

So I can use before filters in my controllers, I modified app/controllers/application.rb to include AuthenticatedSystem:
class ApplicationController < ActionController::Base
helper :all
include AuthenticatedSystem
end

I adjusted the language in app/models/person_mailer.rb, especially replacing the YOURSITE references to be the URL of my site:
@body[:url] =
"http://www.landlessness.net/activate/#{person.activation_code}"

Friday, October 10, 2008

Passing optional local variables to a partial

In verbose_index.html.erb i want to pass some additional parameters to the _thing.html.erb partial:
<% @things.each do |thing| %>
<%= render :partial => thing,
:locals => {:some_extra_stuff => some_extra_stuff} %>
<% end %>

in index.html.erb i do not want to pass any additional parameters to the _thing.html.erb partial:
<%= render :partial => things %>

when I hit verbose_index everything will work fine. but when I hit index, I'll see this error:
undefined local variable or method 'some_extra_stuff'
for #

to fix, in _thing.html.erb I add a check to see if the local variable is assigned:
<% unless local_assigns[:some_extra_stuff].nil? %>
<%= some_extra_stuff %>
<% end %>

Could not find any SCM named git

I have been wrestling with capistrano and git, when I ran the following I got an error:

$ cap deploy:check
could not find any SCM named `git'

I ran 'which git' by hand on both my local box and remote box.

on the remote box:
$ which git
/usr/local/bin/git

on the local box:
$ which git
/usr/local/git/bin//git

not sure what the issue was, but I tried to set the following in my deploy.rb without success:

set :local_scm_command, "/usr/local/git/bin/git"
set :scm_command, "/usr/local/bin/git"

ultimately, my fix was to uninstall and reinstall git on my local machine.

now on the local box:
$ which git
/usr/local/bin/git

now
$ cap deploy:check
You appear to have all necessary dependencies installed