hideable 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,9 @@
4
4
 
5
5
  A simple way to provide hideability to ActiveRecord models. Mark records as hidden instead of destroying them for good.
6
6
 
7
- ## Use
7
+ ## Usage
8
+
9
+ ### Setup
8
10
 
9
11
  Run the generator to create migrations for all of the models you want to hide:
10
12
 
@@ -13,7 +15,7 @@ rails generate hideable:migration Foo Bar
13
15
  rake db:migrate
14
16
  ```
15
17
 
16
- Declare your models `hideable`:
18
+ Make your models `hideable`:
17
19
 
18
20
  ```ruby
19
21
  class Foo < ActiveRecord::Base
@@ -23,6 +25,8 @@ end
23
25
  ```
24
26
 
25
27
  This will add `#hide!`, `#unhide!` and `#hidden?` instance methods and `.hidden` and `.not_hidden` scopes.
28
+
29
+ ### Associations
26
30
 
27
31
  Hideable is designed to work in a similar way to the `:dependent` option on associations too, if that's what you want.
28
32
 
@@ -50,6 +54,39 @@ foo.hide!
50
54
  foo.bar.hidden? #=> true
51
55
  ```
52
56
 
57
+ Hiding dependents requires the records to be instantiated and can be quite expensive depending on the number of records. You can move the operation into the background where suitable. Instead of `:dependent => :hide`, use the `:updated` option to provide a method name or callable that will be called when the object is hidden or unhidden. The following example demonstrates the use of [Sidekiq](http://github.com/mperham/sidekiq) to hide dependents asynchronously.
58
+
59
+ ```ruby
60
+ class Foo < ActiveRecord::Base
61
+ has_one :bar
62
+
63
+ extend Hideable::ActiveRecord
64
+ hideable :updated => :update_bars
65
+
66
+ private
67
+ def update_bars
68
+ HideableWorker.perform_async(self.class.name, self.id)
69
+ end
70
+ end
71
+
72
+ class Bar < ActiveRecord::Base
73
+ belongs_to :foo
74
+
75
+ extend Hideable::ActiveRecord
76
+ hideable
77
+ end
78
+
79
+ require 'sidekiq'
80
+ class HideableWorker
81
+ include Sidekiq::Worker
82
+
83
+ def perform(klass_name, id)
84
+ klass = klass_name.constantize
85
+ klass.find(id).update_hideable_dependencies!
86
+ end
87
+ end
88
+ ```
89
+
53
90
  ## Why?
54
91
 
55
92
  Knowing that you can hide or unhide a record is often better destroying it for good. Other gems – such as [paranoia](https://github.com/radar/paranoia) and [acts_as_paranoid](https://github.com/technoweenie/acts_as_paranoid) – do a similar job by overriding `#destroy`, which I don't like.
@@ -4,8 +4,11 @@ module Hideable
4
4
  def hideable(options = {})
5
5
  send :include, InstanceMethods
6
6
  class_attribute :hide_dependents
7
- self.hide_dependents = options[:dependent] == :hide ? true : false
7
+ self.hide_dependents = (options[:dependent] == :hide) ? true : false
8
8
  after_save :update_hideable_dependents!, :if => :update_hideable_dependents?
9
+ if options[:updated]
10
+ after_save options[:updated]
11
+ end
9
12
  end
10
13
 
11
14
  def hidden
@@ -19,7 +19,7 @@ module Hideable
19
19
  end
20
20
 
21
21
  def update_hideable_dependents?
22
- self.hidden_at_changed?
22
+ self.class.hide_dependents && self.hidden_at_changed?
23
23
  end
24
24
 
25
25
  def update_hideable_dependents!
@@ -38,11 +38,7 @@ module Hideable
38
38
 
39
39
  def update_reflected_record?(reflection)
40
40
  macros = [:has_many, :has_one, :has_and_belongs_to_many]
41
- (
42
- macros.include?(reflection.macro) &&
43
- reflection.options[:through].nil? &&
44
- self.class.hide_dependents == true
45
- )
41
+ macros.include?(reflection.macro) && reflection.options[:through].nil?
46
42
  end
47
43
 
48
44
  end
@@ -1,3 +1,3 @@
1
1
  module Hideable
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hideable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-11 00:00:00.000000000 Z
12
+ date: 2013-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -93,7 +93,7 @@ dependencies:
93
93
  version: 0.5.2
94
94
  description: Enables soft-deletion in ActiveRecord by marking records as hidden
95
95
  email:
96
- - joe@tribesports.com
96
+ - joecorcoran@gmail.com
97
97
  executables: []
98
98
  extensions: []
99
99
  extra_rdoc_files: []
@@ -105,7 +105,7 @@ files:
105
105
  - lib/hideable.rb
106
106
  - README.md
107
107
  - LICENSE.txt
108
- homepage: http://rubygems.org/gems/hideable
108
+ homepage: http://github.com/joecorcoran/judge
109
109
  licenses: []
110
110
  post_install_message:
111
111
  rdoc_options: []