Extending acts_as_commentable

by Dean

acts_as_commentable is a nice little ruby on rails plugin. It extends your ActiveRecord classes giving them comments. We are going to use comments on all kinds of things, starting with recipes, of course. However, AAC lacks a critical feature: the ability for users to approve comments before they are displayed. In this post I am going to run through extending AAC using acts_as_state_machine.

The first thing I did (and do to all the plugins we use) was pistonize the plugin so I could hack on it without fear of getting my changes destroyed.

I start off simply here by adding two states to the Comment model: :pending and :approved.

  1. class Comment < ActiveRecord::Base
  2.   # The first element of this array is the initial state
  3.   VALID_STATES = [ :pending, :approved ]
  4.   acts_as_state_machine :initial => VALID_STATES[0]
  5.  
  6.   event :approve do
  7.     transitions :from => :pending, :to => :approved
  8.   end
  9.  
  10.   VALID_STATES.each do |_state|
  11.     # Define _state as a state
  12.     state _state
  13.   end
  14.  
  15.   # More code snipped
  16. end

Now we are going to write some real code, so here comes a little RSpec. aac provides three class methods:

  1. class Comment < ActiveRecord::Base
  2.   class << self
  3.   # Helper class method to lookup all comments assigned
  4.   # to all commentable types for a given user.
  5.   def find_comments_by_user(user)
  6.  
  7.   # Helper class method to look up all comments for
  8.   # commentable class name and commentable id.
  9.   def find_comments_for_commentable(commentable_str, commentable_id)
  10.  
  11.   # Helper class method to look up a commentable object
  12.   # given the commentable class name and id
  13.   def find_commentable(commentable_str, commentable_id)
  14.   end

Since it didn’t come with Test::Unit or RSpec tests I wrote up some test for these methods.

  1. describe Comment, "class methods" do
  2.   fixtures :comments, :recipes, :users
  3.   it "should find comments by user" do
  4.     Comment.find_comments_by_user( comments(:comment_one).user ).should all_belong_to( comments(:comment_one).user )
  5.   end
  6.  
  7.   # This could be more specific
  8.   it "should find comments for a particular class" do
  9.     Comment.find_comments_for_commentable( Comments(:comment_one).commentable_type, comments(:comment_one).commentable_id ).should be_an_instance_of(Array)
  10.   end
  11.  
  12.   it "should find all comments for a particular class" do
  13.     # I happen to know that comment_one is a recipe comment
  14.     Comment.find_commentable( "Recipe", comments(:comment_one).commentable_id ).should be_an_instance_of(Recipe)
  15.   end
  16.  
  17. end

If you are confused by should all_belong_to then you should check out my previous post. With these specs out of the way we can go on to adding more new code.

  1.  it "should find approved comments by user" do
  2.     Comment.find_approved_comments_by_user( comments(:comment_one).user ).should all_be_in_state("approved")
  3.   end
  4.  
  5.   it "should find pending comments by user" do
  6.     Comment.find_pending_comments_by_user( comments(:comment_one).user ).should all_be_in_state("pending")
  7.   end
  8. end

Now, normally you would write one spec at a time, but I think I would bore my readers, so I combined these two. Also take note that I am using another custom RSpec matcher all_be_in_state(). It looks a lot like all_belong_to(), so I leave its implementation as an exercise to the reader (unless I can get another blog post out of it). To get these tests to pass I add a few lines of code:

  1.  VALID_STATES.each do |_state|
  2.     # Define _state as a state
  3.     state _state
  4.  
  5.     # Add Comment.find__comments methods
  6.     ( class << self; self; end ).instance_eval do
  7.       define_method "find_#{_state}_comments_by_user" do |_user|
  8.         find_in_state( :all, _state, :conditions => ["user_id = ?", _user.id], :order => "created_at DESC" )
  9.       end
  10.     end
  11.   end

I am not a method_missing kind of guy, and prefer the dynamic-method metaprogramming style. This lot of code defines class methods at runtime that find Comments in specific states. I am actually using whytheluckystiff’s metaid to hide some of the meta-junk, but I thought I should spell it out here for clarity.

