detailed 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/detailed.rb +83 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebb8d712163dd4d41866b153caab4487fc5ea0df
4
+ data.tar.gz: 84f9477a757a7e8703372bc9e3f1a646901a9c78
5
+ SHA512:
6
+ metadata.gz: f4e6e5b0f4f774ae160a589b5e45b245669faa53d6ae13be0e87f1929fb5c9c16b1047a69e2cfccbf92fb329ee8c98cc5401609a603ffb8d98f7f9f4b33fbac3
7
+ data.tar.gz: 3710cb97f3fbccf88cf94021efd5d8c64fdd966186caaa866dc9073251656ec86c34bffb5fd2d02721a95105e25b1ec680c93e9187341ae80d08e25ed8943fd1
data/lib/detailed.rb ADDED
@@ -0,0 +1,83 @@
1
+ module Detailed
2
+ def self.included (mod)
3
+ class << mod
4
+ attr_accessor :subclasses
5
+
6
+ def add_subclass(sc)
7
+ @subclasses ||= []
8
+ @subclasses << sc
9
+
10
+ class_eval do
11
+ has_one :"details_of_#{sc.name.tableize}", class_name: "#{self.name}#{sc.name}Detail", foreign_key: "#{sc.name.tableize.singularize}_id" #, inverse_of: "#{sc.name.tableize}"
12
+ end
13
+ end
14
+
15
+ def all_with_details
16
+ @subclasses ||= []
17
+ @subclasses.inject(self.all) { |a,b| a.includes(:"details_of_#{b.name.tableize}") }
18
+ end
19
+
20
+ def request_details
21
+ self.superclass.add_subclass(self)
22
+
23
+ class_eval do
24
+ #has_one :details, class_name: "#{self.superclass.name}#{self.name}Detail", dependent: :destroy
25
+ accepts_nested_attributes_for :"details_of_#{self.name.tableize}"
26
+ #default_scope :include => :details
27
+ default_scope -> { includes(:"details_of_#{self.name.tableize}") }
28
+
29
+ alias :details :"details_of_#{self.name.tableize}"
30
+ alias :details= :"details_of_#{self.name.tableize}="
31
+
32
+ def initialize *s, &blk
33
+ super *s, &blk
34
+ self.details ||= Object.const_get("#{self.class.superclass.name}#{self.class.name}Detail").new
35
+ end
36
+
37
+ alias :__old_method_missing :method_missing
38
+ alias :__old_respond_to? :respond_to?
39
+ alias :__old_methods :methods
40
+ alias :__old_write_attribute :write_attribute
41
+ alias :__old_read_attribute :read_attribute
42
+ alias :__old_query_attribute :query_attribute
43
+
44
+ def query_attribute a
45
+ __old_query_attribute(a) or (details and details.send(:query_attribute, a))
46
+ end
47
+
48
+ def read_attribute a
49
+ __old_read_attribute(a) or (details and details.send(:read_attribute, a))
50
+ end
51
+
52
+ def write_attribute a, b
53
+ __old_write_attribute a, b
54
+ rescue ActiveModel::MissingAttributeError => e
55
+ begin
56
+ details.send :write_attribute, a, b
57
+ rescue ActiveModel::MissingAttributeError
58
+ raise e
59
+ end
60
+ end
61
+
62
+ def method_missing a, *b
63
+ __old_method_missing a, *b
64
+ rescue NoMethodError, NameError => e
65
+ begin
66
+ details.send a, *b
67
+ rescue NoMethodError, NameError
68
+ raise e
69
+ end
70
+ end
71
+
72
+ def respond_to? *a
73
+ __old_respond_to?(*a) or (details ? details.respond_to?(*a) : false)
74
+ end
75
+
76
+ def methods *a
77
+ __old_methods(*a) | (details ? details.methods(*a) : [])
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: detailed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Łabanowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An ActiveRecord module giving you the power of multiple table inheritance
14
+ while still resorting to an API resembling single table inheritance
15
+ email: marcin@6irc.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/detailed.rb
21
+ homepage: http://github.com/czaks/detailed
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Compromise between single and multiple table inheritance
45
+ test_files: []