type_balancer_rails 0.2.0 → 0.2.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.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +4 -0
  3. data/.rubocop.yml +8 -23
  4. data/CHANGELOG.md +14 -2
  5. data/README.md +15 -0
  6. data/example/.dockerignore +51 -0
  7. data/example/.rspec +1 -0
  8. data/example/.rubocop.yml +132 -0
  9. data/example/Dockerfile +72 -0
  10. data/example/Gemfile +77 -0
  11. data/example/Gemfile.lock +449 -0
  12. data/example/README.md +42 -0
  13. data/example/Rakefile +6 -0
  14. data/example/app/assets/images/.keep +0 -0
  15. data/example/app/assets/stylesheets/application.css +10 -0
  16. data/example/app/controllers/application_controller.rb +4 -0
  17. data/example/app/controllers/concerns/.keep +0 -0
  18. data/example/app/controllers/contents_controller.rb +11 -0
  19. data/example/app/controllers/posts_controller.rb +5 -0
  20. data/example/app/helpers/application_helper.rb +2 -0
  21. data/example/app/helpers/contents_helper.rb +2 -0
  22. data/example/app/helpers/posts_helper.rb +2 -0
  23. data/example/app/jobs/application_job.rb +7 -0
  24. data/example/app/mailers/application_mailer.rb +4 -0
  25. data/example/app/models/application_record.rb +3 -0
  26. data/example/app/models/concerns/.keep +0 -0
  27. data/example/app/models/content.rb +3 -0
  28. data/example/app/models/post.rb +3 -0
  29. data/example/app/views/contents/balance_by_category.html.erb +27 -0
  30. data/example/app/views/contents/balance_by_content_type.html.erb +2 -0
  31. data/example/app/views/layouts/application.html.erb +27 -0
  32. data/example/app/views/layouts/mailer.html.erb +13 -0
  33. data/example/app/views/layouts/mailer.text.erb +1 -0
  34. data/example/app/views/posts/balance_by_category.html.erb +2 -0
  35. data/example/app/views/posts/index.html.erb +25 -0
  36. data/example/app/views/pwa/manifest.json.erb +22 -0
  37. data/example/app/views/pwa/service-worker.js +26 -0
  38. data/example/bin/brakeman +7 -0
  39. data/example/bin/dev +2 -0
  40. data/example/bin/docker-entrypoint +14 -0
  41. data/example/bin/rails +4 -0
  42. data/example/bin/rake +4 -0
  43. data/example/bin/rubocop +8 -0
  44. data/example/bin/setup +34 -0
  45. data/example/bin/thrust +5 -0
  46. data/example/config/application.rb +42 -0
  47. data/example/config/boot.rb +4 -0
  48. data/example/config/cable.yml +10 -0
  49. data/example/config/credentials.yml.enc +1 -0
  50. data/example/config/database.yml +41 -0
  51. data/example/config/environment.rb +5 -0
  52. data/example/config/environments/development.rb +72 -0
  53. data/example/config/environments/production.rb +89 -0
  54. data/example/config/environments/test.rb +53 -0
  55. data/example/config/initializers/assets.rb +7 -0
  56. data/example/config/initializers/content_security_policy.rb +25 -0
  57. data/example/config/initializers/filter_parameter_logging.rb +8 -0
  58. data/example/config/initializers/inflections.rb +16 -0
  59. data/example/config/locales/en.yml +31 -0
  60. data/example/config/master.key +1 -0
  61. data/example/config/puma.rb +41 -0
  62. data/example/config/routes.rb +8 -0
  63. data/example/config/storage.yml +34 -0
  64. data/example/config.ru +6 -0
  65. data/example/db/migrate/20250427174733_create_posts.rb +10 -0
  66. data/example/db/migrate/20250427174747_create_contents.rb +11 -0
  67. data/example/db/schema.rb +28 -0
  68. data/example/db/seeds.rb +9 -0
  69. data/example/lib/tasks/.keep +0 -0
  70. data/example/lib/tasks/dev_fixtures.rake +12 -0
  71. data/example/script/.keep +0 -0
  72. data/example/spec/controllers/contents_controller_spec.rb +27 -0
  73. data/example/spec/controllers/posts_controller_spec.rb +16 -0
  74. data/example/spec/features/contents_balancing_spec.rb +18 -0
  75. data/example/spec/features/posts_balancing_spec.rb +16 -0
  76. data/example/spec/models/content_spec.rb +19 -0
  77. data/example/spec/models/post_spec.rb +12 -0
  78. data/example/spec/rails_helper.rb +35 -0
  79. data/example/spec/spec_helper.rb +94 -0
  80. data/example/storage/.keep +0 -0
  81. data/example/storage/development.sqlite3 +0 -0
  82. data/example/storage/test.sqlite3 +0 -0
  83. data/example/test/fixtures/contents.yml +19 -0
  84. data/example/test/fixtures/posts.yml +16 -0
  85. data/example/vendor/.keep +0 -0
  86. data/lib/type_balancer/rails/collection_methods.rb +58 -33
  87. data/lib/type_balancer/rails/version.rb +1 -1
  88. data/type_balancer_rails.gemspec +1 -1
  89. metadata +85 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b638243aa1d2aa29637bf2696d7ce7e8625bed13c17bb73122ddb0d9f1356e6a
4
- data.tar.gz: 4ac1aa33b569d4da75786e0a8a14ae35a5c2fb20fd3d86cdb1637ef630719809
3
+ metadata.gz: 57e73c24c2f2b4fab4f8cf8588cfe74a2be42f2392ff39195f7cb2021d8640f7
4
+ data.tar.gz: 2268520c561f4747aa76fd9913f8b33e05ba629ae7829a8bd863a2c58107c1e5
5
5
  SHA512:
6
- metadata.gz: 7fd6d6bcd22be7a11c2bd9d5181253c31f0df27966d7c4a68258c3a7e9b6690a42c4e7e4320d2ed543c4155caf2ae7a79e361202bf37661f2f6f6282f98ff29f
7
- data.tar.gz: 22503105265aa58554a3e839d431ca3e4ac8d15f39c2b90a3099175cdd383bbdf4deb0982c97886a50ea6da71057258e308e8e4467ced627980c689dfd205f85
6
+ metadata.gz: 7c8d71e437fc6ed17b3549581397e890b1d8e6ba0eb69161d9760d739dd0c32ba44765db7f7e2b215db6d96a59273064801bc8a0eea8f88f268745eb34c2f30a
7
+ data.tar.gz: db58f0c79fd64b464d5756e8636abcc6c79c4c7efdc1466ac7ea3578a0a498ede362eefd589779683fbb22488cf9dea5ec3759ac302b0024f0c8e1bd8dfba590
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
4
+ --exclude-pattern 'example/spec/**/*'
data/.rubocop.yml CHANGED
@@ -1,9 +1,11 @@
1
- require:
2
- - rubocop-rspec
1
+ plugins:
3
2
  - rubocop-rails
4
3
  - rubocop-capybara
5
4
  - rubocop-factory_bot
6
5
 
6
+ require:
7
+ - rubocop-rspec
8
+
7
9
  AllCops:
8
10
  TargetRubyVersion: 3.2
9
11
  NewCops: enable
@@ -17,6 +19,7 @@ AllCops:
17
19
  - 'db/schema.rb'
18
20
  - 'db/migrate/*.rb'
19
21
  - 'Gemfile.lock'
22
+ - 'example/**/*'
20
23
 
21
24
  Style/Documentation:
22
25
  Enabled: false
@@ -75,6 +78,9 @@ RSpec/SubjectStub:
75
78
  RSpec/StubbedMock:
76
79
  Enabled: false
77
80
 
81
+ RSpec/ReceiveMessages:
82
+ Enabled: false
83
+
78
84
  RSpec/DescribeClass:
79
85
  Exclude:
80
86
  - 'spec/integration/**/*_spec.rb'
@@ -118,27 +124,6 @@ FactoryBot/FactoryNameStyle:
118
124
  FactoryBot/SyntaxMethods:
119
125
  Enabled: false
120
126
 
121
- RSpecRails/AvoidSetupHook:
122
- Enabled: false
123
-
124
- RSpecRails/HaveHttpStatus:
125
- Enabled: false
126
-
127
- RSpecRails/HttpStatus:
128
- Enabled: false
129
-
130
- RSpecRails/InferredSpecType:
131
- Enabled: false
132
-
133
- RSpecRails/MinitestAssertions:
134
- Enabled: false
135
-
136
- RSpecRails/NegationBeValid:
137
- Enabled: false
138
-
139
- RSpecRails/TravelAround:
140
- Enabled: false
141
-
142
127
  Layout/LineLength:
143
128
  Max: 120
144
129
 
data/CHANGELOG.md CHANGED
@@ -1,12 +1,24 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.2.0] - 2024-04-27
3
+ ## [0.2.3] - 2025-04-29
4
+
5
+ - Fixed issue with returning correctly balance items
6
+ - Fixed RuboCop configuration and addressed violations
7
+ - Reorganized CI workflow to properly handle example app tests as integration suite
8
+ - Improved test organization and coverage
9
+ - Added proper RuboCop configuration for example app with inheritance handling
10
+
11
+ ## [0.2.2] - 2025-04-27
12
+
13
+ - Version sync release: Ensures the gem version and git tag match after a previous mismatch (v0.2.1 tag pointed to v0.2.0 code). No code changes from 0.2.1.
14
+
15
+ ## [0.2.0] - 2025-04-27
4
16
 
5
17
  - Refactored `balance_by_type` to only send `id` and type field to TypeBalancer for efficiency and clarity
6
18
  - Ensured robust ordering of returned records based on balanced ids (supports flat and nested balancer output)
7
19
  - Full TDD coverage for all new and refactored logic (unit and integration tests)
8
20
  - Added GitHub Actions workflow for automated gem releases on tag push
9
21
 
10
- ## [0.1.0] - 2025-04-10
22
+ ## [0.1.0] - 2025-04-26
11
23
 
12
24
  - Initial release
data/README.md CHANGED
@@ -28,6 +28,17 @@ $ gem install type_balancer_rails
28
28
 
29
29
  ## Usage
30
30
 
31
+ To balance records by a given type field, use the following syntax:
32
+
33
+ ```ruby
34
+ Post.balance_by_type(type_field: :media_type)
35
+ Content.balance_by_type(type_field: :category)
36
+ ```
37
+
38
+ > **Note:** Passing a symbol directly (e.g., `balance_by_type(:media_type)`) is not currently supported. Always use the options hash syntax as shown above.
39
+
40
+ > **Note:** Type field values are automatically converted to strings, so you don't need to handle string conversion manually. For example, both `:article` and `"article"` as type values will work correctly.
41
+
31
42
  ### Basic Usage
32
43
 
33
44
  The gem adds a `balance_by_type` method to your ActiveRecord relations. Here's how to use it:
@@ -71,6 +82,10 @@ The `balance_by_type` method preserves the ActiveRecord query interface:
71
82
  .per(20)
72
83
  ```
73
84
 
85
+ ## Planned Enhancements
86
+
87
+ - Support for passing a symbol directly to `balance_by_type`, e.g., `balance_by_type(:media_type)`, for more ergonomic usage. This is planned for a future version.
88
+
74
89
  ## Development
75
90
 
76
91
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,51 @@
1
+ # See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files.
2
+
3
+ # Ignore git directory.
4
+ /.git/
5
+ /.gitignore
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore all environment files.
11
+ /.env*
12
+
13
+ # Ignore all default key files.
14
+ /config/master.key
15
+ /config/credentials/*.key
16
+
17
+ # Ignore all logfiles and tempfiles.
18
+ /log/*
19
+ /tmp/*
20
+ !/log/.keep
21
+ !/tmp/.keep
22
+
23
+ # Ignore pidfiles, but keep the directory.
24
+ /tmp/pids/*
25
+ !/tmp/pids/.keep
26
+
27
+ # Ignore storage (uploaded files in development and any SQLite databases).
28
+ /storage/*
29
+ !/storage/.keep
30
+ /tmp/storage/*
31
+ !/tmp/storage/.keep
32
+
33
+ # Ignore assets.
34
+ /node_modules/
35
+ /app/assets/builds/*
36
+ !/app/assets/builds/.keep
37
+ /public/assets
38
+
39
+ # Ignore CI service files.
40
+ /.github
41
+
42
+ # Ignore Kamal files.
43
+ /config/deploy*.yml
44
+ /.kamal
45
+
46
+ # Ignore development files
47
+ /.devcontainer
48
+
49
+ # Ignore Docker-related files
50
+ /.dockerignore
51
+ /Dockerfile*
data/example/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,132 @@
1
+ # Omakase Ruby styling for Rails
2
+ inherit_gem: { rubocop-rails-omakase: rubocop.yml }
3
+
4
+ inherit_mode:
5
+ override:
6
+ - Exclude
7
+ - Include
8
+
9
+ plugins:
10
+ - rubocop-capybara
11
+ - rubocop-factory_bot
12
+
13
+ require:
14
+ - rubocop-rspec
15
+
16
+ # Overwrite or add rules to create your own house style
17
+ #
18
+ # # Use `[a, [b, c]]` not `[ a, [ b, c ] ]`
19
+ # Layout/SpaceInsideArrayLiteralBrackets:
20
+ # Enabled: false
21
+
22
+ AllCops:
23
+ NewCops: enable
24
+ Include:
25
+ - '**/*.rb'
26
+ - '**/*.rake'
27
+ - '**/Rakefile'
28
+ - '**/config.ru'
29
+ Exclude:
30
+ - 'bin/**/*'
31
+ - 'db/**/*'
32
+ - 'tmp/**/*'
33
+ - 'vendor/**/*'
34
+ - 'log/**/*'
35
+ - 'node_modules/**/*'
36
+
37
+ RSpec/IncludeExamples:
38
+ Enabled: true
39
+
40
+ RSpec/MultipleExpectations:
41
+ Enabled: false
42
+
43
+ RSpec/ExampleLength:
44
+ Enabled: false
45
+
46
+ RSpec/NestedGroups:
47
+ Max: 4
48
+
49
+ RSpec/MultipleMemoizedHelpers:
50
+ Enabled: false
51
+
52
+ RSpec/MessageSpies:
53
+ Enabled: false
54
+
55
+ RSpec/VerifiedDoubles:
56
+ Enabled: false
57
+
58
+ RSpec/MessageChain:
59
+ Enabled: false
60
+
61
+ RSpec/NoExpectationExample:
62
+ Enabled: false
63
+
64
+ RSpec/SubjectStub:
65
+ Enabled: false
66
+
67
+ RSpec/StubbedMock:
68
+ Enabled: false
69
+
70
+ RSpec/DescribeClass:
71
+ Exclude:
72
+ - 'spec/integration/**/*_spec.rb'
73
+
74
+ RSpec/RepeatedExample:
75
+ Enabled: false
76
+
77
+ RSpec/MultipleDescribes:
78
+ Enabled: false
79
+
80
+ RSpec/RepeatedExampleGroupDescription:
81
+ Enabled: false
82
+
83
+ Capybara/FindAllFirst:
84
+ Enabled: true
85
+
86
+ Capybara/MatchStyle:
87
+ Enabled: true
88
+
89
+ Capybara/NegationMatcher:
90
+ Enabled: true
91
+
92
+ Capybara/NegationMatcherAfterVisit:
93
+ Enabled: true
94
+
95
+ Capybara/RedundantWithinFind:
96
+ Enabled: true
97
+
98
+ Capybara/SpecificActions:
99
+ Enabled: true
100
+
101
+ Capybara/SpecificFinders:
102
+ Enabled: true
103
+
104
+ Capybara/SpecificMatcher:
105
+ Enabled: true
106
+
107
+ Capybara/RSpec/HaveSelector:
108
+ Enabled: true
109
+
110
+ FactoryBot/AssociationStyle:
111
+ Enabled: true
112
+
113
+ FactoryBot/ConsistentParenthesesStyle:
114
+ Enabled: true
115
+
116
+ FactoryBot/ExcessiveCreateList:
117
+ Enabled: true
118
+
119
+ FactoryBot/FactoryAssociationWithStrategy:
120
+ Enabled: true
121
+
122
+ FactoryBot/FactoryNameStyle:
123
+ Enabled: true
124
+
125
+ FactoryBot/IdSequence:
126
+ Enabled: true
127
+
128
+ FactoryBot/RedundantFactoryOption:
129
+ Enabled: true
130
+
131
+ FactoryBot/SyntaxMethods:
132
+ Enabled: true
@@ -0,0 +1,72 @@
1
+ # syntax=docker/dockerfile:1
2
+ # check=error=true
3
+
4
+ # This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
5
+ # docker build -t example .
6
+ # docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name example example
7
+
8
+ # For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
9
+
10
+ # Make sure RUBY_VERSION matches the Ruby version in .ruby-version
11
+ ARG RUBY_VERSION=3.2.5
12
+ FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
13
+
14
+ # Rails app lives here
15
+ WORKDIR /rails
16
+
17
+ # Install base packages
18
+ RUN apt-get update -qq && \
19
+ apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
20
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
21
+
22
+ # Set production environment
23
+ ENV RAILS_ENV="production" \
24
+ BUNDLE_DEPLOYMENT="1" \
25
+ BUNDLE_PATH="/usr/local/bundle" \
26
+ BUNDLE_WITHOUT="development"
27
+
28
+ # Throw-away build stage to reduce size of final image
29
+ FROM base AS build
30
+
31
+ # Install packages needed to build gems
32
+ RUN apt-get update -qq && \
33
+ apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \
34
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
35
+
36
+ # Install application gems
37
+ COPY Gemfile Gemfile.lock ./
38
+ RUN bundle install && \
39
+ rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
40
+ bundle exec bootsnap precompile --gemfile
41
+
42
+ # Copy application code
43
+ COPY . .
44
+
45
+ # Precompile bootsnap code for faster boot times
46
+ RUN bundle exec bootsnap precompile app/ lib/
47
+
48
+ # Precompiling assets for production without requiring secret RAILS_MASTER_KEY
49
+ RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
50
+
51
+
52
+
53
+
54
+ # Final stage for app image
55
+ FROM base
56
+
57
+ # Copy built artifacts: gems, application
58
+ COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
59
+ COPY --from=build /rails /rails
60
+
61
+ # Run and own only the runtime files as a non-root user for security
62
+ RUN groupadd --system --gid 1000 rails && \
63
+ useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
64
+ chown -R rails:rails db log storage tmp
65
+ USER 1000:1000
66
+
67
+ # Entrypoint prepares the database.
68
+ ENTRYPOINT ["/rails/bin/docker-entrypoint"]
69
+
70
+ # Start server via Thruster by default, this can be overwritten at runtime
71
+ EXPOSE 80
72
+ CMD ["./bin/thrust", "./bin/rails", "server"]
data/example/Gemfile ADDED
@@ -0,0 +1,77 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
4
+ gem "rails", "~> 8.0.2"
5
+ # The modern asset pipeline for Rails [https://github.com/rails/propshaft]
6
+ gem "propshaft"
7
+ # Use sqlite3 as the database for Active Record
8
+ gem "sqlite3", ">= 2.1"
9
+ # Use the Puma web server [https://github.com/puma/puma]
10
+ gem "puma", ">= 5.0"
11
+ # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
12
+ gem "importmap-rails"
13
+ # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
14
+ gem "turbo-rails"
15
+ # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
16
+ gem "stimulus-rails"
17
+ # Build JSON APIs with ease [https://github.com/rails/jbuilder]
18
+ gem "jbuilder"
19
+
20
+ # Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
21
+ # gem "bcrypt", "~> 3.1.7"
22
+
23
+ # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
24
+ gem "tzinfo-data", platforms: %i[ windows jruby ]
25
+
26
+ # Use the database-backed adapters for Rails.cache, Active Job, and Action Cable
27
+ gem "solid_cache"
28
+ gem "solid_queue"
29
+ gem "solid_cable"
30
+
31
+ # Reduces boot times through caching; required in config/boot.rb
32
+ gem "bootsnap", require: false
33
+
34
+ # Deploy this application anywhere as a Docker container [https://kamal-deploy.org]
35
+ gem "kamal", require: false
36
+
37
+ # Add HTTP asset caching/compression and X-Sendfile acceleration to Puma [https://github.com/basecamp/thruster/]
38
+ gem "thruster", require: false
39
+
40
+ # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
41
+ # gem "image_processing", "~> 1.2"
42
+
43
+ group :development, :test do
44
+ # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
45
+ gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"
46
+
47
+ # Static analysis for security vulnerabilities [https://brakemanscanner.org/]
48
+ gem "brakeman", require: false
49
+
50
+ gem "pry"
51
+
52
+ # Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
53
+ gem "rubocop-rails-omakase", require: false
54
+
55
+ gem 'type_balancer_rails', path: '..'
56
+ end
57
+
58
+ group :development do
59
+ # Use console on exceptions pages [https://github.com/rails/web-console]
60
+ gem "web-console"
61
+ end
62
+
63
+ gem "rspec-rails", "~> 7.1", :groups => [:development, :test]
64
+
65
+ gem "database_cleaner-active_record", "~> 2.2", :group => :test
66
+
67
+ group :test do
68
+ gem 'rails-controller-testing'
69
+ gem 'capybara'
70
+ gem 'launchy'
71
+ end
72
+
73
+ gem "rubocop-rspec", "~> 3.6", :groups => [:development, :test]
74
+
75
+ gem "rubocop-capybara", "~> 2.22", :groups => [:development, :test]
76
+
77
+ gem "rubocop-factory_bot", "~> 2.27", :groups => [:development, :test]