ninja-model 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ninja_model/base.rb +1 -0
- data/lib/ninja_model/callbacks.rb +6 -2
- data/lib/ninja_model/persistence.rb +44 -41
- data/lib/ninja_model/rails_ext/active_record.rb +20 -3
- data/lib/ninja_model/validation.rb +14 -7
- data/lib/ninja_model/version.rb +1 -1
- metadata +22 -22
data/lib/ninja_model/base.rb
CHANGED
@@ -1,61 +1,64 @@
|
|
1
1
|
module NinjaModel
|
2
|
-
class Base
|
3
|
-
define_model_callbacks :save, :create, :update, :destroy
|
4
|
-
end
|
5
|
-
|
6
2
|
module Persistence
|
3
|
+
extend ActiveSupport::Concern
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
result = new_record? ? create : update
|
11
|
-
changed_attributes.clear if result
|
12
|
-
result
|
13
|
-
end
|
5
|
+
included do
|
6
|
+
define_model_callbacks :save, :create, :update, :destroy
|
14
7
|
end
|
15
8
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
module InstanceMethods
|
10
|
+
def save(*)
|
11
|
+
run_callbacks :save do
|
12
|
+
result = new_record? ? create : update
|
13
|
+
changed_attributes.clear if result
|
14
|
+
result
|
20
15
|
end
|
21
|
-
@persisted
|
22
16
|
end
|
23
|
-
end
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
|
18
|
+
def create
|
19
|
+
run_callbacks :create do
|
20
|
+
if self.class.adapter.create(self)
|
21
|
+
@persisted = true
|
22
|
+
end
|
23
|
+
@persisted
|
24
|
+
end
|
28
25
|
end
|
29
|
-
end
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
def update
|
28
|
+
run_callbacks :update do
|
29
|
+
self.class.adapter.update(self)
|
30
|
+
end
|
31
|
+
end
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
def new_record?
|
34
|
+
!@persisted
|
35
|
+
end
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
def destroyed?
|
38
|
+
@destroyed
|
39
|
+
end
|
40
|
+
|
41
|
+
def persisted?
|
42
|
+
@persisted && !destroyed?
|
43
|
+
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
def destroy
|
46
|
+
run_callbacks :destroy do
|
47
|
+
if self.class.adapter.destroy(self)
|
48
|
+
@destroyed = true
|
49
|
+
end
|
50
|
+
@destroyed
|
47
51
|
end
|
48
|
-
@destroyed
|
49
52
|
end
|
50
|
-
end
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
def reload
|
55
|
+
self.class.adapter.reload(self)
|
56
|
+
end
|
55
57
|
|
56
|
-
|
57
|
-
|
58
|
-
|
58
|
+
def update_attributes(attributes)
|
59
|
+
self.attributes = attributes
|
60
|
+
save
|
61
|
+
end
|
59
62
|
end
|
60
63
|
end
|
61
64
|
end
|
@@ -70,7 +70,9 @@ module NinjaModel
|
|
70
70
|
if NinjaModel.ninja_model?(klass)
|
71
71
|
has_one_without_active_record(association_id, options.merge(:class_name => klass.name.underscore))
|
72
72
|
else
|
73
|
-
proxy.handle_association(:has_one, association_id, options)
|
73
|
+
reflection = proxy.handle_association(:has_one, association_id, options)
|
74
|
+
write_inheritable_hash :reflections, association_id => reflection
|
75
|
+
reflection
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
@@ -83,7 +85,9 @@ module NinjaModel
|
|
83
85
|
if NinjaModel.ninja_model?(klass)
|
84
86
|
belongs_to_without_active_record(association_id, options.merge(:class_name => klass.name.underscore))
|
85
87
|
else
|
86
|
-
proxy.handle_association(:belongs_to, association_id, options)
|
88
|
+
reflection = proxy.handle_association(:belongs_to, association_id, options)
|
89
|
+
write_inheritable_hash :reflections, association_id => reflection
|
90
|
+
reflection
|
87
91
|
end
|
88
92
|
end
|
89
93
|
|
@@ -96,12 +100,24 @@ module NinjaModel
|
|
96
100
|
if NinjaModel.ninja_model?(klass)
|
97
101
|
has_many_without_active_record(association_id, options.merge(:class_name => klass.name.underscore))
|
98
102
|
else
|
99
|
-
proxy.handle_association(:has_many, association_id, options)
|
103
|
+
reflection = proxy.handle_association(:has_many, association_id, options)
|
104
|
+
write_inheritable_hash :reflections, association_id => reflection
|
105
|
+
reflection
|
100
106
|
end
|
101
107
|
end
|
102
108
|
|
103
109
|
alias_method_chain :has_many, :active_record
|
104
110
|
|
111
|
+
def reflect_on_association_with_active_record(association_id)
|
112
|
+
if read_inheritable_attribute(:proxy) && proxy.proxy_klass.reflections.include?(association_id)
|
113
|
+
proxy.proxy_klass.reflect_on_association(association_id)
|
114
|
+
else
|
115
|
+
reflect_on_association_without_active_record(association_id)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
alias_method_chain :reflect_on_association, :active_record
|
120
|
+
|
105
121
|
def proxy
|
106
122
|
read_inheritable_attribute(:proxy) || write_inheritable_attribute(:proxy, Associations::ActiveRecordProxy.new(self))
|
107
123
|
end
|
@@ -126,6 +142,7 @@ module NinjaModel
|
|
126
142
|
|
127
143
|
module Associations
|
128
144
|
class ActiveRecordProxy
|
145
|
+
attr_reader :proxy_klass
|
129
146
|
def initialize(ninja_model)
|
130
147
|
@klass = ninja_model
|
131
148
|
@klass.class_eval do
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module NinjaModel
|
2
|
-
class Base
|
3
|
-
include ActiveModel::Validations
|
4
|
-
define_model_callbacks :validation
|
5
|
-
end
|
6
2
|
|
7
3
|
module Validation
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
include ActiveModel::Validations
|
8
|
+
define_model_callbacks :validation
|
9
|
+
end
|
8
10
|
|
9
11
|
def save(options={})
|
10
|
-
|
11
|
-
valid?(options.is_a?(Hash) ? options[:context] : nil) ? super : false
|
12
|
-
end
|
12
|
+
perform_validations(options) ? super : false
|
13
13
|
end
|
14
14
|
|
15
15
|
def valid?(context = nil)
|
@@ -17,5 +17,12 @@ module NinjaModel
|
|
17
17
|
output = super(context)
|
18
18
|
errors.empty? && output
|
19
19
|
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def perform_validations(options)
|
24
|
+
perform_validation = options[:validate] != false
|
25
|
+
perform_validation ? valid?(options[:context]) : true
|
26
|
+
end
|
20
27
|
end
|
21
28
|
end
|
data/lib/ninja_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ninja-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-04 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &9709260 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *9709260
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &9708520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.9.2
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *9708520
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &9705000 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.8.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *9705000
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: mocha
|
49
|
-
requirement: &
|
49
|
+
requirement: &9704320 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 0.10.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *9704320
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: nokogiri
|
60
|
-
requirement: &
|
60
|
+
requirement: &9703640 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.5.0
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *9703640
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: guard-rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &9702980 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 0.5.10
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *9702980
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: libnotify
|
82
|
-
requirement: &
|
82
|
+
requirement: &9702340 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: 0.6.0
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *9702340
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: yard
|
93
|
-
requirement: &
|
93
|
+
requirement: &9701740 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ~>
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: 0.7.4
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *9701740
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: redcarpet
|
104
|
-
requirement: &
|
104
|
+
requirement: &9701160 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ~>
|
@@ -109,7 +109,7 @@ dependencies:
|
|
109
109
|
version: 2.0.0
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *9701160
|
113
113
|
description: Pseudo-ORM for Ruby/Rails with an ActiveRecord-like interface
|
114
114
|
email: theprime@codingprime.com
|
115
115
|
executables: []
|
@@ -190,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
190
190
|
version: '0'
|
191
191
|
segments:
|
192
192
|
- 0
|
193
|
-
hash:
|
193
|
+
hash: -1070647489390734539
|
194
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
195
|
none: false
|
196
196
|
requirements:
|
@@ -199,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
199
199
|
version: '0'
|
200
200
|
segments:
|
201
201
|
- 0
|
202
|
-
hash:
|
202
|
+
hash: -1070647489390734539
|
203
203
|
requirements: []
|
204
204
|
rubyforge_project: ninja-model
|
205
205
|
rubygems_version: 1.8.10
|