active_record-framing 0.1.0.pre.8 → 0.1.0.pre.9

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
  SHA256:
3
- metadata.gz: ab0c33dbeba1bf12e4f49d8e51fada71519e03993edc8a15821b88bbabc37061
4
- data.tar.gz: 74750a483858cd179c5051db28d3bc272c9440bacf51fc8c50cc0f43c48806ba
3
+ metadata.gz: 1c45ff57cff6a36c65d09cc5c6e33a8d394c0f35eb6097f64213e458d41d4509
4
+ data.tar.gz: 8bd322afcc0c2406367c43354baa15f60291063fce86a91c8bb24dfaeaefd5f2
5
5
  SHA512:
6
- metadata.gz: ae7e047fefd79d7bb836b04b69bcc7378b6fae4057fbfe6299cf0ec3fd37904e9b9e034ebaf86107f628c8b2efb319946d00bbc4a4111a3fb7eff257db577300
7
- data.tar.gz: 2b09eaecb22f6d66fe20b9215589f0f816f0538f53e5b8be833fd09c1e68e4f58e9168d74d81774de89f4ac0e8528bc95c1b781bb06d84a28b9f32ed180a85d5
6
+ metadata.gz: ebfb3761c9c471e80b2b7e77d235f1b74c85dcbb185f470da6f6f8732489802ababfb31ee039680781afdfd5d353d7417e8e0e62e2408bc1010044a1c540cfeb
7
+ data.tar.gz: faba924aef8072dc5c98a7bfc6e67decbadab3bc7eaa75b162c9de4a4410716bb89513124c0a27df1d8dc0a55c88507aa3883b5c01207330d4528bf818f55e56
@@ -10,6 +10,9 @@ module ActiveRecord
10
10
  def self.prepended(subclass)
11
11
  subclass.singleton_class.class_eval do
12
12
  alias_method :unframed_all, :all
13
+
14
+ # Subclasses need to be able to call its parent's
15
+ protected :relation
13
16
  end
14
17
 
15
18
  subclass.include Default
@@ -57,6 +57,22 @@ module ActiveRecord
57
57
  self.ignore_default_frame = false
58
58
  end
59
59
 
60
+ protected
61
+
62
+ def build_frame(frames, arel_table, base_rel = nil, &block)
63
+ cte_relation = frames.inject(base_rel.clone) do |collection, frame|
64
+ frame = frame.respond_to?(:to_proc) ? frame : frame.method(:call)
65
+ # Exec the frame, or grab the default_frame (by calling relation)
66
+ frame_relation = base_rel.instance_exec(&frame)
67
+ collection.merge!(frame_relation || base_rel)
68
+ end
69
+
70
+ relation.frame!(Arel::Nodes::As.new(arel_table, cte_relation.arel)).tap do |rel|
71
+ extension = Module.new(&block) if block_given?
72
+ rel.extending!(extension) if extension
73
+ end
74
+ end
75
+
60
76
  private
61
77
 
62
78
  def ignore_default_frame=(ignore)
@@ -135,14 +151,7 @@ module ActiveRecord
135
151
  elsif default_frames.any?
136
152
  ignore_default_frame do
137
153
  cte_table = Arel::Table.new(table_name)
138
-
139
- cte_relation = default_frames.inject(relation) do |default_frame, frame|
140
- frame = frame.respond_to?(:to_proc) ? frame : frame.method(:call)
141
- default_frame.merge!(relation.instance_exec(&frame))
142
- end
143
-
144
- base_rel ||= relation
145
- base_rel.frame!(Arel::Nodes::As.new(cte_table, cte_relation.arel))# if cte_relation
154
+ build_frame(default_frames, cte_table, base_rel || relation)
146
155
  end
147
156
  end
148
157
  end
@@ -18,20 +18,20 @@ module ActiveRecord
18
18
  # You can define a frame that applies to all finders using
19
19
  # {default_frame}[rdoc-ref:Framing::Default::ClassMethods#default_frame].
20
20
  def all
21
- rel = unframed_all
22
21
  if (current_frame = self.current_frame)
23
22
  if self == current_frame.klass
24
23
  current_frame.clone
25
24
  else
26
- rel.merge!(current_frame)
25
+ unframed_all.merge!(current_frame)
27
26
  end
28
27
  else
29
- default_framed(rel)
28
+ # default_framed.merge!(unframed_all)
29
+ unframed_all.merge!(default_framed)
30
30
  end
31
31
  end
32
32
 
33
33
  # def default_framed(frame = relation) # :nodoc:
34
- def default_framed(frame = nil) # :nodoc:
34
+ def default_framed(frame = relation) # :nodoc:
35
35
  !ignore_default_frame? && !ActiveRecord::Framing.disabled? && build_default_frame(frame) || frame
