type_balancer_rails 0.2.3 → 0.2.4
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 +4 -4
- data/CHANGELOG.md +7 -0
- data/example/Gemfile.lock +1 -1
- data/example/spec/fixtures/contents.yml +19 -0
- data/example/spec/fixtures/posts.yml +16 -0
- data/example/spec/models/content_spec.rb +32 -0
- data/example/spec/models/post_spec.rb +43 -0
- data/example/storage/test.sqlite3 +0 -0
- data/lib/type_balancer/rails/active_record_extension.rb +9 -7
- data/lib/type_balancer/rails/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c4859dc726a940a595ec48b50f3f57b676a713284985a567cae18b7b9e3fbaf
|
4
|
+
data.tar.gz: 685d88e62604917c4d848cdaa3ab9ed8bb9e3dea603ff801e69053dd5f46e01d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13cc2051f82762ff5c6b1b2c9c3c4b61f59760b88765a2927888e163a2b5ed878818eb0995c52f9aac534e30df48bd16eb0d1df581594f7a854029cf7fbd21bc
|
7
|
+
data.tar.gz: 6ecf0a7791776aa4260a6a213567f462e1be228d4ef706edb873a65734c7bf1dad4a0e48520e14b3e2b9c071a1c390acf827dcc9f4e33f4b79fa8ec88348b82a
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.4] - 2025-04-29
|
4
|
+
|
5
|
+
- Fixed critical ActiveRecord extension compatibility issue with `all` method
|
6
|
+
- Improved ActiveRecord integration by properly maintaining method signatures
|
7
|
+
- Enhanced test coverage for ActiveRecord core functionality
|
8
|
+
- Ensured compatibility with internal ActiveRecord operations like `reload`
|
9
|
+
|
3
10
|
## [0.2.3] - 2025-04-29
|
4
11
|
|
5
12
|
- Fixed issue with returning correctly balance items
|
data/example/Gemfile.lock
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Skewed distribution for demoing balancing on two fields
|
2
|
+
<% 1.upto(30) do |i| %>
|
3
|
+
news_video_<%= i %>:
|
4
|
+
title: "News Video <%= i %>"
|
5
|
+
category: news
|
6
|
+
content_type: video
|
7
|
+
<% end %>
|
8
|
+
<% 1.upto(15) do |i| %>
|
9
|
+
blog_article_<%= i %>:
|
10
|
+
title: "Blog Article <%= i %>"
|
11
|
+
category: blog
|
12
|
+
content_type: article
|
13
|
+
<% end %>
|
14
|
+
<% 1.upto(5) do |i| %>
|
15
|
+
tutorial_image_<%= i %>:
|
16
|
+
title: "Tutorial Image <%= i %>"
|
17
|
+
category: tutorial
|
18
|
+
content_type: image
|
19
|
+
<% end %>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Skewed distribution for demoing balancing
|
2
|
+
<% 1.upto(35) do |i| %>
|
3
|
+
video_post_<%= i %>:
|
4
|
+
title: "Video Post <%= i %>"
|
5
|
+
media_type: "video"
|
6
|
+
<% end %>
|
7
|
+
<% 1.upto(10) do |i| %>
|
8
|
+
article_post_<%= i %>:
|
9
|
+
title: "Article Post <%= i %>"
|
10
|
+
media_type: "article"
|
11
|
+
<% end %>
|
12
|
+
<% 1.upto(5) do |i| %>
|
13
|
+
image_post_<%= i %>:
|
14
|
+
title: "Image Post <%= i %>"
|
15
|
+
media_type: "image"
|
16
|
+
<% end %>
|
@@ -16,4 +16,36 @@ RSpec.describe Content, type: :model do
|
|
16
16
|
expect(balanced).not_to eq(original)
|
17
17
|
expect(balanced.uniq.sort).to contain_exactly('article', 'image', 'video')
|
18
18
|
end
|
19
|
+
|
20
|
+
describe 'type balancing with real records' do
|
21
|
+
before do
|
22
|
+
described_class.delete_all
|
23
|
+
@content1 = described_class.create!(title: 'Content 1', content_type: 'blog')
|
24
|
+
@content2 = described_class.create!(title: 'Content 2', content_type: 'news')
|
25
|
+
@content3 = described_class.create!(title: 'Content 3', content_type: 'blog')
|
26
|
+
end
|
27
|
+
|
28
|
+
after do
|
29
|
+
described_class.delete_all
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'works with dynamic type field configuration' do
|
33
|
+
# Test with content_type field
|
34
|
+
balanced = described_class.all.balance_by_type(type_field: :content_type)
|
35
|
+
expect(balanced.map(&:content_type)).to eq([ 'news', 'blog', 'blog' ])
|
36
|
+
|
37
|
+
# Test with where clause
|
38
|
+
blogs = described_class.where(content_type: 'blog').balance_by_type(type_field: :content_type)
|
39
|
+
expect(blogs.count).to eq(2)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'works with complex queries and dynamic type field' do
|
43
|
+
result = described_class.where(content_type: [ 'blog', 'news' ])
|
44
|
+
.order(:title)
|
45
|
+
.limit(2)
|
46
|
+
.balance_by_type(type_field: :content_type)
|
47
|
+
expect(result.length).to eq(2)
|
48
|
+
expect(result.map(&:content_type)).to include('blog', 'news')
|
49
|
+
end
|
50
|
+
end
|
19
51
|
end
|
@@ -9,4 +9,47 @@ RSpec.describe Post, type: :model do
|
|
9
9
|
expect(balanced).not_to eq(original)
|
10
10
|
expect(balanced.uniq.sort).to contain_exactly('article', 'image', 'video')
|
11
11
|
end
|
12
|
+
|
13
|
+
describe 'type balancing with real records' do
|
14
|
+
let(:video_post) { described_class.create!(title: 'Post 1', media_type: 'video') }
|
15
|
+
let(:article_post) { described_class.create!(title: 'Post 2', media_type: 'article') }
|
16
|
+
let(:another_video_post) { described_class.create!(title: 'Post 3', media_type: 'video') }
|
17
|
+
|
18
|
+
before do
|
19
|
+
described_class.delete_all
|
20
|
+
video_post; article_post; another_video_post # Create the records
|
21
|
+
end
|
22
|
+
|
23
|
+
after do
|
24
|
+
described_class.delete_all
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'works with basic ActiveRecord methods' do
|
28
|
+
# Test .all
|
29
|
+
balanced = described_class.all.balance_by_type
|
30
|
+
expect(balanced.map(&:media_type)).to eq([ 'article', 'video', 'video' ])
|
31
|
+
|
32
|
+
# Test .where
|
33
|
+
videos = described_class.where(media_type: 'video').balance_by_type
|
34
|
+
expect(videos.count).to eq(2)
|
35
|
+
|
36
|
+
# Test .order
|
37
|
+
ordered = described_class.order(:title).balance_by_type
|
38
|
+
expect(ordered.map(&:title)).to contain_exactly('Post 1', 'Post 2', 'Post 3')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'works with reload' do
|
42
|
+
post = video_post.reload
|
43
|
+
expect(post.media_type).to eq('video')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'works with complex queries' do
|
47
|
+
result = described_class.where(media_type: [ 'video', 'article' ])
|
48
|
+
.order(:title)
|
49
|
+
.limit(2)
|
50
|
+
.balance_by_type
|
51
|
+
expect(result.length).to eq(2)
|
52
|
+
expect(result.map(&:media_type)).to include('video', 'article')
|
53
|
+
end
|
54
|
+
end
|
12
55
|
end
|
Binary file
|
@@ -9,12 +9,15 @@ module TypeBalancer
|
|
9
9
|
extend ActiveSupport::Concern
|
10
10
|
|
11
11
|
included do
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
# TODO: Future Enhancement - Lazy Loading of CollectionMethods
|
13
|
+
# Currently, CollectionMethods is included globally as soon as any model extends ActiveRecordExtension.
|
14
|
+
# This could be optimized to only include CollectionMethods when balance_by_type is first called on a model.
|
15
|
+
# This would make the extension truly opt-in at the model level and prevent unnecessary inclusion
|
16
|
+
# in models that don't use type balancing.
|
17
|
+
|
18
|
+
# Only include CollectionMethods if it hasn't been included yet
|
19
|
+
unless ActiveRecord::Relation.included_modules.include?(TypeBalancer::Rails::CollectionMethods)
|
20
|
+
ActiveRecord::Relation.include(TypeBalancer::Rails::CollectionMethods)
|
18
21
|
end
|
19
22
|
end
|
20
23
|
|
@@ -35,7 +38,6 @@ module TypeBalancer
|
|
35
38
|
relation = all
|
36
39
|
return [] unless relation.is_a?(ActiveRecord::Relation)
|
37
40
|
|
38
|
-
relation.extend(CollectionMethods)
|
39
41
|
relation.balance_by_type(options)
|
40
42
|
end
|
41
43
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: type_balancer_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carl Smith
|
@@ -154,6 +154,8 @@ files:
|
|
154
154
|
- example/spec/controllers/posts_controller_spec.rb
|
155
155
|
- example/spec/features/contents_balancing_spec.rb
|
156
156
|
- example/spec/features/posts_balancing_spec.rb
|
157
|
+
- example/spec/fixtures/contents.yml
|
158
|
+
- example/spec/fixtures/posts.yml
|
157
159
|
- example/spec/models/content_spec.rb
|
158
160
|
- example/spec/models/post_spec.rb
|
159
161
|
- example/spec/rails_helper.rb
|