Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create link hash in after_create callback.

  • Loading branch information...
commit d1f44b2c8348269f2e7827a832a8e3b3af4cff8a 1 parent afcf384
@gabebw authored
View
3  Gemfile
@@ -3,7 +3,8 @@ source :rubygems
gem 'sinatra', '~> 1.3.2'
gem 'redis', '~> 2.2.2'
gem 'rake', '~> 0.9.2'
-gem 'ohm', '~> 0.1.5'
+gem 'ohm', '~> 1.0.0.rc1'
+gem 'ohm-contrib', '~> 1.0.0.rc2'
group :test do
gem 'rack-test'
View
9 Gemfile.lock
@@ -11,8 +11,11 @@ GEM
multi_json (1.2.0)
nest (1.1.1)
redis
- ohm (0.1.5)
+ ohm (1.0.0.rc1)
nest (~> 1.0)
+ scrivener (~> 0.0.3)
+ ohm-contrib (1.0.0.rc2)
+ ohm (= 1.0.0.rc1)
rack (1.4.1)
rack-protection (1.2.0)
rack
@@ -28,6 +31,7 @@ GEM
rspec-expectations (2.9.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
+ scrivener (0.0.3)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
@@ -39,7 +43,8 @@ PLATFORMS
DEPENDENCIES
factory_girl (~> 3.1.0)
- ohm (~> 0.1.5)
+ ohm (~> 1.0.0.rc1)
+ ohm-contrib (~> 1.0.0.rc2)
rack-test
rake (~> 0.9.2)
redis (~> 2.2.2)
View
17 app/models/link.rb
@@ -1,11 +1,26 @@
+require_relative '../../lib/encoder'
+
class Link < Ohm::Model
+ include Ohm::Callbacks
+
attribute :url
attribute :hash
+ index :url
index :hash
def validate
assert_present :url
- assert_present :hash
+ end
+
+ protected
+
+ def after_create
+ set_hash
+ end
+
+ def set_hash
+ self.hash = Encoder.new(self.id.to_i).encode
+ save
end
end
View
1  lib/ohm_setup.rb
@@ -1,3 +1,4 @@
require 'ohm'
+require 'ohm/contrib'
# Ohm.connect
View
3  spec/factories.rb
@@ -2,9 +2,8 @@
factory :link do
ignore do
url 'http://example.com'
- sequence(:hash) { |n| "hash#{n}" }
end
- initialize_with { Link.create(url: url, hash: hash) }
+ initialize_with { Link.new(url: url) }
end
end
View
32 spec/models/link_spec.rb
@@ -3,13 +3,35 @@
describe Link do
context 'validations' do
it 'requires url to be present' do
- link = Link.create(url: nil)
- link.errors.assoc(:url)[1].should == :not_present
+ link = Link.new(url: nil)
+ link.valid?
+ link.errors[:url].should include :not_present
+ end
+ end
+
+ context 'callbacks' do
+ it 'sets hash after create' do
+ link = FactoryGirl.create(:link)
+ link.hash.should == 'b'
+ end
+
+ it 'persists hash after create' do
+ link = FactoryGirl.create(:link)
+ Link.find(hash: link.hash).should_not be_empty
+ end
+ end
+
+ context 'indexes' do
+ it 'has an index on url' do
+ lambda do
+ Link.find(url: 'whatever')
+ end.should_not raise_error(Ohm::IndexNotFound)
end
- it 'requires hash to be present' do
- link = Link.create(hash: nil)
- link.errors.assoc(:hash)[1].should == :not_present
+ it 'has an index on hash' do
+ lambda do
+ Link.find(hash: 'whatever')
+ end.should_not raise_error(Ohm::IndexNotFound)
end
end
end

0 comments on commit d1f44b2

Please sign in to comment.
Something went wrong with that request. Please try again.