not_yet_active_record 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/lib/generators/not_yet_active_record/templates/unsaved_object.rb +6 -1
- data/lib/not_yet_active_record.rb +6 -0
- data/lib/not_yet_active_record/version.rb +1 -1
- data/spec/lib/not_yet_active_record_spec.rb +33 -14
- data/spec/not_yet.sqlite3 +0 -0
- data/spec/support/data.rb +2 -2
- data/spec/support/models.rb +17 -3
- data/spec/support/schema.rb +11 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 936ce9b73b27e6ad75f453bafd5cea59ec2f1ea5
|
4
|
+
data.tar.gz: c1339de53bac0419fe321826461198056125a1d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf838bfd808eef816e3fe2fbd47aed6fc3a3889ee15b03eea80d7a46ca8c16de3a62f9f22a068e8623faa45e5de6b111a477f339624fe076227856019c521a40
|
7
|
+
data.tar.gz: e50d860bf6fac0688aaf623c5fa64fd2feaf886fbdea61acba035d5a1ee38ee4df24cb9b99de9b382ba20efba2d71fe9371772f3d3a72cb67c65582547587857
|
@@ -7,6 +7,11 @@ class UnsavedObject < ActiveRecord::Base
|
|
7
7
|
att_hash.delete("id")
|
8
8
|
att_hash.delete("created_at")
|
9
9
|
att_hash.delete("updated_at")
|
10
|
-
model_name.constantize.new
|
10
|
+
item_obj = model_name.constantize.new
|
11
|
+
att_hash.each do |key, value|
|
12
|
+
sym = (key + "=").to_sym
|
13
|
+
item_obj.send(sym, value)
|
14
|
+
end
|
15
|
+
item_obj
|
11
16
|
end
|
12
17
|
end
|
@@ -14,6 +14,12 @@ module NotYetActiveRecord
|
|
14
14
|
module InstanceMethods
|
15
15
|
def not_yet_save
|
16
16
|
att_hash = self.attributes
|
17
|
+
|
18
|
+
class_name = self.class.name
|
19
|
+
association_keys = class_name.constantize.reflections.keys
|
20
|
+
association_keys.each do |assoc_sym|
|
21
|
+
att_hash[assoc_sym.to_s] = self.send(assoc_sym)
|
22
|
+
end
|
17
23
|
unsaved = UnsavedObject.new()
|
18
24
|
unsaved.serialized_item = att_hash.to_yaml
|
19
25
|
unsaved.model_name = self.class.name
|
@@ -1,39 +1,42 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "ActiveRecord instance" do
|
4
|
-
|
4
|
+
before(:each) do
|
5
|
+
@order = Order.new(ref_num: "5432")
|
6
|
+
end
|
7
|
+
|
5
8
|
it "should respond to not_yet" do
|
6
|
-
|
9
|
+
@order.should respond_to(:not_yet_save)
|
7
10
|
end
|
8
11
|
|
9
12
|
it "should be saved as an unsaved object" do
|
10
13
|
before = UnsavedObject.count
|
11
|
-
|
14
|
+
@order.not_yet_save
|
12
15
|
after = UnsavedObject.count
|
13
|
-
|
16
|
+
after.should == before+1
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
17
20
|
describe "ActiveRecord class" do
|
18
21
|
before(:each) do
|
19
|
-
@
|
20
|
-
@
|
22
|
+
@order = Order.new(ref_num: "1234")
|
23
|
+
@order.not_yet_save
|
21
24
|
@unsaved = UnsavedObject.last
|
22
25
|
end
|
23
26
|
|
24
27
|
it "should respond to not_yet_saved" do
|
25
|
-
|
28
|
+
Order.should respond_to(:not_yet_saved)
|
26
29
|
end
|
27
30
|
|
28
31
|
it "should return an array when not_yet_saved is called" do
|
29
|
-
|
32
|
+
Order.not_yet_saved.should be_an_instance_of(Array)
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
33
36
|
describe "UnsavedObject instance" do
|
34
37
|
before(:each) do
|
35
|
-
@
|
36
|
-
@
|
38
|
+
@order = Order.new(ref_num: "1234")
|
39
|
+
@order.not_yet_save
|
37
40
|
@unsaved = UnsavedObject.last
|
38
41
|
end
|
39
42
|
|
@@ -42,15 +45,31 @@ describe "UnsavedObject instance" do
|
|
42
45
|
end
|
43
46
|
|
44
47
|
it "should have the correct attribute model_name" do
|
45
|
-
@
|
48
|
+
@order.class.name.should == @unsaved.model_name
|
46
49
|
end
|
47
50
|
|
48
|
-
it "should recreate a
|
49
|
-
@unsaved.item.class.name.should == @
|
51
|
+
it "should recreate a @order object" do
|
52
|
+
@unsaved.item.class.name.should == @order.class.name
|
50
53
|
end
|
51
54
|
|
52
55
|
it "should have the same attributes as the original object" do
|
53
|
-
@unsaved.item.
|
56
|
+
@unsaved.item.ref_num.should == @order.ref_num
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "association" do
|
62
|
+
before(:each) do
|
63
|
+
@user = User.new(name: "jack")
|
64
|
+
@user.save
|
65
|
+
@order = Order.new(ref_num: "1234")
|
66
|
+
@order.user = @user
|
67
|
+
@order.not_yet_save
|
68
|
+
@unsaved = UnsavedObject.last
|
69
|
+
@new_copy = @unsaved.item
|
54
70
|
end
|
55
71
|
|
72
|
+
it "should have maintain associations" do
|
73
|
+
@new_copy.user.id.should == @order.user.id
|
74
|
+
end
|
56
75
|
end
|
data/spec/not_yet.sqlite3
CHANGED
Binary file
|
data/spec/support/data.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
o = Order.create(ref_num: "1234")
|
2
|
+
o.not_yet_save
|
data/spec/support/models.rb
CHANGED
@@ -1,7 +1,16 @@
|
|
1
|
-
class
|
1
|
+
class Order < ActiveRecord::Base
|
2
|
+
attr_accessible :ref_num
|
3
|
+
|
4
|
+
belongs_to :user
|
5
|
+
end
|
6
|
+
|
7
|
+
class User < ActiveRecord::Base
|
8
|
+
attr_accessible :name
|
9
|
+
|
10
|
+
has_many :orders
|
2
11
|
end
|
3
12
|
|
4
|
-
class
|
13
|
+
class UnsavedObject < ActiveRecord::Base
|
5
14
|
attr_accessible :model_name, :serialized_item
|
6
15
|
|
7
16
|
def item
|
@@ -10,6 +19,11 @@ class UnsavedObjects < ActiveRecord::Base
|
|
10
19
|
att_hash.delete("id")
|
11
20
|
att_hash.delete("created_at")
|
12
21
|
att_hash.delete("updated_at")
|
13
|
-
model_name.constantize.new
|
22
|
+
item_obj = model_name.constantize.new
|
23
|
+
att_hash.each do |key, value|
|
24
|
+
sym = (key + "=").to_sym
|
25
|
+
item_obj.send(sym, value)
|
26
|
+
end
|
27
|
+
item_obj
|
14
28
|
end
|
15
29
|
end
|
data/spec/support/schema.rb
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
ActiveRecord::Schema.define do
|
2
2
|
self.verbose = false
|
3
3
|
|
4
|
-
create_table :
|
5
|
-
t.string :
|
4
|
+
create_table :orders, force: true do |t|
|
5
|
+
t.string :ref_num
|
6
6
|
t.timestamps
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
create_table :users, force: true do |t|
|
10
|
+
t.string :name
|
11
|
+
end
|
12
|
+
|
13
|
+
create_table :unsaved_objects, force: true do |t|
|
14
|
+
t.text :serialized_item
|
15
|
+
t.string :model_name
|
12
16
|
|
13
|
-
|
14
|
-
|
17
|
+
t.timestamps
|
18
|
+
end
|
15
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: not_yet_active_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Frey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|