activemodel-associations 0.1.0 → 0.1.1
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/README.md +7 -0
- data/lib/active_model/associations.rb +2 -2
- data/lib/active_model/associations/active_record_reflection.rb +3 -3
- data/lib/active_model/associations/association_scope_extension.rb +1 -1
- data/lib/active_model/associations/override_methods.rb +1 -1
- data/lib/active_model/associations/version.rb +1 -1
- data/spec/lib/active_model/association_spec.rb +26 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05b48e5387b4b96ce1a5b67a9d40dc2d9f3b2ccc
|
4
|
+
data.tar.gz: 74414a8659cc8a1f854f4f95fa7a2eceb5b8045d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eab7092d4d6a60a114fdd5a88d9572cf83d621afc300f802e775f8ac60078830d0b581738bd2200560460e24f7b29e3697964b4468d20312e6aae85e87f3fe06
|
7
|
+
data.tar.gz: f5d09635fd3d18e37c18167137ba0528002d8f230e76e8380912b3940261ed12c78ef77df90e72068025a463d944897077cd30cc55af21307a55dd20c0c8909a
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Activemodel::Associations
|
2
|
+
[](http://badge.fury.io/rb/activemodel-associations)
|
2
3
|
[](https://travis-ci.org/joker1007/activemodel-associations)
|
3
4
|
[](https://coveralls.io/r/joker1007/activemodel-associations)
|
5
|
+
[](https://codeclimate.com/github/joker1007/activemodel-associations)
|
4
6
|
|
5
7
|
`has_many` and `belongs_to` macro for Plain Ruby Object.
|
6
8
|
|
@@ -18,6 +20,11 @@ Or install it yourself as:
|
|
18
20
|
|
19
21
|
$ gem install activemodel-associations
|
20
22
|
|
23
|
+
## Support
|
24
|
+
- activerecord-4.0.x
|
25
|
+
- activerecord-4.1.x
|
26
|
+
- activerecord-4.2.x
|
27
|
+
|
21
28
|
## Usage
|
22
29
|
|
23
30
|
### belongs\_to
|
@@ -18,7 +18,7 @@ module ActiveModel
|
|
18
18
|
# define association like ActiveRecord
|
19
19
|
def belongs_to(name, scope = nil, options = {})
|
20
20
|
reflection = ActiveRecord::Associations::Builder::BelongsTo.build(self, name, scope, options)
|
21
|
-
if ActiveRecord.version
|
21
|
+
if ActiveRecord.version >= Gem::Version.new("4.1")
|
22
22
|
ActiveRecord::Reflection.add_reflection self, name, reflection
|
23
23
|
end
|
24
24
|
end
|
@@ -32,7 +32,7 @@ module ActiveModel
|
|
32
32
|
end
|
33
33
|
|
34
34
|
reflection = ActiveRecord::Associations::Builder::HasManyForActiveModel.build(self, name, scope, options, &extension)
|
35
|
-
if ActiveRecord.version
|
35
|
+
if ActiveRecord.version >= Gem::Version.new("4.1")
|
36
36
|
ActiveRecord::Reflection.add_reflection self, name, reflection
|
37
37
|
end
|
38
38
|
|
@@ -3,7 +3,7 @@ module ActiveModel::Associations
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
if ActiveRecord.version
|
6
|
+
if ActiveRecord.version >= Gem::Version.new("4.1.2")
|
7
7
|
class_attribute :_reflections
|
8
8
|
self._reflections = ActiveSupport::HashWithIndifferentAccess.new
|
9
9
|
else
|
@@ -13,7 +13,7 @@ module ActiveModel::Associations
|
|
13
13
|
end
|
14
14
|
|
15
15
|
module ClassMethods
|
16
|
-
if ActiveRecord.version
|
16
|
+
if ActiveRecord.version < Gem::Version.new("4.1")
|
17
17
|
def create_reflection(macro, name, scope, options, active_record)
|
18
18
|
case macro
|
19
19
|
when :has_many, :belongs_to
|
@@ -27,7 +27,7 @@ module ActiveModel::Associations
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def reflect_on_association(association)
|
30
|
-
if ActiveRecord.version
|
30
|
+
if ActiveRecord.version >= Gem::Version.new("4.1.2")
|
31
31
|
_reflections[association.to_s]
|
32
32
|
else
|
33
33
|
reflections[association]
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module ActiveModel::Associations
|
2
2
|
module AssociationScopeExtension
|
3
|
-
if ActiveRecord.version
|
3
|
+
if ActiveRecord.version < Gem::Version.new("4.1")
|
4
4
|
def add_constraints(scope)
|
5
5
|
if reflection.options[:active_model]
|
6
6
|
target_ids = reflection.options[:target_ids]
|
@@ -11,7 +11,7 @@ module ActiveModel::Associations
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
alias :generated_feature_methods :generated_association_methods \
|
14
|
-
if ActiveRecord.version
|
14
|
+
if ActiveRecord.version < Gem::Version.new("4.1")
|
15
15
|
|
16
16
|
# override
|
17
17
|
def dangerous_attribute_method?(name)
|
@@ -19,6 +19,30 @@ describe ActiveModel::Associations do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
class PostBase
|
23
|
+
extend ActiveModel::Naming
|
24
|
+
extend ActiveModel::Callbacks
|
25
|
+
include ActiveModel::Model
|
26
|
+
include ActiveModel::Associations
|
27
|
+
include ActiveModel::Conversion
|
28
|
+
include ActiveModel::Dirty
|
29
|
+
include ActiveModel::Validations
|
30
|
+
|
31
|
+
attr_accessor :user_id
|
32
|
+
|
33
|
+
def [](attr)
|
34
|
+
self.send(attr)
|
35
|
+
end
|
36
|
+
|
37
|
+
def []=(attr, value)
|
38
|
+
self.send("#{attr}=", value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Post < PostBase
|
43
|
+
belongs_to :user
|
44
|
+
end
|
45
|
+
|
22
46
|
it "extends constructor" do
|
23
47
|
comment = Comment.new(body: "foo")
|
24
48
|
expect(comment.body).to eq "foo"
|
@@ -41,9 +65,11 @@ describe ActiveModel::Associations do
|
|
41
65
|
describe "defined accessor" do
|
42
66
|
let(:user) { User.create(name: "joker1007") }
|
43
67
|
let(:comment) { Comment.new(user_id: user.id) }
|
68
|
+
let(:post) { Post.new(user_id: user.id) }
|
44
69
|
|
45
70
|
it "defined accessor loads target ActiveRecord instance" do
|
46
71
|
expect(comment.user).to eq user
|
72
|
+
expect(post.user).to eq user
|
47
73
|
end
|
48
74
|
|
49
75
|
it "receives target ActiveRecord instance, and set foreign_key attributes" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel-associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joker1007
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -247,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
247
247
|
version: '0'
|
248
248
|
requirements: []
|
249
249
|
rubyforge_project:
|
250
|
-
rubygems_version: 2.4.
|
250
|
+
rubygems_version: 2.4.6
|
251
251
|
signing_key:
|
252
252
|
specification_version: 4
|
253
253
|
summary: ActiveRecord Association Helper for PORO (Plain Old Ruby Object)
|