Abhijit HiremagalurAbhijit Hiremagalur
Standup 01/12/2009: require & class reloading, acts_as_fu
edit Posted by Abhijit Hiremagalur on Monday January 12, 2009 at 11:52PM

Interesting Things

  • Using 'require' explicitly interferes with class reloading in Rails

Frederick Cheung discusses this in more detail here. This might be related to the Selenium + class reloading issues some pivots have experienced in recent weeks. The alternative is to rely on Rails automagic loading or 'require_dependency'.

  • acts_as_fu makes writing database independent tests for models is easier

Props to pivot Pat Nakajima for creating acts_as_fu.

Comments

  1. Pat Nakajima Pat Nakajima on January 13, 2009 at 06:03AM

    Hey, glad you like acts_as_fu. Just so you know, it's not actually database independent. It sets up a sqlite3 database under the covers that requires no configuration. It also allows you to manually specify a different database configuration if you need one.

  2. grosser grosser on January 13, 2009 at 08:55AM

    Setting up such a database by hand is not very problematic, and it keeps your code "real", so any other developer can understand it without needing to dig into acts_as_fu (which is a very descriptive name btw)

    What i would like in a gem is something simpler: setup_ar_memory_db() the rest(migration+class) i would rather do by hand.

    def setup_ar_memory_db
      require 'active_record'
      ActiveRecord::Base.configurations = {"test" => {
        :adapter => "sqlite3",
        :database => ":memory:",
      }.with_indifferent_access}
    
      ActiveRecord::Base.logger = Logger.new("/dev/null")
      ActiveRecord::Base.establish_connection(:test)
    end
    
    setup_ar_memory_db
    
    ActiveRecord::Schema.define(:version => 1) do
      create_table "feeds" do |t|
        t.string "feed_url"
        t.timestamp "feed_updated_at"
        t.text "feed_data"
        t.timestamps
      end
    end
    
    
    class Feed < ActiveRecord::Base
      acts_as_feed
    end
    
  3. grosser grosser on January 13, 2009 at 09:20AM

    ok done: http://github.com/grosser/testing_database

    sudo gem install grosser-testing_database
    
    require 'testing_database'
    TestingDatabase.setup
    ActiveRecord::Schema.define(:version => 1) do
      create_table :users do |t|
        t.string :name
      end
    end
    class User < ActiveRecord::Base
    end
    User.create!(:name=>'EZ')
    
  4. Pat Nakajima Pat Nakajima on January 13, 2009 at 06:45PM

    Hey grosser,

    You're right, it's definitely easy to set this stuff up manually, but after doing it enough times, I wanted a way to only setup the things I care about, and not worry about anything else. That leaves us with this:

    build_model(:users) do
      string :name
    end
    

    Also, acts_as_fu automatically sets and unsets the class constant, meaning you have a pristine version of your ActiveRecord class every time you call build_model. That's pretty nice to have when you're manipulating the class in your tests.

    It's certainly concise at the risk of being ambiguous, but it's worked quite well for me in a wide variety of circumstances.

    Finally, about the name: Many ActiveRecord plugins have names like "acts_as" or "-fu", and my project is aimed at making those plugins easier to test. Therefore, it can increase your "acts_as"-fu, or if you prefer, it can act as "fu".

    Also, I just can't resist a smarmy project name.

  5. grosser grosser on January 15, 2009 at 09:06AM

    -- automatically sets and unsets the class constant sounds good!

    how about a public method in acts_as_fu that does "setup_ar_memory_db" so everybody can be happy.

    --> new project name: instant_database :D

  6. grosser grosser on January 15, 2009 at 09:09AM

    oh, that would be ActsAsFu.connect! silly me, did not read the docs :) ill give it a try then!