has_alter_ego 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +9 -0
- data/README.md +23 -4
- data/lib/has_alter_ego/dumper.rb +27 -1
- data/lib/has_alter_ego.rb +20 -0
- data/lib/tasks/has_alter_ego.rake +6 -1
- data/test/db/fixtures/alter_egos/cars.yml +9 -1
- data/test/has_alter_ego_test.rb +20 -1
- metadata +5 -4
data/Changelog.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# 0.0.3 (2010-06-10)
|
2
|
+
* Add a rake task for dumping the current database objects into an alter_egos YAML file
|
3
|
+
* Do not bring back destroyed objects
|
4
|
+
|
5
|
+
# 0.0.2 (2010-06-09)
|
6
|
+
* Added Rails3 support
|
7
|
+
|
8
|
+
# 0.0.1 (2010-06-09)
|
9
|
+
* Initial release with basic functionality
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Then
|
|
20
20
|
script/generate has_alter_ego
|
21
21
|
rake db:migrate
|
22
22
|
|
23
|
-
Rails 3
|
23
|
+
## Rails 3
|
24
24
|
### As a plugin
|
25
25
|
rails plugin install git://github.com/aduffeck/has_alter_ego.git
|
26
26
|
rails generate has_alter_ego
|
@@ -35,10 +35,10 @@ Then
|
|
35
35
|
rake db:migrate
|
36
36
|
|
37
37
|
# Usage
|
38
|
-
|
38
|
+
## General
|
39
39
|
The seed data is defined in YAML files called after the model's table. The files are expected in **db/fixtures/alter_egos**.
|
40
40
|
|
41
|
-
Say you have a Model Car
|
41
|
+
Say you have a Model *Car*. has_alter_ego is enabled with the *has_alter_ego* method:
|
42
42
|
|
43
43
|
create_table :cars do |t|
|
44
44
|
t.string :brand
|
@@ -50,7 +50,7 @@ Say you have a Model Car. has_alter_ego is enabled with the *has_alter_ego* meth
|
|
50
50
|
has_alter_ego
|
51
51
|
end
|
52
52
|
|
53
|
-
You
|
53
|
+
You could then create a file **db/fixtures/alter_egos/cars.yml** with the seed data:
|
54
54
|
|
55
55
|
1:
|
56
56
|
brand: Lotus
|
@@ -74,6 +74,18 @@ and you'd automagically have those objects available in your database.
|
|
74
74
|
=> #<Car id: 1, brand: "Lotus", model: "Elise">
|
75
75
|
|
76
76
|
Whenever the seed definition changes the objects in the database inherit the changes unless they have been overridden.
|
77
|
+
When a seed object was destroyed in the database it will not be added again.
|
78
|
+
|
79
|
+
**Note:** If the table has a numeric primary key has_alter_ego reserves the first n IDs for seed objects (default=1000),
|
80
|
+
so the next non-seed object will get the ID 1001.
|
81
|
+
The number of reserved objects can be set with the optional *:reserved_space* parameter, e.g.
|
82
|
+
|
83
|
+
has_alter_ego :reserved_space => 5000
|
84
|
+
|
85
|
+
You always have to make sure that no seed IDs clash with IDs in the database.
|
86
|
+
|
87
|
+
|
88
|
+
## Advanced stuff
|
77
89
|
You can check if an object was created from seed definition with *has_alter_ego?*:
|
78
90
|
|
79
91
|
@car = Car.find(1)
|
@@ -110,5 +122,12 @@ If you don't want to inherit changes for an object without actually modifying it
|
|
110
122
|
@car.alter_ego_state
|
111
123
|
=> "default"
|
112
124
|
|
125
|
+
# Generating seed data from the database
|
126
|
+
|
127
|
+
has_alter_ego has a rake task for dumping the current database content into a seed file. It is called like this:
|
128
|
+
|
129
|
+
rake has_alter_ego::dump MODEL=Car
|
130
|
+
|
131
|
+
That will fill **db/fixtures/alter_egos/cars.yml** with the database objects.
|
113
132
|
|
114
133
|
Copyright (c) 2010 André Duffeck, released under the MIT license
|
data/lib/has_alter_ego/dumper.rb
CHANGED
@@ -1,6 +1,32 @@
|
|
1
1
|
module HasAlterEgo
|
2
2
|
class Dumper
|
3
|
-
def dump
|
3
|
+
def self.dump
|
4
|
+
raise "You need to pass a MODEL=<model name> argument to rake" if ENV["MODEL"].blank?
|
5
|
+
klaas = ENV["MODEL"].constantize
|
6
|
+
|
7
|
+
yml_file = File.join(RAILS_ROOT, "db", "fixtures", "alter_egos", klaas.table_name + ".yml")
|
8
|
+
yml = {}
|
9
|
+
if File.exists?(yml_file)
|
10
|
+
File.open(yml_file) do |yf|
|
11
|
+
yml = YAML::load( yf )
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
puts yml.inspect
|
16
|
+
klaas.all.each do |o|
|
17
|
+
key = o[klaas.primary_key]
|
18
|
+
yml[key] ||= {}
|
19
|
+
o.attributes.keys.each do |a|
|
20
|
+
next if a == klaas.primary_key
|
21
|
+
yml[key][a] = o[a]
|
22
|
+
end
|
23
|
+
o.build_alter_ego unless o.alter_ego
|
24
|
+
o.alter_ego.state = 'default'
|
25
|
+
o.save_without_alter_ego
|
26
|
+
end
|
27
|
+
File.open(yml_file, File::WRONLY|File::TRUNC|File::CREAT) do |yf|
|
28
|
+
yf.write yml.to_yaml
|
29
|
+
end
|
4
30
|
end
|
5
31
|
end
|
6
32
|
end
|
data/lib/has_alter_ego.rb
CHANGED
@@ -13,6 +13,7 @@ module HasAlterEgo
|
|
13
13
|
class_eval do
|
14
14
|
has_one :alter_ego, :as => :alter_ego_object
|
15
15
|
alias_method :save_without_alter_ego, :save
|
16
|
+
alias_method :destroy_without_alter_ego, :destroy
|
16
17
|
send :include, InstanceMethods
|
17
18
|
reserve_space(opts[:reserved_space])
|
18
19
|
parse_yml
|
@@ -51,6 +52,10 @@ module HasAlterEgo
|
|
51
52
|
db_object.save_without_alter_ego
|
52
53
|
end
|
53
54
|
else
|
55
|
+
# Check for destroyed alter_egos
|
56
|
+
alter_ego = AlterEgo.find_by_alter_ego_object_id_and_alter_ego_object_type(primary_key, self.name)
|
57
|
+
return if alter_ego.try(:state) == "destroyed"
|
58
|
+
|
54
59
|
db_object = self.new
|
55
60
|
db_object[self.primary_key] = primary_key
|
56
61
|
yml[primary_key].keys.each do |attr|
|
@@ -88,14 +93,29 @@ module HasAlterEgo
|
|
88
93
|
self.alter_ego.state = 'modified'
|
89
94
|
self.alter_ego.save
|
90
95
|
end
|
96
|
+
if ActiveRecord::VERSION::MAJOR == 3 and !perform_validation.is_a?(Hash)
|
97
|
+
perform_validation = {:validate => perform_validation}
|
98
|
+
end
|
91
99
|
save_without_alter_ego perform_validation
|
92
100
|
end
|
93
101
|
|
102
|
+
def destroy
|
103
|
+
if self.alter_ego
|
104
|
+
self.alter_ego.state = 'destroyed'
|
105
|
+
self.alter_ego.save
|
106
|
+
end
|
107
|
+
destroy_without_alter_ego
|
108
|
+
end
|
109
|
+
|
94
110
|
def pin!
|
95
111
|
self.alter_ego.state = 'pinned'
|
96
112
|
self.alter_ego.save
|
97
113
|
end
|
98
114
|
|
115
|
+
def ban!
|
116
|
+
|
117
|
+
end
|
118
|
+
|
99
119
|
def reset
|
100
120
|
self.alter_ego.state = 'default'
|
101
121
|
self.alter_ego.save
|
data/test/has_alter_ego_test.rb
CHANGED
@@ -35,7 +35,7 @@ class HasAlterEgoTest < Test::Unit::TestCase
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def test_create_objects_from_yml
|
38
|
-
assert_equal
|
38
|
+
assert_equal 6, Car.count
|
39
39
|
c1 = Car.find(1)
|
40
40
|
assert_equal "Lotus", c1.brand
|
41
41
|
assert_equal "Elise", c1.model
|
@@ -153,4 +153,23 @@ class HasAlterEgoTest < Test::Unit::TestCase
|
|
153
153
|
orangejuice.save
|
154
154
|
assert !orangejuice.has_alter_ego?
|
155
155
|
end
|
156
|
+
|
157
|
+
def test_destroyed_object_leaves_destroyed_alter_ego
|
158
|
+
c = Car.find(6)
|
159
|
+
alter_ego = c.alter_ego
|
160
|
+
assert_equal "default", alter_ego.state
|
161
|
+
|
162
|
+
c.destroy
|
163
|
+
alter_ego.reload
|
164
|
+
assert_equal "destroyed", alter_ego.state
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_destroyed_objects_do_not_return
|
168
|
+
assert Car.find_by_id(5)
|
169
|
+
Car.find_by_id(5).destroy
|
170
|
+
assert !Car.find_by_id(5)
|
171
|
+
|
172
|
+
Car.parse_yml
|
173
|
+
assert !Car.find_by_id(5)
|
174
|
+
end
|
156
175
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_alter_ego
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Andr\xC3\xA9 Duffeck"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-06-
|
18
|
+
date: 2010-06-10 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- lib/generators/has_alter_ego_generator.rb
|
35
35
|
- lib/has_alter_ego.rb
|
36
36
|
- lib/tasks/has_alter_ego.rake
|
37
|
+
- Changelog.md
|
37
38
|
- README.md
|
38
39
|
- Rakefile
|
39
40
|
- rails/init.rb
|