Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ jobs:
- { ruby: '3.2', mongoid: '8', rails: '7.2', mongodb: '6.0' }
- { ruby: '3.3', mongoid: '9', rails: '8.1', mongodb: '7.0' }
- { ruby: '3.4', mongoid: '9', rails: '8.1', mongodb: '8.0' }
- { ruby: '4.0', mongoid: '9', rails: '8.1', mongodb: '9.0' }
- { ruby: '4.0', mongoid: '9', rails: '8.1', mongodb: '9.0', coverage: true }
env:
MONGOID_VERSION: ${{ matrix.mongoid }}
RAILS_VERSION: ${{ matrix.rails }}
COVERAGE: ${{ matrix.ruby == '4.0' && 'true' || 'false' }}
BUNDLE_GEMFILE: gemfiles/mongoid_${{ matrix.mongoid }}.gemfile
COVERAGE: ${{ matrix.coverage }}
services:
mongo:
image: mongo:${{ matrix.mongodb }}
Expand All @@ -44,7 +43,7 @@ jobs:
bundle exec ruby -Ilib -e 'require "mongoid-observers"; puts "Mongoid #{Mongoid::VERSION}"'
bundle exec rake
- name: Report coverage to Coveralls
if: matrix.ruby == '4.0'
if: matrix.coverage
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
14 changes: 14 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
4 => '4.2',
5 => '4.2',
6 => '5.2',
7 => '6.1',
8 => '7.2',
9 => '8.1'
}.each do |mongoid, rails|
appraise "mongoid_#{mongoid}" do
gem 'mongoid', "~> #{mongoid}.0"
gem 'railties', "~> #{rails}.0"
gem 'bigdecimal', mongoid < 6 ? '< 2' : '>= 0'
end
end
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 0.4.1 (Next)

