Created
April 26, 2012 13:57
-
-
Save jdickey/2499810 to your computer and use it in GitHub Desktop.
Simplest possible use of Enumerize. Trying to track down my bug in Gist 2497174.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'rails', '3.2.3' # change as required | |
require 'active_record' | |
require 'enumerize' | |
require 'awesome_print' | |
# Print out what version we're running | |
puts "Active Record #{ActiveRecord::VERSION::STRING}" | |
# Connect to an in-memory sqlite3 database (more on this in a moment) | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'sqlite3', | |
:database => ':memory:' | |
) | |
ActiveRecord::Schema.define do | |
create_table :users, :force => true do |t| | |
t.string :name | |
end | |
create_table :articles, :force => true do |t| | |
t.integer :author_id | |
t.string :name | |
t.string :status | |
end | |
end | |
class User < ActiveRecord::Base | |
attr_accessible :name | |
has_many :articles | |
end | |
class Article < ActiveRecord::Base | |
include Enumerize | |
# attr_accessor :status | |
# validates_presence_of :name, :status | |
belongs_to :author, :class_name => 'User' | |
# ##### KEY LINE BELOW ##### # | |
enumerize :status, :in => [:new, :draft, :private, :published] #, :default => :new | |
# ##### KEY LINE ABOVE ##### # | |
end | |
author = User.create! :name => 'The Author' | |
puts "Author valid? " + author.valid?.to_s | |
article = Article.new :name => 'An Article', :status => :new | |
article.author = author | |
article.status = :published | |
puts "Article valid? " + article.valid?.to_s | |
awesome_print article | |
awesome_print article.status.published? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment