multi_tabular 0.0.4 → 0.1.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
  SHA1:
3
- metadata.gz: 9197583d5aa69ee835744afa733e81a644b1638d
4
- data.tar.gz: 6fb7cf658217c52422373fc30bb40ff48b4b574d
3
+ metadata.gz: 890cbc7eb08a2f62b8b9512157d08e32c6ed680b
4
+ data.tar.gz: ce5aa03794fd4f26ff1860715cfffe6b260b0e24
5
5
  SHA512:
6
- metadata.gz: 3ce5a4df015b72f2af3b149b0d1c88a81ebcb34c74e6b57467ec0071e54270eab3d744a93269c255ab9fe463c9f35bc527d0ae9aa9a515a51379fec1264823ac
7
- data.tar.gz: 3bd9984ebe80303ebb4e11c9aed26dd19d75f782ad9cd1fe6471759dcd8073bc852308f18fe592444ba2041274caee96fda1c7b72acd894fe2592e88c17cb5c6
6
+ metadata.gz: b6df7b38b8c532dce37b3c0f1450115ce9e5697e04e193c7deb2801bf377e06affff19e71ad8e992c7e881b6c15edbd16f275be560f9fcb48df8ee255c941118
7
+ data.tar.gz: cdce04097cd6bae09cef8f64257ba40e217ca05501843aa8ca72e81998a49bd77e25e72d6ceae864a2da893c57fff8bfff8f90682e87ccb550c5e80fe5b46c96
@@ -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
- # TODO make this work
22
- # child.table_name = child.name.underscore.sub('/', '_').pluralize
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
@@ -1,3 +1,3 @@
1
1
  module MultiTabular
2
- VERSION = '0.0.4'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_tabular
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Junger