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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ec85494c47d00e50cec3985b1151f3b03b17fe2
4
- data.tar.gz: 1f34b172ccd9ed4bdadd35a6fba6387f9fc828bf
3
+ metadata.gz: 936ce9b73b27e6ad75f453bafd5cea59ec2f1ea5
4
+ data.tar.gz: c1339de53bac0419fe321826461198056125a1d3
5
5
  SHA512:
6
- metadata.gz: 65cead476572ff0208538270abe2d7fcde1f2ede2fff56fd929919807e5e5d97872023a7f630cf6bb77ac328995d306132a6544cfebba12ead17fdb722d79846
7
- data.tar.gz: 83ecdbb345d2948deb8e3dad2cf80e9223f6861f4b67f2473709058c3b06817bb8ad7a19fcacd3e3e61928cb63eb9692fd19ec86db5937987183d8e27ba20a5a
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(att_hash)
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,3 +1,3 @@
1
1
  module NotYetActiveRecord
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,39 +1,42 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "ActiveRecord instance" do
4
- post = Post.first
4
+ before(:each) do
5
+ @order = Order.new(ref_num: "5432")
6
+ end
7
+
5
8
  it "should respond to not_yet" do
6
- post.should respond_to(:not_yet_save)
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
- post.not_yet_save
14
+ @order.not_yet_save
12
15
  after = UnsavedObject.count
13
- before.should == after-1
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
- @post = Post.first
20
- @post.not_yet_save
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
- Post.should respond_to(:not_yet_saved)
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
- Post.not_yet_saved.should be_an_instance_of(Array)
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
- @post = Post.first
36
- @post.not_yet_save
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
- @post.class.name.should == @unsaved.model_name
48
+ @order.class.name.should == @unsaved.model_name
46
49
  end
47
50
 
48
- it "should recreate a post object" do
49
- @unsaved.item.class.name.should == @post.class.name
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.text.should == @post.text
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
Binary file
@@ -1,2 +1,2 @@
1
- p = Post.create(:text => "First post!")
2
- p.not_yet_save
1
+ o = Order.create(ref_num: "1234")
2
+ o.not_yet_save
@@ -1,7 +1,16 @@
1
- class Post < ActiveRecord::Base
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 UnsavedObjects < ActiveRecord::Base
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(att_hash)
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
@@ -1,15 +1,19 @@
1
1
  ActiveRecord::Schema.define do
2
2
  self.verbose = false
3
3
 
4
- create_table :posts, force: true do |t|
5
- t.string :text
4
+ create_table :orders, force: true do |t|
5
+ t.string :ref_num
6
6
  t.timestamps
7
7
  end
8
8
 
9
- # create_table :unsaved_objects do |t|
10
- # t.text :serialized_item
11
- # t.string :model_name
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
- # t.timestamps
14
- # end
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.3
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-15 00:00:00.000000000 Z
11
+ date: 2013-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord