rails_dynamic_associations 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5f47dcb824c67ac26af7a708bb2e9b09245ae0c9
4
- data.tar.gz: 0331171dd8bd2411b9725d0c65cd928719929d4f
2
+ SHA256:
3
+ metadata.gz: 5f6bd7977b55a33be10430aa26ce160f7a2735ac42f787ce1e912a408a937575
4
+ data.tar.gz: fdf7eb97220f6a909791dfb2b05501b498058205901dfc0711d74a5f7a7fa62b
5
5
  SHA512:
6
- metadata.gz: 5f859193b24729bb546465ae5a94555047e481a2f75ee89017898cc2f80947d601c61126b61f8f429a291c396fafa79733c583efb6a92e95562288be77c45b8b
7
- data.tar.gz: 3bfd71ad52e675ec4039f4312996770028bc6ca6bcabfe21908db6d2f27c6086db0044418021ef4834561c2c57de82297ac3c0a3ca2282d376558d96eb09a884
6
+ metadata.gz: ccf96404c1fcd13e3f0137f25c606dcaef612f793ed315a7ff20ac4b20320d2f45abfa0b43823d46e27d2b19a06b88cc3341448ec1b86596926a67f52216c5cc
7
+ data.tar.gz: 8e74754a0bbc56c286003378cd43b134665cd8598f6102f983fcbae31d7e44fb315181a707c263a4802da367d0906397db35a2cb5e197ae9f3c8fea7834b77cb
data/README.md CHANGED
@@ -63,7 +63,7 @@ You will get:
63
63
  book.editor_people
64
64
  ```
65
65
 
66
- #### `User` special case
66
+ #### Special cases
67
67
 
68
68
  In case you have set up relations with a `User` model you'll get a slightly different naming:
69
69
 
@@ -80,6 +80,9 @@ In case you have set up relations with a `User` model you'll get a slightly diff
80
80
  book.editors
81
81
  ```
82
82
 
83
+ The list of models to be handled this way can be set with `actor_model_names` configuration parameter.
84
+ It includes `User` by default.
85
+
83
86
  ###### TODO
84
87
 
85
88
  * Describe self-referential associations.
@@ -9,7 +9,7 @@ class Relation < ActiveRecord::Base
9
9
  references(:roles).includes :role
10
10
  }
11
11
 
