entity_status 0.0.2 → 0.0.4
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.
- checksums.yaml +4 -4
- data/README.md +26 -1
- data/entity_status.gemspec +1 -11
- data/lib/entity_status/version.rb +1 -1
- data/lib/entity_status.rb +22 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3070ced0cb40d38e50e9733b6505bbd81e704657
|
4
|
+
data.tar.gz: 93b83f3feba9d72b8fb82014bcea55bfe8702bce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72d2c292db1620a0a04a59abf4531a471db8806eb8602b62d364129eaa96a027e0af63b33e0e00d4facc38dfef0ad84b24de973567d99b6978fde5d4bc58214e
|
7
|
+
data.tar.gz: 455e82496fa9a1f900ef97d8cb79ee51b32b31550dd2a207f3934c6017b6466aa2d2750211be66f075554ad77707dd009da4a7eeb8ff194ea83b28bd286d8d02
|
data/README.md
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
entity_status
|
2
2
|
=============
|
3
|
+
[](http://badge.fury.io/rb/entity_status)
|
3
4
|
|
4
5
|
Ruby Gem for adding status helper methods to an activerecord model.
|
5
6
|
|
6
7
|
### Installlation
|
7
8
|
|
8
|
-
gem 'entity_status'
|
9
|
+
gem 'entity_status'
|
9
10
|
|
10
11
|
Bundle install
|
11
12
|
|
@@ -33,6 +34,28 @@ You can configure multiple status columns per model by calling entity_status mul
|
|
33
34
|
entity_status :moderation_status, [:pending,:approved,:rejected]
|
34
35
|
end
|
35
36
|
|
37
|
+
Adding soft destroy
|
38
|
+
|
39
|
+
class Post < ActiveRecord::Base
|
40
|
+
include EntityStatus
|
41
|
+
entity_status :status, [:incomplete,:complete], {destroyed_status: 'destroyed'}
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
All of the normal helpers will exist with the `destroyed_status` with an additional helper on the entity for `destroyed_status`. It will also create a default_scope to not include the soft destroyed entires by default.
|
46
|
+
|
47
|
+
Post.destroyed_status #=> 'destroyed'
|
48
|
+
|
49
|
+
Post.all.count #=> 3
|
50
|
+
Post.unscoped.all.count #=> 9
|
51
|
+
Post.destroyed #=> will return the active record relation for all soft destroyed entitities
|
52
|
+
Post.destroyed.count #=> 6
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
36
59
|
### Using EntityStatus
|
37
60
|
You can now start using the helper methods:
|
38
61
|
|
@@ -51,3 +74,5 @@ You can now start using the helper methods:
|
|
51
74
|
end
|
52
75
|
|
53
76
|
|
77
|
+
|
78
|
+
|
data/entity_status.gemspec
CHANGED
@@ -8,7 +8,6 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.email = ["jbueler@gmail.com"]
|
9
9
|
s.homepage = "https://github.com/jbueler/entity_status"
|
10
10
|
s.summary = "Simple entity status module for fetching and setting entities by status string."
|
11
|
-
# s.rubyforge_project = "friendly_id"
|
12
11
|
s.files = `git ls-files`.split("\n")
|
13
12
|
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
14
13
|
s.require_paths = ["lib"]
|
@@ -18,17 +17,8 @@ Gem::Specification.new do |s|
|
|
18
17
|
|
19
18
|
s.add_dependency 'activerecord'
|
20
19
|
|
21
|
-
# s.add_development_dependency 'railties', '~> 4.0.0'
|
22
|
-
# s.add_development_dependency 'minitest', '>= 4.4.0'
|
23
|
-
# s.add_development_dependency 'mocha', '~> 0.13.3'
|
24
|
-
# s.add_development_dependency 'yard'
|
25
|
-
# s.add_development_dependency 'i18n'
|
26
|
-
# s.add_development_dependency 'ffaker'
|
27
|
-
# s.add_development_dependency 'simplecov'
|
28
|
-
# s.add_development_dependency 'redcarpet'
|
29
|
-
|
30
20
|
s.description = <<-EOM
|
31
21
|
A simple work in progress gem for adding status string to activerecord models.
|
32
22
|
Creates scopes and status setting/testing methods.
|
33
23
|
EOM
|
34
|
-
end
|
24
|
+
end
|
data/lib/entity_status.rb
CHANGED
@@ -9,19 +9,28 @@ end
|
|
9
9
|
module EntityStatus
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
module ClassMethods
|
12
|
-
def entity_status(column_name="status",status_array = [])
|
12
|
+
def entity_status(column_name="status",status_array = [], options = { destroyed_status: nil})
|
13
13
|
status_array = (status_array.count > 0) ? status_array : %W(pending open closed)
|
14
|
+
|
15
|
+
if options[:destroyed_status]
|
16
|
+
status_array << options[:destroyed_status]
|
17
|
+
self.set_default_scope(column_name, options[:destroyed_status])
|
18
|
+
|
19
|
+
define_method 'destroyed_status' do
|
20
|
+
options[:destroyed_status]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
14
24
|
self.create_statuses_method(column_name.to_s.pluralize,status_array)
|
15
25
|
status_array.each do |st|
|
16
26
|
self.create_finder_methods(column_name,st)
|
17
|
-
|
18
27
|
# add dynamic state setters based on the status string
|
19
28
|
# example: Post.first.pending #=> 'pending'
|
20
29
|
define_method st do
|
21
30
|
self.send("#{column_name}=".to_sym,st.to_s)
|
22
31
|
self
|
23
32
|
end
|
24
|
-
|
33
|
+
|
25
34
|
# Add boolean state checks based on the status string
|
26
35
|
# example: Post.first.pending? #=> true
|
27
36
|
define_method "#{st}?" do
|
@@ -30,7 +39,15 @@ module EntityStatus
|
|
30
39
|
end
|
31
40
|
|
32
41
|
end
|
33
|
-
|
42
|
+
|
43
|
+
def set_default_scope(column_name,destroyed_status)
|
44
|
+
self.default_scope where.not(column_name.to_sym => destroyed_status)
|
45
|
+
# klass = self.to_s
|
46
|
+
# metaclass.instance_eval do
|
47
|
+
# default_scope where.not(column_name.to_sym => destroyed_status)
|
48
|
+
# end
|
49
|
+
end
|
50
|
+
|
34
51
|
def create_statuses_method(name,statuses)
|
35
52
|
klass = self.to_s
|
36
53
|
metaclass.instance_eval do
|
@@ -54,10 +71,6 @@ module EntityStatus
|
|
54
71
|
end
|
55
72
|
|
56
73
|
module InstanceMethods
|
57
|
-
def state
|
58
|
-
self.status.to_s
|
59
|
-
end
|
60
|
-
|
61
74
|
def dependant_state_update(state)
|
62
75
|
self.update_attributes status: state
|
63
76
|
self.reflections.each do |rel|
|
@@ -81,6 +94,7 @@ module EntityStatus
|
|
81
94
|
|
82
95
|
if receiver.method_defined?(:attr_accessible)
|
83
96
|
receiver.attr_accessible :status
|
97
|
+
receiver.attr_accessible :destroyed_status
|
84
98
|
# receiver.after_create :default_status
|
85
99
|
end
|
86
100
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: entity_status
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Bueler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|