associationist 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: 4c677453494e691f75964f69bcb31bf51c570ca8
4
- data.tar.gz: 32d6145dcc5dba97839d56c5a0120bbf186fdc74
3
+ metadata.gz: c13e33e027bf5c4f2179768a057f58ec9b9dbebb
4
+ data.tar.gz: 8bbf078f6efa66a7f50ba4472745b8dbfdf8cfa8
5
5
  SHA512:
6
- metadata.gz: baa71bfc317a28311ce5fc555bab2b7a370314ab3803b4551e51af1e7cab49370ffbaedfbadc2ca2271c840797ef45fe92dd17a9251516ef0148c229e60c1946
7
- data.tar.gz: 3dd0780d1b8a0928af9b73c8e2754f1200ee62219e7dd4e2bc166a621a45ec5a8b490070d3af3d7db2b5a50586d2dbaacae513253f4f57c32ee5c25aa50e17ea
6
+ metadata.gz: 69c81e0b47375b0a1cebd5eac7c294b1f582f04d65f099d99a6c7969d77131b80d2970da2c235cdc96922f57087f7a7d25e6b14530d88e59c0fe481ab85e7a95
7
+ data.tar.gz: 6701a7aba3615e53fe18045a5db0aa66e9605369a4b91818e9b890b4f8b5a3107663ecdb8ca80d14d872e0c2389700181c8911676c481247cedaa6cce05e1ced
@@ -0,0 +1,20 @@
1
+ module Associationist
2
+ module Associations
3
+ class CollectionAssociation < ::ActiveRecord::Associations::CollectionAssociation
4
+ def association_scope
5
+ reflection.config.scope_proc.call(owner)
6
+ end
7
+
8
+ case
9
+ when ActiveRecord.version < Gem::Version.new('5.2.0')
10
+ def skip_statement_cache?
11
+ true
12
+ end
13
+ when ActiveRecord.version >= Gem::Version.new('5.2.0')
14
+ def skip_statement_cache? scope
15
+ true
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,23 +1,41 @@
1
1
  module Associationist
2
2
  module Associations
3
3
  module Preloader
4
- class SingularAssociation < ActiveRecord::Associations::Preloader::SingularAssociation
5
- def associated_records_by_owner preloader
6
- reflection.config.preloader_proc.call(owners).map{|k, v| [k, [v]]}
4
+ class SingularAssociation
5
+ def initialize klass, owners, reflection, preload_scope
6
+ @owners = owners
7
+ @reflection = reflection
7
8
  end
8
9
 
10
+ def run preloader
11
+ @reflection.config.preloader_proc.call(@owners).map do |record, value|
12
+ record.association(@reflection.name).target = value
13
+ end
14
+ end
9
15
  end
10
16
 
11
17
  end
12
18
  end
13
19
 
14
20
  module ActiveRecordPreloaderPatch
15
- def preloader_for(reflection, owners, rhs_klass)
16
- config = reflection.options[:associationist]
17
- if config
18
- Associationist::Associations::Preloader::SingularAssociation
19
- else
20
- super
21
+ case
22
+ when ActiveRecord.version < Gem::Version.new('5.2.0')
23
+ def preloader_for(reflection, owners, rhs_klass)
24
+ config = reflection.options[:associationist]
25
+ if config
26
+ Associationist::Associations::Preloader::SingularAssociation
27
+ else
28
+ super
29
+ end
30
+ end
31
+ when ActiveRecord.version >= Gem::Version.new('5.2.0')
32
+ def preloader_for(reflection, owners)
33
+ config = reflection.options[:associationist]
34
+ if config
35
+ Associationist::Associations::Preloader::SingularAssociation
36
+ else
37
+ super
38
+ end
21
39
  end
22
40
  end
23
41
  end
@@ -1,12 +1,20 @@
1
1
  module Associationist
2
2
  module Associations
3
3
  class SingularAssociation < ::ActiveRecord::Associations::SingularAssociation
4
- def scope
5
- raise NotImplementedError
4
+ def association_scope
5
+ if reflection.config.scope_proc
6
+ reflection.config.scope_proc.call(owner)
7
+ else
8
+ raise NotImplementedError
9
+ end
6
10
  end
7
11
 
8
12
  def find_target
9
- reflection.config.loader_proc.call(owner)
13
+ if reflection.config.scope_proc
14
+ super
15
+ else
16
+ reflection.config.loader_proc.call(owner)
17
+ end
10
18
  end
11
19
 
12
20
  def find_target?
@@ -14,7 +22,11 @@ module Associationist
14
22
  end
