acts_as_removable 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7668aae0ef68e4f3a34049142e66af8ae3dde45
4
+ data.tar.gz: 3368f24e31ebd5cc6af0a17a74424e1aec5c2a7b
5
+ SHA512:
6
+ metadata.gz: 07025ebbedcd757e04a3e014e7811d2347b50d741b431d826db279afbcb4bd7fd96b86a6f48155163e069ac2f31a09e0cdd7b84592306ae5d8205c38d3537f1e
7
+ data.tar.gz: 10ed3f98b93efb322e4d22d02e0c3089fcea6e5792c9525e7f53c876f2c675a896a6b0c2cccb6f567720b852213f57785418cd4fc37ea33a76f52492f0c5b968
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_removable.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Florian Schwab
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # ActsAsRemovable
2
+
3
+ This gem allows you to easily manage ActiveRecord objects that are pseudo destroyed a.k.a. removed.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_removable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_removable
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ # a column removed_at of type timestamp is required
23
+ class MyModel < ActiveRecord::Base
24
+ acts_as_removable
25
+ end
26
+
27
+ record = MyModel.create
28
+ record.removed? # => false
29
+ record.remove
30
+ record.removed? # => true
31
+ ```
32
+
33
+ You can also specify the column to use:
34
+ ```ruby
35
+ class MyModel < ActiveRecord::Base
36
+ acts_as_removable column_name: :some_column
37
+ end
38
+ ```
39
+
40
+ And you can use callbacks:
41
+ ```ruby
42
+ class MyModel < ActiveRecord::Base
43
+ acts_as_removable
44
+
45
+ before_save do |r|
46
+ puts "Before removing record"
47
+ end
48
+
49
+ after_remove :after_remove_method
50
+ def after_remove_method
51
+ puts "After removing record"
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## Code Status
57
+
58
+ * [![Build Status](https://api.travis-ci.org/SICSoftwareGmbH/acts_as_removable.png)](https://travis-ci.org/SICSoftwareGmbH/acts_as_removable)
59
+ * [![Dependencies](https://gemnasium.com/SICSoftwareGmbH/acts_as_removable.png?travis)](https://gemnasium.com/SICSoftwareGmbH/acts_as_removable)
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_removable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_removable"
8
+ spec.version = ActsAsRemovable::VERSION
9
+ spec.authors = ["Florian Schwab"]
10
+ spec.email = ["florian.schwab@sic-software.com"]
11
+ spec.description = %q{Simplifies handling of pseudo removed records}
12
+ spec.summary = %q{Simplifies handling of pseudo removed records}
13
+ spec.homepage = "https://github.com/SICSoftwareGmbH/acts_as_removable"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", "~> 4.0.0.rc2"
22
+ spec.add_dependency "activerecord", "~> 4.0.0.rc2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "sqlite3"
28
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsRemovable
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'active_support/concern'
2
+ require 'active_record'
3
+ require "acts_as_removable/version"
4
+
5
+ module ActsAsRemovable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def acts_as_removable(options={})
14
+ self.class_eval do
15
+ @_acts_as_removable_column_name = options[:column_name] || 'removed_at'
16
+ def self._acts_as_removable_column_name
17
+ @_acts_as_removable_column_name
18
+ end
19
+
20
+ default_scope -> {where("#{self.table_name}.#{self._acts_as_removable_column_name} IS NULL")}
21
+ scope :removed, -> {where("#{self.table_name}.#{self._acts_as_removable_column_name} IS NOT NULL")}
22
+
23
+ define_callbacks :remove
24
+
25
+ def self.before_remove(*args, &block)
26
+ set_callback :remove, :before, *args, &block
27
+ end
28
+
29
+ def self.after_remove(*args, &block)
30
+ set_callback :remove, :after, *args, &block
31
+ end
32
+
33
+ def removed?
34
+ self.send(self.class._acts_as_removable_column_name).present?
35
+ end
36
+
37
+ def remove
38
+ run_callbacks :remove do
39
+ self.update_attributes(self.class._acts_as_removable_column_name => Time.now)
40
+ end
41
+ end
42
+
43
+ def remove!
44
+ run_callbacks :remove do
45
+ self.update_attributes!(self.class._acts_as_removable_column_name => Time.now)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ ActiveRecord::Base.send(:include, ActsAsRemovable)
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'acts_as_removable' do
4
+ class MyModel < ActiveRecord::Base
5
+ acts_as_removable
6
+ attr_accessor :callback_before, :callback_after
7
+ before_remove do |r|
8
+ r.callback_before = true
9
+ end
10
+ after_remove do |r|
11
+ r.callback_after = true
12
+ end
13
+ end
14
+
15
+ class MySecondModel < ActiveRecord::Base
16
+ acts_as_removable column_name: :use_this_column
17
+ end
18
+
19
+ before do
20
+ # setup database
21
+ db_file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'acts_as_removable.db'))
22
+ Dir::mkdir(File.dirname(db_file)) unless File.exists?(File.dirname(db_file))
23
+ ActiveRecord::Base.establish_connection(
24
+ :adapter => "sqlite3",
25
+ :database => "#{File.expand_path(File.join(File.dirname(__FILE__), '..'))}/tmp/acts_as_removable.db"
26
+ )
27
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'my_models'")
28
+ ActiveRecord::Base.connection.create_table(:my_models) do |t|
29
+ t.timestamp :removed_at
30
+ end
31
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'my_second_models'")
32
+ ActiveRecord::Base.connection.create_table(:my_second_models) do |t|
33
+ t.string :name
34
+ t.timestamp :use_this_column
35
+ end
36
+ end
37
+
38
+ it "test column and check method" do
39
+ [[MyModel.create!, :removed_at], [MySecondModel.create!, :use_this_column]].each do |r, column_name|
40
+ r.removed?.should be_false
41
+ r.send(column_name).should be_nil
42
+
43
+ r.remove
44
+ r.removed?.should be_true
45
+ r.send(column_name).should be_a(Time)
46
+ end
47
+ end
48
+
49
+ it "test callbacks" do
50
+ r = MyModel.create!
51
+ r.callback_before.should be_false
52
+ r.callback_after.should be_false
53
+ r.remove
54
+ r.callback_before.should be_true
55
+ r.callback_after.should be_true
56
+ end
57
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'acts_as_removable'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_removable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Florian Schwab
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0.rc2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0.rc2
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0.rc2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0.rc2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Simplifies handling of pseudo removed records
98
+ email:
99
+ - florian.schwab@sic-software.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .ruby-version
106
+ - .travis.yml
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - acts_as_removable.gemspec
112
+ - lib/acts_as_removable.rb
113
+ - lib/acts_as_removable/version.rb
114
+ - spec/acts_as_removable_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/SICSoftwareGmbH/acts_as_removable
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Simplifies handling of pseudo removed records
140
+ test_files:
141
+ - spec/acts_as_removable_spec.rb
142
+ - spec/spec_helper.rb