rails_dynamic_associations 0.1.1 → 0.2.0.real
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/app/models/relation.rb +6 -16
- data/config/initializers/model_associations.rb +1 -3
- data/lib/rails_dynamic_associations/active_record/associations.rb +2 -2
- data/lib/rails_dynamic_associations/active_record/relations.rb +0 -47
- data/lib/rails_dynamic_associations/active_record.rb +0 -2
- data/lib/rails_dynamic_associations/version.rb +1 -1
- metadata +7 -8
- data/lib/rails_dynamic_associations/active_record/roles.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43cb344fd8b7ef4cd4f603260a48ad84c2fd3010
|
4
|
+
data.tar.gz: 121001d4ff410084fe0e1306735a8f78bf40e931
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce004cfa30905fbfd731e1be5e084ccb10bbadf1018966d4a45f3cda25fd93258f1b330c70ffad9871ae3cb768b6a88358c8b0caa696fbd835adc786782c4270
|
7
|
+
data.tar.gz: 46953fe6aafe0cc8b6e7b3d15890d54f786f310f27ad4bf9b8d9aefac2947c0c6475406724d68a30ea57baa00ae3cae99b2ad7c6bb62e3616f6fcc1164b93e52
|
data/app/models/relation.rb
CHANGED
@@ -22,8 +22,6 @@ class Relation < ActiveRecord::Base
|
|
22
22
|
|
23
23
|
scope method, -> (object) {
|
24
24
|
case object
|
25
|
-
when nil then
|
26
|
-
all
|
27
25
|
when Symbol then
|
28
26
|
send "#{method}_#{object}"
|
29
27
|
when Class then
|
@@ -39,20 +37,6 @@ class Relation < ActiveRecord::Base
|
|
39
37
|
of_abstract.to_abstract
|
40
38
|
}
|
41
39
|
|
42
|
-
scope :applied, -> {
|
43
|
-
where.not source_id: nil,
|
44
|
-
target_id: nil
|
45
|
-
}
|
46
|
-
|
47
|
-
scope :named, -> (*names) {
|
48
|
-
if names.present? then
|
49
|
-
where roles: { name: names.flatten.map(&:to_s) }
|
50
|
-
else
|
51
|
-
where.not roles: { name: nil }
|
52
|
-
end.
|
53
|
-
includes :role
|
54
|
-
}
|
55
|
-
|
56
40
|
def self.seed source, target, roles = nil
|
57
41
|
(roles.present? ? by_roles(roles) : [ self ]).map do |scope|
|
58
42
|
scope.create source_type: source,
|
@@ -66,6 +50,12 @@ class Relation < ActiveRecord::Base
|
|
66
50
|
end
|
67
51
|
|
68
52
|
|
53
|
+
def name= role_name
|
54
|
+
self.role = role_name &&
|
55
|
+
Role.find_or_initialize_by(name: role_name)
|
56
|
+
end
|
57
|
+
|
58
|
+
|
69
59
|
# Using polymorphic associations in combination with single table inheritance (STI) is
|
70
60
|
# a little tricky. In order for the associations to work as expected, ensure that you
|
71
61
|
# store the base model for the STI models in the type column of the polymorphic
|
@@ -1,9 +1,7 @@
|
|
1
1
|
ActiveSupport.on_load :model_class do
|
2
2
|
for type in RailsDynamicAssociations.directions.keys do
|
3
3
|
for relation in send("#{type}_relations").abstract.select(&:"#{type}_type") do
|
4
|
-
setup_relation type, relation.send("#{type}_type").constantize, relation.role
|
5
|
-
attr_accessible "#{association.to_s.singularize}_ids"
|
6
|
-
end
|
4
|
+
setup_relation type, relation.send("#{type}_type").constantize, relation.role
|
7
5
|
end
|
8
6
|
end if self != Relation and Relation.table_exists? # needed for DB migrations & schema initializing
|
9
7
|
end
|
@@ -21,8 +21,8 @@ module RailsDynamicAssociations::ActiveRecord
|
|
21
21
|
def define_relations_association type, target = self, role = nil
|
22
22
|
:"#{role ? association_name(type, target, role).to_s.singularize : type}_relations".tap do |association|
|
23
23
|
unless association.in? reflections then
|
24
|
-
has_many association,
|
25
|
-
as: (RailsDynamicAssociations.directions.keys - [
|
24
|
+
has_many association, role && -> { where role_id: role.id },
|
25
|
+
as: (RailsDynamicAssociations.directions.keys - [type]).first, class_name: 'Relation'
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -4,11 +4,6 @@ module RailsDynamicAssociations::ActiveRecord
|
|
4
4
|
module Relations
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
included do
|
8
|
-
extend ClassAndInstanceMethods
|
9
|
-
include ClassAndInstanceMethods
|
10
|
-
end
|
11
|
-
|
12
7
|
module ClassMethods
|
13
8
|
RailsDynamicAssociations.opposite_directions.each &-> (association, method) do
|
14
9
|
define_method "#{association}_relations" do
|
@@ -16,47 +11,5 @@ module RailsDynamicAssociations::ActiveRecord
|
|
16
11
|
end
|
17
12
|
end
|
18
13
|
end
|
19
|
-
|
20
|
-
module ClassAndInstanceMethods
|
21
|
-
# TODO: use keyword arguments
|
22
|
-
def find_relations args = {}
|
23
|
-
directions = RailsDynamicAssociations.directions
|
24
|
-
|
25
|
-
for association, method in directions do
|
26
|
-
args[association] = args.delete method
|
27
|
-
end
|
28
|
-
|
29
|
-
as = [ args[:as] ].flatten
|
30
|
-
|
31
|
-
if directions.keys.inject(nil) { |r, k| !r ^ !args[k] } then # direction specified
|
32
|
-
for association, method in directions do
|
33
|
-
next unless args[association]
|
34
|
-
|
35
|
-
return (as.present? ?
|
36
|
-
source_relations.named(as) :
|
37
|
-
source_relations
|
38
|
-
).send method, args[association]
|
39
|
-
end
|
40
|
-
else # both directions
|
41
|
-
directions.map do |association, method|
|
42
|
-
as.present? ?
|
43
|
-
send("#{association}_relations").named(as) :
|
44
|
-
send("#{association}_relations")
|
45
|
-
end.sum
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def relative? args = {}
|
50
|
-
find_relations(args).
|
51
|
-
present?
|
52
|
-
end
|
53
|
-
|
54
|
-
def relatives args = {}
|
55
|
-
find_relations(args).
|
56
|
-
map { |r|
|
57
|
-
([ r.source, r.target ] - [ self ]).first
|
58
|
-
}.uniq
|
59
|
-
end
|
60
|
-
end
|
61
14
|
end
|
62
15
|
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'rails_dynamic_associations/active_record/associations'
|
2
2
|
require 'rails_dynamic_associations/active_record/relations'
|
3
|
-
require 'rails_dynamic_associations/active_record/roles'
|
4
3
|
|
5
4
|
ActiveSupport.on_load :active_record do
|
6
5
|
include RailsDynamicAssociations::ActiveRecord::Associations
|
7
6
|
include RailsDynamicAssociations::ActiveRecord::Relations
|
8
|
-
include RailsDynamicAssociations::ActiveRecord::Roles
|
9
7
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_dynamic_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0.real
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Senko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.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: '
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rails_model_load_hook
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,7 +73,6 @@ files:
|
|
73
73
|
- lib/rails_dynamic_associations/active_record.rb
|
74
74
|
- lib/rails_dynamic_associations/active_record/associations.rb
|
75
75
|
- lib/rails_dynamic_associations/active_record/relations.rb
|
76
|
-
- lib/rails_dynamic_associations/active_record/roles.rb
|
77
76
|
- lib/rails_dynamic_associations/engine.rb
|
78
77
|
- lib/rails_dynamic_associations/version.rb
|
79
78
|
- lib/tasks/rails_dynamic_associations_tasks.rake
|
@@ -92,12 +91,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
91
|
version: '0'
|
93
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
93
|
requirements:
|
95
|
-
- - "
|
94
|
+
- - ">"
|
96
95
|
- !ruby/object:Gem::Version
|
97
|
-
version:
|
96
|
+
version: 1.3.1
|
98
97
|
requirements: []
|
99
98
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.4.5
|
101
100
|
signing_key:
|
102
101
|
specification_version: 4
|
103
102
|
summary: DB-driven model associations for Rails.
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
|
-
module RailsDynamicAssociations::ActiveRecord
|
4
|
-
module Roles
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
included do
|
8
|
-
extend ClassAndInstanceMethods
|
9
|
-
include ClassAndInstanceMethods
|
10
|
-
end
|
11
|
-
|
12
|
-
module ClassAndInstanceMethods
|
13
|
-
def roles to: nil, &block
|
14
|
-
if block_given? then
|
15
|
-
source_relations.instance_eval &block
|
16
|
-
else
|
17
|
-
source_relations
|
18
|
-
end.
|
19
|
-
to(to).
|
20
|
-
map(&:name).uniq
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|