15
23
 
16
24
  def klass
17
- Object
25
+ if reflection.config.scope_proc
26
+ super
27
+ else
28
+ Object
29
+ end
18
30
  end
19
31
 
20
32
  def force_reload_reader
@@ -22,6 +34,18 @@ module Associationist
22
34
  target
23
35
  end
24
36
 
37
+ case
38
+ when ActiveRecord.version < Gem::Version.new('5.2.0')
39
+ def skip_statement_cache?
40
+ true
41
+ end
42
+ when ActiveRecord.version >= Gem::Version.new('5.2.0')
43
+ def skip_statement_cache? scope
44
+ true
45
+ end
46
+ end
47
+
48
+
25
49
  end
26
50
  end
27
51
  end
@@ -0,0 +1,26 @@
1
+ module Associationist
2
+ module Builder
3
+ class CollectionAssociation < ::ActiveRecord::Associations::Builder::CollectionAssociation
4
+ def self.macro
5
+ :has_many
6
+ end
7
+
8
+ def self.valid_options(options)
9
+ super + [:associationist]
10
+ end
11
+
12
+ def self.valid_dependent_options
13
+ [:destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception]
14
+ end
15
+
16
+ def self.create_reflection(model, name, scope, options, extension = nil)
17
+ raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
18
+
19
+ validate_options(options)
20
+
21
+ scope = build_scope(scope, extension)
22
+ Reflection::CollectionReflection.new(name, scope, options, model)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -23,7 +23,21 @@ module Associationist
23
23
  end
24
24
 
25
25
  def type
26
- @config[:type]
26
+ @config[:type] || :singular
27
+ end
28
+
29
+ def scope_proc
30
+ @config[:scope]
31
+ end
32
+
33
+ def check
34
+ if @config[:loader] && @config[:scope]
35
+ raise "cannot define both loader and scope"
36
+ end
37
+
38
+ if @config[:type] && !@config[:scope]
39
+ raise "type option only effects when scope defined"
40
+ end
27
41
  end
28
42
  end
29
43
  end
@@ -6,9 +6,18 @@ module Associationist
6
6
 
7
7
  def included base
8
8
  config = Config.new @raw_config
9
+ config.check
10
+
9
11
  reflection_options = {associationist: config}
10
12
 
11
- reflection = Builder::SingularAssociation.build(base, config.name, nil, reflection_options)
13
+ case config.type
14
+ when :singular
15
+ reflection = Builder::SingularAssociation.build(base, config.name, nil, reflection_options)
16
+ when :collection
17
+ reflection = Builder::CollectionAssociation.build(base, config.name, nil, reflection_options)
18
+ else
19
+ raise "unknown type #{config.type.inspect}"
20
+ end
12
21
  ::ActiveRecord::Reflection.add_reflection base, config.name, reflection
13
22
  end
14
23
 
@@ -0,0 +1,17 @@
1
+ module Associationist
2
+ module Reflection
3
+ class CollectionReflection < ::ActiveRecord::Reflection::HasManyReflection
4
+ def association_class
5
+ Associations::CollectionAssociation
6
+ end
7
+
8
+ def check_eager_loadable!
9
+
10
+ end
11
+
12
+ def config
13
+ options[:associationist]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Associationist
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: associationist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - CicholGricenchos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-10 00:00:00.000000000 Z
11
+ date: 2019-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.0'
22
+ version: '6'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.0'
32
+ version: '6'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: pry
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -94,11 +94,14 @@ extensions: []
94
94
  extra_rdoc_files: []
95
95
  files:
96
96
  - lib/associationist.rb
97
+ - lib/associationist/associations/collection_association.rb
97
98
  - lib/associationist/associations/preloader/singular_association.rb
98
99
  - lib/associationist/associations/singular_association.rb
100
+ - lib/associationist/builder/collection_association.rb
99
101
  - lib/associationist/builder/singular_association.rb
100
102
  - lib/associationist/config.rb
101
103
  - lib/associationist/mixin.rb
104
+ - lib/associationist/reflection/collection_reflection.rb
102
105
  - lib/associationist/reflection/singular_reflection.rb
103
106
  - lib/associationist/version.rb
104
107
  homepage: https://github.com/CicholGricenchos/associationist
@@ -121,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
124
  version: '0'
122
125
  requirements: []
123
126
  rubyforge_project:
124
- rubygems_version: 2.5.2.1
127
+ rubygems_version: 2.5.2.3
125
128
  signing_key:
126
129
  specification_version: 4
127
130
  summary: ''