famili 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +60 -1
- data/lib/famili/mother.rb +22 -6
- data/lib/famili.rb +1 -1
- data/spec/db/schema.rb +9 -1
- data/spec/famili_spec.rb +31 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -14,7 +14,7 @@ We meet some problems with factory-girl:
|
|
14
14
|
|
15
15
|
To define factory/mother for model User just create class in your lib directory:
|
16
16
|
|
17
|
-
|
17
|
+
#lib/famili/user.rb`
|
18
18
|
|
19
19
|
module Famili
|
20
20
|
class User < Mother
|
@@ -31,6 +31,16 @@ To define factory/mother for model User just create class in your lib directory:
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
+
|
35
|
+
#lib/famili/article
|
36
|
+
|
37
|
+
module Famili
|
38
|
+
class Article < Mother
|
39
|
+
#creating association
|
40
|
+
user { Famili::User.create }
|
41
|
+
title { "article by #{user.last_name}" }
|
42
|
+
end
|
43
|
+
end
|
34
44
|
|
35
45
|
|
36
46
|
And u can use it anywhere in tests or controllers:
|
@@ -39,6 +49,54 @@ And u can use it anywhere in tests or controllers:
|
|
39
49
|
Famili::User.build(:fist_name=>'Override') # build model not saving
|
40
50
|
Famili::User.hash(:fist_name=>'Override') #get attributes hash
|
41
51
|
|
52
|
+
Famili::Article.create #create article with user
|
53
|
+
|
54
|
+
== Inheritance
|
55
|
+
|
56
|
+
U can inherite mothers just like plain ruby classes,
|
57
|
+
Just think each declaration field_name {...} as method definition
|
58
|
+
|
59
|
+
module Famili
|
60
|
+
class User < Mother
|
61
|
+
name { "nicola" }
|
62
|
+
end
|
63
|
+
|
64
|
+
class Person < User
|
65
|
+
email { "#{name}@emial.com" }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
== Mother methods
|
70
|
+
|
71
|
+
Mother have some usable methods, which can be used
|
72
|
+
|
73
|
+
module Famili
|
74
|
+
class User < Mother
|
75
|
+
last_name { 'nicola' }
|
76
|
+
login { "#{last_name}_#{unique}" }
|
77
|
+
number { sequence_number }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
* sequence_number - incremented with each instance
|
82
|
+
* unique - just unique string
|
83
|
+
* we a planing add more
|
84
|
+
|
85
|
+
== Nutshell
|
86
|
+
|
87
|
+
each declaration
|
88
|
+
|
89
|
+
field_name { block }
|
90
|
+
|
91
|
+
just create method
|
92
|
+
|
93
|
+
def field_name
|
94
|
+
end
|
95
|
+
|
96
|
+
and register attribuite to copy in model
|
97
|
+
|
98
|
+
field :field_name
|
99
|
+
|
42
100
|
== Install
|
43
101
|
|
44
102
|
in config/environment.rb:
|
@@ -55,6 +113,7 @@ and then
|
|
55
113
|
== CHANGE LOG:
|
56
114
|
|
57
115
|
0.0.1 - created
|
116
|
+
0.0.2 - add inheritance, and mother methods [unique,sequence_number]
|
58
117
|
|
59
118
|
== TODO
|
60
119
|
|
data/lib/famili/mother.rb
CHANGED
@@ -1,14 +1,30 @@
|
|
1
|
+
require 'date'
|
1
2
|
module Famili
|
2
|
-
def before_save(model)
|
3
|
-
end
|
4
|
-
|
5
|
-
def after_create(model)
|
6
|
-
end
|
7
3
|
|
8
4
|
class Mother
|
5
|
+
|
6
|
+
def before_save(model)
|
7
|
+
end
|
8
|
+
|
9
|
+
def after_create(model)
|
10
|
+
end
|
11
|
+
|
12
|
+
def unique
|
13
|
+
"#{Time.now.to_s}__#{rand(100)}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def sequence_number
|
17
|
+
@sequence_number||= self.class.objects_sequence_number
|
18
|
+
end
|
19
|
+
|
9
20
|
class << self
|
10
21
|
alias :class_name :name
|
11
22
|
|
23
|
+
def objects_sequence_number
|
24
|
+
@sequence_number ||=0
|
25
|
+
@sequence_number += 1
|
26
|
+
end
|
27
|
+
|
12
28
|
def parent_class=(klass)
|
13
29
|
@parent_class = klass
|
14
30
|
end
|
@@ -30,7 +46,7 @@ module Famili
|
|
30
46
|
end
|
31
47
|
|
32
48
|
def attribures
|
33
|
-
@attribures||=parent_class && parent_class.attribures
|
49
|
+
@attribures||=parent_class && parent_class.attribures.clone
|
34
50
|
@attribures||=[]
|
35
51
|
@attribures.uniq!
|
36
52
|
@attribures
|
data/lib/famili.rb
CHANGED
data/spec/db/schema.rb
CHANGED
@@ -4,12 +4,20 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
4
4
|
t.string :first_name, :limit => 25
|
5
5
|
t.string :middle_name, :limit => 25
|
6
6
|
t.string :name, :limit => 25
|
7
|
-
t.string :login, :limit =>
|
7
|
+
t.string :login, :limit => 100
|
8
8
|
t.string :email, :limit => 100
|
9
|
+
t.string :number, :limit => 100
|
9
10
|
t.string :crypted_password, :limit => 40
|
10
11
|
t.string :salt, :limit => 40
|
11
12
|
t.datetime :last_login_datetime
|
12
13
|
t.datetime :deleted_at
|
13
14
|
t.timestamps
|
14
15
|
end
|
16
|
+
|
17
|
+
create_table :articles do |t|
|
18
|
+
t.integer :user_id
|
19
|
+
t.string :title, :limit => 25
|
20
|
+
t.text :content
|
21
|
+
t.timestamps
|
22
|
+
end
|
15
23
|
end
|
data/spec/famili_spec.rb
CHANGED
@@ -6,15 +6,23 @@ describe Famili do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
after :all do
|
9
|
-
|
9
|
+
TestDbUtils.drop_database
|
10
10
|
end
|
11
11
|
|
12
12
|
class User < ActiveRecord::Base
|
13
|
+
has_many :articles
|
14
|
+
end
|
15
|
+
|
16
|
+
class Article < ActiveRecord::Base
|
17
|
+
belongs_to :user
|
13
18
|
end
|
14
19
|
|
15
20
|
module Famili
|
16
21
|
class User < Mother
|
17
22
|
last_name { 'nicola' }
|
23
|
+
login { "#{last_name}_#{unique}" }
|
24
|
+
number { sequence_number }
|
25
|
+
|
18
26
|
|
19
27
|
def before_save(user)
|
20
28
|
user.first_name = 'first_name'
|
@@ -23,6 +31,11 @@ describe Famili do
|
|
23
31
|
def after_create(model)
|
24
32
|
end
|
25
33
|
end
|
34
|
+
|
35
|
+
class Article < Mother
|
36
|
+
user { Famili::User.create }
|
37
|
+
title { "article by #{user.last_name}" }
|
38
|
+
end
|
26
39
|
end
|
27
40
|
|
28
41
|
it "should create model" do
|
@@ -34,4 +47,21 @@ describe Famili do
|
|
34
47
|
ivan = Famili::User.create(:name=>'ivan')
|
35
48
|
ivan.name.should == 'ivan'
|
36
49
|
end
|
50
|
+
|
51
|
+
it "should create model with asscociation" do
|
52
|
+
article = Famili::Article.create
|
53
|
+
article.user.should_not be_nil
|
54
|
+
article.user.class.should == ::User
|
55
|
+
article.title.should == "article by nicola"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "mother should have unique,sequence_number methods" do
|
59
|
+
Famili::User.new.should respond_to(:unique)
|
60
|
+
Famili::User.new.should respond_to(:sequence_number)
|
61
|
+
u1 = Famili::User.create
|
62
|
+
u2 = Famili::User.create
|
63
|
+
seq_number = Famili::User.new.sequence_number
|
64
|
+
next_seq_number = Famili::User.new.sequence_number
|
65
|
+
next_seq_number.should == (seq_number + 1)
|
66
|
+
end
|
37
67
|
end
|
metadata
CHANGED