multi_tabular 0.0.4 → 0.1.0
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 +4 -4
- data/lib/multi_tabular/super.rb +51 -11
- data/lib/multi_tabular/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 890cbc7eb08a2f62b8b9512157d08e32c6ed680b
|
|
4
|
+
data.tar.gz: ce5aa03794fd4f26ff1860715cfffe6b260b0e24
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b6df7b38b8c532dce37b3c0f1450115ce9e5697e04e193c7deb2801bf377e06affff19e71ad8e992c7e881b6c15edbd16f275be560f9fcb48df8ee255c941118
|
|
7
|
+
data.tar.gz: cdce04097cd6bae09cef8f64257ba40e217ca05501843aa8ca72e81998a49bd77e25e72d6ceae864a2da893c57fff8bfff8f90682e87ccb550c5e80fe5b46c96
|
data/lib/multi_tabular/super.rb
CHANGED
|
@@ -1,25 +1,65 @@
|
|
|
1
|
+
require 'active_support/concern'
|
|
2
|
+
|
|
1
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
|
+
|
|
2
26
|
# For superclasses of an MTI construct.
|
|
3
27
|
# These classes can provide shared logic, but are not an activerecord-model on their own,
|
|
4
28
|
# meaning they have no corresponding table in the database.
|
|
5
29
|
module Super
|
|
30
|
+
extend ActiveSupport::Concern
|
|
31
|
+
|
|
32
|
+
module ClassMethods
|
|
33
|
+
def child_has_one(assoc_name, opts = {})
|
|
34
|
+
@child_assocs_list ||= []
|
|
35
|
+
@child_assocs_list << {
|
|
36
|
+
multiple: false,
|
|
37
|
+
name: assoc_name,
|
|
38
|
+
opts: opts
|
|
39
|
+
}
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def child_has_many(assoc_name, opts = {})
|
|
43
|
+
@child_assocs_list ||= []
|
|
44
|
+
@child_assocs_list << {
|
|
45
|
+
multiple: true,
|
|
46
|
+
name: assoc_name,
|
|
47
|
+
opts: opts
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
6
52
|
def self.included(base)
|
|
7
53
|
base.abstract_class = true
|
|
8
54
|
|
|
9
55
|
def base.inherited(child)
|
|
10
|
-
# Allow converting the class name to an assumed foreign key in an associated model's database.
|
|
11
|
-
# If a different foreign should be used, you can override this method or simply declare the foreign key manually.
|
|
12
|
-
def child.to_foreign_key
|
|
13
|
-
name.underscore.sub('/', '_').singularize << '_id'
|
|
14
|
-
end
|
|
15
|
-
|
|
16
56
|
return if self.eql?(child)
|
|
17
|
-
if self.respond_to?(:inherited_associations)
|
|
18
|
-
self.inherited_associations(child)
|
|
19
|
-
end
|
|
20
57
|
|
|
21
|
-
|
|
22
|
-
|
|
58
|
+
MultiTabular::Helpers.define_associations(child, @child_assocs_list)
|
|
59
|
+
|
|
60
|
+
super
|
|
61
|
+
|
|
62
|
+
child.table_name = child.name.underscore.sub('/', '_').pluralize
|
|
23
63
|
end
|
|
24
64
|
end
|
|
25
65
|
end
|