activemodel-associations 0.0.1 → 0.0.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d9c6e4e10b3abbddc32adb581c7986a5cb62efc
|
4
|
+
data.tar.gz: 5dc2e3d9acb6351c8d041f252e43454d221bd3e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdb223dd6a00f9571869a04cde42dbaabe997fefd9a02fc9bb9dac6ceb964b48f36ef84f5b8c41f259083ff7ddc315875013e01278cb6038961ce63919669e09
|
7
|
+
data.tar.gz: 1b75beaef925ef9149e7ebcefaf167eb094263a2b552224e56972d5ea6fafa331ab293d0960679378bbf71ffcf3813b910b19fe93bacd07f31749c615a48db8d
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Activemodel::
|
1
|
+
# Activemodel::Associations
|
2
2
|
[](https://travis-ci.org/joker1007/activemodel-associations)
|
3
3
|
[](https://coveralls.io/r/joker1007/activemodel-associations)
|
4
4
|
|
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = ActiveModel::Associations::VERSION
|
9
9
|
spec.authors = ["joker1007"]
|
10
10
|
spec.email = ["kakyoin.hierophant@gmail.com"]
|
11
|
-
spec.summary = %q{ActiveRecord Association Helper for PORO}
|
12
|
-
spec.description = %q{ActiveRecord Association Helper for PORO}
|
13
|
-
spec.homepage = ""
|
11
|
+
spec.summary = %q{ActiveRecord Association Helper for PORO (Plain Old Ruby Object)}
|
12
|
+
spec.description = %q{ActiveRecord Association Helper for PORO (Plain Old Ruby Object)}
|
13
|
+
spec.homepage = "https://github.com/joker1007/activemodel-associations"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -12,20 +12,43 @@ module ActiveRecord::Associations
|
|
12
12
|
|
13
13
|
# full replace simplely
|
14
14
|
def replace(other_array)
|
15
|
+
original_target = load_target.dup
|
15
16
|
other_array.each { |val| raise_on_type_mismatch!(val) }
|
16
17
|
target_ids = reflection.options[:target_ids]
|
17
18
|
owner[target_ids] = other_array.map(&:id)
|
19
|
+
|
20
|
+
old_records = original_target - other_array
|
21
|
+
old_records.each do |record|
|
22
|
+
@target.delete(record)
|
23
|
+
end
|
24
|
+
|
25
|
+
other_array.each do |record|
|
26
|
+
if index = @target.index(record)
|
27
|
+
@target[index] = record
|
28
|
+
else
|
29
|
+
@target << record
|
30
|
+
end
|
31
|
+
end
|
18
32
|
end
|
19
33
|
|
20
|
-
# no need
|
34
|
+
# no need transaction
|
21
35
|
def concat(*records)
|
36
|
+
load_target
|
22
37
|
flatten_records = records.flatten
|
23
38
|
flatten_records.each { |val| raise_on_type_mismatch!(val) }
|
24
39
|
target_ids = reflection.options[:target_ids]
|
25
40
|
owner[target_ids] ||= []
|
26
41
|
owner[target_ids].concat(flatten_records.map(&:id))
|
27
|
-
|
28
|
-
|
42
|
+
|
43
|
+
flatten_records.each do |record|
|
44
|
+
if index = @target.index(record)
|
45
|
+
@target[index] = record
|
46
|
+
else
|
47
|
+
@target << record
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
target
|
29
52
|
end
|
30
53
|
end
|
31
54
|
end
|
@@ -132,8 +132,8 @@ describe ActiveModel::Associations do
|
|
132
132
|
end
|
133
133
|
|
134
134
|
describe "defined accessor" do
|
135
|
-
let(:user1) { User.create(name: "joker1007") }
|
136
|
-
let(:user2) { User.create(name: "kakyoin") }
|
135
|
+
let!(:user1) { User.create(name: "joker1007") }
|
136
|
+
let!(:user2) { User.create(name: "kakyoin") }
|
137
137
|
let(:group) { Group.new(user_ids: [user1.id, user2.id]) }
|
138
138
|
|
139
139
|
it "returns ActiveRecord CollectionProxy of target class" do
|
@@ -148,6 +148,21 @@ describe ActiveModel::Associations do
|
|
148
148
|
expect(group.users).to eq [user1, user2]
|
149
149
|
end
|
150
150
|
|
151
|
+
it "replace target, and set target_ids attributes" do
|
152
|
+
expect(group.users).to eq [user1, user2]
|
153
|
+
group.users = [user1]
|
154
|
+
expect(group.users).to eq [user1]
|
155
|
+
group.users = []
|
156
|
+
expect(group.users).to be_empty
|
157
|
+
end
|
158
|
+
|
159
|
+
it "receives target ActiveRecord CollectionProxy, and set target_ids attributes" do
|
160
|
+
group = Group.new
|
161
|
+
expect(group.users).to be_empty
|
162
|
+
group.users = User.all
|
163
|
+
expect(group.users).to eq [user1, user2]
|
164
|
+
end
|
165
|
+
|
151
166
|
it "can replace having association" do
|
152
167
|
user3 = User.create(name: "jotaro")
|
153
168
|
group = Group.new
|
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joker1007
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -150,7 +150,7 @@ dependencies:
|
|
150
150
|
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
|
-
description: ActiveRecord Association Helper for PORO
|
153
|
+
description: ActiveRecord Association Helper for PORO (Plain Old Ruby Object)
|
154
154
|
email:
|
155
155
|
- kakyoin.hierophant@gmail.com
|
156
156
|
executables: []
|
@@ -182,7 +182,7 @@ files:
|
|
182
182
|
- spec/db/migrate/10_create_users.rb
|
183
183
|
- spec/lib/active_model/association_spec.rb
|
184
184
|
- spec/spec_helper.rb
|
185
|
-
homepage:
|
185
|
+
homepage: https://github.com/joker1007/activemodel-associations
|
186
186
|
licenses:
|
187
187
|
- MIT
|
188
188
|
metadata: {}
|
@@ -205,7 +205,7 @@ rubyforge_project:
|
|
205
205
|
rubygems_version: 2.2.2
|
206
206
|
signing_key:
|
207
207
|
specification_version: 4
|
208
|
-
summary: ActiveRecord Association Helper for PORO
|
208
|
+
summary: ActiveRecord Association Helper for PORO (Plain Old Ruby Object)
|
209
209
|
test_files:
|
210
210
|
- spec/db/migrate/10_create_users.rb
|
211
211
|
- spec/lib/active_model/association_spec.rb
|