Tuesday, March 12, 2013

Create EventBase

This post is one in a series on how I achieved composite classes and multi-table inheritance. For background, see Composite classes and multi-table inheritance.

For proper attribution, I'll note that a whole lot of the code in this post came directly from or was inspired by Multiple Table Inheritance with ActiveRecord.

In this post, I'm going to describe how I created the EventBase model.

Create the EventBase model

In the command window, I typed:
$ rails g model EventBase

Which output something like:
      invoke  active_record
      create    db/migrate/20130312011851_create_event_bases.rb
      create    app/models/event_base.rb
      invoke    rspec
      create      spec/models/event_base_spec.rb
      invoke      factory_girl
      create        spec/factories/event_bases.rb

Edit and Run the Migration to Create Database Tables for EventBase

I then opened the EventBase migration and edited it:
class CreateEventBases < ActiveRecord::Migration
  def change
    create_table :event_bases do |t|
      t.string      :summary
      t.string      :description
      t.datetime    :start_datetime
      t.datetime    :end_datetime
      t.integer     :recur_rule_id
      t.integer     :eventable_id
      t.string      :eventable_type
      t.timestamps
    end
  end
end 

It's probably worth noting :eventable_id and :eventable_type. Eventually, I am going to create a polymorphic association between EventBase and the classes that I want to inherit from EventBase. These two column will allow specific instances of EventBase to be associated with any other Class (in my case, Event and Trip).

Back at the command line, I ran the migration:
$ bundle exec rake db:migrate

Which gave me something like:
==  CreateEventBases: migrating ===============================================
-- create_table(:event_bases)
NOTICE:  CREATE TABLE will create implicit sequence "event_bases_id_seq" for serial column "event_bases.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "event_bases_pkey" for table "event_bases"
   -> 0.0075s
==  CreateEventBases: migrated (0.0077s) ====================================== 

Then I opened event_base.rb and edited it:
class EventBase < ActiveRecord::Base
  
  # ==========================================================================================
  #  Associations
  # ==========================================================================================  
    belongs_to  :eventable, polymorphic: true, dependent: :destroy
                
end

'Eventable' is going to be any class that can act as an event.

At this point, I should have a basic EventBase model working. So, I wrote a simple test:

require 'spec_helper'

describe EventBase do
  describe "respond_to" do
    it "  - new" do
      @eb = EventBase.new
      @eb.respond_to?("description").should  be_true
      @eb.description = "test description"
      @eb.save.should be_true
      @eb.description.should == "test description"
    end
  end
end 

In my spec/models directory, I ran:
$ rspec event_base_spec.rb --format documentation

And I got:
EventBase
  respond_to
    - new

Finished in 0.12013 seconds
1 example, 0 failures

Super! It works. Next up: First Cut of the ActsAsEvent module.

No comments:

Post a Comment