* Your contribution here.
* [#11](https://github.com/mongoid/mongoid-observers/pull/11): Add Appraisal-managed Rails integration tests for observer initialization, persisted callbacks and model reloading - [@dblock](https://github.com/dblock).

### 0.4.0 (2026/10/15)

* [#9](https://github.com/mongoid/mongoid-observers/pull/9): Add historical release notes and enforce changelog formatting with Danger - [@dblock](https://github.com/dblock).
Expand Down
10 changes: 3 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
source 'https://rubygems.org'

mongoid_version = ENV.fetch('MONGOID_VERSION', '9')
rails_version = ENV.fetch('RAILS_VERSION', '8.1')

gem 'mongoid', "~> #{mongoid_version}.0"
gem 'railties', "~> #{rails_version}.0"
gem 'bigdecimal', rails_version.to_f < 5 ? '< 2' : '>= 0'
gem 'appraisal', '~> 2.5'
gem 'railties'
gem 'json', '< 3' # Older ActiveSupport passes the removed quirks_mode option.
gem 'ostruct'
gem 'rake'
gem 'rspec', '~> 3.13'
gem 'danger', require: false
gem 'danger-changelog', require: false
gem 'danger-pr-comment', require: false
gem 'simplecov', '~> 0.22', require: false if ENV['COVERAGE'] == 'true'
gem 'simplecov', '~> 0.22', require: false

# Specify your gem's dependencies in mongoid-observers.gemspec
gemspec
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ Observers are singletons and that call instantiates and registers them.

## Development

Run `bundle install` and `bundle exec rake`. Set `MONGOID_VERSION` and `RAILS_VERSION` before bundling to select another dependency combination, or use the files under `gemfiles/` with `BUNDLE_GEMFILE`. Set `MONGODB_HOST` to override `localhost:27017`.
Run `bundle install`, then `bundle exec appraisal mongoid_9 bundle install` and `bundle exec appraisal mongoid_9 rake`. `Appraisals` defines supported Mongoid/Rails pairs; select another appraisal with a compatible Ruby, or use its generated Gemfile with `BUNDLE_GEMFILE`. Set `MONGODB_HOST` to override `localhost:27017`.

The default task runs unit specs and a separate Rails integration suite. Run only the fixture application's tests with `bundle exec appraisal mongoid_9 rake integration`. These boot a real Rails application, persist observed records, and verify observer registration after Rails unloads and reloads the model classes.

Run `COVERAGE=true bundle install` followed by `COVERAGE=true bundle exec rake` to generate SimpleCov coverage in `coverage/`. GitHub Actions uploads the Ruby 4.0 job's report using the official Coveralls action.

Expand Down
16 changes: 10 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ require "rspec/core"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList["spec/**/*_spec.rb"]
spec.pattern = FileList["spec/**/*_spec.rb"].exclude("spec/integration/**/*")
end

task :default => :spec
RSpec::Core::RakeTask.new(:integration) do |spec|
spec.pattern = FileList["spec/integration/**/*_spec.rb"]
end

task :default => [:spec, :integration]

namespace :spec do
%w(mongoid_4 mongoid_5 mongoid_6 mongoid_7 mongoid_8 mongoid_9).each do |gemfile|
desc "Run Tests against #{gemfile}"
task gemfile do
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle install --quiet"
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
sh "bundle exec appraisal #{gemfile} bundle install --quiet"
sh "bundle exec appraisal #{gemfile} rake"
end
end

desc "Run Tests against Mongoid versions (requires compatible Ruby versions)"
task :all do
%w(mongoid_4 mongoid_5 mongoid_6 mongoid_7 mongoid_8 mongoid_9).each do |gemfile|
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle install --quiet"
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
sh "bundle exec appraisal #{gemfile} bundle install --quiet"
sh "bundle exec appraisal #{gemfile} rake"
end
end
end
21 changes: 18 additions & 3 deletions gemfiles/mongoid_4.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
ENV['MONGOID_VERSION'] = '4'
ENV['RAILS_VERSION'] = '4.2'
eval_gemfile '../Gemfile'
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", "~> 2.5"
gem "railties", "~> 4.2.0"
gem "json", "< 3"
gem "ostruct"
gem "rake"
gem "rspec", "~> 3.13"
gem "danger", require: false
gem "danger-changelog", require: false
gem "danger-pr-comment", require: false
gem "simplecov", "~> 0.22", require: false
gem "mongoid", "~> 4.0"
gem "bigdecimal", "< 2"

gemspec path: "../"
21 changes: 18 additions & 3 deletions gemfiles/mongoid_5.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
ENV['MONGOID_VERSION'] = '5'
ENV['RAILS_VERSION'] = '4.2'
eval_gemfile '../Gemfile'
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", "~> 2.5"
gem "railties", "~> 4.2.0"
gem "json", "< 3"
gem "ostruct"
gem "rake"
gem "rspec", "~> 3.13"
gem "danger", require: false
gem "danger-changelog", require: false
gem "danger-pr-comment", require: false
gem "simplecov", "~> 0.22", require: false
gem "mongoid", "~> 5.0"
gem "bigdecimal", "< 2"

gemspec path: "../"
21 changes: 18 additions & 3 deletions gemfiles/mongoid_6.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
ENV['MONGOID_VERSION'] = '6'
ENV['RAILS_VERSION'] = '5.2'
eval_gemfile '../Gemfile'
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", "~> 2.5"
gem "railties", "~> 5.2.0"
gem "json", "< 3"
gem "ostruct"
gem "rake"
gem "rspec", "~> 3.13"
gem "danger", require: false
gem "danger-changelog", require: false
gem "danger-pr-comment", require: false
gem "simplecov", "~> 0.22", require: false
gem "mongoid", "~> 6.0"
gem "bigdecimal", ">= 0"

gemspec path: "../"
21 changes: 18 additions & 3 deletions gemfiles/mongoid_7.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
ENV['MONGOID_VERSION'] = '7'
ENV['RAILS_VERSION'] = '6.1'
eval_gemfile '../Gemfile'
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", "~> 2.5"
gem "railties", "~> 6.1.0"
gem "json", "< 3"
gem "ostruct"
gem "rake"
gem "rspec", "~> 3.13"
gem "danger", require: false
gem "danger-changelog", require: false
gem "danger-pr-comment", require: false
gem "simplecov", "~> 0.22", require: false
gem "mongoid", "~> 7.0"
gem "bigdecimal", ">= 0"

gemspec path: "../"
21 changes: 18 additions & 3 deletions gemfiles/mongoid_8.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
ENV['MONGOID_VERSION'] = '8'
ENV['RAILS_VERSION'] = '7.2'
eval_gemfile '../Gemfile'
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", "~> 2.5"
gem "railties", "~> 7.2.0"
gem "json", "< 3"
gem "ostruct"
gem "rake"
gem "rspec", "~> 3.13"
gem "danger", require: false
gem "danger-changelog", require: false
gem "danger-pr-comment", require: false
gem "simplecov", "~> 0.22", require: false
gem "mongoid", "~> 8.0"
gem "bigdecimal", ">= 0"

gemspec path: "../"
21 changes: 18 additions & 3 deletions gemfiles/mongoid_9.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
ENV['MONGOID_VERSION'] = '9'
ENV['RAILS_VERSION'] = '8.1'
eval_gemfile '../Gemfile'
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", "~> 2.5"
gem "railties", "~> 8.1.0"
gem "json", "< 3"
gem "ostruct"
gem "rake"
gem "rspec", "~> 3.13"
gem "danger", require: false
gem "danger-changelog", require: false
gem "danger-pr-comment", require: false
gem "simplecov", "~> 0.22", require: false
gem "mongoid", "~> 9.0"
gem "bigdecimal", ">= 0"

gemspec path: "../"
6 changes: 6 additions & 0 deletions spec/integration/app/app/models/entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Entry
include Mongoid::Document

field :name, type: String
field :observed, type: Boolean, default: false
end
5 changes: 5 additions & 0 deletions spec/integration/app/app/models/entry_observer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class EntryObserver < Mongoid::Observer
def before_create(entry)
entry.observed = true
end
end
17 changes: 17 additions & 0 deletions spec/integration/app/config/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "logger"
require "rails"
require "mongoid-observers"
require "rails/application"

module ObserverIntegration
class Application < Rails::Application
config.root = File.expand_path("..", __dir__)
config.eager_load = false
config.cache_classes = false
config.logger = ActiveSupport::Logger.new($stdout)
config.log_level = :error
config.secret_key_base = "a" * 64
config.session_store :disabled
config.mongoid.observers = [:entry_observer]
end
end
4 changes: 4 additions & 0 deletions spec/integration/app/config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require_relative "application"

Mongoid.load! File.expand_path("../../../config/mongoid.yml", __dir__), :test
ObserverIntegration::Application.initialize!
27 changes: 27 additions & 0 deletions spec/integration/observers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require_relative "rails_helper"

describe "Rails observer integration" do
it "applies application configuration and invokes observers on persisted records" do
expect(Mongoid.observers).to eq([:entry_observer])
entry = Entry.create!(name: "initialized")

expect(entry.reload.observed).to eq(true)
end

it "reloads models and registers their observers again" do
original_model = Entry
original_observer = EntryObserver

if Rails.application.respond_to?(:reloader)
Rails.application.reloader.reload!
else
ActionDispatch::Reloader.cleanup!
ActionDispatch::Reloader.prepare!
end

expect(Entry).not_to equal(original_model)
expect(EntryObserver).not_to equal(original_observer)
entry = Entry.create!(name: "reloaded")
expect(entry.reload.observed).to eq(true)
end
end
16 changes: 16 additions & 0 deletions spec/integration/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ENV["RAILS_ENV"] = "test"

if ENV["COVERAGE"] == "true"
require "simplecov"
SimpleCov.command_name "Rails integration"
SimpleCov.start do
track_files "lib/**/*.rb"
add_filter "/spec/"
end
end

require_relative "app/config/environment"

RSpec.configure do |config|
config.before(:each) { Mongoid.purge! }
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
if ENV["COVERAGE"] == "true"
require "simplecov"
SimpleCov.command_name "Unit"
SimpleCov.start do
track_files "lib/**/*.rb"
add_filter "/spec/"
Expand Down
7 changes: 7 additions & 0 deletions spec/version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "spec_helper"

describe Mongoid::Observers do
it "defines a semantic version" do
expect(Mongoid::Observers::VERSION).to match(/\A\d+\.\d+\.\d+\z/)
end
end
Loading