Permalink
Browse files

Update for current Cucumber

  • Loading branch information...
1 parent 126619b commit f9804d6b6df6b19658f9dbb5b0100bc28fd5a3cc @fidothe committed Feb 23, 2012
View
@@ -1,4 +1,5 @@
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
require 'url_keyed_object/active_record'
+require 'active_record'
-require 'spec/expectations'
+require 'rspec/expectations'
View
@@ -1,15 +1,19 @@
require 'fileutils'
Before("@db") do |scenario|
- require 'activerecord'
+ require 'active_record'
+ require 'url_keyed_object/active_record'
+
@db_dir = ::Dir.mktmpdir
begin
- # Create the SQLite database
+ # Create the SQLite database, pretend the Railtie has run...
+ @recording_logger = RecordingLogger.new($stderr)
+
+ ActiveRecord::Base.extend UrlKeyedObject::ActiveRecord
ActiveRecord::Base.establish_connection({'adapter' => 'sqlite3',
'database' => "#{@db_dir}/feature.sqlite3", 'pool' => 5, 'timeout' => 5000
})
ActiveRecord::Base.connection
- @recording_logger = RecordingLogger.new($stderr)
ActiveRecord::Base.logger = @recording_logger
rescue
$stderr.puts $!, *($!.backtrace)
@@ -21,4 +25,4 @@
ActiveRecord::Base.connection.disconnect!
FileUtils.remove_entry_secure @db_dir
@recording_logger.reset_recorder!
-end
+end
@@ -4,25 +4,25 @@ class RecordingLogger < Logger
def recorded_messages
@recorded_messages ||= {}
end
-
+
alias_method :add_original, :add
def add(severity, message = nil, progname = nil, &block)
record_it(severity, message)
super
end
-
-
+
+
def reset_recorder!
@recorded_messages = {}
end
-
+
def has_message_for_severity?(severity)
(!recorded_messages[severity].nil? && !recorded_messages[severity].empty?)
end
-
+
private
-
+
def record_it(severity, message)
(recorded_messages[severity] ||= []) << message
end
-end
+end
@@ -1,52 +1,51 @@
@db
Feature: Using with ActiveRecord
- In order to generate and validate URL keys in an ActiveRecord::Base object
+ In order to generate and validate URL keys in an ActiveRecord::Base model in a Rails project
A Rails developer
Wants to include and use UrlKeyedObject
-
+
# The most basic use case is for a 5-character ID, stored in the url_key column.
- #
- # Simply including the UrlKeyedObject::ActiveRecord module is enough.
+ #
+ # Simply extended the UrlKeyedObject::ActiveRecord module and calling has_url_key is enough.
# This makes the url_key attribute protected from mass assignment and read-only.
Background:
Given a database, with this table defined:
"""
create_table :things do |t|
t.string :url_key, :null => false
-
+
t.timestamps
end
"""
And this class:
"""
class Thing < ActiveRecord::Base
- acts_as_url_keyed
+ has_url_key
end
"""
-
+
Scenario: An unsaved model object with UrlKeyedObject included
When I make a bare instance:
"""
@instance = Thing.new
"""
Then @instance.url_key should be nil
-
+
Scenario: A saved model object with UrlKeyedObject included
When I make and save an instance:
"""
@instance = Thing.new
@instance.save!
"""
Then @instance.url_key should match /^[a-z0-9]{5}$/
-
+
Scenario: Attempting to mass-assign url_key ought to fail
When I make an instance using mass-assignment:
"""
@instance = Thing.new(:url_key => 'abcde')
"""
Then @instance.url_key should be nil
- And a warning should have been logged
-
+
Scenario: Attempting to set url_key ought to fail
When I make an instance and set the value of url_key manually:
"""
@@ -1,52 +1,51 @@
@db
Feature: More advanced usage with ActiveRecord
- In order to generate and validate non-standard URL keys in an ActiveRecord::Base object
+ In order to generate and validate non-standard URL keys in an ActiveRecord::Base model in a Rails project
A Rails developer
Wants to include and use UrlKeyedObject
-
+
# The default case is for a 5-character ID, stored in the url_key column.
# Here we have an existing column we want to re-use, and we want a longer URL key
- #
+ #
# calling acts_as_url_keyed and setting a couple of options is all that's needed.
Background:
Given a database, with this table defined:
"""
create_table :advanced_things do |t|
t.string :opaque_id, :null => false
-
+
t.timestamps
end
"""
And this class:
"""
class AdvancedThing < ActiveRecord::Base
- acts_as_url_keyed :url_key_column => :opaque_id, :url_key_length => 8
+ has_url_key :column => :opaque_id, :length => 8
end
"""
-
+
Scenario: An unsaved model object with UrlKeyedObject included
When I make a bare instance:
"""
@instance = AdvancedThing.new
"""
Then @instance.opaque_id should be nil
-
+
Scenario: A saved model object with an 8-character URL key on #opaque_id
When I make and save an instance:
"""
@instance = AdvancedThing.new
@instance.save!
"""
Then @instance.opaque_id should match /^[a-z0-9]{8}$/
-
+
Scenario: Attempting to mass-assign opaque_id ought to fail
When I make an instance using mass-assignment:
"""
@instance = AdvancedThing.new(:opaque_id => 'abcde')
"""
Then @instance.opaque_id should be nil
- And a warning should have been logged
-
+
Scenario: Attempting to set opaque_id ought to fail
When I make an instance and set the value of opaque_id manually:
"""

0 comments on commit f9804d6

Please sign in to comment.