entity_status 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b9055d9bc4b1e2183811345e24badeb103964f6
4
+ data.tar.gz: 9cd7237e3d61c0db38933a0894ca58d10beea598
5
+ SHA512:
6
+ metadata.gz: 785f9970f80d3e701333347fb62a0b6f0fec35437c126e68943acffc9f00f5d28495d78efe8fc935d064b2f4308386f388a78670d3b26037a90fbbb5137d8666
7
+ data.tar.gz: db165a4c878892483d4ac452ced51804410f3644e3a5242e604902a18879787d8b39a72cafb19b4c855e22988cb74539e24a6a7c3de6236ee405debee44e271a
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Jeremy Bueler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ entity_status
2
+ =============
3
+
4
+ Ruby Gem for adding status helper methods to an activerecord model.
5
+
6
+ ### Installlation
7
+
8
+ gem 'entity_status', git: 'git@github.com:jbueler/entity_status.git'
9
+
10
+ Bundle install
11
+
12
+ This gem will modify a string column in your database and is configurable to which column you want to use.
13
+ To change the column used for the status call `entity_status <#column_name#>`
14
+
15
+ Include module in your Model, this will get you default statuses of `pending`, `open`, `closed`
16
+
17
+ class Post < ActiveRecord::Base
18
+ include EntityStatus # the default column EntityStatus will look for is `status`
19
+ end
20
+
21
+ You can configure custom statuses per model by calling entity_status in your Model:
22
+
23
+ class Post < ActiveRecord::Base
24
+ include EntityStatus
25
+ entity_status :status, [:incomplete,:complete]
26
+ end
27
+
28
+ You can configure multiple status columns per model by calling entity_status multiple times in your Model:
29
+
30
+ class Post < ActiveRecord::Base
31
+ include EntityStatus
32
+ entity_status :status, [:incomplete,:complete]
33
+ entity_status :moderation_status, [:pending,:approved,:rejected]
34
+ end
35
+
36
+ ### Using EntityStatus
37
+ You can now start using the helper methods:
38
+
39
+ Post.incomplete #=> will return all Post entities with status == 'incomplete'
40
+ p = Post.incomplete.first
41
+ p.complete? #=> false
42
+ p.complete #=> returns p with p.status == 'complete'
43
+ p.complete? #=> true
44
+
45
+ // USING THE SECOND ENTITY STATUS
46
+ p.approved #=> returns p with p.moderation_status == 'approved'
47
+ if(p.approved? && p.complete?)
48
+ // THIS IS APPROVED AND COMPLETED
49
+ elsif(!p.approved?)
50
+ // THIS IS NOT APPROVED
51
+ end
52
+
53
+
Binary file
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require File.expand_path("../lib/entity_status/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "entity_status"
6
+ s.version = EntityStatus::VERSION
7
+ s.authors = ["Jeremy Bueler"]
8
+ s.email = ["jbueler@gmail.com"]
9
+ s.homepage = "https://github.com/jbueler/entity_status"
10
+ s.summary = "Simple entity status module for fetching and setting entities by status string."
11
+ # s.rubyforge_project = "friendly_id"
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test}/*`.split("\n")
14
+ s.require_paths = ["lib"]
15
+ s.license = 'MIT'
16
+
17
+ s.required_ruby_version = '>= 1.9.3'
18
+
19
+ s.add_dependency 'activerecord'
20
+
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
+ s.description = <<-EOM
31
+ A simple work in progress gem for adding status string to activerecord models.
32
+ Creates scopes and status setting/testing methods.
33
+ EOM
34
+ end
@@ -0,0 +1,3 @@
1
+ module EntityStatus
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,87 @@
1
+ class Object
2
+ # GIVES US THE ABILITY TO ADD DYNMAIC CLASS METHODS TO THE STATUSES MODULE....
3
+ # http://ryanangilly.com/post/234897271/dynamically-adding-class-methods-in-ruby
4
+ def metaclass
5
+ class << self; self; end
6
+ end
7
+ end
8
+
9
+ module EntityStatus
10
+ extend ActiveSupport::Concern
11
+ module ClassMethods
12
+ def entity_status(column_name="status",status_array = [])
13
+ status_array = (status_array.count > 0) ? status_array : %W(pending open closed)
14
+ self.create_statuses_method(column_name.to_s.pluralize,status_array)
15
+ status_array.each do |st|
16
+ self.create_finder_methods(column_name,st)
17
+
18
+ # add dynamic state setters based on the status string
19
+ # example: Post.first.pending #=> 'pending'
20
+ define_method st do
21
+ self.send("#{column_name}=".to_sym,st.to_s)
22
+ self
23
+ end
24
+
25
+ # Add boolean state checks based on the status string
26
+ # example: Post.first.pending? #=> true
27
+ define_method "#{st}?" do
28
+ (self.send(column_name) == st) ? true :false
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ def create_statuses_method(name,statuses)
35
+ klass = self.to_s
36
+ metaclass.instance_eval do
37
+ # attr_accessor :statuses
38
+ # @@statuses = statuses
39
+ define_method(name){
40
+ return statuses
41
+ }
42
+ end
43
+ end
44
+
45
+ def create_finder_methods(column_name,name)
46
+ klass = self.to_s
47
+ metaclass.instance_eval do
48
+ define_method(name){
49
+ where(column_name.to_sym => name)
50
+ }
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ module InstanceMethods
57
+ def state
58
+ self.status.to_s
59
+ end
60
+
61
+ def dependant_state_update(state)
62
+ self.update_attributes status: state
63
+ self.reflections.each do |rel|
64
+ if rel.last.klass.method_defined? :dependant_state_update
65
+ if rel.last.options[:dependent] == :destroy
66
+ items = [] + self.send(rel.last.name)
67
+ items.each do |item|
68
+ item.dependant_state_update state
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ def self.included(receiver)
78
+ receiver.extend ClassMethods
79
+ receiver.send :include, InstanceMethods
80
+ receiver.entity_status if receiver.respond_to? :entity_status
81
+
82
+ if receiver.method_defined?(:attr_accessible)
83
+ receiver.attr_accessible :status
84
+ # receiver.after_create :default_status
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: entity_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Bueler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: |2
28
+ A simple work in progress gem for adding status string to activerecord models.
29
+ Creates scopes and status setting/testing methods.
30
+ email:
31
+ - jbueler@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.md
39
+ - entity_status-0.0.1.gem
40
+ - entity_status.gemspec
41
+ - lib/entity_status.rb
42
+ - lib/entity_status/version.rb
43
+ homepage: https://github.com/jbueler/entity_status
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.9.3
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.1.5
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Simple entity status module for fetching and setting entities by status string.
67
+ test_files: []