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

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: a84c35cce6f059189796de3ed8b120fdf0c078c2fa688bf0e70cd46f41f9ce2b
4
- data.tar.gz: f5678727edd6b2ad75939bce7b8c434684970dc3773b1bf7742c1b4e2d4cdafc
3
+ metadata.gz: ab0c33dbeba1bf12e4f49d8e51fada71519e03993edc8a15821b88bbabc37061
4
+ data.tar.gz: 74750a483858cd179c5051db28d3bc272c9440bacf51fc8c50cc0f43c48806ba
5
5
  SHA512:
6
- metadata.gz: 40d135da789de110cef85ad302cfb8db66d2ef6a826c48c14eea4d8a767f433dcd8469b6fe41b3ce8219ef73593b0d26edcf53aabf64275970618c829d349880
7
- data.tar.gz: 4f80b40511c0d1b0e17e8aef18c05ef660724c2fc3fdb873e5682b0c974653cf98a0135bff61c1ffedee8d3bceeb0c429ed18f47d78c60f00aafa9fba1e49eb2
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.name]
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.name] = value
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, instance_writer: false, instance_predicate: false
11
- class_attribute :default_frame_override, instance_writer: false, instance_predicate: false
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
- private
45
-
46
- # Use this macro in your model to set a default frame for all operations on
47
- # the model.
48
- #
49
- # class Article < ActiveRecord::Base
50
- # default_frame { where(published: true) }
51
- # end
52
- #
53
- # Article.all # => # Fires "WITH articles AS (SELECT * FROM articles WHERE published = true) SELECT * FROM articles"
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
- self.default_frames += [frame]
99
- end
60
+ private
100
61
 
101
- def build_default_frame(base_rel = nil)
102
- return if abstract_class?
62
+ def ignore_default_frame=(ignore)
63
+ FrameRegistry.set_value_for(:ignore_default_frame, base_class, ignore)
64
+ end
103
65
 
104
- if default_frame_override.nil?
105
- self.default_frame_override = !Base.is_a?(method(:default_frame).owner)
106
- end
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
- if default_frame_override
109
- # The user has defined their own default frame method, so call that
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
- evaluate_default_frame do
120
- # Create CTE here
121
+ def build_default_frame(base_rel = nil)
122
+ return if abstract_class?
121
123
 
122
- cte_relation = default_frames.inject(relation) do |default_frame, frame|
123
- frame = frame.respond_to?(:to_proc) ? frame : frame.method(:call)
124
- default_frame.merge!(relation.instance_exec(&frame))
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
- base_rel ||= relation
128
- base_rel.frame!(Arel::Nodes::As.new(Arel::Table.new(table_name), cte_relation.arel))# if cte_relation
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
- end
135
+ elsif default_frames.any?
136
+ ignore_default_frame do
137
+ cte_table = Arel::Table.new(table_name)
132
138
 
133
- def ignore_default_frame=(ignore)
134
- FrameRegistry.set_value_for(:ignore_default_frame, base_class, ignore)
135
- end
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
- # The ignore_default_frame flag is used to prevent an infinite recursion
138
- # situation where a default frame references a frame which has a default
139
- # frame which references a frame...
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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Framing
3
- VERSION = "0.1.0-7"
3
+ VERSION = "0.1.0-8"
4
4
  end
5
5
  end
@@ -0,0 +1,3 @@
1
+ require 'active_record/framing/default'
2
+ require 'active_record/framing/named'
3
+ require 'active_record/framing/railtie' if defined?(Rails::Railtie)
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.7
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-05-26 00:00:00.000000000 Z
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