public
Description: Life On The Edge With Merb, DataMapper & RSpec
Homepage: http://blog.new-bamboo.co.uk
Clone URL: git://github.com/deimos1986/book_mdar.git
stories
deimos1986 (author)
Sun Jun 15 13:32:03 -0700 2008
commit  ad57e97f5ed4be404e6d4eb3a3d2b1d5cd422389
tree    5a647046ff5aee898faed714fc45a1d99d8dde12
parent  90e73767c36abdb0d3c70a58e14df5f62bfe184d
...
10
11
12
 
 
 
 
 
13
14
15
...
10
11
12
13
14
15
16
17
18
19
20
0
@@ -10,6 +10,11 @@ With Merb, DataMapper & RSpec.
0
 * `webrat` - git clone git://github.com/gwynm/webrat.git
0
 
0
 
0
+### For stories
0
+MERB_ENV=test rake story[all]
0
+MERB_ENV=test rake dm:db:automigrate
0
+
0
+
0
 ## License
0
 
0
 Dual licensed under the MIT and GPL licenses:
...
1
2
3
 
 
 
 
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
0
@@ -1,5 +1,30 @@
0
 class Post
0
   include DataMapper::Resource
0
 
0
+ property :id, Integer, :serial => true
0
+ property :title, String, :length => 0..255, :auto_validation => false
0
+ property :body, Text
0
+ property :original_uri, String, :length => 0..255, :auto_validation => false
0
+ property :created_at, DateTime
0
+ property :can_be_displayed, Boolean, :default => false
0
   
0
+ # user creation
0
+ validates_present :title, :when => [:default, :display]
0
+ validates_present :body, :when => [:default, :display, :import]
0
+
0
+ # automated import
0
+ validates_length :original_uri, :in => 0..255, :when => :import
0
+
0
+ # a callback to set can_be_displayed appropriately (more on these later)
0
+ before :save do
0
+ self.can_be_displayed = true if self.valid? :display
0
+ end
0
+
0
+ # before save call the instance method make_permalink
0
+ before :save, :make_permalink
0
+
0
+ def make_permalink
0
+ # self.title = PermalinkFu.permalink(self.title)
0
+ end
0
+
0
 end
...
4
5
6
7
 
8
9
10
...
4
5
6
 
7
8
9
10
0
@@ -4,7 +4,7 @@
0
 
0
 <%= error_messages_for :post %>
0
 
0
-<% form_for(@post, :action => url(:posts) ) do |f| %>
0
+<% form_for(@post, :action => url(:posts)) do %>
0
   <p>
0
     <%= submit_button "Create" %>
0
   </p>
...
52
53
54
 
55
56
57
...
52
53
54
55
56
57
58
0
@@ -52,6 +52,7 @@ Gem.path.unshift(Merb.root / "gems")
0
 # another part of your configuration relies on libraries specified
0
 # here.
0
 dependencies "merb-more","merb_helpers"
0
+dependencies "dm-validations"
0
 # dependencies "RedCloth", "merb_helpers"
0
 # OR
0
 # dependency "RedCloth", "> 3.0"
...
9
10
11
 
 
 
 
 
 
 
 
12
...
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -9,3 +9,11 @@ Spec::Runner.configure do |config|
0
   config.include(Merb::Test::RouteHelper)
0
   config.include(Merb::Test::ControllerHelper)
0
 end
0
+
0
+def valid_post_attrs
0
+ {
0
+ :title => "title, " + String.random_alphanumeric,
0
+ :body => "body, " + String.random_alphanumeric,
0
+ :user_id => rand(9)
0
+ }
0
+end
0
\ No newline at end of file
...
15
16
17
 
 
 
 
18
19
20
...
15
16
17
18
19
20
21
22
23
24
0
@@ -15,6 +15,10 @@ class String
0
   def has_text?(t)
0
     include?(t)
0
   end
0
+
0
+ def self.random_alphanumeric(size=16)
0
+ (1..size).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join
0
+ end
0
 end
0
 
0
 Dir['stories/steps/**/*.rb'].each do |steps_file|
...
5
6
7
8
 
 
 
 
9
10
11
12
...
5
6
7
 
8
9
10
11
12
13
14
15
0
@@ -5,7 +5,10 @@ steps_for(:posts) do
0
   end
0
   
0
   Given "there are posts" do
0
- Post.create()
0
+ 2.times do
0
+ post = Post.new(valid_post_attrs)
0
+ post.save
0
+ end
0
   end
0
   
0
 end
0
\ No newline at end of file
...
3
4
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
8
...
3
4
5
 
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
@@ -3,4 +3,15 @@ Story: Posts
0
   Scenario: View a lists of posts
0
     Given there are posts
0
     When I visit /
0
- Then I should see a list of posts
0
\ No newline at end of file
0
+ Then I should see a list of posts
0
+
0
+ Scenario: Adding a new post
0
+ Given I am an admin
0
+ When I visit /posts/new
0
+ Then I should see the text New Post
0
+ When I enter "My first post" in title
0
+ And I enter "This is the body" in body
0
+ When I click the button publish
0
+ Then I should see the newly created post
0
+
0
+
0
\ No newline at end of file

Comments

    No one has commented yet.