active_record-framing 0.1.0.pre.7 → 0.1.0.pre.8
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab0c33dbeba1bf12e4f49d8e51fada71519e03993edc8a15821b88bbabc37061
|
4
|
+
data.tar.gz: 74750a483858cd179c5051db28d3bc272c9440bacf51fc8c50cc0f43c48806ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae7e047fefd79d7bb836b04b69bcc7378b6fae4057fbfe6299cf0ec3fd37904e9b9e034ebaf86107f628c8b2efb319946d00bbc4a4111a3fb7eff257db577300
|
7
|
+
data.tar.gz: 2b09eaecb22f6d66fe20b9215589f0f816f0538f53e5b8be833fd09c1e68e4f58e9168d74d81774de89f4ac0e8528bc95c1b781bb06d84a28b9f32ed180a85d5
|
@@ -18,6 +18,17 @@ module ActiveRecord
|
|
18
18
|
subclass.extend ClassMethods
|
19
19
|
end
|
20
20
|
|
21
|
+
def self.disable
|
22
|
+
@disabled = true
|
23
|
+
yield if block_given?
|
24
|
+
ensure
|
25
|
+
@disabled = false
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.disabled?
|
29
|
+
@disabled == true
|
30
|
+
end
|
31
|
+
|
21
32
|
module ClassMethods # :nodoc:
|
22
33
|
def current_frame
|
23
34
|
FrameRegistry.value_for(:current_frame, self)
|
@@ -62,25 +73,13 @@ module ActiveRecord
|
|
62
73
|
# Obtains the value for a given +frame_type+ and +model+.
|
63
74
|
def value_for(frame_type, model)
|
64
75
|
raise_invalid_frame_type!(frame_type)
|
65
|
-
return @registry[frame_type][model
|
76
|
+
return @registry[frame_type][model]# if skip_inherited_frame
|
66
77
|
end
|
67
78
|
|
68
|
-
# def value_for(frame_type, model, skip_inherited_frame = false)
|
69
|
-
# raise_invalid_frame_type!(frame_type)
|
70
|
-
# return @registry[frame_type][model.name] if skip_inherited_frame
|
71
|
-
# klass = model
|
72
|
-
# base = model.base_class
|
73
|
-
# while klass <= base
|
74
|
-
# value = @registry[frame_type][klass.name]
|
75
|
-
# return value if value
|
76
|
-
# klass = klass.superclass
|
77
|
-
# end
|
78
|
-
# end
|
79
|
-
|
80
79
|
# Sets the +value+ for a given +frame_type+ and +model+.
|
81
80
|
def set_value_for(frame_type, model, value)
|
82
81
|
raise_invalid_frame_type!(frame_type)
|
83
|
-
@registry[frame_type][model
|
82
|
+
@registry[frame_type][model] = value
|
84
83
|
end
|
85
84
|
|
86
85
|
private
|
@@ -7,8 +7,13 @@ module ActiveRecord
|
|
7
7
|
|
8
8
|
included do
|
9
9
|
# Stores the default frame for the class.
|
10
|
-
class_attribute :default_frames,
|
11
|
-
|
10
|
+
class_attribute :default_frames,
|
11
|
+
instance_writer: false,
|
12
|
+
instance_predicate: false
|
13
|
+
|
14
|
+
class_attribute :default_frame_override,
|
15
|
+
instance_writer: false,
|
16
|
+
instance_predicate: false
|
12
17
|
|
13
18
|
self.default_frames = []
|
14
19
|
self.default_frame_override = nil
|
@@ -41,109 +46,106 @@ module ActiveRecord
|
|
41
46
|
FrameRegistry.value_for(:ignore_default_frame, base_class)
|
42
47
|
end
|
43
48
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
# The #default_frame is not applied while updating/creating/building a record.
|
56
|
-
#
|
57
|
-
# Article.new.published # => nil
|
58
|
-
# Article.create.published # => nil
|
59
|
-
# Article.first.update(name: 'A Tale of Two Cities').published # => nil
|
60
|
-
#
|
61
|
-
# (You can also pass any object which responds to +call+ to the
|
62
|
-
# +default_frame+ macro, and it will be called when building the
|
63
|
-
# default frame.)
|
64
|
-
#
|
65
|
-
# If you use multiple #default_frame declarations in your model then
|
66
|
-
# they will be merged together:
|
67
|
-
#
|
68
|
-
# class Article < ActiveRecord::Base
|
69
|
-
# default_frame { where(published: true) }
|
70
|
-
# default_frame { where(rating: 'G') }
|
71
|
-
# end
|
72
|
-
#
|
73
|
-
# Article.all # => WITH articles AS (SELECT * FROM articles WHERE published = true AND rating = 'G') SELECT * FROM articles
|
74
|
-
#
|
75
|
-
# This is also the case with inheritance and module includes where the
|
76
|
-
# parent or module defines a #default_frame and the child or including
|
77
|
-
# class defines a second one.
|
78
|
-
#
|
79
|
-
# If you need to do more complex things with a default frame, you can
|
80
|
-
# alternatively define it as a class method:
|
81
|
-
#
|
82
|
-
# class Article < ActiveRecord::Base
|
83
|
-
# def self.default_frame
|
84
|
-
# # Should return a frame, you can call 'super' here etc.
|
85
|
-
# end
|
86
|
-
# end
|
87
|
-
def default_frame(frame = nil) # :doc:
|
88
|
-
frame = Proc.new if block_given?
|
89
|
-
|
90
|
-
if frame.is_a?(Relation) || !frame.respond_to?(:call)
|
91
|
-
raise ArgumentError,
|
92
|
-
"Support for calling #default_frame without a block is removed. For example instead " \
|
93
|
-
"of `default_frame where(color: 'red')`, please use " \
|
94
|
-
"`default_frame { where(color: 'red') }`. (Alternatively you can just redefine " \
|
95
|
-
"self.default_frame.)"
|
96
|
-
end
|
49
|
+
# The ignore_default_frame flag is used to prevent an infinite recursion
|
50
|
+
# situation where a default frame references a frame which has a default
|
51
|
+
# frame which references a frame...
|
52
|
+
def ignore_default_frame
|
53
|
+
return if ignore_default_frame?
|
54
|
+
self.ignore_default_frame = true
|
55
|
+
yield
|
56
|
+
ensure
|
57
|
+
self.ignore_default_frame = false
|
58
|
+
end
|
97
59
|
|
98
|
-
|
99
|
-
end
|
60
|
+
private
|
100
61
|
|
101
|
-
|
102
|
-
|
62
|
+
def ignore_default_frame=(ignore)
|
63
|
+
FrameRegistry.set_value_for(:ignore_default_frame, base_class, ignore)
|
64
|
+
end
|
103
65
|
|
104
|
-
|
105
|
-
|
106
|
-
|
66
|
+
# Use this macro in your model to set a default frame for all operations on
|
67
|
+
# the model.
|
68
|
+
#
|
69
|
+
# class Article < ActiveRecord::Base
|
70
|
+
# default_frame { where(published: true) }
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# Article.all # => # Fires "WITH articles AS (SELECT * FROM articles WHERE published = true) SELECT * FROM articles"
|
74
|
+
#
|
75
|
+
# The #default_frame is not applied while updating/creating/building a record.
|
76
|
+
#
|
77
|
+
# Article.new.published # => nil
|
78
|
+
# Article.create.published # => nil
|
79
|
+
# Article.first.update(name: 'A Tale of Two Cities').published # => nil
|
80
|
+
#
|
81
|
+
# (You can also pass any object which responds to +call+ to the
|
82
|
+
# +default_frame+ macro, and it will be called when building the
|
83
|
+
# default frame.)
|
84
|
+
#
|
85
|
+
# If you use multiple #default_frame declarations in your model then
|
86
|
+
# they will be merged together:
|
87
|
+
#
|
88
|
+
# class Article < ActiveRecord::Base
|
89
|
+
# default_frame { where(published: true) }
|
90
|
+
# default_frame { where(rating: 'G') }
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# Article.all # => WITH articles AS (SELECT * FROM articles WHERE published = true AND rating = 'G') SELECT * FROM articles
|
94
|
+
#
|
95
|
+
# This is also the case with inheritance and module includes where the
|
96
|
+
# parent or module defines a #default_frame and the child or including
|
97
|
+
# class defines a second one.
|
98
|
+
#
|
99
|
+
# If you need to do more complex things with a default frame, you can
|
100
|
+
# alternatively define it as a class method:
|
101
|
+
#
|
102
|
+
# class Article < ActiveRecord::Base
|
103
|
+
# def self.default_frame
|
104
|
+
# # Should return a frame, you can call 'super' here etc.
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
def default_frame(frame = nil) # :doc:
|
108
|
+
frame = Proc.new if block_given?
|
109
|
+
|
110
|
+
if frame.is_a?(Relation) || !frame.respond_to?(:call)
|
111
|
+
raise ArgumentError,
|
112
|
+
"Support for calling #default_frame without a block is removed. For example instead " \
|
113
|
+
"of `default_frame where(color: 'red')`, please use " \
|
114
|
+
"`default_frame { where(color: 'red') }`. (Alternatively you can just redefine " \
|
115
|
+
"self.default_frame.)"
|
116
|
+
end
|
107
117
|
|
108
|
-
|
109
|
-
|
110
|
-
evaluate_default_frame do
|
111
|
-
if frame = default_frame
|
112
|
-
(base_rel ||= relation).frame!(frame)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
elsif default_frames.any?
|
116
|
-
# cte_table = arel_table
|
117
|
-
cte_table = Arel::Table.new(table_name)
|
118
|
+
self.default_frames += [frame]
|
119
|
+
end
|
118
120
|
|
119
|
-
|
120
|
-
|
121
|
+
def build_default_frame(base_rel = nil)
|
122
|
+
return if abstract_class?
|
121
123
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
124
|
+
if default_frame_override.nil?
|
125
|
+
self.default_frame_override = !::ActiveRecord::Base.is_a?(method(:default_frame).owner)
|
126
|
+
end
|
126
127
|
|
127
|
-
|
128
|
-
|
128
|
+
if default_frame_override
|
129
|
+
# The user has defined their own default frame method, so call that
|
130
|
+
ignore_default_frame do
|
131
|
+
if (frame = default_frame)
|
132
|
+
(base_rel ||= relation).frame!(frame)
|
129
133
|
end
|
130
134
|
end
|
131
|
-
|
135
|
+
elsif default_frames.any?
|
136
|
+
ignore_default_frame do
|
137
|
+
cte_table = Arel::Table.new(table_name)
|
132
138
|
|
133
|
-
|
134
|
-
|
135
|
-
|
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
|
136
143
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
def evaluate_default_frame
|
141
|
-
return if ignore_default_frame?
|
142
|
-
self.ignore_default_frame = true
|
143
|
-
yield
|
144
|
-
ensure
|
145
|
-
self.ignore_default_frame = false
|
144
|
+
base_rel ||= relation
|
145
|
+
base_rel.frame!(Arel::Nodes::As.new(cte_table, cte_relation.arel))# if cte_relation
|
146
|
+
end
|
146
147
|
end
|
148
|
+
end
|
147
149
|
end
|
148
150
|
end
|
149
151
|
end
|
@@ -19,7 +19,7 @@ module ActiveRecord
|
|
19
19
|
# {default_frame}[rdoc-ref:Framing::Default::ClassMethods#default_frame].
|
20
20
|
def all
|
21
21
|
rel = unframed_all
|
22
|
-
if current_frame = self.current_frame
|
22
|
+
if (current_frame = self.current_frame)
|
23
23
|
if self == current_frame.klass
|
24
24
|
current_frame.clone
|
25
25
|
else
|
@@ -32,7 +32,7 @@ module ActiveRecord
|
|
32
32
|
|
33
33
|
# def default_framed(frame = relation) # :nodoc:
|
34
34
|
def default_framed(frame = nil) # :nodoc:
|
35
|
-
!ignore_default_frame? && build_default_frame(frame) || frame
|
35
|
+
!ignore_default_frame? && !ActiveRecord::Framing.disabled? && build_default_frame(frame) || frame
|
36
36
|
end
|
37
37
|
|
38
38
|
# Adds a class method for retrieving and querying objects.
|
@@ -149,7 +149,6 @@ module ActiveRecord
|
|
149
149
|
the_frame = body.respond_to?(:to_proc) ? body : body.method(:call)
|
150
150
|
cte_relation = relation.merge!(relation.instance_exec(&the_frame) || relation)
|
151
151
|
|
152
|
-
delegator = self.name.to_sym
|
153
152
|
new_class = self.const_set constant, (Class.new(self) do |klass|
|
154
153
|
klass.abstract_class = true
|
155
154
|
klass.table_name = superclass.table_name
|
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.
|
4
|
+
version: 0.1.0.pre.8
|
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-
|
11
|
+
date: 2019-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- LICENSE
|
149
149
|
- README.md
|
150
150
|
- lib/active_record-framing.rb
|
151
|
+
- lib/active_record/framing.rb
|
151
152
|
- lib/active_record/framing/attribute_methods.rb
|
152
153
|
- lib/active_record/framing/compat/active_record_4_2.rb
|
153
154
|
- lib/active_record/framing/compat/active_record_5_0.rb
|