multi_tabular 0.2.1 → 0.2.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
  SHA1:
3
- metadata.gz: 822715ac774251bb81727fedfc6db648d0ab048e
4
- data.tar.gz: c0184f55b76aaebeb6e145d91319eb726d2ad2fc
3
+ metadata.gz: a767212ee20dcdfcb39c02cce9000122783fef9f
4
+ data.tar.gz: e44a08327174999294b032705709a636d99c7b17
5
5
  SHA512:
6
- metadata.gz: 9820d4db1f6349f1f37c51aac1a2e046cfd066a07e350e5b5821171cf5e842887ea3230f12608eb610239f45b7e0373e782ba2115c413480f530fefc07e0ebb9
7
- data.tar.gz: 1bccdf0977fff3ae271f7e86aeeb52525b98369dd22e035a9515b4cfc9346bee834cb42996eae3b5025934ccb42f968808dd65d3cdc68683f2ca1aa0f74332cf
6
+ metadata.gz: 3cd4f7d2beea812a9c1fcbd4f0ffdc2a3cd1f0a181faaae1d83017c3b3abc5d42363a0bf54c4330554c3890f4fdb194014070d7a8448bcba0a6987ce9fa08cfe
7
+ data.tar.gz: be77faffd4c049c199e6e5fad349f9e5939daa1e84aaeaf02c1d1c7c809b2b3f86d6cc5c1d76d45052112cd1972953212987c0d70ed5e3d6cc0f93c9a4e85534
@@ -8,14 +8,17 @@ module MultiTabular
8
8
 
9
9
  def self.define_associations(klass, assoc_list)
10
10
  return if assoc_list.nil?
11
- assoc_list.each do |assoc|
12
11
 
13
- assoc[:opts][:foreign_key] ||= to_foreign_key(klass) unless assoc[:opts][:through]
12
+ assoc_list.each do |assoc|
13
+ options = assoc[:opts].deep_dup
14
+ options[:foreign_key] ||= to_foreign_key(klass) unless options[:through]
15
+ options[:inverse_of] = reflection_symbol(klass) if options[:define_inverse]
16
+ options.delete(:define_inverse)
14
17
 
15
18
  if assoc[:multiple]
16
- klass.has_many assoc[:name], assoc[:opts]
19
+ klass.has_many assoc[:name], options
17
20
  else
18
- klass.has_one assoc[:name], assoc[:opts]
21
+ klass.has_one assoc[:name], options
19
22
  end
20
23
  end
21
24
  end
@@ -25,5 +28,10 @@ module MultiTabular
25
28
  def self.reflection_symbol(klass)
26
29
  klass.to_s.gsub(/::/, '_').underscore.to_sym
27
30
  end
31
+
32
+ # Check if guessed or passed in constant is a class and is a descendant of ActiveRecord::Base
33
+ def self.validate_base_class(bc)
34
+ bc.is_a?(Class) && bc < ActiveRecord::Base
35
+ end
28
36
  end
29
37
  end
@@ -31,9 +31,12 @@ module MultiTabular
31
31
  # The method tries to guess the base class itself based on the association name, but if namespaces or an arbitrary
32
32
  # association name is used, the name of the base class should be passed as string with base_class: 'BaseClassName"
33
33
  def belongs_to_mti(assoc_sym, params = {})
34
- # Check if guessed or passed in constant is a class and is a descendant of ActiveRecord::Base
35
- def validate_base_class(bc)
36
- bc.is_a?(Class) && bc < ActiveRecord::Base
34
+ def sanitized(params = {})
35
+ whitelist = [:inverse_of]
36
+
37
+ params.delete_if do |key, _|
38
+ !whitelist.include?(key)
39
+ end
37
40
  end
38
41
 
39
42
  if params.key? :base_class
@@ -43,14 +46,18 @@ module MultiTabular
43
46
  end
44
47
 
45
48
  # Raise an error if the base class doesn't saturate the conditions.
46
- unless validate_base_class base_class
49
+ unless Helpers.validate_base_class(base_class)
47
50
  fail InvalidBaseClassError.new "#{base_class} is not a valid base class."
48
51
  end
49
52
 
50
53
  base_class.descendants.each do |descendant|
51
- self.belongs_to Helpers.reflection_symbol(descendant),
52
- class_name: descendant.to_s,
53
- foreign_key: "#{Helpers.reflection_symbol(descendant)}_id"
54
+ options = {
55
+ class_name: descendant.to_s,
56
+ foreign_key: "#{Helpers.reflection_symbol(descendant)}_id"
57
+ }
58
+
59
+ self.belongs_to Helpers.reflection_symbol(descendant), options.merge(sanitized(params))
60
+
54
61
  end
55
62
 
56
63
  # Define the getter method for retrieving a referenced MTI record.
@@ -1,3 +1,3 @@
1
1
  module MultiTabular
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.2'
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_tabular
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Junger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-02 00:00:00.000000000 Z
11
+ date: 2015-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: activesupport
15
29
  requirement: !ruby/object:Gem::Requirement