type_balancer_rails 0.2.3 → 0.2.5

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: 57e73c24c2f2b4fab4f8cf8588cfe74a2be42f2392ff39195f7cb2021d8640f7
4
- data.tar.gz: 2268520c561f4747aa76fd9913f8b33e05ba629ae7829a8bd863a2c58107c1e5
3
+ metadata.gz: 3bc4c138e98b60b2b2979d941ec39f74d24d85c7b1a89208123edd669c89640e
4
+ data.tar.gz: 295ba9223522c4fee8284ef0c78f9101a06a55aafc433bd4a9c013e486e95251
5
5
  SHA512:
6
- metadata.gz: 7c8d71e437fc6ed17b3549581397e890b1d8e6ba0eb69161d9760d739dd0c32ba44765db7f7e2b215db6d96a59273064801bc8a0eea8f88f268745eb34c2f30a
7
- data.tar.gz: db58f0c79fd64b464d5756e8636abcc6c79c4c7efdc1466ac7ea3578a0a498ede362eefd589779683fbb22488cf9dea5ec3759ac302b0024f0c8e1bd8dfba590
6
+ metadata.gz: 26724989d4b906dd3b00fb7ad2ccaaf81235a5ba170f194453062fc5a871585e81140ca227aa42ed69994bfb17ee4a60c45f5c3fadedd9b2597f4a3b3f8bd859
7
+ data.tar.gz: f0df68c064bd140e1dcdec9f512e5ace8e39c8f5c3de3117c38cd502383f87f1b99881a653493684b57ed9c7daaaaa91419c3699860e01b8a34769c2d5078bec
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.5] - 2025-04-30
4
+
5
+ - Updated type_balancer dependency to ~> 0.2.0 for improved performance and better position calculations
6
+
7
+ ## [0.2.4] - 2025-04-29
8
+
9
+ - Fixed critical ActiveRecord extension compatibility issue with `all` method
10
+ - Improved ActiveRecord integration by properly maintaining method signatures
11
+ - Enhanced test coverage for ActiveRecord core functionality
12
+ - Ensured compatibility with internal ActiveRecord operations like `reload`
13
+
3
14
  ## [0.2.3] - 2025-04-29
4
15
 
5
16
  - Fixed issue with returning correctly balance items
data/example/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- type_balancer_rails (0.2.2)
4
+ type_balancer_rails (0.2.5)
5
5
  activerecord (>= 7.0, < 9.0)
6
6
  activesupport (>= 7.0, < 9.0)
7
- type_balancer (~> 0.1, >= 0.1.4)
7
+ type_balancer (~> 0.2.0)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -381,7 +381,7 @@ GEM
381
381
  turbo-rails (2.0.13)
382
382
  actionpack (>= 7.1.0)
383
383
  railties (>= 7.1.0)
384
- type_balancer (0.1.4)
384
+ type_balancer (0.2.0)
385
385
  tzinfo (2.0.6)
386
386
  concurrent-ruby (~> 1.0)
387
387
  unicode-display_width (3.1.4)
@@ -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
- class << self
13
- def all
14
- relation = super
15
- relation.extend(TypeBalancer::Rails::CollectionMethods)
16
- relation
17
- end
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TypeBalancer
4
4
  module Rails
5
- VERSION = '0.2.3'
5
+ VERSION = '0.2.5'
6
6
  end
7
7
  end
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  # Runtime dependencies
33
33
  spec.add_dependency 'activerecord', '>= 7.0', '< 9.0'
34
34
  spec.add_dependency 'activesupport', '>= 7.0', '< 9.0'
35
- spec.add_dependency 'type_balancer', '~> 0.1', '>= 0.1.4'
35
+ spec.add_dependency 'type_balancer', '~> 0.2.0'
36
36
 
37
37
  # For more information and examples about making a new gem, check out our
38
38
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_balancer_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-04-30 00:00:00.000000000 Z
11
+ date: 2025-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -56,20 +56,14 @@ dependencies:
56
56
  requirements:
57
57
  - - "~>"
58
58
  - !ruby/object:Gem::Version
59
- version: '0.1'
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 0.1.4
59
+ version: 0.2.0
63
60
  type: :runtime
64
61
  prerelease: false
65
62
  version_requirements: !ruby/object:Gem::Requirement
66
63
  requirements:
67
64
  - - "~>"
68
65
  - !ruby/object:Gem::Version
69
- version: '0.1'
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: 0.1.4
66
+ version: 0.2.0
73
67
  description: Provides Rails integration for the type_balancer gem
74
68
  email:
75
69
  - carl@llwebconsulting.com
@@ -154,6 +148,8 @@ files:
154
148
  - example/spec/controllers/posts_controller_spec.rb
155
149
  - example/spec/features/contents_balancing_spec.rb
156
150
  - example/spec/features/posts_balancing_spec.rb
151
+ - example/spec/fixtures/contents.yml
152
+ - example/spec/fixtures/posts.yml
157
153
  - example/spec/models/content_spec.rb
158
154
  - example/spec/models/post_spec.rb
159
155
  - example/spec/rails_helper.rb