Skip to content

Commit

Permalink
Add rubocop + add Github Actions (#67)
Browse files Browse the repository at this point in the history
* Add rubocop and fix rubocop items
* Replace Travis CI with Github Actions
* Fix specs
* Fix specs + more rubies
* Enforce mongoid less than 7.3
  • Loading branch information
johnnyshields authored Jul 24, 2021
1 parent 1287613 commit 128f2a9
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 128 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI RSpec Test

on: [push, pull_request]

jobs:
build:
name: >-
${{ matrix.ruby }}
env:
CI: true
TESTOPTS: -v
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: true
matrix:
ruby: [2.4, 2.5, 2.6, 2.7, 3.0, jruby, truffleruby]
experimental: [false]
include:
- ruby: head
experimental: true
- ruby: jruby-head
experimental: true
- ruby: truffleruby-head
experimental: true

steps:
- name: repo checkout
uses: actions/checkout@v2

- name: start mongodb
uses: supercharge/[email protected]
with:
mongodb-version: 4.4
mongodb-replica-set: rs0

- name: load ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler: 2

- name: bundle install
run: bundle install --jobs 4 --retry 3

- name: test
timeout-minutes: 10
run: bundle exec rake spec
continue-on-error: ${{ matrix.experimental }}
48 changes: 48 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
inherit_from: .rubocop_todo.yml

AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 2.6
Exclude:
- spec/**/*
- vendor/**/*

Bundler/DuplicatedGem:
Exclude:
- 'Gemfile'

Gemspec/RequiredRubyVersion:
Enabled: false

Layout/EmptyLineAfterGuardClause:
Enabled: false

Layout/IndentationWidth:
IgnoredPatterns:
- '^\s*module'

Layout/LineLength:
Max: 120

Layout/SpaceInsideBlockBraces:
SpaceBeforeBlockParameters: false

Naming/FileName:
Exclude:
- 'lib/mongoid-paranoia.rb'

Style/Documentation:
Enabled: false

Style/DocumentDynamicEvalDefinition:
Enabled: false

Style/DoubleNegation:
Enabled: false

Style/OptionalBooleanParameter:
Enabled: false

Style/RaiseArgs:
EnforcedStyle: compact
20 changes: 20 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Metrics/AbcSize:
Enabled: false

Metrics/BlockLength:
Enabled: false

Metrics/ClassLength:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/ModuleLength:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'
gemspec name: 'mongoid_paranoia'

Expand Down
16 changes: 9 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require "bundler"
# frozen_string_literal: true

require 'bundler'
Bundler.setup
Bundler::GemHelper.install_tasks

require "rspec/core/rake_task"
require "rake"
require 'rspec/core/rake_task'
require 'rake'

$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift File.expand_path('lib', __dir__)

RSpec::Core::RakeTask.new("spec") do |spec|
spec.pattern = "spec/**/*_spec.rb"
RSpec::Core::RakeTask.new('spec') do |spec|
spec.pattern = 'spec/**/*_spec.rb'
end

task :default => :spec
task default: :spec
4 changes: 3 additions & 1 deletion lib/mongoid-paranoia.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require "mongoid/paranoia"
# frozen_string_literal: true

require 'mongoid/paranoia'
63 changes: 33 additions & 30 deletions lib/mongoid/paranoia.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# encoding: utf-8
# frozen_string_literal: true

require 'mongoid/paranoia/monkey_patches'
require 'mongoid/paranoia/configuration'
require 'active_support'
require 'active_support/deprecation'

module Mongoid

# Include this module to get soft deletion of root level documents.
# This will add a deleted_at field to the +Document+, managed automatically.
# Potentially incompatible with unique indices. (if collisions with deleted items)
Expand All @@ -20,25 +20,23 @@ module Paranoia
extend ActiveSupport::Concern

class << self
attr_accessor :configuration
end

def self.configuration
@configuration ||= Configuration.new
end
def configuration
@configuration ||= Configuration.new
end

def self.reset
@configuration = Configuration.new
end
def reset
@configuration = Configuration.new
end

# Allow the paranoid +Document+ to use an alternate field name for deleted_at.
#
# @example
# Mongoid::Paranoia.configure do |c|
# c.paranoid_field = :myFieldName
# end
def self.configure
yield(configuration)
# Allow the paranoid +Document+ to use an alternate field name for deleted_at.
#
# @example
# Mongoid::Paranoia.configure do |c|
# c.paranoid_field = :myFieldName
# end
def configure
yield(configuration)
end
end

included do
Expand Down Expand Up @@ -97,7 +95,13 @@ def persisted?
alias orig_remove :remove

def remove(_ = {})
return false unless catch(:abort) { apply_delete_dependencies! }
return false unless catch(:abort) do
if respond_to?(:apply_destroy_dependencies!)
apply_destroy_dependencies!
else
apply_delete_dependencies!
end
end
time = self.deleted_at = Time.now
_paranoia_update('$set' => { paranoid_field => time })
@destroyed = true
Expand Down Expand Up @@ -129,7 +133,7 @@ def delete!
def destroyed?
(@destroyed ||= false) || !!deleted_at
end
alias :deleted? :destroyed?
alias deleted? destroyed?

# Restores a previously soft-deleted document. Handles this by removing the
# deleted_at flag.
Expand All @@ -146,8 +150,8 @@ def destroyed?
# @since 1.0.0
def restore(opts = {})
run_callbacks(:restore) do
_paranoia_update("$unset" => { paranoid_field => true })
attributes.delete("deleted_at")
_paranoia_update('$unset' => { paranoid_field => true })
attributes.delete('deleted_at')
@destroyed = false
restore_relations if opts[:recursive]
true
Expand All @@ -160,13 +164,12 @@ def to_param
end

def restore_relations
self.relations.each_pair do |name, association|
relations.each_pair do |name, association|
next unless association.dependent == :destroy
relation = self.send(name)
if relation.present? && relation.paranoid?
Array.wrap(relation).each do |doc|
doc.restore(:recursive => true)
end
relation = send(name)
next unless relation.present? && relation.paranoid?
Array.wrap(relation).each do |doc|
doc.restore(recursive: true)
end
end
end
Expand All @@ -180,7 +183,7 @@ def restore_relations
#
# @return [ Collection ] The root collection.
def paranoid_collection
embedded? ? _root.collection : self.collection
embedded? ? _root.collection : collection
end

# Get the field to be used for paranoid operations.
Expand Down
2 changes: 2 additions & 0 deletions lib/mongoid/paranoia/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Mongoid
module Paranoia
class Configuration
Expand Down
7 changes: 4 additions & 3 deletions lib/mongoid/paranoia/monkey_patches.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true

module Mongoid
module Paranoia
module Document
Expand All @@ -14,7 +15,7 @@ module Document
end
end

Mongoid::Document.send(:include, Mongoid::Paranoia::Document)
Mongoid::Document.include Mongoid::Paranoia::Document

module Mongoid
module Association
Expand All @@ -38,7 +39,7 @@ def destroy(parent, relation, doc)
if !doc.embedded? || parent.new_record? || doc.paranoid?
destroy_document(relation, doc)
else
parent.flagged_destroys.push(->{ destroy_document(relation, doc) })
parent.flagged_destroys.push(-> { destroy_document(relation, doc) })
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/mongoid/paranoia/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Mongoid
module Paranoia
VERSION = '0.4.0'.freeze
VERSION = '0.4.0'
end
end
4 changes: 3 additions & 1 deletion lib/mongoid_paranoia.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require "mongoid/paranoia"
# frozen_string_literal: true

require 'mongoid/paranoia'
15 changes: 9 additions & 6 deletions mongoid_paranoia.gemspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path('lib', __dir__)
require 'mongoid/paranoia/version'

Gem::Specification.new do |gem|
gem.name = 'mongoid_paranoia'
gem.version = Mongoid::Paranoia::VERSION
gem.authors = ['Durran Jordan', 'Josef Šimánek']
gem.email = ['[email protected]', '[email protected]']
gem.description = %q{There may be times when you don't want documents to actually get deleted from the database, but "flagged" as deleted. Mongoid provides a Paranoia module to give you just that.}
gem.summary = %q{Paranoid documents}
gem.description = 'Provides a Paranoia module documents which soft-deletes documents.'
gem.summary = 'Paranoid documents'
gem.homepage = 'https://github.com/simi/mongoid-paranoia'
gem.license = 'MIT'

gem.files = Dir.glob('lib/**/*') + %w(LICENSE README.md)
gem.files = Dir.glob('lib/**/*') + %w[LICENSE README.md]
gem.test_files = Dir.glob('{perf,spec}/**/*')
gem.require_paths = ['lib']

gem.add_dependency 'mongoid', '~> 7.0'
gem.add_dependency 'mongoid', '>= 7.0', '< 7.3'

gem.add_development_dependency 'rubocop', '>= 1.8.1'
end
Loading

0 comments on commit 128f2a9

Please sign in to comment.