has_sti 0.1 → 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.
- checksums.yaml +4 -4
- data/README.rdoc +17 -1
- data/lib/has_sti/active_record_extensions.rb +39 -10
- data/lib/has_sti/version.rb +1 -1
- metadata +23 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7a7d16773fe76443cfa47a9c55825af2639a01b3
|
|
4
|
+
data.tar.gz: 084dfbfb2fb7b3287a074dde9c013a4a60f279a6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6422d27461eb2880de4d260d859dcdc07a87b651196db1b14c2ad375b0e02069eb0ef064d177b8c13b00165da1d58739fa91830e07f9b2695622c8d0ab653c5a
|
|
7
|
+
data.tar.gz: 43b2c593bd7d9136c5bd2eb188c7bd5a9db8d3e9b2f92656e92fef7ca8060fdd348fd5a670cc6ef9a7183ccc173af7dfe38ba158380f9d449c7f4f4b857ac566
|
data/README.rdoc
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
{<img src="https://travis-ci.org/arekf/has_sti.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/arekf/has_sti]
|
|
1
|
+
{<img src="https://travis-ci.org/arekf/has_sti.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/arekf/has_sti] {<img src="https://codeclimate.com/github/arekf/has_sti/badges/gpa.svg" />}[https://codeclimate.com/github/arekf/has_sti] {<img src="https://codeclimate.com/github/arekf/has_sti/badges/coverage.svg" />}[https://codeclimate.com/github/arekf/has_sti] {<img src="https://badge.fury.io/rb/has_sti.svg" alt="Gem Version" />}[http://badge.fury.io/rb/has_sti]
|
|
2
2
|
|
|
3
3
|
= has_sti
|
|
4
4
|
|
|
@@ -45,6 +45,22 @@ And that's it. Now you have following methods and scopes available:
|
|
|
45
45
|
User.admin => [some_admin]
|
|
46
46
|
User.reader => []
|
|
47
47
|
|
|
48
|
+
You can also disable helper methods:
|
|
49
|
+
class User < ActiveRecord::Base
|
|
50
|
+
has_sti :admin, :reader, helper_methods: false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
or scopes:
|
|
54
|
+
class User < ActiveRecord::Base
|
|
55
|
+
has_sti :admin, :reader, scopes: false
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
or even set it per child class:
|
|
59
|
+
class User < ActiveRecord::Base
|
|
60
|
+
has_sti :admin, scopes: false
|
|
61
|
+
has_sti :reader, helper_methods: false
|
|
62
|
+
end
|
|
63
|
+
|
|
48
64
|
No configuration is required. Custom STI column name is supported.
|
|
49
65
|
|
|
50
66
|
== License
|
|
@@ -10,32 +10,61 @@ module HasSti
|
|
|
10
10
|
# class Animal < ActiveRecord::Base
|
|
11
11
|
# has_sti :cat, :dog, :parrot
|
|
12
12
|
# end
|
|
13
|
+
#
|
|
13
14
|
# Helper methods will be created:
|
|
15
|
+
#
|
|
14
16
|
# cat = Cat.first
|
|
15
17
|
# cat.cat? => true
|
|
16
18
|
# cat.parrot? => false
|
|
19
|
+
#
|
|
17
20
|
# Also, you can use scopes on parent model, like:
|
|
18
21
|
# Animal.cat => array of Cats
|
|
19
|
-
|
|
22
|
+
#
|
|
23
|
+
# If you do not need helper methods, or scopes, just disable them:
|
|
24
|
+
# class Animal < ActiveRecord::Base
|
|
25
|
+
# has_sti :cat, :dog, :parrot, helper_methods: false
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# or:
|
|
29
|
+
# class Animal < ActiveRecord::Base
|
|
30
|
+
# has_sti :cat, :dog, :parrot, scopes: false
|
|
31
|
+
# end
|
|
32
|
+
def has_sti(*klasses, helper_methods: true, scopes: true)
|
|
33
|
+
return if !helper_methods && !scopes
|
|
34
|
+
|
|
20
35
|
raise HasSti::Exceptions::NoDescendantsError if klasses.count.zero?
|
|
21
36
|
|
|
22
|
-
|
|
23
|
-
|
|
37
|
+
klasses.each do |klass|
|
|
38
|
+
define_helper_method(klass) if helper_methods
|
|
39
|
+
define_scope(klass) if scopes
|
|
40
|
+
end
|
|
41
|
+
end
|
|
24
42
|
|
|
25
|
-
|
|
43
|
+
private
|
|
26
44
|
|
|
27
|
-
|
|
28
|
-
|
|
45
|
+
def define_helper_method(klass)
|
|
46
|
+
define_method helper_method_name(klass) do
|
|
29
47
|
self.class.name == klass.to_s.classify
|
|
30
48
|
end
|
|
49
|
+
end
|
|
31
50
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
51
|
+
def define_scope(klass)
|
|
52
|
+
superclass = superclass_for(klass)
|
|
53
|
+
superclass.define_singleton_method scope_method_name(klass) do
|
|
54
|
+
where(superclass.inheritance_column => class_name(klass))
|
|
35
55
|
end
|
|
36
56
|
end
|
|
37
57
|
|
|
38
|
-
|
|
58
|
+
def superclass_for(klass)
|
|
59
|
+
superclass = constant(klass).superclass
|
|
60
|
+
raise HasSti::Exceptions::NoSuperclassError if superclass.nil? || model_superclasses.include?(superclass)
|
|
61
|
+
|
|
62
|
+
superclass
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def model_superclasses
|
|
66
|
+
[ActiveRecord::Base, Object, BasicObject]
|
|
67
|
+
end
|
|
39
68
|
|
|
40
69
|
def class_name(klass)
|
|
41
70
|
klass.to_s.classify
|
data/lib/has_sti/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: has_sti
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Arkadiusz Fal
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-02-
|
|
11
|
+
date: 2015-02-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- - "
|
|
17
|
+
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 4.
|
|
19
|
+
version: 4.0.0
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
|
-
- - "
|
|
24
|
+
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 4.
|
|
26
|
+
version: 4.0.0
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: rails
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -66,6 +66,20 @@ dependencies:
|
|
|
66
66
|
- - ">="
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
68
|
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: codeclimate-test-reporter
|
|
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'
|
|
69
83
|
description: has_sti provides helper methods for Single Table Inheritance models of
|
|
70
84
|
Active Record. It creates methods that allow to determine exact type of model and
|
|
71
85
|
scopes that make easy to find records of certain type.
|
|
@@ -92,9 +106,9 @@ require_paths:
|
|
|
92
106
|
- lib
|
|
93
107
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
108
|
requirements:
|
|
95
|
-
- - "
|
|
109
|
+
- - ">="
|
|
96
110
|
- !ruby/object:Gem::Version
|
|
97
|
-
version:
|
|
111
|
+
version: 2.0.0
|
|
98
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
113
|
requirements:
|
|
100
114
|
- - ">="
|
|
@@ -102,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
102
116
|
version: '0'
|
|
103
117
|
requirements: []
|
|
104
118
|
rubyforge_project:
|
|
105
|
-
rubygems_version: 2.
|
|
119
|
+
rubygems_version: 2.4.5
|
|
106
120
|
signing_key:
|
|
107
121
|
specification_version: 4
|
|
108
122
|
summary: Active Record extension that provides helper methods for Single Table Inheritance
|