12
- association_directions.shortcuts.each &-> (association, method) do
12
+ association_directions.shortcuts.each &-> ((association, method)) do
13
13
  scope "#{method}_abstract", -> (object = nil) {
14
14
  if object then
15
15
  send("#{method}_abstract").
@@ -26,15 +26,17 @@ class Relation < ActiveRecord::Base
26
26
 
27
27
  scope method, -> (object) {
28
28
  case object
29
- when nil then
30
- # all
31
- when Symbol then
29
+ when ActiveRecord::Base, nil
30
+ where association => object
31
+ when Class
32
+ where "#{association}_type" => object.ancestors.select { _1 <= object.base_class }.map(&:name)
33
+ when ActiveRecord::Relation
34
+ send(method, object.klass)
35
+ .where "#{association}_id" => object
36
+ when Symbol
32
37
  send "#{method}_#{object}"
33
- when Class then
34
- where "#{association}_type" => object.base_class.name
35
38
  else
36
- where "#{association}_type" => object.class.base_class.name,
37
- "#{association}_id" => object.id
39
+ raise ArgumentError, "no relations for #{object.inspect}"
38
40
  end
39
41
  }
40
42
  end
@@ -50,9 +52,7 @@ class Relation < ActiveRecord::Base
50
52
 
51
53
  scope :named, -> (*names) {
52
54
  case names
53
- when [[]] then # i.e. `named []`
54
- # all
55
- when [] then # i.e. `named`
55
+ when [] # i.e. `named`
56
56
  where.not role_id: nil
57
57
  else
58
58
  with_roles { named *names }
@@ -3,6 +3,6 @@
3
3
  #
4
4
  class String
5
5
  def passivize
6
- sub(/(e?d?|[eo]r|ant)$/, 'ed')
6
+ sub(/(e?d?|ing|[eo]r|ant|(t)ion)$/, '\\2ed')
7
7
  end unless method_defined? :passivize
8
8
  end
@@ -13,24 +13,31 @@ module RailsDynamicAssociations::ActiveRecord
13
13
  define_association type, target
14
14
  define_association type, target, role if role
15
15
 
16
- for association, method in association_directions.recursive do
17
- define_recursive_methods association, method if association.in? reflections and not method_defined? method
18
- end
16
+ association_directions.recursive
17
+ .select { |association, method| reflect_on_association association }
18
+ .reject { |association, method| method_defined? method }
19
+ .each { |association, method| define_recursive_methods association, method }
20
+ end
21
+
22
+ def actor?
23
+ actor_models.any? { self <= _1 }
19
24
  end
20
25
 
21
26
  private
22
27
 
23
28
  def define_relations_association type, target = self, role = nil
24
- :"#{role ? association_name(type, target, role).to_s.singularize : type}_relations".tap do |association|
25
- unless association.in? reflections then
26
- has_many association, role && -> { where role_id: role.id },
27
- as: association_directions.opposite(type), class_name: 'Relation'
28
- end
29
+ relations_association_name(type, target, role).tap do |association|
30
+ next if reflect_on_association association
31
+
32
+ has_many association, role && -> { where role_id: role.id },
33
+ as: association_directions.opposite(type), class_name: 'Relation'
29
34
  end
30
35
  end
31
36
 
32
37
  def define_association type, target = self, role = nil
33
- unless (association = association_name(type, target, role)).in? reflections then
38
+ association_name(type, target, role).tap do |association|
39
+ next if reflect_on_association association
40
+
34
41
  has_many association,
35
42
  through: define_relations_association(type, target, role),
36
43
  source: type,
@@ -77,6 +84,10 @@ module RailsDynamicAssociations::ActiveRecord
77
84
  end
78
85
  end
79
86
 
87
+ def relations_association_name type, target = self, role = nil
88
+ :"#{role ? association_name(type, target, role).to_s.singularize : type}_relations"
89
+ end
90
+
80
91
  def association_name type, target = self, role = nil
81
92
  if role then
82
93
  association_name_with_role type, target, role
@@ -86,7 +97,7 @@ module RailsDynamicAssociations::ActiveRecord
86
97
  end
87
98
 
88
99
  def association_name_with_role type, target, role
89
- if target == self or target <= User then
100
+ if target == self or target.actor?
90
101
  {
91
102
  source: role.name,
92
103
  target: "#{role.name.passivize}_#{target.name.split('::').reverse.join}",
@@ -10,7 +10,16 @@ module RailsDynamicAssociations::ActiveRecord
10
10
  end
11
11
 
12
12
  module ClassMethods
13
- RailsDynamicAssociations::Config.association_directions.opposite_shortcuts.each &-> (association, method) do
13
+ include RailsDynamicAssociations::Config
14
+
15
+ def relations
16
+ @relations ||=
17
+ association_directions.to_h do
18
+ [ _1, Relation.abstract.send(association_directions.opposite_shortcuts[_1], self) ]
19
+ end
20
+ end
21
+
22
+ association_directions.opposite_shortcuts.each &-> ((association, method)) do
14
23
  define_method "#{association}_relations" do
15
24
  Relation.send method, self
16
25
  end
@@ -58,5 +58,11 @@ module RailsDynamicAssociations
58
58
  end
59
59
  end
60
60
  end
61
+
62
+ def actor_models
63
+ @actor_models ||=
64
+ Engine.config.actor_model_names
65
+ .filter_map &:safe_constantize
66
+ end
61
67
  end
62
68
  end
@@ -16,5 +16,9 @@ module RailsDynamicAssociations
16
16
  },
17
17
  },
18
18
  }
19
+
20
+ config.actor_model_names = %w[
21
+ User
22
+ ]
19
23
  end
20
24
  end
@@ -1,3 +1,3 @@
1
1
  module RailsDynamicAssociations
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Senko
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-17 00:00:00.000000000 Z
11
+ date: 2023-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -78,11 +78,11 @@ files:
78
78
  - lib/rails_dynamic_associations/engine.rb
79
79
  - lib/rails_dynamic_associations/version.rb
80
80
  - lib/tasks/rails_dynamic_associations_tasks.rake
81
- homepage: https://github.com/softpro/rails_dynamic_associations
81
+ homepage: https://github.com/Alexander-Senko/rails_dynamic_associations
82
82
  licenses:
83
83
  - MIT
84
84
  metadata: {}
85
- post_install_message:
85
+ post_install_message:
86
86
  rdoc_options: []
87
87
  require_paths:
88
88
  - lib
@@ -90,16 +90,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
91
  - - ">="
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: '2.7'
94
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  requirements: []
100
- rubyforge_project:
101
- rubygems_version: 2.5.1
102
- signing_key:
100
+ rubygems_version: 3.4.0.dev
101
+ signing_key:
103
102
  specification_version: 4
104
103
  summary: DB-driven model associations for Rails.
105
104
  test_files: []