Created
April 6, 2011 21:41
-
-
Save Porta/906597 to your computer and use it in GitHub Desktop.
This file contains 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
class Event < Ohm::Model | |
KINDS = %w[creation approval disapproval reception delivery] | |
reference :user, lambda { |id| User.find(id)} | |
reference :item, lambda { |id| id.nil? ? nil : Item.find(id)} | |
reference :transaction, lambda { |id| id.nil? ? nil : Transaction.find(id)} | |
attribute :kind | |
attribute :text | |
attribute :created_at | |
index :user | |
index :item | |
index :transaction | |
def validate | |
assert_present :kind | |
assert_present :text | |
#KINDS = %w[creation approval disapproval reception delivery] | |
assert KINDS.include?(self.kind), [:kind, :not_valid] | |
#cheap before_create hook | |
self.created_at = Time.now | |
end | |
end |
This file contains 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
require 'test_helper' | |
class EventTest < ActiveSupport::TestCase | |
# Replace this with your real tests. | |
test "a event should have a text" do | |
e = Event.new(:kind => "creation") | |
assert_false e.valid? | |
end | |
test "a event should have a kind" do | |
e = Event.new(:text => "Hola, manola") | |
assert_false e.valid? | |
end | |
test "a event should have a kind and text" do | |
e = Event.new(:text => "Hola, manola", :kind => "creation") | |
assert e.valid? | |
end | |
test "a event kind should be within defined list" do | |
Event::KINDS.each do |kind| | |
e = Event.new(:text => "Hola, manola", :kind => kind) | |
assert e.valid? | |
end | |
end | |
test "a event kind should not be different than the ones defined in list" do | |
wrong_kind = "pinga" | |
assert_false Event::KINDS.include? wrong_kind | |
e = Event.new(:text => "Hola, manola", :kind => wrong_kind) | |
assert_false e.valid? | |
assert_equal e.errors, [[:kind, :not_valid]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment