vandal 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf367fee82cd7e3934b039ba595687054889ffc9
4
- data.tar.gz: 78c1d4f0a6937e7805a455c74b2757e5b040eeba
3
+ metadata.gz: fece4c65b959612f8fb5faac683c5e8bbd83d857
4
+ data.tar.gz: a51419ed4045344409cfe60bae47b7911f538d8c
5
5
  SHA512:
6
- metadata.gz: a58aa19c65b8fbffef67d86c951e2cd1cc2b548d8ff285c2159276e4427be3429948c4889691bf8ef60833b93d9b1e8455c53bcbeeb230cf340714624ea2aedb
7
- data.tar.gz: e8f897d04150a71c86d00df54d7d42d56f3e8dfa46de45f87daf20ca0eb010d60a217d90064b581d1a1a723850095f9ebb3dbaa92c90cb4d51265afc7e470540
6
+ metadata.gz: 23b0d2fa48fc9d920edead140b2f74a14537d50b19c1dea56722925c55f04d38a71acede6b68f975677bc877b3055d38926ba659ba153475007768e1a2d6b9b0
7
+ data.tar.gz: 101e90b49be4619b669c87a7a0ee52f0bc9d76fd297731beffc7ee633d80822c2571cdaff86e4a1a729b2aff11b4c2b4b5db41e824075c9db508303c80b1377e
data/.circleci/config.yml CHANGED
@@ -9,14 +9,14 @@ jobs:
9
9
 
10
10
  - restore_cache:
11
11
  keys:
12
- - api-struct-bundle-v2-{{ checksum "api_struct.gemspec" }}
12
+ - vandal-bundle-v2-{{ checksum "vandal.gemspec" }}
13
13
 
14
14
  - run:
15
15
  name: Bundle Install
16
16
  command: bundle check || bundle install
17
17
 
18
18
  - save_cache:
19
- key: api-struct-bundle-v2-{{ checksum "api_struct.gemspec" }}
19
+ key: vandal-bundle-v2-{{ checksum "vandal.gemspec" }}
20
20
  paths:
21
21
  - vendor/bundle
22
22
 
data/README.md CHANGED
@@ -1,38 +1,44 @@
1
1
  # Vandal
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/vandal`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A small gem that help to *delete an ActiveRecord instance or collection with associations* (skipping callbacks or validations)
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
7
+ Add this line to your application's Gemfile and then execute `bundle install`
10
8
 
11
9
  ```ruby
12
10
  gem 'vandal'
13
11
  ```
14
12
 
15
- And then execute:
13
+ ## Usage
16
14
 
17
- $ bundle
15
+ Vandal gem adds 2 methods to `ActiveRecord::Base`
18
16
 
19
- Or install it yourself as:
17
+ ## #vandal_destroy!
20
18
 
21
- $ gem install vandal
19
+ Delete an ActiveRecord instance with associations even if the callbacks return false or rescue error.
22
20
 
23
- ## Usage
21
+ ```ruby
22
+ class User
23
+ has_many :followers # Does not contain dependent: :destroy
24
+ end
24
25
 
25
- TODO: Write usage instructions here
26
+ User.find_by(id: 1).vandal_destroy!
27
+ ```
28
+
29
+ Followers will deleted along with `User`.
26
30
 
27
- ## Development
31
+ ## #vandal_destroy_all!
28
32
 
29
- 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.
33
+ Applies `vandal_destroy!` for ActiveRecord collection
30
34
 
31
- 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).
35
+ ```ruby
36
+ User.all.vandal_destroy_all!
37
+ ```
32
38
 
33
39
  ## Contributing
34
40
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/kirillweb/vandal.
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kirillshevch/vandal.
36
42
 
37
43
  ## License
38
44
 
@@ -1,3 +1,3 @@
1
1
  module Vandal
2
- VERSION = '0.0.0'
2
+ VERSION = '0.0.1'
3
3
  end
data/lib/vandal.rb CHANGED
@@ -1,5 +1,42 @@
1
1
  require 'vandal/version'
2
+ require 'pry-byebug'
2
3
 
3
4
  module Vandal
4
- # Your code goes here...
5
+ module Destroy
6
+ extend ::ActiveSupport::Concern
7
+ DELETE_OPTIONS = [:destroy, :delete, :delete_all]
8
+
9
+ def vandal_destroy
10
+ begin
11
+ @destroying ||= false
12
+ return if @destroying
13
+ @destroying = true
14
+ self.class.transaction do
15
+ self.class.related_associations.each do |association|
16
+ related = self.send(association[:name])
17
+ related.send("vandal_destroy#{association[:collection] ? '_all': ''}") if related.present?
18
+ end
19
+ self.delete
20
+ end
21
+ ensure
22
+ @destroying = false
23
+ end
24
+ end
25
+
26
+ module ClassMethods
27
+ def vandal_destroy_all
28
+ transaction { find_each { |r| r.vandal_destroy! } }
29
+ end
30
+
31
+ def related_associations
32
+ @related_associations ||= reflections.select do |name, association|
33
+ association.options[:dependent].in?(DELETE_OPTIONS)
34
+ end.collect { |name, association| { name: name, collection: association.collection? } }
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ class ActiveRecord::Base
41
+ include Vandal::Destroy
5
42
  end
data/vandal.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['Kirill Shevchenko']
9
9
  spec.email = ['hello@kirillshevch.com']
10
10
 
11
- spec.summary = 'ActiveRecord force destroy record with associations.'
11
+ spec.summary = 'ActiveRecord force destroy records with associations'
12
12
  spec.description = spec.summary
13
13
  spec.homepage = 'https://github.com/kirillshevch/vandal'
14
14
  spec.license = 'MIT'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vandal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Shevchenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-10 00:00:00.000000000 Z
11
+ date: 2019-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description: ActiveRecord force destroy record with associations.
83
+ description: ActiveRecord force destroy records with associations
84
84
  email:
85
85
  - hello@kirillshevch.com
86
86
  executables: []
@@ -124,5 +124,5 @@ rubyforge_project:
124
124
  rubygems_version: 2.6.13
125
125
  signing_key:
126
126
  specification_version: 4
127
- summary: ActiveRecord force destroy record with associations.
127
+ summary: ActiveRecord force destroy records with associations
128
128
  test_files: []