seed 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ Seed
2
+ ====
3
+
4
+ Another seeding library for Ruby
5
+
6
+ Usage
7
+ -----
8
+
9
+ Planting a seed
10
+ ---------------
11
+
12
+ Seed supports two syntaxes:
13
+
14
+ User.seed(:bob) do |user|
15
+ user.name = 'Bob'
16
+ end
17
+
18
+ or:
19
+
20
+ User.seed(:bob, :name => 'Bob')
21
+
22
+ Attempting to overwrite an existing seed will result in an error.
23
+
24
+ Retrieving a seed
25
+ -----------------
26
+
27
+ User.seed(:bob)
28
+
29
+ Attempting to retrieve a non-existent seed will result in an error.
30
+
31
+ Rake Task
32
+ ---------
33
+
34
+ Seed comes with a rake task to make creating multiple "seeds" easier. In your Rakefile, add the following:
35
+
36
+ require 'seed/task'
37
+
38
+ Create a db/seeds directory and add a named seed, such as development.rb.
39
+
40
+ Running rake db:seed:development will then load your seeds from db/seeds/development.rb.
data/lib/seed.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  class Seed
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.3'
3
+ @seeds = {}
3
4
 
4
- def self.plant(klass, name, attributes={}, &block)
5
+ def self.plant(klass, name, attributes={}, &block)
5
6
  raise RuntimeError, "You cannot overwrite an existing seed" if self.planted?(klass, name)
6
-
7
+
7
8
  if block_given?
8
9
  @seeds[klass][name] = klass.create!(&block)
9
10
  else
@@ -16,10 +17,14 @@ class Seed
16
17
  end
17
18
 
18
19
  def self.planted?(klass, name)
19
- @seeds ||= {}
20
20
  @seeds[klass] ||= {}
21
21
  @seeds[klass][name]
22
22
  end
23
+
24
+ def self.row(klass)
25
+ @seeds[klass]
26
+ end
27
+
23
28
  end
24
29
 
25
30
  class ActiveRecord::Base
@@ -34,4 +39,8 @@ class ActiveRecord::Base
34
39
  end
35
40
  end
36
41
  end
42
+
43
+ def self.seeds
44
+ Seed.row(self) || raise(RuntimeError, "No row of seeds for #{self}")
45
+ end
37
46
  end
data/spec/db/schema.rb CHANGED
@@ -1,5 +1,13 @@
1
1
  ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :users, :force => true do |t|
3
- t.string :email, :password
3
+ t.string :email, :password, :roles
4
+ end
5
+
6
+ create_table :roles, :force => true do |t|
7
+ t.string :role, :name
8
+ end
9
+
10
+ create_table :user_roles, :force => true do |t|
11
+ t.belongs_to :user, :role
4
12
  end
5
13
  end
data/spec/seed_spec.rb CHANGED
@@ -4,7 +4,25 @@ require 'active_record'
4
4
  require 'seed'
5
5
 
6
6
  class User < ActiveRecord::Base
7
+ has_many :user_roles
8
+ has_many :users, :through => :user_roles
9
+
7
10
  validates_presence_of :email
11
+
12
+ attr_protected :roles
13
+ end
14
+
15
+ class Role < ActiveRecord::Base
16
+ has_many :user_roles
17
+ has_many :users, :through => :user_roles
18
+ end
19
+
20
+ class UserRole < ActiveRecord::Base
21
+ belongs_to :role
22
+ belongs_to :user
23
+ end
24
+
25
+ class Admin < User
8
26
  end
9
27
 
10
28
  describe Seed do
@@ -26,14 +44,22 @@ describe Seed do
26
44
  User.seed(:tester, :email => 'test@testing.com')
27
45
  end.should change(User, :count).by(1)
28
46
  end
29
-
47
+
30
48
  describe "given a block" do
31
49
  it "should create a new seed" do
32
50
  lambda do
33
- User.seed(:block_user) do |user|
34
- user.email = 'test@test.com'
51
+ Role.seed(:system_administrator) do |role|
52
+ role.name = 'System Administrator'
53
+ role.role = 'system_administrator'
35
54
  end
36
- end.should change(User, :count).by(1)
55
+ end.should change(Role, :count).by(1)
56
+ end
57
+
58
+ it "should set a protected attribute" do
59
+ User.seed(:user_with_role) do |user|
60
+ user.email = 'user_with_role@example.com'
61
+ user.roles = [Role.seed(:system_administrator)]
62
+ end
37
63
  end
38
64
  end
39
65
  end
@@ -50,4 +76,16 @@ describe Seed do
50
76
  @user.should == User.seed(:new_user)
51
77
  end
52
78
  end
79
+
80
+ describe "retrieving a row" do
81
+ it "should raise a RuntimeError when retrieving an invalid row" do
82
+ lambda do
83
+ Admin.seeds
84
+ end.should raise_error(RuntimeError)
85
+ end
86
+
87
+ it "should return a row of seeds" do
88
+ User.seeds.should_not be_empty
89
+ end
90
+ end
53
91
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 2
9
- version: 1.0.2
8
+ - 3
9
+ version: 1.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jeremy Durham
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-07 00:00:00 -04:00
17
+ date: 2010-06-12 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -32,7 +32,7 @@ files:
32
32
  - tasks/seeds.rake
33
33
  - spec/db/schema.rb
34
34
  - spec/seed_spec.rb
35
- - README
35
+ - README.md
36
36
  has_rdoc: true
37
37
  homepage: http://www.jeremydurham.com
38
38
  licenses: []
data/README DELETED
@@ -1 +0,0 @@
1
- Seed - Another simple seeding library