has_alter_ego 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.0.4 (2010-06-11)
2
+ * fix some bugs esp. wrt associations
3
+ * added a on_seed hook which allows for custom login on seed
4
+
1
5
  # 0.0.3 (2010-06-10)
2
6
  * Add a rake task for dumping the current database objects into an alter_egos YAML file
3
7
  * Do not bring back destroyed objects
data/README.md CHANGED
@@ -7,11 +7,6 @@ overridden it in the database.
7
7
  # Installation
8
8
 
9
9
  ## Rails 2.3.x
10
- ### As a plugin
11
- script/plugin install git://github.com/aduffeck/has_alter_ego.git
12
- script/generate has_alter_ego
13
- rake db:migrate
14
-
15
10
  ### As a gem
16
11
  Add the following line to your **config/environment.rb** file:
17
12
  config.gem "has_alter_ego"
@@ -20,12 +15,13 @@ Then
20
15
  script/generate has_alter_ego
21
16
  rake db:migrate
22
17
 
23
- ## Rails 3
24
18
  ### As a plugin
25
- rails plugin install git://github.com/aduffeck/has_alter_ego.git
26
- rails generate has_alter_ego
19
+ script/plugin install git://github.com/aduffeck/has_alter_ego.git
20
+ script/generate has_alter_ego
27
21
  rake db:migrate
28
22
 
23
+
24
+ ## Rails 3
29
25
  ### As a gem
30
26
  Add the following line to your **Gemfile** file:
31
27
  gem "has_alter_ego"
@@ -34,6 +30,12 @@ Then
34
30
  rails generate has_alter_ego
35
31
  rake db:migrate
36
32
 
33
+ ### As a plugin
34
+ rails plugin install git://github.com/aduffeck/has_alter_ego.git
35
+ rails generate has_alter_ego
36
+ rake db:migrate
37
+
38
+
37
39
  # Usage
38
40
  ## General
39
41
  The seed data is defined in YAML files called after the model's table. The files are expected in **db/fixtures/alter_egos**.
@@ -122,6 +124,22 @@ If you don't want to inherit changes for an object without actually modifying it
122
124
  @car.alter_ego_state
123
125
  => "default"
124
126
 
127
+ # Custom logic on seed
128
+
129
+ has_alter_ego provides a hook for adding custom logic when an object is created or updated from the seed definitions.
130
+ Just add a method *on_seed(attributes)* to your Model and you'll have access to all the seed attributes.
131
+
132
+ **Note:** You should not call save from within the hook or the objects will be marked as modified.
133
+
134
+ Example:
135
+ class Car < ActiveRecord::Base
136
+ has_alter_ego
137
+
138
+ def on_seed(attributes)
139
+ self.price = attributes["price_without_vat"] * VAT_FACTOR
140
+ end
141
+ end
142
+
125
143
  # Generating seed data from the database
126
144
 
127
145
  has_alter_ego has a rake task for dumping the current database content into a seed file. It is called like this:
data/Rakefile CHANGED
@@ -17,10 +17,10 @@ end
17
17
  desc 'Generate documentation for the has_alter_ego plugin.'
18
18
  Rake::RDocTask.new(:rdoc) do |rdoc|
19
19
  rdoc.rdoc_dir = 'rdoc'
20
- rdoc.title = 'Schizophrenia'
20
+ rdoc.title = 'HasAlterEgo'
21
21
  rdoc.options << '--line-numbers' << '--inline-source'
22
- rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('README.md')
23
23
  rdoc.rdoc_files.include('lib/**/*.rb')
24
24
  end
25
25
 
26
- Rake::GemPackageTask.new(eval(File.read("has_alter_ego.gemspec"))) { |pkg| }
26
+ Rake::GemPackageTask.new(eval(File.read("has_alter_ego.gemspec"))) { |pkg| }
data/lib/has_alter_ego.rb CHANGED
@@ -28,7 +28,7 @@ module HasAlterEgo
28
28
  o = self.new
29
29
  o[self.primary_key] = space
30
30
  o.save_without_alter_ego
31
- o.destroy
31
+ o.delete
32
32
  return
33
33
  end
34
34
 
@@ -47,8 +47,10 @@ module HasAlterEgo
47
47
  raise "There is already a #{db_object.class} with id #{db_object.id} in the database." unless db_object.has_alter_ego?
48
48
  if db_object.alter_ego.state == 'default'
49
49
  yml[primary_key].keys.each do |attr|
50
- db_object[attr] = yml[primary_key][attr]
50
+ next unless db_object.respond_to?(attr)
51
+ db_object.send(attr+"=", yml[primary_key][attr])
51
52
  end
53
+ db_object.on_seed(yml[primary_key])
52
54
  db_object.save_without_alter_ego
53
55
  end
54
56
  else
