detailed 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/detailed.rb +106 -4
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebb8d712163dd4d41866b153caab4487fc5ea0df
4
- data.tar.gz: 84f9477a757a7e8703372bc9e3f1a646901a9c78
3
+ metadata.gz: 2efe8712c8cebafa02154279d77781c1c8c9ae0b
4
+ data.tar.gz: 3cb6c4f19e28eb1867faa19fc94c77b708a3fa34
5
5
  SHA512:
6
- metadata.gz: f4e6e5b0f4f774ae160a589b5e45b245669faa53d6ae13be0e87f1929fb5c9c16b1047a69e2cfccbf92fb329ee8c98cc5401609a603ffb8d98f7f9f4b33fbac3
7
- data.tar.gz: 3710cb97f3fbccf88cf94021efd5d8c64fdd966186caaa866dc9073251656ec86c34bffb5fd2d02721a95105e25b1ec680c93e9187341ae80d08e25ed8943fd1
6
+ metadata.gz: 5dd9307c2b7fd553f9de88db1e44ada0942b245e302db7418ac6fc6e01fc4b4f6f70612b2ee8571ba527f6d37161ca2b04d77572c938a3a8583f417c3989a26d
7
+ data.tar.gz: 4626cdf7a0824c5df4d2223dd5de21c44fb97e9fdfbef36f929ef2cedb2d4e71aa2302778351c0b435252abedb2e4dae31252ade01d27b337f20b3c1fe613894
data/lib/detailed.rb CHANGED
@@ -1,4 +1,6 @@
1
- module Detailed
1
+ require "active_record/associations/association_scope.rb"
2
+
3
+ module Detailed
2
4
  def self.included (mod)
3
5
  class << mod
4
6
  attr_accessor :subclasses
@@ -24,13 +26,12 @@ module Detailed
24
26
  #has_one :details, class_name: "#{self.superclass.name}#{self.name}Detail", dependent: :destroy
25
27
  accepts_nested_attributes_for :"details_of_#{self.name.tableize}"
26
28
  #default_scope :include => :details
27
- default_scope -> { includes(:"details_of_#{self.name.tableize}") }
29
+ default_scope -> { includes(:"details_of_#{self.name.tableize}").joins(:"details_of_#{self.name.tableize}") }
28
30
 
29
31
  alias :details :"details_of_#{self.name.tableize}"
30
32
  alias :details= :"details_of_#{self.name.tableize}="
31
33
 
32
- def initialize *s, &blk
33
- super *s, &blk
34
+ after_initialize do
34
35
  self.details ||= Object.const_get("#{self.class.superclass.name}#{self.class.name}Detail").new
35
36
  end
36
37
 
@@ -80,4 +81,105 @@ module Detailed
80
81
  end
81
82
  end
82
83
  end
84
+
85
+ module AssociationScope
86
+ def self.included cl
87
+ cl.class_eval do
88
+ def maybe_split(table, field, reflection, scope)
89
+ if field.match /\./
90
+ table, field = field.split(/\./, 2)
91
+ table = alias_tracker.aliased_table_for(table, table_alias_for(reflection, self.reflection != reflection))
92
+
93
+ #scope = scope.merge()
94
+ end
95
+
96
+ [table, field, scope]
97
+ end
98
+
99
+ # Extend add_constraints support for "table.column" notation
100
+ #
101
+ # This gives us power to do eg.:
102
+ # has_many :frames, foreign_key: "project_frame_details.glass_id"
103
+ def add_constraints(scope)
104
+ tables = construct_tables
105
+
106
+ chain.each_with_index do |reflection, i|
107
+ table, foreign_table = tables.shift, tables.first
108
+
109
+ if reflection.source_macro == :has_and_belongs_to_many
110
+ join_table = tables.shift
111
+
112
+ scope = scope.joins(join(
113
+ join_table,
114
+ table[association_primary_key].
115
+ eq(join_table[association_foreign_key])
116
+ ))
117
+
118
+ table, foreign_table = join_table, tables.first
119
+ end
120
+
121
+ if reflection.source_macro == :belongs_to
122
+ if reflection.options[:polymorphic]
123
+ key = reflection.association_primary_key(self.klass)
124
+ else
125
+ key = reflection.association_primary_key
126
+ end
127
+
128
+ foreign_key = reflection.foreign_key
129
+ else
130
+ key = reflection.foreign_key
131
+ foreign_key = reflection.active_record_primary_key
132
+ end
133
+
134
+ # this is our addition
135
+ table, key, scope = maybe_split(table, key, reflection, scope)
136
+ foreign_table, foreign_key, scope = maybe_split(foreign_table, foreign_key, reflection, scope)
137
+ # end
138
+
139
+ if reflection == chain.last
140
+ bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key]
141
+ scope = scope.where(table[key].eq(bind_val))
142
+
143
+ if reflection.type
144
+ value = owner.class.base_class.name
145
+ bind_val = bind scope, table.table_name, reflection.type.to_s, value
146
+ scope = scope.where(table[reflection.type].eq(bind_val))
147
+ end
148
+ else
149
+ constraint = table[key].eq(foreign_table[foreign_key])
150
+
151
+ if reflection.type
152
+ type = chain[i + 1].klass.base_class.name
153
+ constraint = constraint.and(table[reflection.type].eq(type))
154
+ end
155
+
156
+ scope = scope.joins(join(foreign_table, constraint))
157
+ end
158
+
159
+ # Exclude the scope of the association itself, because that
160
+ # was already merged in the #scope method.
161
+ scope_chain[i].each do |scope_chain_item|
162
+ klass = i == 0 ? self.klass : reflection.klass
163
+ item = eval_scope(klass, scope_chain_item)
164
+
165
+ if scope_chain_item == self.reflection.scope
166
+ scope.merge! item.except(:where, :includes)
167
+ end
168
+
169
+ scope.includes! item.includes_values
170
+ scope.where_values += item.where_values
171
+ scope.order_values |= item.order_values
172
+ end
173
+ end
174
+
175
+ scope
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+
183
+ class ActiveRecord::Associations::AssociationScope
184
+ include Detailed::AssociationScope
83
185
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: detailed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Łabanowski