symmetry 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
|
!binary "U0hBMQ==":
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eeaa72663ec6aa7831238b732485f139048bd0df
|
4
|
+
data.tar.gz: b5a31fb66056e892132fdc796061b830c24e64f9
|
5
5
|
!binary "U0hBNTEy":
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51cc648a21f96c9e162c920c399c5e118f12a4e082f5ba1e3d1e4d44653a3cb756e2e5445e29c09483d8d02b2a696c92d073e6b4f882b1911e21b618786cf287
|
7
|
+
data.tar.gz: b6b8a19ab82cf76eec806912cc25a1fe833f867c7d51954f3719d4f00216746aa8650d6fcd49c7b1cc4216624f132247d6817351e0d73979afd894775db2811b
|
data/README.rdoc
CHANGED
@@ -6,4 +6,99 @@
|
|
6
6
|
{<img src="https://coveralls.io/repos/djonasson/symmetry/badge.png?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/djonasson/symmetry]
|
7
7
|
{<img src="https://badge.fury.io/rb/symmetry.png" alt="Gem Version" />}[http://badge.fury.io/rb/symmetry]
|
8
8
|
|
9
|
-
Symmetry gives you a simple way of creating symmetric has_and_belongs_to_many relationships (
|
9
|
+
Symmetry gives you a simple way of creating symmetric has_and_belongs_to_many relationships (friends, neighbors, etc.) in your Active Record models.
|
10
|
+
|
11
|
+
|
12
|
+
== Installation
|
13
|
+
|
14
|
+
Add it to your Gemfile:
|
15
|
+
|
16
|
+
gem "symmetry"
|
17
|
+
|
18
|
+
Install the migrations:
|
19
|
+
|
20
|
+
rake symmetry_engine:install:migrations db:migrate
|
21
|
+
|
22
|
+
|
23
|
+
== Usage
|
24
|
+
|
25
|
+
Define a symmetric relationship:
|
26
|
+
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
symmetric_relationship :friends
|
29
|
+
end
|
30
|
+
|
31
|
+
class Country < ActiveRecord::Base
|
32
|
+
symmetric_relationship :neighbors, polymorphic_relationship_name: :neighborships
|
33
|
+
end
|
34
|
+
|
35
|
+
Use it:
|
36
|
+
|
37
|
+
# User
|
38
|
+
|
39
|
+
irb(main)> u = User.create(name: "Daniel")
|
40
|
+
=> #<User id: 1, name: "Daniel", ...>
|
41
|
+
|
42
|
+
irb(main)> u.friends
|
43
|
+
=> []
|
44
|
+
|
45
|
+
irb(main)> u.friend_relationships
|
46
|
+
=> []
|
47
|
+
|
48
|
+
|
49
|
+
# Country
|
50
|
+
|
51
|
+
irb(main)> c = Country.create(name: "Sweden")
|
52
|
+
=> #<Country id: 1, name: "Sweden", ...>
|
53
|
+
|
54
|
+
irb(main)> c.neighbors << Country.create(name: "Norway")
|
55
|
+
=> [#<Country id: 2, name: "Norway", ...>]
|
56
|
+
|
57
|
+
irb(main)> c.neighbors
|
58
|
+
=> [#<Country id: 2, name: "Norway", ...>]
|
59
|
+
|
60
|
+
irb(main)> Country.find(2).neighbors
|
61
|
+
=> [#<Country id: 1, name: "Sweden", ...>]
|
62
|
+
|
63
|
+
irb(main)> c.neighborships
|
64
|
+
=> [#<SymmetricRelationship id: 1, owner_id: 1, owner_type: "Country", relation_id: 2, relation_type: "Country", relationship_name: "neighbors" ...>, ...]
|
65
|
+
|
66
|
+
It is possible to define multiple symmetric relationships on the same model:
|
67
|
+
|
68
|
+
class Politician
|
69
|
+
symmetric_relationship :friends
|
70
|
+
symmetric_relationship :enemies
|
71
|
+
end
|
72
|
+
|
73
|
+
irb(main)> p1 = Politician.create(name: 'A')
|
74
|
+
=> #<Politician id: 1, name: "A", ...>
|
75
|
+
|
76
|
+
irb(main)> p2 = Politician.create(name: 'B')
|
77
|
+
=> #<Politician id: 2, name: "B", ...>
|
78
|
+
|
79
|
+
irb(main)> p3 = Politician.create(name: 'C')
|
80
|
+
=> #<Politician id: 3, name: "C", ...>
|
81
|
+
|
82
|
+
irb(main)> p1.friends
|
83
|
+
=> []
|
84
|
+
|
85
|
+
irb(main)> p1.enemies
|
86
|
+
=> []
|
87
|
+
|
88
|
+
irb(main)> p1.friends << p2
|
89
|
+
=> [#<Politician id: 2, name: "B", ...>]
|
90
|
+
|
91
|
+
irb(main)> p1.enemies << p3
|
92
|
+
=> [#<Politician id: 3, name: "C", ...>]
|
93
|
+
|
94
|
+
irb(main)> p1.friends.count
|
95
|
+
=> 1
|
96
|
+
|
97
|
+
irb(main)> p1.enemies.count
|
98
|
+
=> 1
|
99
|
+
|
100
|
+
irb(main)> p2.friends.count
|
101
|
+
=> 1
|
102
|
+
|
103
|
+
irb(main)> p2.enemies.count
|
104
|
+
=> 0
|
@@ -1,28 +1,26 @@
|
|
1
1
|
class SymmetricRelationship < ActiveRecord::Base
|
2
|
-
attr_accessible :owner_id, :owner_type, :relation_id, :relation_type
|
2
|
+
attr_accessible :owner_id, :owner_type, :relation_id, :relation_type, :relationship_name
|
3
3
|
|
4
4
|
belongs_to :owner, polymorphic: true
|
5
5
|
belongs_to :relation, polymorphic: true
|
6
6
|
|
7
|
-
validates :owner_id, presence: true, uniqueness: { scope: [:owner_type, :relation_id, :relation_type] }
|
7
|
+
validates :owner_id, presence: true, uniqueness: { scope: [:owner_type, :relation_id, :relation_type, :relationship_name] }
|
8
8
|
validates :owner_type, presence: true
|
9
9
|
validates :relation_id, presence: true
|
10
10
|
validates :relation_type, presence: true
|
11
|
+
validates :relationship_name, presence: true
|
11
12
|
|
12
|
-
after_create :
|
13
|
-
after_destroy :
|
13
|
+
after_create :create_symetric_relationship
|
14
|
+
after_destroy :destroy_symetric_relationship
|
14
15
|
|
15
16
|
private
|
16
17
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
end
|
18
|
+
def create_symetric_relationship
|
19
|
+
attrs = { owner_id: relation_id, owner_type: relation_type, relation_id: owner_id, relation_type: owner_type, relationship_name: relationship_name }
|
20
|
+
self.class.create(attrs) if self.class.where(attrs).empty?
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
self.class.where(owner_id: relation_id, owner_type: relation_type, relation_id: owner_id, relation_type: owner_type).
|
25
|
-
record.delete
|
26
|
-
end
|
23
|
+
def destroy_symetric_relationship
|
24
|
+
self.class.where(owner_id: relation_id, owner_type: relation_type, relation_id: owner_id, relation_type: owner_type, relationship_name: relationship_name).delete_all
|
27
25
|
end
|
28
26
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module Symmetry
|
2
2
|
module ActiveRecord
|
3
3
|
|
4
|
-
def
|
5
|
-
options.assert_valid_keys(:
|
4
|
+
def symmetric_relationship(relationship_name, options = {})
|
5
|
+
options.assert_valid_keys(:polymorphic_relationship_name)
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
relationship_name_singular = relationship_name.to_s.singularize
|
8
|
+
polymorphic_relationship_name = options[:polymorphic_relationship_name].presence || "#{relationship_name_singular}_relationships"
|
9
9
|
|
10
|
-
attr_accessible "#{
|
11
|
-
has_many
|
12
|
-
has_many
|
10
|
+
attr_accessible "#{relationship_name_singular}_ids"
|
11
|
+
has_many polymorphic_relationship_name, class_name: "SymmetricRelationship", as: :owner, dependent: :destroy, conditions: { relationship_name: relationship_name }
|
12
|
+
has_many relationship_name, through: polymorphic_relationship_name, as: :relation, source: :relation, source_type: self.name
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
data/lib/symmetry/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: symmetry
|
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
|
- Daniel Jonasson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,18 +16,18 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 3.
|
19
|
+
version: 3.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 3.
|
26
|
+
version: 3.0.0
|
27
27
|
description: |-
|
28
28
|
Symmetry gives you a simple way of creating symmetric
|
29
|
-
has_and_belongs_to_many relationships (
|
30
|
-
|
29
|
+
has_and_belongs_to_many relationships (friends, neighbors, etc.) in your
|
30
|
+
Active Record models. More information can be found at:
|
31
31
|
https://github.com/djonasson/worldwise
|
32
32
|
email:
|
33
33
|
- daniel@guadeo.com
|
@@ -37,6 +37,7 @@ extra_rdoc_files: []
|
|
37
37
|
files:
|
38
38
|
- app/models/symmetric_relationship.rb
|
39
39
|
- config/routes.rb
|
40
|
+
- db/migrate/20130323091106_add_relationship_name_to_symmetric_relationships.rb
|
40
41
|
- db/migrate/20130201161328_create_symmetric_relationships.rb
|
41
42
|
- lib/tasks/testing_tasks.rake
|
42
43
|
- lib/symmetry/active_record.rb
|