trasher 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a3a5ee170b8c5b522d8896729b6b36f5cd8dd031b81ad764776da644907d2c5
4
+ data.tar.gz: e41428560300105882ff88e136f4d7fad8eb4914f6c347d33c061d88030f980e
5
+ SHA512:
6
+ metadata.gz: 4c296204560003e0eb61e437c0857cb5e7fabb7c3616e6f94173a125428791ebe7e952615995b6c5a7fcb5be0c94ffe62128c2087160f50964e67a0133e0ef41
7
+ data.tar.gz: 1d9cec8917a3ebdd1c5c1b78336dadb26973daa3d8e2f48b263b36767770296cede343e40065abf4bd7aeafb1b739b01d6cfcd7b37807bb46f8048a52e7807cc
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .byebug_history
10
+ .idea
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at zain@sendoso.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in trasher.gemspec
6
+ gemspec
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ trasher (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (5.2.4.2)
10
+ activesupport (= 5.2.4.2)
11
+ activerecord (5.2.4.2)
12
+ activemodel (= 5.2.4.2)
13
+ activesupport (= 5.2.4.2)
14
+ arel (>= 9.0)
15
+ activesupport (5.2.4.2)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ arel (9.0.0)
21
+ ast (2.4.0)
22
+ byebug (11.0.1)
23
+ concurrent-ruby (1.1.6)
24
+ diff-lcs (1.3)
25
+ i18n (1.8.2)
26
+ concurrent-ruby (~> 1.0)
27
+ jaro_winkler (1.5.4)
28
+ minitest (5.14.0)
29
+ parallel (1.19.1)
30
+ parser (2.7.1.2)
31
+ ast (~> 2.4.0)
32
+ pg (0.18.4)
33
+ rainbow (3.0.0)
34
+ rake (13.0.1)
35
+ rexml (3.2.4)
36
+ rspec (3.9.0)
37
+ rspec-core (~> 3.9.0)
38
+ rspec-expectations (~> 3.9.0)
39
+ rspec-mocks (~> 3.9.0)
40
+ rspec-core (3.9.2)
41
+ rspec-support (~> 3.9.3)
42
+ rspec-expectations (3.9.2)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-mocks (3.9.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.9.0)
48
+ rspec-support (3.9.3)
49
+ rubocop (0.82.0)
50
+ jaro_winkler (~> 1.5.1)
51
+ parallel (~> 1.10)
52
+ parser (>= 2.7.0.1)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ rexml
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 1.4.0, < 2.0)
57
+ ruby-progressbar (1.10.1)
58
+ thread_safe (0.3.6)
59
+ tzinfo (1.2.7)
60
+ thread_safe (~> 0.1)
61
+ unicode-display_width (1.7.0)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ activerecord (~> 5.2.0)
68
+ bundler (~> 2.1.4)
69
+ byebug (~> 11.0.1)
70
+ pg (~> 0.18.4)
71
+ rake (~> 13.0)
72
+ rspec (~> 3.0)
73
+ rubocop (~> 0.82)
74
+ trasher!
75
+
76
+ BUNDLED WITH
77
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 zainonrails
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # Trasher
2
+
3
+ Soft Delete
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'trasher'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install trasher
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # include this in any ActiveRecord based model that you want to soft delete.
25
+ class User < ActiveRecord::Base
26
+ include Trasher
27
+
28
+ # the rest of your model code...
29
+ end
30
+ ```
31
+
32
+ ```ruby
33
+ class User < ActiveRecord::Base
34
+ include Trasher
35
+ # call this method if you want set a predefined default scope.
36
+ lets_trash default_scope: true
37
+
38
+ # the rest of your model code...
39
+ end
40
+ ```
41
+
42
+ To work properly, the tables that back these models must have a deleted_at timestamp column and deleted_by_id integer columns.
43
+ A `MissingColumn` exception is raised if the model does not have required columns.
44
+
45
+ ```ruby
46
+ class AddDeletedAtColumnToUsers < ActiveRecord::Migration
47
+
48
+ def change
49
+ add_column :users, :deleted_at, :datetime
50
+ add_reference :users, :deleted_by, foreign_key: { to_table: :users }
51
+ end
52
+
53
+ end
54
+ ```
55
+
56
+ ```ruby
57
+ user1 = User.create(name: 'Name', email: 'name@whois.com')
58
+ user2 = User.create(name: 'AnotherName', email: 'anothername@whois.com')
59
+
60
+ # to trash/ soft delete some record. User/Post/Comment
61
+ user1.trash!(user2)
62
+ # this will update deleted_at and deleted_by_id attributes of user model to current timestamp and user id.
63
+
64
+ # the trash! method raises an TrashedWithoutTrashedBy exception if a user object is not specified
65
+ # this could be provided through current user method if using devise gem
66
+
67
+ # to recover any trashed object, simply use
68
+ user1.recover!
69
+ # this will update deleted_at and deleted_by_id attributes of user model to nil.
70
+ ```
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+
76
+ Run `ruby spec/support/db_setup` to create the required database and tables.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xainbutt/trasher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
87
+
88
+ ## Code of Conduct
89
+
90
+ Everyone interacting in the Trasher project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/trasher/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'spec/support/db_setup'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'trasher'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ require 'trasher/version'
2
+ require 'trasher/trashable'
3
+
4
+ module Trasher
5
+ class Error < StandardError; end
6
+
7
+ class TrashedWithoutTrashedBy < Error
8
+ def message
9
+ 'Cannot trash record without deleted by.'
10
+ end
11
+ end
12
+
13
+ class MissingColumn < Error
14
+ def message
15
+ 'Missing deleted_at or deleted_by_id column(s)'
16
+ end
17
+ end
18
+
19
+
20
+ def self.included(base)
21
+ unless base.ancestors.include?(ActiveRecord::Base)
22
+ raise "You can only include this if #{base} extends ActiveRecord::Base"
23
+ end
24
+ base.extend(ClassMethods)
25
+ end
26
+
27
+ def self.deleted_at_column?(klass)
28
+ klass.columns.map(&:name).include?('deleted_at')
29
+ end
30
+
31
+ def self.deleted_by_column?(klass)
32
+ klass.columns.map(&:name).include?('deleted_by_id')
33
+ end
34
+
35
+ module ClassMethods
36
+ def lets_trash(options = {})
37
+ unless Trasher.deleted_at_column?(self) && Trasher.deleted_by_column?(self)
38
+ raise MissingColumn
39
+ end
40
+ include Trasher::Trashable
41
+
42
+ return unless options[:default_scope]
43
+
44
+ default_scope do
45
+ where(deleted_at: nil, deleted_by: nil)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ module Trasher
2
+ module Trashable
3
+ def self.included(base)
4
+ base.belongs_to :deleted_by,
5
+ class_name: 'User',
6
+ foreign_key: :deleted_by_id,
7
+ optional: true
8
+
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def with_deleted
14
+ scope = all
15
+ scope.where_clause.send(:predicates).delete(with_deleted_scope_sql)
16
+ scope
17
+ end
18
+
19
+ def with_deleted_scope_sql
20
+ all.table[:deleted_at].eq(nil).to_sql
21
+ end
22
+ end
23
+ end
24
+
25
+ def trashed?
26
+ deleted_at.present?
27
+ end
28
+
29
+ def trash!(trashed_by, trashed_at = nil)
30
+ raise TrashedWithoutTrashedBy unless trashed_by.present?
31
+
32
+ trashed_at ||= DateTime.now
33
+ _trash(trashed_at, trashed_by.try(:id))
34
+ end
35
+
36
+ def recover!
37
+ _trash(nil, nil)
38
+ end
39
+
40
+ private
41
+
42
+ def _trash(deleted_at, deleted_by_id)
43
+ update_columns(deleted_at: deleted_at, deleted_by_id: deleted_by_id)
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Trasher
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trasher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'trasher'
8
+ spec.version = Trasher::VERSION
9
+ spec.authors = ['Zain Butt']
10
+ spec.email = %w[xainbutt28@gmail.com zain@sendoso.com]
11
+
12
+ spec.summary = 'Soft Delete records with deleted_at and deleted_by'
13
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = %w[lib]
24
+
25
+ spec.add_development_dependency 'activerecord', '~> 5.2.0'
26
+ spec.add_development_dependency 'bundler', '~> 2.1.4'
27
+ spec.add_development_dependency 'byebug', '~> 11.0.1'
28
+ spec.add_development_dependency 'pg', '~> 0.18.4'
29
+ spec.add_development_dependency 'rake', '~> 13.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop', '~> 0.82'
32
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trasher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Zain Butt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.4
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 11.0.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 11.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.18.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.18.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.82'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.82'
111
+ description:
112
+ email:
113
+ - xainbutt28@gmail.com
114
+ - zain@sendoso.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/trasher.rb
131
+ - lib/trasher/trashable.rb
132
+ - lib/trasher/version.rb
133
+ - trasher.gemspec
134
+ homepage:
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubygems_version: 3.0.3
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Soft Delete records with deleted_at and deleted_by
157
+ test_files: []