rails_dynamic_associations 0.3.1 → 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
2
  SHA256:
3
- metadata.gz: 8b7b48b2770c9ae7b6273ddfa0c792f074424f2e0b90973ea5683c34cdf738b6
4
- data.tar.gz: 759bb756a6b7d0ad50542bddf4310b1b55d4e6ea1fed072a72fc740b1bc17002
3
+ metadata.gz: 5f6bd7977b55a33be10430aa26ce160f7a2735ac42f787ce1e912a408a937575
4
+ data.tar.gz: fdf7eb97220f6a909791dfb2b05501b498058205901dfc0711d74a5f7a7fa62b
5
5
  SHA512:
6
- metadata.gz: 8f213dcd1c0317e892b4a8f849bdab9be75d9033ecdb401a7147f8ff8cac2862a8a45c1de2732ff841a24e2187e871bf947584679d2b5e4194fe50529bc6a3b0
7
- data.tar.gz: 69af7d101d87093b6f1a240a9b4b8890b34442c5dd7ca68285d317cdc072c00da77f2fce1297c4267d429f5a92781951061c0073f012832727fc10ce6979db0e
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.
@@ -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
@@ -19,6 +19,10 @@ module RailsDynamicAssociations::ActiveRecord
19
19
  .each { |association, method| define_recursive_methods association, method }
20
20
  end
21
21
 
22
+ def actor?
23
+ actor_models.any? { self <= _1 }
24
+ end
25
+
22
26
  private
23
27
 
24
28
  def define_relations_association type, target = self, role = nil
@@ -93,7 +97,7 @@ module RailsDynamicAssociations::ActiveRecord
93
97
  end
94
98
 
95
99
  def association_name_with_role type, target, role
96
- if target == self or target <= User then
100
+ if target == self or target.actor?
97
101
  {
98
102
  source: role.name,
99
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.1'
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.1
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: 2022-09-13 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,15 +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
- rubygems_version: 3.3.5
101
- signing_key:
100
+ rubygems_version: 3.4.0.dev
101
+ signing_key:
102
102
  specification_version: 4
103
103
  summary: DB-driven model associations for Rails.
104
104
  test_files: []