Well, now we have a Comment class with two states and code to limit finds to cmments in a specific state. Right now, that is all I have. Here is the full code for the Comment class and the RSpec. You will see another custom RSpec matcher here, require_a().

  1. class Comment < ActiveRecord::Base
  2.  
  3.   # The first element of this array is the initial state
  4.   VALID_STATES = [ :pending, :approved ]
  5.  
  6.   acts_as_state_machine :initial => VALID_STATES[0]
  7.  
  8.   belongs_to :commentable, :polymorphic => true
  9.   belongs_to :user
  10.  
  11.   event :approve do
  12.     transitions :from => :pending, :to => :approved
  13.   end
  14.  
  15.   validates_associated :user
  16.   validates_presence_of :comment, :commentable_id, :commentable_type, :state,                           :user_id
  17.  
  18.   VALID_STATES.each do |_state|
  19.     # Define _state as a state
  20.     state _state
  21.  
  22.     # Add Comment.find_<state>_comments methods
  23.     meta_def "find_#{_state}_comments_by_user" do |_user|
  24.       find_in_state( :all, _state, :conditions => ["user_id = ?", _user.id],
  25.                      :order => "created_at DESC" )
  26.     end
  27.   end
  28.  
  29.   class < < self
  30.  
  31.     # Helper class method to look up a commentable object
  32.     # given the commentable class name and id
  33.     def find_commentable(commentable_str, commentable_id)
  34.       commentable_str.constantize.find(commentable_id)
  35.     end
  36.  
  37.     # This could be refactored into find_<state>_comments_by_user (somehow)
  38.     def find_comments_by_user(_user)
  39.       find( :all, :conditions => ["user_id = ?", _user.id],
  40.             :order => "created_at DESC" )
  41.     end
  42.  
  43.     # Helper class method to look up all comments for
  44.     # commentable class name and commentable id.
  45.     def find_comments_for_commentable(commentable_str, commentable_id)
  46.       find( :all,
  47.             :conditions => [ "commentable_type = ? and commentable_id = ?",
  48.                              commentable_str, commentable_id ],
  49.             :order => "created_at DESC" )
  50.     end
  51.  
  52.   end
  53.  
  54. end</state>
  1. require File.dirname(__FILE__) + ‘/../../../../spec/spec_helper’
  2.  
  3. module CommentSpecHelper
  4.  
  5. end
  6.  
  7. describe Comment do
  8.  
  9.   fixtures :comments
  10.  
  11.   include CommentSpecHelper
  12.  
  13.   before(:each) do
  14.     @comment = Comment.new
  15.   end
  16.  
  17.   it "should start out in pending state" do
  18.     @comment.state.should == "pending"
  19.   end
  20.  
  21.   it "sould transition to approved" do
  22.     @comment = comments(:pending_comment)
  23.     @comment.approve!
  24.     @comment.state.should == "approved"
  25.   end
  26.  
  27.   it "should require a comment" do
  28.     @comment.should require_a(:comment)
  29.   end
  30.  
  31.   it "should require a commentable_id" do
  32.     @comment.should require_a(:commentable_id)
  33.   end
  34.  
  35.   it "should require a commentable_type" do
  36.     @comment.should require_a(:commentable_type)
  37.   end
  38.  
  39.   it "should require a state" do
  40.     @comment.should require_a(:state)
  41.   end
  42.  
  43.   it "should require a user_id" do
  44.     @comment.should require_a(:user_id)
  45.   end
  46.  
  47. end
  48.  
  49. describe Comment, "class methods" do
  50.  
  51.   fixtures :comments, :recipes, :users
  52.  
  53.   it "should find all comments for a particular class" do
  54.     # I happen to know that comment_one is a recipe comment
  55.     Comment.find_commentable( "Recipe", comments(:comment_one).commentable_id ).should be_an_instance_of(Recipe)
  56.   end
  57.  
  58.   it "should find comments by user" do
  59.     Comment.find_comments_by_user( comments(:comment_one).user ).should all_belong_to( comments(:comment_one).user )
  60.   end
  61.  
  62.   it "should find approved comments by user" do
  63.     Comment.find_approved_comments_by_user( comments(:comment_one).user ).should all_be_in_state("approved")
  64.   end
  65.  
  66.   it "should find pending comments by user" do
  67.     Comment.find_pending_comments_by_user( comments(:comment_one).user ).should all_be_in_state("pending")
  68.   end
  69.  
  70.   # This could be more specific
  71.   it "should find comments for a particular class" do
  72.     Comment.find_comments_for_commentable( comments(:comment_one).commentable_type, comments(:comment_one).commentable_id ).should be_an_instance_of(Array)
  73.   end
  74.  
  75. end

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 8:48 pm

