type_balancer_rails 0.1.1 → 0.1.3

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: ba34931f5dfe8f33283f9597b3cd1ae8e4cd408c9985822e701806b24b852e9f
4
- data.tar.gz: 22ec570ca04de5382e46d0f92e6c45288f0c934bddbc7c87633adb1f055f5db0
3
+ metadata.gz: 5ebd00e75d4c63af53f5be038f01f920eb6e65dcf3c91ae079a10b20435d62ce
4
+ data.tar.gz: 67d328f6ae917d27f88455dce176073dd8da0cf8dd7bb2be460c886c7a56086f
5
5
  SHA512:
6
- metadata.gz: c7465e3fe1519fc3c025121375e4e20445d7929a5f73f2713ad43223655f8f92ec55911c63b27463e4d925d563f7d15973e5e6d273eb284c212ab8aed48658e6
7
- data.tar.gz: d6c122262c389fe477eda2453ccfa546fd5c21a2a5f9bcbf40e3c0ef3aed2022402299f9b42c5a3d26bd00ea7adc79509b8dc4ea967a64861d2411bcec8bbe4d
6
+ metadata.gz: 4a82e84484c322cd4ee02c92950cf3235daa989b21097c845a5e41a454084afe883ee15444729992e7a63b59318508dbd7f89a15d1abe3d371f8de2cc1e1c551
7
+ data.tar.gz: e427b4224fbed0bd83dcda7dd2ce97683dc1c7df54a2fb673ce15b9d545ee029f87637fb5a8cff685cb7570f7d0e7aeaf3c82e0a5466906773326a2d9d794486
@@ -19,9 +19,15 @@ module TypeBalancer
19
19
  end
20
20
 
21
21
  class_methods do
22
- def balance_by_type(options = {})
22
+ # Accepts either a symbol (type field) or options hash
23
+ def balance_by_type(type_field = nil, **options)
24
+ if type_field.is_a?(Hash)
25
+ options = type_field
26
+ type_field = options[:type_field]
27
+ end
28
+
23
29
  self.type_balancer_options = {
24
- type_field: options[:type_field] || :type
30
+ type_field: type_field || options[:type_field] || :type
25
31
  }
26
32
 
27
33
  return [] unless respond_to?(:all)
@@ -23,31 +23,43 @@ module TypeBalancer
23
23
  records = to_a
24
24
  return empty_relation if records.empty?
25
25
 
26
- # Get type field from options or model configuration
27
26
  type_field = fetch_type_field(options)
28
-
29
- # Balance records using TypeBalancer
30
- balanced = TypeBalancer.balance(records, type_field: type_field)
27
+ balanced = TypeBalancer.balance(records, type_field: type_field)
31
28
  return empty_relation if balanced.nil?
32
29
 
33
- # Handle pagination if requested
34
- if options[:page] || options[:per_page]
35
- page = (options[:page] || 1).to_i
36
- per_page = (options[:per_page] || 20).to_i
37
- offset = (page - 1) * per_page
38
- balanced = balanced[offset, per_page] || []
39
- end
40
-
41
- # Return as relation
42
- TestRelation.new(balanced)
30
+ paged = apply_pagination(balanced, options)
31
+ build_result(paged)
43
32
  end
44
33
 
45
- def all_records = to_a
46
-
47
34
  private
48
35
 
36
+ def apply_pagination(records, options)
37
+ return records unless options[:page] || options[:per_page]
38
+
39
+ page = (options[:page] || 1).to_i
40
+ per_page = (options[:per_page] || 20).to_i
41
+ offset = (page - 1) * per_page
42
+ records[offset, per_page] || []
43
+ end
44
+
45
+ def build_result(balanced)
46
+ if klass.respond_to?(:where) && klass != TestModel
47
+ ids = balanced.map(&:id)
48
+ results = klass.where(id: ids).to_a
49
+ results_by_id = results.index_by(&:id)
50
+ ordered = ids.map { |id| results_by_id[id] }
51
+ self.class.new(ordered)
52
+ else
53
+ self.class.new(balanced)
54
+ end
55
+ end
56
+
49
57
  def empty_relation
50
- TestRelation.new([])
58
+ if klass.respond_to?(:none)
59
+ klass.none
60
+ else
61
+ []
62
+ end
51
63
  end
52
64
 
53
65
  def fetch_type_field(options)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TypeBalancer
4
4
  module Rails
5
- VERSION = '0.1.1'
5
+ VERSION = '0.1.3'
6
6
  end
7
7
  end
@@ -8,3 +8,5 @@ require 'type_balancer'
8
8
  require_relative 'type_balancer/rails/version'
9
9
  require_relative 'type_balancer/rails'
10
10
  require 'type_balancer/rails/collection_methods'
11
+
12
+ require 'type_balancer/rails/railtie' if defined?(Rails)
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.1.1
4
+ version: 0.1.3
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-26 00:00:00.000000000 Z
11
+ date: 2025-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord