multi_tabular 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/multi_tabular/references.rb +10 -14
- data/lib/multi_tabular/super.rb +0 -22
- data/lib/multi_tabular/version.rb +1 -1
- data/lib/multi_tabular.rb +1 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcc9947c6c1012e9ce4323ea9a229c17f11297d2
|
4
|
+
data.tar.gz: c1ef6779f525bea94bf34297a0c44ddec2f57ed9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 242eff27bd59d336e151e1d32bb4b0583a00d8a717e1d8db83638b7d88dae0a8021113f5c52b5ab8e1d10c9dd7c743403af4fff3c453fe700863bdabe5917966
|
7
|
+
data.tar.gz: 87902e0b64cdc70963ba7c1c59e58e42c86fbb4947960640a84681fcbe21d29dd1ae2bad34eae9237991742a5e1d383f9724765c4202ba7a17b61ce6edc7e505
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
1
3
|
module MultiTabular
|
2
4
|
# For classes that reference an MTI class
|
3
5
|
module References
|
@@ -5,7 +7,7 @@ module MultiTabular
|
|
5
7
|
|
6
8
|
included do
|
7
9
|
def reflection_assignment_method(klass)
|
8
|
-
self.class.reflect_on_association(reflection_symbol(klass)).name.to_s + '='
|
10
|
+
self.class.reflect_on_association(Helpers.reflection_symbol(klass)).name.to_s + '='
|
9
11
|
end
|
10
12
|
|
11
13
|
def reflection_assignment_symbol(sym)
|
@@ -16,16 +18,10 @@ module MultiTabular
|
|
16
18
|
# list of defined associations within the current class
|
17
19
|
def association_methods(mti_base_class)
|
18
20
|
(mti_base_class.descendants << mti_base_class).map{|s|
|
19
|
-
assoc = self.class.reflect_on_association(reflection_symbol s)
|
21
|
+
assoc = self.class.reflect_on_association(Helpers.reflection_symbol s)
|
20
22
|
assoc ? assoc.name : nil
|
21
23
|
}.compact
|
22
24
|
end
|
23
|
-
|
24
|
-
# for a given class, returns the appropriate symbol
|
25
|
-
# to pass to the ActiveRecord method reflect_on_association
|
26
|
-
def reflection_symbol(klass)
|
27
|
-
klass.to_s.sub('::', '_').underscore.to_sym
|
28
|
-
end
|
29
25
|
end
|
30
26
|
|
31
27
|
module ClassMethods
|
@@ -40,12 +36,6 @@ module MultiTabular
|
|
40
36
|
bc.is_a?(Class) && bc < ActiveRecord::Base
|
41
37
|
end
|
42
38
|
|
43
|
-
# for a given class, returns the appropriate symbol
|
44
|
-
# to pass to the ActiveRecord method reflect_on_association
|
45
|
-
def reflection_symbol(klass)
|
46
|
-
klass.to_s.sub('::', '_').underscore.to_sym
|
47
|
-
end
|
48
|
-
|
49
39
|
if params.key? :base_class
|
50
40
|
base_class = Module.const_get params[:base_class]
|
51
41
|
else
|
@@ -57,6 +47,12 @@ module MultiTabular
|
|
57
47
|
fail InvalidBaseClassError.new "#{base_class} is not a valid base class."
|
58
48
|
end
|
59
49
|
|
50
|
+
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
|
+
end
|
55
|
+
|
60
56
|
# Define the getter method for retrieving a referenced MTI record.
|
61
57
|
# Each association method for the base class is invoked and first that returns a result will be used.
|
62
58
|
define_method(assoc_sym.to_s) do
|
data/lib/multi_tabular/super.rb
CHANGED
@@ -1,28 +1,6 @@
|
|
1
1
|
require 'active_support/concern'
|
2
2
|
|
3
3
|
module MultiTabular
|
4
|
-
module Helpers
|
5
|
-
# Allow converting the class name to an assumed foreign key in an associated model's database.
|
6
|
-
# If a different foreign should be used, you can override this method or simply declare the foreign key manually.
|
7
|
-
def self.to_foreign_key(klass)
|
8
|
-
klass.name.underscore.sub('/', '_').singularize << '_id'
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.define_associations(klass, assoc_list)
|
12
|
-
return if assoc_list.nil?
|
13
|
-
assoc_list.each do |assoc|
|
14
|
-
|
15
|
-
assoc[:opts][:foreign_key] ||= to_foreign_key(klass) unless assoc[:opts][:through]
|
16
|
-
|
17
|
-
if assoc[:multiple]
|
18
|
-
klass.has_many assoc[:name], assoc[:opts]
|
19
|
-
else
|
20
|
-
klass.has_one assoc[:name], assoc[:opts]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
4
|
# For superclasses of an MTI construct.
|
27
5
|
# These classes can provide shared logic, but are not an activerecord-model on their own,
|
28
6
|
# meaning they have no corresponding table in the database.
|
data/lib/multi_tabular.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_tabular
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-02
|
11
|
+
date: 2015-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -28,14 +28,14 @@ dependencies:
|
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '4.1'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '4.1'
|
41
41
|
description: |-
|