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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57e73c24c2f2b4fab4f8cf8588cfe74a2be42f2392ff39195f7cb2021d8640f7
4
- data.tar.gz: 2268520c561f4747aa76fd9913f8b33e05ba629ae7829a8bd863a2c58107c1e5
3
+ metadata.gz: 9c4859dc726a940a595ec48b50f3f57b676a713284985a567cae18b7b9e3fbaf
4
+ data.tar.gz: 685d88e62604917c4d848cdaa3ab9ed8bb9e3dea603ff801e69053dd5f46e01d
5
5
  SHA512:
6
- metadata.gz: 7c8d71e437fc6ed17b3549581397e890b1d8e6ba0eb69161d9760d739dd0c32ba44765db7f7e2b215db6d96a59273064801bc8a0eea8f88f268745eb34c2f30a
7
- data.tar.gz: db58f0c79fd64b464d5756e8636abcc6c79c4c7efdc1466ac7ea3578a0a498ede362eefd589779683fbb22488cf9dea5ec3759ac302b0024f0c8e1bd8dfba590
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- type_balancer_rails (0.2.2)
4
+ type_balancer_rails (0.2.4)
5
5
  activerecord (>= 7.0, < 9.0)
6
6
  activesupport (>= 7.0, < 9.0)
7
7
  type_balancer (~> 0.1, >= 0.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.4'
6
6
  end
7
7
  end
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.3
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