active_record-framing 0.1.0.pre.4 → 0.1.0.pre.5

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: f02f550ffc7f48015697d04cdc5ad1a54a3693c4c0fcb141500224fc78cfeec7
4
- data.tar.gz: 96ef5227beb750c1e6845ecaafd7d11b5dc888f31a96acf29c2a26dde37d544e
3
+ metadata.gz: 50f9ead434db94b752243dc9e02394e66b3ddfffce6ef928d956a216b60f1152
4
+ data.tar.gz: 8076e981ada88139570808b4a557ab2ff8fc142da0968194f16a4ad343af2ccd
5
5
  SHA512:
6
- metadata.gz: e73a79efe0958a13aaf6845cd0ee546bd8dcc7520cbc062f91aa1d7dab36ecd707f1c0f4f454f1b3027369ea1ac74f61112a2167b9507204861478981474785e
7
- data.tar.gz: b90c1d6ea6bba1888cc6a19693682409ea553415073ae14c23db7431855d596b86b0cbb1c8edc81e9f9bfa1032d0b6c0545fbd2b056d09408aa36d2c27840fe6
6
+ metadata.gz: 766afa51855672ad7ad6d0dad90f7dcd3431f2ef3a26a9c1d56c7eb77d2bef6cff5cc1a29b0f2c0a6e8289f72e7da27737e7d199ec6074969c4850e7cbe31f89
7
+ data.tar.gz: f57a1021461f0dbe7d769badd5267eada20a0c331e801bea521326454089873fa0ed474be55bcac6b075af6afe704cb1452f6e8b80f2d663e9e4ad2e6db3d691
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  [![Build Status](https://travis-ci.org/TwilightCoders/active_record-framing.svg)](https://travis-ci.org/TwilightCoders/active_record-framing)
4
4
  [![Maintenence ](https://api.codeclimate.com/v1/badges/762cdcd63990efa768b0/maintainability)](https://codeclimate.com/github/TwilightCoders/active_record-framing/maintainability)
5
5
  [![Coverage ](https://codeclimate.com/github/TwilightCoders/active_record-framing/badges/coverage.svg)](https://codeclimate.com/github/TwilightCoders/active_record-framing/coverage)
6
- [![Dependencies](https://img.shields.io/librariesio/github/twilightcoders/active_record-framing.svg)](https://depfu.com/github/TwilightCoders/active_record-framing)
7
6
 
8
7
  # ActiveRecord::Framing
9
8
 
@@ -13,7 +12,7 @@ Unlike scopes, they do not affect the values of attributes upon creation.
13
12
 
14
13
  ## Requirements
15
14
 
16
- - Ruby 2.3+
15
+ - Ruby 2.4+
17
16
  - ActiveRecord 4.2+
18
17
 
19
18
  ## Installation
@@ -0,0 +1,13 @@
1
+ {
2
+ frames: {},
3
+ reframe: {}
4
+ }.each do |value_name, default_value|
5
+ define_method("#{value_name}_values") do
6
+ @values[value_name] || default_value
7
+ end
8
+ define_method("#{value_name}_values=") do |values|
9
+ raise ImmutableRelation if @loaded
10
+ check_cached_relation
11
+ @values[value_name] = values
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ {
2
+ frames: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH,
3
+ reframe: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH
4
+ }.each do |value_name, default_value|
5
+ define_method("#{value_name}_values") do
6
+ @values[value_name] || default_value
7
+ end
8
+ define_method("#{value_name}_values=") do |values|
9
+ assert_mutability!
10
+ @values[value_name] = values
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ {
2
+ frames: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH,
3
+ reframe: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH
4
+ }.each do |value_name, default_value|
5
+ define_method("#{value_name}_values") do
6
+ get_value(value_name)
7
+ end
8
+ define_method("#{value_name}_values=") do |value|
9
+ set_value(value_name, value)
10
+ end
11
+ ::ActiveRecord::Relation::DEFAULT_VALUES[value_name] = default_value
12
+ end
@@ -63,18 +63,6 @@ module ActiveRecord
63
63
  return @registry[frame_type][model.name]
64
64
  end
65
65
 
66
- # def value_for(frame_type, model, skip_inherited_frame = false)
67
- # raise_invalid_frame_type!(frame_type)
68
- # return @registry[frame_type][model.name] if skip_inherited_frame
69
- # klass = model
70
- # base = model.base_class
71
- # while klass <= base
72
- # value = @registry[frame_type][klass.name]
73
- # return value if value
74
- # klass = klass.superclass
75
- # end
76
- # end
77
-
78
66
  # Sets the +value+ for a given +frame_type+ and +model+.
79
67
  def set_value_for(frame_type, model, value)
80
68
  raise_invalid_frame_type!(frame_type)
@@ -37,15 +37,6 @@ module ActiveRecord
37
37
  block_given? ? unframed_all.framing { yield } : unframed_all
38
38
  end
39
39
 
40
- # Are there attributes associated with this frame?
41
- def frame_attributes? # :nodoc:
42
- super || default_frames.any? || respond_to?(:default_frame)
43
- end
44
-
45
- def before_remove_const #:nodoc:
46
- self.current_frame = nil
47
- end
48
-
49
40
  def ignore_default_frame?
50
41
  FrameRegistry.value_for(:ignore_default_frame, base_class)
51
42
  end
@@ -142,6 +142,8 @@ module ActiveRecord
142
142
  end
143
143
 
144
144
  constant = frame_name.to_s.classify.to_sym
145
+ valid_frame_name?(constant)
146
+
145
147
  arel_tn = "#{frame_name}/#{self.table_name}"
146
148
 
147
149
  the_frame = body.respond_to?(:to_proc) ? body : body.method(:call)
@@ -184,9 +186,9 @@ module ActiveRecord
184
186
  private
185
187
 
186
188
  def valid_frame_name?(name)
187
- if respond_to?(name, true) && logger
189
+ if const_defined?(name) && logger
188
190
  logger.warn "Creating frame :#{name}. " \
189
- "Overwriting existing method #{self.name}.#{name}."
191
+ "Overwriting existing const #{self.name}::#{name}."
190
192
  end
191
193
  end
192
194
  end
@@ -3,67 +3,55 @@ module ActiveRecord
3
3
  module QueryMethods
4
4
 
5
5
  if ::ActiveRecord.version >= Gem::Version.new("5.1") # 5.1+
6
- {
7
- frames: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH,
8
- reframe: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH
9
- }.each do |value_name, default_value|
10
- define_method("#{value_name}_values") do
11
- get_value(value_name)
12
- end
13
- define_method("#{value_name}_values=") do |value|
14
- set_value(value_name, value)
15
- end
16
- ::ActiveRecord::Relation::DEFAULT_VALUES[value_name] = default_value
17
- end
6
+ require 'active_record/framing/compat/active_record_5_1'
18
7
  elsif ::ActiveRecord.version >= Gem::Version.new("5.0") # 5.0+
19
- {
20
- frames: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH,
21
- reframe: ::ActiveRecord::Relation::FROZEN_EMPTY_HASH
22
- }.each do |value_name, default_value|
23
- define_method("#{value_name}_values") do
24
- @values[value_name] || default_value
25
- end
26
- define_method("#{value_name}_values=") do |values|
27
- assert_mutability!
28
- @values[value_name] = values
29
- end
30
- end
8
+ require 'active_record/framing/compat/active_record_5_0'
31
9
  elsif ::ActiveRecord.version >= Gem::Version.new("4.2") # 4.2+
32
- {
33
- frames: {},
34
- reframe: {}
35
- }.each do |value_name, default_value|
36
- define_method("#{value_name}_values") do
37
- @values[value_name] || default_value
38
- end
39
- define_method("#{value_name}_values=") do |values|
40
- raise ImmutableRelation if @loaded
41
- check_cached_relation
42
- @values[value_name] = values
43
- end
44
- end
10
+ require 'active_record/framing/compat/active_record_4_2'
45
11
  else
46
12
  raise NotImplementedError, "ActiveRecord::Framing does not support Rails #{::ActiveRecord.version}"
47
13
  end
48
14
 
49
- def from!(value, subquery_name = nil) # :nodoc:
50
- super.tap do |rel|
51
- frames_values = frames_values.merge(value.frames_values) if value.is_a?(::ActiveRecord::Relation)
52
- end
53
- end
15
+ # def from!(value, subquery_name = nil) # :nodoc:
16
+ # super.tap do |rel|
17
+ # if value.is_a?(::ActiveRecord::Relation) && value.frames_values.any?
18
+ # self.frames_values = self.frames_values.merge(value.frames_values)
19
+ # value.frames_values.clear
20
+ # end
21
+ # end
22
+ # end
54
23
 
55
- def frame(value)
56
- spawn.frame!(value)
57
- end
24
+ # def frame(value)
25
+ # spawn.frame!(value)
26
+ # end
58
27
 
59
28
  def frame!(value)
60
- if key = frame_key(value)
61
- self.frames_values = self.frames_values.merge(key => value)
29
+ value = value.all if (value.is_a?(Class) && value < ::ActiveRecord::Base)
30
+
31
+ cte = \
32
+ case value
33
+ when ::ActiveRecord::Relation
34
+ # {value.table.name => value.frames_values}
35
+ value.frames_values
36
+ when Arel::Nodes::As
37
+ {value.left.name => value}
38
+ when String
39
+ {arel_table.name => value}
40
+ else
41
+ {}
62
42
  end
43
+
44
+ self.frames_values = self.frames_values.merge(cte)
45
+
63
46
  self
64
47
  end
65
48
 
66
49
  def reframe(*args)
50
+ spawn.reframe!(*args)
51
+ end
52
+
53
+ # TODO: Consistent keys class: symbol, string, Arel::Table
54
+ def reframe!(*args)
67
55
  args.flatten!
68
56
  # TODO: Convert array (if present) to nil hash {value => nil}
69
57
  # and merge with hash (if present) as second arg
@@ -11,7 +11,7 @@ module ActiveRecord::Framing
11
11
  ActiveSupport.on_load(:active_record) do
12
12
  ::ActiveRecord::Base.prepend(ActiveRecord::Framing)
13
13
  ::ActiveRecord::Relation.prepend(ActiveRecord::Framing::Relation)
14
- ::ActiveRecord::Relation.include(ActiveRecord::Framing::QueryMethods)
14
+ ::ActiveRecord::Relation.prepend(ActiveRecord::Framing::QueryMethods)
15
15
  ::ActiveRecord::Relation.prepend(ActiveRecord::Framing::SpawnMethods)
16
16
  end
17
17
  end
@@ -59,19 +59,6 @@ module ActiveRecord
59
59
  end
60
60
  end
61
61
 
62
- def frame_key(cte)
63
- case cte
64
- when ActiveRecord::Relation
65
- cte.table.name
66
- when Arel::Nodes::As
67
- cte.left.name
68
- when String
69
- cte
70
- else
71
- nil
72
- end
73
- end
74
-
75
62
  # Frame all queries to the current frame.
76
63
  #
77
64
  # Comment.where(post_id: 1).framing do
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Framing
3
- VERSION = "0.1.0-4"
3
+ VERSION = "0.1.0-5"
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.4
4
+ version: 0.1.0.pre.5
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-14 00:00:00.000000000 Z
11
+ date: 2019-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -34,32 +34,60 @@ dependencies:
34
34
  name: pg
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - "~>"
44
+ - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
47
  - !ruby/object:Gem::Dependency
48
- name: pry-byebug
48
+ name: bundler
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - "~>"
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: '3'
53
+ version: '0'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - "~>"
58
+ - - ">="
59
59
  - !ruby/object:Gem::Version
60
- version: '3'
60
+ version: '0'
61
61
  - !ruby/object:Gem::Dependency
62
- name: bundler
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: combustion
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: pry-byebug
63
91
  requirement: !ruby/object:Gem::Requirement
64
92
  requirements:
65
93
  - - ">="
@@ -76,30 +104,44 @@ dependencies:
76
104
  name: rake
77
105
  requirement: !ruby/object:Gem::Requirement
78
106
  requirements:
79
- - - "~>"
107
+ - - ">="
80
108
  - !ruby/object:Gem::Version
81
- version: '12.0'
109
+ version: '0'
82
110
  type: :development
83
111
  prerelease: false
84
112
  version_requirements: !ruby/object:Gem::Requirement
85
113
  requirements:
86
- - - "~>"
114
+ - - ">="
87
115
  - !ruby/object:Gem::Version
88
- version: '12.0'
116
+ version: '0'
89
117
  - !ruby/object:Gem::Dependency
90
- name: combustion
118
+ name: database_cleaner
91
119
  requirement: !ruby/object:Gem::Requirement
92
120
  requirements:
93
- - - "~>"
121
+ - - ">="
94
122
  - !ruby/object:Gem::Version
95
- version: '0.7'
123
+ version: '0'
96
124
  type: :development
97
125
  prerelease: false
98
126
  version_requirements: !ruby/object:Gem::Requirement
99
127
  requirements:
100
- - - "~>"
128
+ - - ">="
101
129
  - !ruby/object:Gem::Version
102
- version: '0.7'
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: simplecov
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
103
145
  description: Allows for larger level scoping (framing) that affect complicated queries
104
146
  more holistically
105
147
  email:
@@ -113,6 +155,9 @@ files:
113
155
  - README.md
114
156
  - lib/active_record/framing.rb
115
157
  - lib/active_record/framing/attribute_methods.rb
158
+ - lib/active_record/framing/compat/active_record_4_2.rb
159
+ - lib/active_record/framing/compat/active_record_5_0.rb
160
+ - lib/active_record/framing/compat/active_record_5_1.rb
116
161
  - lib/active_record/framing/core_extension.rb
117
162
  - lib/active_record/framing/default.rb
118
163
  - lib/active_record/framing/named.rb
@@ -134,7 +179,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
179
  requirements:
135
180
  - - ">="
136
181
  - !ruby/object:Gem::Version
137
- version: '2.3'
182
+ version: '2.4'
138
183
  required_rubygems_version: !ruby/object:Gem::Requirement
139
184
  requirements:
140
185
  - - ">"