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.
@@ -14,6 +14,7 @@ require 'active_record/aggregations'
14
14
 
15
15
  module NinjaModel
16
16
  class Base
17
+ include Callbacks
17
18
  include AttributeMethods
18
19
  include Identity
19
20
  include Persistence
@@ -1,5 +1,9 @@
1
1
  module NinjaModel
2
- class Base
3
- extend ActiveModel::Callbacks
2
+ module Callbacks
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ extend ActiveModel::Callbacks
7
+ end
4
8
  end
5
9
  end
@@ -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
- def save(*)
9
- run_callbacks :save do
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
- def create
17
- run_callbacks :create do
18
- if self.class.adapter.create(self)
19
- @persisted = true
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
- def update
26
- run_callbacks :update do
27
- self.class.adapter.update(self)
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
- def new_record?
32
- !@persisted
33
- end
27
+ def update
28
+ run_callbacks :update do
29
+ self.class.adapter.update(self)
30
+ end
31
+ end
34
32
 
35
- def destroyed?
36
- @destroyed
37
- end
33
+ def new_record?
34
+ !@persisted
35
+ end
38
36
 
39
- def persisted?
40
- @persisted && !destroyed?
41
- end
37
+ def destroyed?
38
+ @destroyed
39
+ end
40
+
41
+ def persisted?
42
+ @persisted && !destroyed?
43
+ end
42
44
 
43
- def destroy
44
- run_callbacks :destroy do
45
- if self.class.adapter.destroy(self)
46
- @destroyed = true
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
- def reload
53
- self.class.adapter.reload(self)
54
- end
54
+ def reload
55
+ self.class.adapter.reload(self)
56
+ end
55
57
 
56
- def update_attributes(attributes)
57
- self.attributes = attributes
58
- save
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
- run_callbacks :validation do
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
@@ -1,3 +1,3 @@
1
1
  module NinjaModel
2
- VERSION = "0.7.3"
2
+ VERSION = "0.8.0"
3
3
  end
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.7.3
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-01-26 00:00:00.000000000Z
12
+ date: 2012-02-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &13910660 !ruby/object:Gem::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: *13910660
24
+ version_requirements: *9709260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &13909960 !ruby/object:Gem::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: *13909960
35
+ version_requirements: *9708520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &13909200 !ruby/object:Gem::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: *13909200
46
+ version_requirements: *9705000
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mocha
49
- requirement: &13908580 !ruby/object:Gem::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: *13908580
57
+ version_requirements: *9704320
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: nokogiri
60
- requirement: &13907940 !ruby/object:Gem::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: *13907940
68
+ version_requirements: *9703640
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
- requirement: &13907120 !ruby/object:Gem::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: *13907120
79
+ version_requirements: *9702980
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: libnotify
82
- requirement: &13906300 !ruby/object:Gem::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: *13906300
90
+ version_requirements: *9702340
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: yard
93
- requirement: &13905640 !ruby/object:Gem::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: *13905640
101
+ version_requirements: *9701740
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: redcarpet
104
- requirement: &13899780 !ruby/object:Gem::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: *13899780
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: 3063035657045402617
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: 3063035657045402617
202
+ hash: -1070647489390734539
203
203
  requirements: []
204
204
  rubyforge_project: ninja-model
205
205
  rubygems_version: 1.8.10