hideable 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +20 -0
- data/README.md +32 -24
- data/lib/hideable/active_record.rb +3 -3
- data/lib/hideable/active_record/instance_methods.rb +14 -10
- data/lib/hideable/version.rb +1 -1
- metadata +3 -2
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012-3 Joe Corcoran
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -8,39 +8,47 @@ A simple way to provide hideability to ActiveRecord models. Mark records as hidd
|
|
8
8
|
|
9
9
|
Run the generator to create migrations for all of the models you want to hide:
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
```bash
|
12
|
+
rails generate hideable:migration Foo Bar
|
13
|
+
rake db:migrate
|
14
|
+
```
|
13
15
|
|
14
16
|
Declare your models `hideable`:
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
```ruby
|
19
|
+
class Foo < ActiveRecord::Base
|
20
|
+
extend Hideable::ActiveRecord
|
21
|
+
hideable
|
22
|
+
end
|
23
|
+
```
|
20
24
|
|
21
|
-
This will add
|
25
|
+
This will add `#hide!`, `#unhide!` and `#hidden?` instance methods and `.hidden` and `.not_hidden` scopes.
|
22
26
|
|
23
27
|
Hideable is designed to work in a similar way to the `:dependent` option on associations too, if that's what you want.
|
24
28
|
|
25
|
-
|
26
|
-
|
29
|
+
```ruby
|
30
|
+
class Foo < ActiveRecord::Base
|
31
|
+
has_one :bar
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
33
|
+
extend Hideable::ActiveRecord
|
34
|
+
hideable :dependent => :hide
|
35
|
+
end
|
31
36
|
|
32
|
-
|
33
|
-
|
37
|
+
class Bar < ActiveRecord::Base
|
38
|
+
belongs_to :foo
|
34
39
|
|
35
|
-
|
36
|
-
|
37
|
-
|
40
|
+
extend Hideable::ActiveRecord
|
41
|
+
hideable
|
42
|
+
end
|
43
|
+
```
|
38
44
|
|
39
45
|
All models that you wish to hide, including dependents, must be declared as `hideable`.
|
40
46
|
|
41
|
-
|
42
|
-
|
43
|
-
|
47
|
+
```ruby
|
48
|
+
foo = Foo.not_hidden.first
|
49
|
+
foo.hide!
|
50
|
+
foo.bar.hidden? #=> true
|
51
|
+
```
|
44
52
|
|
45
53
|
## Why?
|
46
54
|
|
@@ -50,12 +58,12 @@ Knowing that you can hide or unhide a record is often better destroying it for g
|
|
50
58
|
|
51
59
|
Install the general dependencies, including all the versions of `activerecord` that `hideable` is tested against.
|
52
60
|
|
53
|
-
```
|
54
|
-
|
61
|
+
```bash
|
62
|
+
bundle install && rake appraisal:install
|
55
63
|
```
|
56
64
|
|
57
65
|
Run tests as follows.
|
58
66
|
|
59
|
-
```
|
60
|
-
|
67
|
+
```bash
|
68
|
+
rake appraisal
|
61
69
|
```
|
@@ -3,9 +3,9 @@ module Hideable
|
|
3
3
|
|
4
4
|
def hideable(options = {})
|
5
5
|
send :include, InstanceMethods
|
6
|
-
class_attribute :
|
7
|
-
self.
|
8
|
-
after_save :update_hideable_dependents
|
6
|
+
class_attribute :hide_dependents
|
7
|
+
self.hide_dependents = options[:dependent] == :hide ? true : false
|
8
|
+
after_save :update_hideable_dependents!, :if => :update_hideable_dependents?
|
9
9
|
end
|
10
10
|
|
11
11
|
def hidden
|
@@ -18,26 +18,30 @@ module Hideable
|
|
18
18
|
self.save!
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
def update_hideable_dependents?
|
22
|
+
self.hidden_at_changed?
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
25
|
+
def update_hideable_dependents!
|
26
|
+
self.class.reflect_on_all_associations.each do |reflection|
|
27
|
+
if update_reflected_record?(reflection)
|
28
|
+
dependent_records = Array(self.send(reflection.name)).compact
|
29
|
+
dependent_records.each do |record|
|
30
|
+
action = self.hidden? ? :hide! : :unhide!
|
31
|
+
record.send(action) if record.respond_to?(action)
|
31
32
|
end
|
32
33
|
end
|
33
34
|
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
34
38
|
|
35
39
|
def update_reflected_record?(reflection)
|
36
40
|
macros = [:has_many, :has_one, :has_and_belongs_to_many]
|
37
41
|
(
|
38
42
|
macros.include?(reflection.macro) &&
|
39
43
|
reflection.options[:through].nil? &&
|
40
|
-
self.class.
|
44
|
+
self.class.hide_dependents == true
|
41
45
|
)
|
42
46
|
end
|
43
47
|
|
data/lib/hideable/version.rb
CHANGED
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.
|
4
|
+
version: 0.2.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-
|
12
|
+
date: 2013-07-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- lib/hideable/version.rb
|
105
105
|
- lib/hideable.rb
|
106
106
|
- README.md
|
107
|
+
- LICENSE.txt
|
107
108
|
homepage: http://rubygems.org/gems/hideable
|
108
109
|
licenses: []
|
109
110
|
post_install_message:
|