Categories: Dean, code, ruby |

No Comments »

 belong_to RSpec matcher

by Dean

I was extending acts_as_commentable and needed a good RSpec test to check the returned objects from its finder methods belonged to the correct user. For example, Comment.find_comments_by_user( :some_user ) should all belong_to :some_user. I’ll be darned if that doesn’t look like a RSpec description. Since there is no all_belong_to matcher, I wrote one.

  1. module ActiveRecordValidations
  2.   class BelongTo
  3.     def initialize(expected)
  4.       @expected = expected
  5.     end
  6.  
  7.     def matches?(args)
  8.       args.all? do |target|
  9.         @target = target
  10.         @target.send(@expected.class.to_s.downcase) == @expected
  11.       end
  12.     end
  13.  
  14.     def failure_message
  15.       "expected #{@target.inspect} to all belong to #{@expected}"
  16.     end
  17.  
  18.     def negative_failure_message
  19.       "expected #{@target.inspect} not to all belong to #{@expected}"
  20.     end
  21.   end
  22.  
  23.   def belong_to(expected)
  24.     BelongTo.new( [expected] )
  25.   end
  26.  
  27.   def all_belong_to(expected)
  28.     BelongTo.new( expected )
  29.   end
  30. end

The matches? method takes an array of objects and goes through them with all? checking that they have a belongs_to the expected thing. Using the example above, each comment object returned by Comment.find_comments_by_user would get tested if comment.user== @user.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 4:59 pm

Categories: Dean, code, ruby |

No Comments »

 Concise Signup & Signin Pages

by Dean

Login, Signup

We are presented with these quick forms all the time. While it is easy to create standard login and signup pages, Amazon.com has a good one:

Amazon Sigin Image

What makes it good?

First of all, the prompts are written in plain English. Amazon sells to a wide slice of the population, meaning that about 15% of their customers are probably not very technology-saavy. (-2?) Anything they can do to ease the operation helps their customers buy.

Secondly, when a user visits Amazon.com, there is only one link: “Your account” instead of separate login and signup links. Simple is generally better.

Also note the standard “forgot your password” link plus an additional “has your email address changed?” question. Both are useful to have close at hand.

Implementation

Rails 2.0 strongly encourages you to design RESTful applications. Login forms are associated with Session objects, while signup forms go with User objects (rather, Brewer objects in our case). A simple redirect in the SessionsController#create method takes care of pointing a user in the right direction.

  1.  
  2. class SessionsController < ApplicationController
  3.   def create
  4.     if params[:signin_action] == ‘new_user’
  5.       redirect_to new_brewer_path( :brewer => {:email => params[:email]} )
  6.     else
  7.       # Do sigin stuff
  8.     end
  9.   end
  10. end

Note that we pass params[:email] to the new_brewer_path so that field is automatically populated on the next page. If you are using the generated scaffold, you will have to change your BrewersController#new method to instantiate a new @brewer object:

  1.  
  2. class BrewersController < ApplicationController
  3.   def new
  4.     @brewer = Brewer.new(params[:brewer])
  5.   end
  6. end

Lastly, here is the extra test:

  1.  
  2. class SessionsControllerTest < Test::Unit::TestCase
  3.   def test_should_redirect_to_new_brewer_if_asked
  4.     an_email = "dean@brewsession.com"
  5.     post :create, :email => an_email, :signin_action => ‘new_user’
  6.     assert_redirected_to new_brewer_path(:brewer => {:email => an_email} )
  7.     assert_nil session[:brewer_id]
  8.   end
  9. end

In a later post I will talk about how to implement the change password action in a RESTful way.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 10:16 am

Categories: Dean, code, ruby |

No Comments »

 Hey, my first rails plugin

by Dean

Ruby on Rails allows for plugins to extend its functionality. My first attempt at one is nothing special but I am going to blog about it anyway :-D