@@ -59,10 +61,12 @@ module HasAlterEgo
59
61
  db_object = self.new
60
62
  db_object[self.primary_key] = primary_key
61
63
  yml[primary_key].keys.each do |attr|
62
- db_object[attr] = yml[primary_key][attr]
64
+ next unless db_object.respond_to?(attr)
65
+ db_object.send(attr+"=" , yml[primary_key][attr])
63
66
  end
64
67
  db_object.build_alter_ego
65
68
  db_object.alter_ego.state = 'default'
69
+ db_object.on_seed(yml[primary_key])
66
70
  db_object.save_without_alter_ego
67
71
  end
68
72
  end
@@ -112,10 +116,6 @@ module HasAlterEgo
112
116
  self.alter_ego.save
113
117
  end
114
118
 
115
- def ban!
116
-
117
- end
118
-
119
119
  def reset
120
120
  self.alter_ego.state = 'default'
121
121
  self.alter_ego.save
@@ -123,6 +123,10 @@ module HasAlterEgo
123
123
  self.class.parse_yml_for self[self.class.primary_key]
124
124
  self.reload
125
125
  end
126
+
127
+ def on_seed attributes
128
+ # Not implemented
129
+ end
126
130
  end
127
131
  end
128
132
  end
@@ -1,6 +1,7 @@
1
1
  1:
2
2
  brand: Lotus
3
3
  model: Elise
4
+ seller_ids: [1, 2]
4
5
 
5
6
  2:
6
7
  brand: Porsche
@@ -13,6 +14,8 @@
13
14
  4:
14
15
  brand: Corvette
15
16
  model: C5
17
+ custom_data:
18
+ tires: 4
16
19
 
17
20
  5:
18
21
  brand: Fiat
@@ -0,0 +1,5 @@
1
+ 1:
2
+ name: Bruno
3
+
4
+ 2:
5
+ name: Harald
@@ -1,8 +1,20 @@
1
1
  require 'test_helper'
2
2
 
3
+
4
+ class Seller < ActiveRecord::Base
5
+ has_and_belongs_to_many :cars
6
+ has_alter_ego
7
+ end
8
+
3
9
  class Car < ActiveRecord::Base
4
10
  has_many :tires
11
+ has_and_belongs_to_many :sellers
5
12
  has_alter_ego
13
+ attr_accessor :tires_count
14
+
15
+ def on_seed attributes
16
+ self[:tires_count] = attributes["custom_data"]["tires"] if attributes["custom_data"]
17
+ end
6
18
  end
7
19
 
8
20
  class Bike < ActiveRecord::Base
@@ -172,4 +184,26 @@ class HasAlterEgoTest < Test::Unit::TestCase
172
184
  Car.parse_yml
173
185
  assert !Car.find_by_id(5)
174
186
  end
187
+
188
+ def test_on_seed
189
+ assert_nil Car.find(1).tires_count
190
+ assert_equal 4, Car.find(4)[:tires_count]
191
+ end
192
+
193
+ def test_habtm
194
+ assert_equal [1,2], Car.find(1).seller_ids
195
+ assert_equal 2, Car.find(1).sellers.size
196
+
197
+ c = Car.find(1)
198
+ c.sellers = [Seller.first]
199
+ c.reload
200
+
201
+ assert_equal [Seller.first], c.sellers
202
+ assert_equal [Seller.first.id], c.seller_ids
203
+
204
+ c.reset
205
+ c.reload
206
+
207
+ assert_equal [1,2], c.seller_ids
208
+ end
175
209
  end
data/test/test_helper.rb CHANGED
@@ -19,6 +19,7 @@ silence_stream(STDOUT) do
19
19
  create_table :cars do |t|
20
20
  t.string :brand
21
21
  t.string :model
22
+ t.integer :tires_count
22
23
  end
23
24
 
24
25
  create_table :bikes
@@ -32,5 +33,14 @@ silence_stream(STDOUT) do
32
33
  t.string :name
33
34
  t.string :color
34
35
  end
36
+
37
+ create_table :sellers do |t|
38
+ t.string :name
39
+ end
40
+ create_table :cars_sellers, :id => false do |t|
41
+ t.integer :car_id
42
+ t.integer :seller_id
43
+ end
44
+
35
45
  end
36
46
  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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
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-10 00:00:00 +02:00
18
+ date: 2010-06-11 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -40,6 +40,7 @@ files:
40
40
  - rails/init.rb
41
41
  - generators/has_alter_ego/templates/create_alter_egos.rb
42
42
  - generators/has_alter_ego/has_alter_ego_generator.rb
43
+ - test/db/fixtures/alter_egos/sellers.yml
43
44
  - test/db/fixtures/alter_egos/bikes.yml
44
45
  - test/db/fixtures/alter_egos/cars.yml
45
46
  - test/db/fixtures/alter_egos/drinks.yml