notahat-machinist 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/machinist/active_record.rb +107 -0
- metadata +2 -1
@@ -0,0 +1,107 @@
|
|
1
|
+
module Machinist
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
|
5
|
+
# This method takes care of converting any associated objects,
|
6
|
+
# in the hash returned by Lathe#assigned_attributed, into their
|
7
|
+
# object ids.
|
8
|
+
#
|
9
|
+
# For example, let's say we have blueprints like this:
|
10
|
+
#
|
11
|
+
# Post.blueprint { }
|
12
|
+
# Comment.blueprint { post }
|
13
|
+
#
|
14
|
+
# Lathe#assigned_attributes will return { :post => ... }, but
|
15
|
+
# we want to pass { :post_id => 1 } to a controller.
|
16
|
+
#
|
17
|
+
# This method takes care of cleaning this up.
|
18
|
+
def self.assigned_attributes_without_associations(lathe)
|
19
|
+
attributes = {}
|
20
|
+
lathe.assigned_attributes.each_pair do |attribute, value|
|
21
|
+
association = lathe.object.class.reflect_on_association(attribute)
|
22
|
+
if association && association.macro == :belongs_to
|
23
|
+
attributes[association.primary_key_name.to_sym] = value.id
|
24
|
+
else
|
25
|
+
attributes[attribute] = value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
attributes
|
29
|
+
end
|
30
|
+
|
31
|
+
# This sets a flag that stops make from saving objects, so
|
32
|
+
# that calls to make from within a blueprint don't create
|
33
|
+
# anything inside make_unsaved.
|
34
|
+
def self.with_save_nerfed
|
35
|
+
begin
|
36
|
+
@@nerfed = true
|
37
|
+
yield
|
38
|
+
ensure
|
39
|
+
@@nerfed = false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
@@nerfed = false
|
44
|
+
def self.nerfed?
|
45
|
+
@@nerfed
|
46
|
+
end
|
47
|
+
|
48
|
+
module Extensions
|
49
|
+
def self.included(base)
|
50
|
+
base.extend(ClassMethods)
|
51
|
+
end
|
52
|
+
|
53
|
+
module ClassMethods
|
54
|
+
def blueprint(&blueprint)
|
55
|
+
@blueprint = blueprint if block_given?
|
56
|
+
@blueprint
|
57
|
+
end
|
58
|
+
|
59
|
+
def make(attributes = {}, &block)
|
60
|
+
lathe = Lathe.run(self.new, attributes)
|
61
|
+
unless Machinist::ActiveRecord.nerfed?
|
62
|
+
lathe.object.save!
|
63
|
+
lathe.object.reload
|
64
|
+
end
|
65
|
+
lathe.object(&block)
|
66
|
+
end
|
67
|
+
|
68
|
+
def make_unsaved(attributes = {})
|
69
|
+
returning(Machinist::ActiveRecord.with_save_nerfed { make(attributes) }) do |object|
|
70
|
+
yield object if block_given?
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def plan(attributes = {})
|
75
|
+
lathe = Lathe.run(self.new, attributes)
|
76
|
+
Machinist::ActiveRecord.assigned_attributes_without_associations(lathe)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
module BelongsToExtensions
|
82
|
+
def make(attributes = {}, &block)
|
83
|
+
lathe = Lathe.run(self.build, attributes)
|
84
|
+
unless Machinist::ActiveRecord.nerfed?
|
85
|
+
lathe.object.save!
|
86
|
+
lathe.object.reload
|
87
|
+
end
|
88
|
+
lathe.object(&block)
|
89
|
+
end
|
90
|
+
|
91
|
+
def plan(attributes = {})
|
92
|
+
lathe = Lathe.run(self.build, attributes)
|
93
|
+
Machinist::ActiveRecord.assigned_attributes_without_associations(lathe)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
class ActiveRecord::Base
|
102
|
+
include Machinist::ActiveRecord::Extensions
|
103
|
+
end
|
104
|
+
|
105
|
+
class ActiveRecord::Associations::BelongsToAssociation
|
106
|
+
include Machinist::ActiveRecord::BelongsToExtensions
|
107
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: notahat-machinist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pete Yandell
|
@@ -24,6 +24,7 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- lib/machinist.rb
|
26
26
|
- lib/sham.rb
|
27
|
+
- lib/machinist/active_record.rb
|
27
28
|
has_rdoc: false
|
28
29
|
homepage: http://github.com/notahat/machinist
|
29
30
|
post_install_message:
|