I needed a way to check if a number is within a range before saving a record to the database. Rails has a number of good validations built in, but not one that does that. In two or three hours, I was able to piece together a simple plugin that adds “validates_range_of” to my AR models. The code is a little ugly until I can get Greg to install a syntax highlighting plugin for this blog.

  1. module ActiveRecord
  2. module Validations
  3. module ClassMethods
  4. def validates_range_of(*attrs)
  5. options = {  :on => :save  }
  6. options.update(attrs.pop) if attrs.last.is_a?(Hash)
  7. attrs.flatten!
  8. unless options[:message]
  9. if options[:maximum] && options[:minimum]
  10. options[:message] = ‘must be betweeen ‘ + options[:minimum].to_s + ‘ and ‘ + options[:maximum].to_s
  11. elsif options[:maximum]
  12. options[:message] = ‘must be less than or equal to ‘ + options[:maximum].to_s
  13. elsif options[:minimum]
  14. options[:message] = ‘must be greater than or equal to ‘ + options[:minimum].to_s
  15. else
  16. raise ArgumentError, ‘Range unspecified.  Specify the :maximum and/or :minimum.’
  17. end
  18. end
  19.  
  20. validates_numericality_of( attrs, options )
  21.  
  22. validates_each(attrs, options) do |record, attr_name, value|
  23. if ((!options[:maximum].nil?) && (!options[:minimum].nil?)) &&
  24. (value > options[:maximum] || value < options[:minimum])
  25. record.errors.add( attr_name, options[:message] )
  26. elsif (!options[:maximum].nil?) && value > options[:maximum]
  27. record.errors.add( attr_name, options[:message] )
  28. elsif (!options[:minimum].nil?) && value < options[:minimum]
  29. record.errors.add( attr_name, options[:message] )
  30. end
  31. end
  32. end
  33. end
  34. end
  35. end

As you can see this plugin extends AR::Validations. So my models can use validates_range_of to check minimum and maximum values. Let’s look at the BJCP Scoresheet I needed it for:

  1. class BjcpScoresheet < ActiveRecord::Base
  2. belongs_to :brew_session
  3. validates_associated :brew_session
  4. validates_range_of :brew_session_id, { :minimum => 1, :message => ‘does not belong to a brew session’ }
  5. validates_range_of :aroma_score, { :minimum => 1, :maximum => 12, :only_integer => true }
  6. validates_range_of :appearance_score, { :minimum => 1, :maximum => 3, :only_integer => true }
  7. validates_range_of :flavor_score, { :minimum => 1, :maximum => 20, :only_integer => true }
  8. validates_range_of :mouthfeel_score, { :minimum => 1, :maximum => 10, :only_integer => true }
  9. validates_range_of :impression_score, { :minimum => 1, :maximum => 10, :only_integer => true }

How nice - reusable user input validation.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 1:46 am

Categories: Dean, code, ruby |

2 Comments »

 Of all the things I miss….

by Dean

Before the death of my drives I had a flexible Measurement class written up. As most of you know brewing involves all kinds of measurement - hop weights, boil volume, bitterness, etc. The class served as a base that more specific classes would inherit and provided the framework for converting between different measurement systems. For example, the SpecificGravity class would inherit Measurement and provide the conversion code to switch between SG and Plato.

The database would save the scalar and units and instantiate an aggregation of these two pieces. When the units changed, back into the database it went with the new values. It was very easy to work with, but took me quite a bit of time to write.

“Why not use one of the libraries already available?” you might ask. The short answer is that they do not fit my needs. Firstly, none of them produced objects that I could stuff in the database as a aggregation - most are intended as great extensions to various number classes. Secondly, and most importantly, none of them dealt with Bitterness, Color or Specific Gravity, which is really why I need a measurement class. Lastly, most of them only handled linear transformations:

m2 = a * m1 + b

To convert from SG to Plato you need to use a cubic polynomial, ugh.

So I wrote my own, then re-wrote it, and it was beautiful. Now I am re-engineering the whole thing. In addition, I’ll have to figure out where I got the Plato to SG reverse conversion. I remember trying to solve the cubic equation, then finding something that actually worked.

It is a little faster-going because I wrote it all before, but I had some tr1cky 31337 code in there that I will have to figure out again.

Always make sure your backups are working and current.

–Dean

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Posted by: Dean @ 9:51 pm

Categories: Dean, code, disaster, ruby |

1 Comment »

Our Sponsors