Post Tagged rails

has_many has many features

Wednesday, 04 April 2009

Not only is `has_many` an excellent way to set up foreign keys ( Person has_many :fingers where fingers represents an Active Record class Finger) but has_many can also define custom relationships. Behold,

class Book < ActiveRecord::Base
  has_many :listings
  has_many :users, :through => :listings
  has_many :active_listings, :class_name => "Listing", :conditions => { :market_status => 1 }
end

.

This way whenever I want to grab all of the active listings that a book has, i can do this:

  @book.active_listings.count

That is all.


Dear Bookstore, screw you. kthxby.

Saturday, 04 April 2009

So on Tuesday of last week I revived an old idea.  I  wanted to create a web app that helps college students trade their textbooks.

Let me explain this idea with an example.

As a student. I want to save as much money as possible. I can do this by not buying my books from the university bookstore. But if I do not buy my books from the bookstore, how then shall i get the required reading material? My solution is to BookUp! So, lets say that I just finished Calculus III and I no longer need my 50lb Calculus book. I go to BookUp, find my Calculus book on the list and then I will create a listing associated with the book. This listing will show the world that I no longer need this book and I wish to give it to a student who does. I could sell it for cash or cashews, it does not matter because BookUp does not assume that much. BookUp is more of a tool to connect students and less of an commerce app. So basically this is a Craig’s List for college textbooks.

Let me know what you internet people think about this idea. . . . And now, in case you missed the link

BookUp


skinny controllers in Rails

Thursday, 03 March 2009

Your controller is so fat I took a screenshot of it last Christmas and it is still printing. We have all heard stories about how we should keep our controllers slim and fit but there can never be a definitive answer on how to accomplish the athletic pose. For you see, there are an infinite amount of ways that your controller can take on husk. In this blog-post I will attempt to define and solve one of these problems.

Let us suppose that you have a User model and a Job model.

class User < ActiveRecord::Base
  has_many :jobs
end
 
class Job < ActiveRecord::Base
  belongs_to :user
end

Ok, so this proverbial Job is a very complex model. It has many attributes that are activated by complex algorithms. The job’s algorithms are influenced by input from the user.

class Job < ActiveRecord::Base
  belongs_to :user
 
  def algorithm!( input )
    case input
    when :variation_one
      # do a bunch of crap
    when :variation_two
      # do even more crap
    end# case
  end# algorithm
 
end# Job

Now that we have this model with extremely complicated algorithms that depends on loads of user input; we need to find a RESTful way to call these methods. Please keep in mind that these methods require the users input which will come from the params hash. Since our app is RESTful, we will be ‘updating’ the object by sending a PUT request with data that will be received by the Jobs controller.

Our edit.html.erb should have something like this in it:

 
<%= f.check_box :important_input_that_is_not_apart_of_the_model %>

This data’s purpose is to instruct the algorithm on how to execute. However, it is not apart of the model. There is NO column that corresponds to this data. So the question now becomes: How do i get this data from the view/controller (params hash) to the model?

The way I attack this problem is by adding this to my model:

 
class Job < ActiveRecord::Base
  belongs_to :user
  attr_accessor :important_input_that_is_not_apart_of_the_model
 
  def before_save
    self.algoritm!( self. important_input_that_is_not_apart_of_the_model )
  end
 
  def algorithm!( input )
 
    case input
    when :variation_one
      # do a bunch of crap
    when :variation_two
      # do even more crap
    end
  end# algorithm
end# Job

In this case we have added NO extra code to our update method in the Jobs controller. However we have added heaps of functionality.
Let me know if there is better way to do this ?


Testing Authentication in Controllers with RSpec

Sunday, 01 January 2009

Writing test code is hard. I becomes much harder when you are incorporating code you found on github into the core of your application. (ie restful authentication)

Fortunately the creators of this library have included a module file in the lib directory called AuthenticatedTestHelper. So in your spec file just add this line:

include AuthenticatedTestHelper

Now, you can now use these methods in your specs. For my current project i had some authentication and authorization. So, I added a method that would give me
a specific type of user.

  def give_me_the( what )
    current_user = mock_model User
    controller.stub!(:current_user).and_return(current_user)
    case what
      when :owner
        current_user.stub!(:has_role?).with('owner').and_return(true)
      when :subscriber
        current_user.stub!(:has_role?).with('subscriber').and_return(true)
    end
    current_user
  end

Whenever i need a user i just do this:

  @user = give_me_the :owner

and now i have a user who is an owner.


Removing Duplicates

Friday, 01 January 2009

Today i was checking in on one of my applications and i had noticed that i forgot to check for unique user entries. This is bad design on my part for several reasons.

  1. When a user submits a form, the submit button should reflect that the request was sent. This can be accomplished by “greying” out the button or by using some AJAZZ progress indication.
  2. It is not that hard to add
validates_uniqueness_of :whatever

nevertheless, I found myself in a position to delete a gaggle of duplicates. Now, the list was certainly manageable but i prefer to let the machines do my work. I sshd’ myself on over to my server and fired up a script/console production. And this is what i did:

#warning! this script can get you fired. 
 
a = People.find(:all)
b = Hash[ *a.map{ |x| [ x[:social_security_number], x] }.flatten ].values
c = a - b
c.each { |x| x.destroy }

What this script does

#1 => grabs all of the people from the database
#2 => loads up all of the people who have the same social
#3 => pull out the duplicates
#4 => makes ActiveRecord calls to remove duplicates from database


Filtering an array with Ruby

Tuesday, 11 November 2008

Ruby madness! I found a most excellent method for filtering data in an Array. To be a bit more specific, I wanted to put a form on my index view to allow a user to filter the data that was returned to them. So, in my case I would have an index to display applicants. These applicants will eventually be hired or denied. Once these applicants have been hired or denied I will need a way to filter these results out of the index view.

Every block of stone has a statue inside it and it is the task of the sculptor to discover it.
- Michelangelo

… and now, the code to release the beauty of our expressive language…

class Array
 
  def matching_members( &some_block )
      find_all( &some_block )
  end
 
  def matching_compound_members( procs_array )
    	procs_array.map do |some_proc|
      	matching_members( &some_proc )
    	end.inject( self ) do |memo,matches|
        memo & matches
    	end
  end
 
end

Now that we have built onto our Array class. It is time to test our new methods with respect to our problem at hand. (filtering applicants from the database) So, because I am an agile developer who is testing like the day after Challenger, I will now write a quick description of what these methods
should do.

  # set up three users to test the filer method
  # applicant1 is hired
  # applicant2 is not hired
  # applicant3 is not hired and has been denied
  @applicants = [applicant1,applicant2,applicant3]
 
  it "should remove applicants that are hired" do
      results = Array.new
      lambdas = Hash.new
      var = "hired"
      lambdas[var] = lambda { |i| i unless i.send(var)  }
      results = @applicants.matching_compound_members(lambdas.values)
      results.include?(@applicant1).should eql( false )
  end

Notice that I am using var to hold a string and then sending it as a message to i. I am testing this way because I plan on taking the input from an HTTP request and then filtering the array based on the contents of params.

Credits: Ruby By Example. This is where i got the filtering code. This is an excellent book!
ruby_cov.jpg