36
36
  end
37
37
 
@@ -146,9 +146,6 @@ module ActiveRecord
146
146
 
147
147
  arel_tn = "#{frame_name}/#{self.table_name}"
148
148
 
149
- the_frame = body.respond_to?(:to_proc) ? body : body.method(:call)
150
- cte_relation = relation.merge!(relation.instance_exec(&the_frame) || relation)
151
-
152
149
  new_class = self.const_set constant, (Class.new(self) do |klass|
153
150
  klass.abstract_class = true
154
151
  klass.table_name = superclass.table_name
@@ -157,7 +154,13 @@ module ActiveRecord
157
154
  superclass.send(:discriminate_class_for_record, record)
158
155
  end
159
156
 
160
- klass.default_frames = []
157
+ klass.default_frames = [body]
158
+
159
+ @current_frame_extension = block
160
+
161
+ def klass.current_frame
162
+ build_frame(default_frames, arel_table, superclass.relation, &@current_frame_extension)
163
+ end
161
164
 
162
165
  @arel_table = superclass.arel_table.dup.tap do |at|
163
166
  at.name = arel_tn
@@ -165,8 +168,6 @@ module ActiveRecord
165
168
 
166
169
  end)
167
170
 
168
- new_class.current_frame = new_class.build_frame(cte_relation, &block)
169
-
170
171
  if dangerous_class_const?(constant)
171
172
  raise ArgumentError, "You tried to define a frame named \"#{constant}\" " \
172
173
  "on the model \"#{self.constant}\", but Active Record already defined " \
@@ -175,13 +176,6 @@ module ActiveRecord
175
176
 
176
177
  end
177
178
 
178
- def build_frame(frame, &block)
179
- extension = Module.new(&block) if block
180
- relation.frame!(Arel::Nodes::As.new(arel_table, frame.arel)).tap do |rel|
181
- rel.extending!(extension) if extension
182
- end
183
- end
184
-
185
179
  private
186
180
 
187
181
  def valid_frame_name?(name)
@@ -31,17 +31,17 @@ module ActiveRecord
31
31
  value = value.all if (value.is_a?(Class) && value < ::ActiveRecord::Base)
32
32
 
33
33
  cte = \
34
- case value
35
- when ::ActiveRecord::Relation
36
- # {value.table.name => value.frames_values}
37
- value.frames_values
38
- when Arel::Nodes::As
39
- {value.left.name => value}
40
- when String
41
- {arel_table.name => value}
42
- else
43
- {}
44
- end
34
+ case value
35
+ when ::ActiveRecord::Relation
36
+ # {value.table.name => value.frames_values}
37
+ value.frames_values
38
+ when Arel::Nodes::As
39
+ {value.left.name => value}
40
+ when String
41
+ {arel_table.name => value}
42
+ else
43
+ {}
44
+ end
45
45
 
46
46
  self.frames_values = self.frames_values.merge(cte)
47
47
 
@@ -30,7 +30,7 @@ module ActiveRecord
30
30
  # NOTE: In Rails 5.2 (at least) we could use the InnerJoin.left.type_caster
31
31
  def build_frames(manager)
32
32
  # NOTE: We cannot early exclude associations because some associations are different from their table names
33
- # TODO: cache known associations?
33
+ # TODO: cache known associations, (renable warning)
34
34
  assocs = klass.reflect_on_all_associations.inject(Hash.new) do |assocs, assoc|
35
35
  begin
36
36
  assocs[assoc.table_name] = assoc
@@ -39,6 +39,7 @@ module ActiveRecord
39
39
  # ActiveRecord::Framing was trying to inspect the association #{assoc.name}
40
40
  # on the #{assoc.active_record.name} model but seems there is an issue
41
41
  # locating the model backing it.
42
+ # Error: #{e.message}
42
43
  # WARN
43
44
  end
44
45
  assocs
@@ -46,7 +47,7 @@ module ActiveRecord
46
47
 
47
48
  manager.join_sources.each do |join_source|
48
49
  next unless join_source&.left&.respond_to?(:name)
49
- if assoc = assocs[join_source.left.name]
50
+ if (assoc = assocs[join_source.left.name])
50
51
  source = reframe_values.fetch(assoc.name) { assoc.klass }
51
52
 
52
53
  join_source.left.name = source.arel_table.name
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Framing
3
- VERSION = "0.1.0-8"
3
+ VERSION = "0.1.0-9"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-framing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.8
4
+ version: 0.1.0.pre.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Stevens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-11 00:00:00.000000000 Z
11
+ date: 2019-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord