Skip to content

Commit

Permalink
Support Active Record <8.0.1
Browse files Browse the repository at this point in the history
ActiveModel::API is only included in ActiveRecord::Base since rails/rails@2c57ec6.

It gets a little messy to explicitly handle ActiveRecord before then. I'd rather not run Active Record in specs and need to run a sqlite DB or whatever.

Instead, let us for now settle for mixing this into all objects, like with_options.

The gem is still conceptually for ActiveModel/ActiveRecord, even though it no longer strictly depends on either outside of specs.
  • Loading branch information
henrik committed Dec 22, 2024
1 parent 4da4e52 commit 7624be4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.1.0] - 2024-12-22

- Support Active Record prior to 8.0.1

## [1.0.0] - 2024-12-22

- Initial release
3 changes: 1 addition & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
PATH
remote: .
specs:
activemodel-with_conditions (1.0.0)
activemodel
activemodel-with_conditions (1.1.0)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 0 additions & 2 deletions activemodel-with_conditions.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "activemodel"
end
15 changes: 4 additions & 11 deletions lib/with_conditions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "active_model"
require_relative "with_conditions/options_merger"
require_relative "with_conditions/version"

Expand All @@ -16,13 +15,7 @@ def with_conditions(if: nil, unless: nil, &block)
end
end

module ActiveModel
module API
singleton_class.prepend(Module.new do
def included(klass)
super
klass.extend WithConditions
end
end)
end
end
# To keep things simple, we include this in all objects, like `with_options`.
#
# Otherwise we would want specs that include Active Record, and we'd want to consider that different versions of Rails use Active Model in different ways (e.g. only recent Rails per 2024-12-22 include ActiveModel::API in ActiveRecord::Base`).
Object.extend WithConditions
2 changes: 1 addition & 1 deletion lib/with_conditions/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module WithConditions
VERSION = "1.0.0"
VERSION = "1.1.0"
end

0 comments on commit 7624be4

Please sign in to comment.