acts_as_label 1.1.0 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/README.rdoc +13 -0
- data/Rakefile +3 -3
- data/acts_as_label.gemspec +18 -45
- data/init.rb +1 -1
- data/lib/acts_as_label/base.rb +25 -26
- data/lib/acts_as_label/version.rb +5 -0
- data/test/acts_as_label_test.rb +7 -2
- data/test/test_helper.rb +1 -1
- metadata +26 -24
- data/VERSION +0 -1
- data/rails/init.rb +0 -1
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
CHANGED
@@ -70,6 +70,19 @@ method +default+. This yields more expressive code.
|
|
70
70
|
User.create!({ :name => "Anonymous Dude", :role => Role.default })
|
71
71
|
|
72
72
|
|
73
|
+
To help avoid excessive querying, the extension overrides the equality operator.
|
74
|
+
This allows record comparisons in a couple of formats:
|
75
|
+
|
76
|
+
u.role == Role.superuser # as you would expect
|
77
|
+
u.role == :superuser # to spare a superfluous query (same as u.role.to_sym == :superuser)
|
78
|
+
|
79
|
+
|
80
|
+
Other useful utility methods:
|
81
|
+
|
82
|
+
+to_s+:: Returns the friendly label value
|
83
|
+
+to_sym+:: Returns the downcased, symbolized system label value
|
84
|
+
|
85
|
+
|
73
86
|
|
74
87
|
== Helpful Links
|
75
88
|
|
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "rake"
|
2
2
|
require "rake/testtask"
|
3
|
-
require "
|
3
|
+
require "rdoc/task"
|
4
4
|
require "jeweler"
|
5
5
|
|
6
6
|
|
@@ -35,8 +35,8 @@ begin
|
|
35
35
|
gemspec.name = "acts_as_label"
|
36
36
|
gemspec.summary = "Gem version of acts_as_label Rails plugin."
|
37
37
|
|
38
|
-
gemspec.add_dependency("activerecord", ">=
|
39
|
-
gemspec.add_development_dependency("activesupport", ">=
|
38
|
+
gemspec.add_dependency("activerecord", ">=3.0.0")
|
39
|
+
gemspec.add_development_dependency("activesupport", ">=3.0.0")
|
40
40
|
gemspec.files.include("lib/**/*.rb")
|
41
41
|
end
|
42
42
|
Jeweler::GemcutterTasks.new
|
data/acts_as_label.gemspec
CHANGED
@@ -1,52 +1,25 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_label/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
|
-
s.name
|
8
|
-
s.version
|
9
|
-
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
6
|
+
s.name = "acts_as_label"
|
7
|
+
s.version = Coroutine::ActsAsLabel::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Coroutine", "John Dugan"]
|
10
|
+
s.email = ["gems@coroutine.com"]
|
11
|
+
s.homepage = "http://github.com/coroutine/acts_as_label"
|
12
|
+
s.summary = %q{This acts_as extension simplifies the process of assigning mutable user-friendly labels to immutable system labels.}
|
13
13
|
s.description = %q{This acts_as extension implements a system label and a friendly label on a class and centralizes the logic for performing validations and accessing items by system label.}
|
14
|
-
s.email = %q{jdugan@coroutine.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"README.rdoc"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".specification",
|
20
|
-
"MIT-LICENSE",
|
21
|
-
"README.rdoc",
|
22
|
-
"Rakefile",
|
23
|
-
"VERSION",
|
24
|
-
"acts_as_label.gemspec",
|
25
|
-
"init.rb",
|
26
|
-
"lib/acts_as_label.rb",
|
27
|
-
"lib/acts_as_label/base.rb",
|
28
|
-
"rails/init.rb",
|
29
|
-
"test/acts_as_label_test.rb",
|
30
|
-
"test/test_helper.rb"
|
31
|
-
]
|
32
|
-
s.homepage = %q{http://github.com/coroutine/acts_as_label}
|
33
|
-
s.require_paths = [%q{lib}]
|
34
|
-
s.rubygems_version = %q{1.8.2}
|
35
|
-
s.summary = %q{Gem version of acts_as_label Rails plugin.}
|
36
14
|
|
37
|
-
|
38
|
-
|
15
|
+
s.add_dependency "rails", ">= 3.0.0"
|
16
|
+
|
17
|
+
s.add_development_dependency "rspec", ">= 2.0.0"
|
18
|
+
|
19
|
+
s.rubyforge_project = "acts_as_label"
|
39
20
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
s.add_dependency(%q<activerecord>, [">= 2.3.4"])
|
45
|
-
s.add_dependency(%q<activesupport>, [">= 2.3.4"])
|
46
|
-
end
|
47
|
-
else
|
48
|
-
s.add_dependency(%q<activerecord>, [">= 2.3.4"])
|
49
|
-
s.add_dependency(%q<activesupport>, [">= 2.3.4"])
|
50
|
-
end
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
51
25
|
end
|
52
|
-
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require "acts_as_label"
|
data/lib/acts_as_label/base.rb
CHANGED
@@ -78,20 +78,19 @@ module Coroutine #:nodoc:
|
|
78
78
|
class_eval do
|
79
79
|
|
80
80
|
# inheritable accessors
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
81
|
+
class_attribute :acts_as_label_system_label_column
|
82
|
+
class_attribute :acts_as_label_label_column
|
83
|
+
class_attribute :acts_as_label_scope
|
84
|
+
class_attribute :acts_as_label_default_system_label
|
85
|
+
|
86
|
+
self.acts_as_label_system_label_column = system_label
|
87
|
+
self.acts_as_label_label_column = label
|
88
|
+
self.acts_as_label_scope = scope
|
89
|
+
self.acts_as_label_default_system_label = default
|
90
|
+
|
91
91
|
# protect attributes
|
92
92
|
attr_readonly system_label
|
93
93
|
|
94
|
-
|
95
94
|
# validations
|
96
95
|
validates_presence_of system_label
|
97
96
|
validates_length_of system_label, :maximum => 255
|
@@ -99,8 +98,6 @@ module Coroutine #:nodoc:
|
|
99
98
|
validates_presence_of label
|
100
99
|
validates_length_of label, :maximum => 255
|
101
100
|
|
102
|
-
|
103
|
-
|
104
101
|
# This method catches all undefined method calls. It first sees if any ancestor
|
105
102
|
# understands the request. If not, it tries to match the method call to an
|
106
103
|
# existing system label. If that is found, it lazily manufacturers a method on the
|
@@ -112,8 +109,6 @@ module Coroutine #:nodoc:
|
|
112
109
|
rescue NoMethodError => e
|
113
110
|
if has_acts_as_label_method?(method)
|
114
111
|
self.__send__(method)
|
115
|
-
elsif method.to_s == "to_ary"
|
116
|
-
# do nothing
|
117
112
|
else
|
118
113
|
throw e
|
119
114
|
end
|
@@ -135,6 +130,8 @@ module Coroutine #:nodoc:
|
|
135
130
|
def #{mn}
|
136
131
|
by_acts_as_label_system_label('#{sl}')
|
137
132
|
end
|
133
|
+
|
134
|
+
alias_method :#{mn.upcase}, :#{mn}
|
138
135
|
end
|
139
136
|
}
|
140
137
|
end
|
@@ -144,17 +141,9 @@ module Coroutine #:nodoc:
|
|
144
141
|
|
145
142
|
|
146
143
|
# This method finds an active record object for the given system label.
|
147
|
-
# It automatically determines the correct syntax for the current version
|
148
|
-
# of rails via duck typing.def typing.
|
149
144
|
#
|
150
145
|
def self.by_acts_as_label_system_label(system_label)
|
151
|
-
|
152
|
-
|
153
|
-
if @by_system_label_has_arel ||= ActiveRecord::Base.respond_to?(:where)
|
154
|
-
where("#{acts_as_label_system_label_column} = ?", sl).first
|
155
|
-
else
|
156
|
-
find(:first, :conditions => ["#{acts_as_label_system_label_column} = ?", sl])
|
157
|
-
end
|
146
|
+
where("#{acts_as_label_system_label_column} = ?", system_label.to_s.upcase).first
|
158
147
|
end
|
159
148
|
|
160
149
|
|
@@ -192,6 +181,16 @@ module Coroutine #:nodoc:
|
|
192
181
|
|
193
182
|
module InstanceMethods
|
194
183
|
|
184
|
+
# This method overrides to_ary to return nil, which tells anything trying to
|
185
|
+
# flatten this we are already flat. This is necessary because we are overriding
|
186
|
+
# active records method missing, which must do this somewhere in the bowels
|
187
|
+
# of rails.
|
188
|
+
#
|
189
|
+
def to_ary
|
190
|
+
nil
|
191
|
+
end
|
192
|
+
|
193
|
+
|
195
194
|
# This method overrides the to_s method to return the friendly label value.
|
196
195
|
#
|
197
196
|
def to_s
|
@@ -214,11 +213,11 @@ module Coroutine #:nodoc:
|
|
214
213
|
# u.role == :superuser
|
215
214
|
#
|
216
215
|
def ==(other)
|
217
|
-
self.to_sym == other.to_sym
|
216
|
+
self.to_sym == (other.to_sym rescue false)
|
218
217
|
end
|
219
218
|
|
220
219
|
end
|
221
220
|
|
222
221
|
end
|
223
222
|
end
|
224
|
-
end
|
223
|
+
end
|
data/test/acts_as_label_test.rb
CHANGED
@@ -293,6 +293,8 @@ class ActsAsLabelTest < ActiveSupport::TestCase
|
|
293
293
|
|
294
294
|
assert (r1 == Role.superuser)
|
295
295
|
assert (r1 == :superuser)
|
296
|
+
|
297
|
+
assert !(r1 == false)
|
296
298
|
end
|
297
299
|
|
298
300
|
def test_to_s
|
@@ -306,7 +308,6 @@ class ActsAsLabelTest < ActiveSupport::TestCase
|
|
306
308
|
end
|
307
309
|
|
308
310
|
def test_upcase_system_label_value
|
309
|
-
|
310
311
|
# default system label column name
|
311
312
|
record = Role.create!({ :system_label => "Customer", :label => "Client" })
|
312
313
|
assert_equal record.system_label, "CUSTOMER"
|
@@ -314,7 +315,11 @@ class ActsAsLabelTest < ActiveSupport::TestCase
|
|
314
315
|
# custom system label column name
|
315
316
|
record = Framework.create!( :system_name => "example", :name => "Example")
|
316
317
|
assert_equal record.system_name, "EXAMPLE"
|
317
|
-
|
318
|
+
end
|
319
|
+
|
320
|
+
def test_support_upcase_accessors
|
321
|
+
assert Role.SUPERUSER == Role.superuser
|
322
|
+
assert Role.SUPERUSER == :superuser
|
318
323
|
end
|
319
324
|
|
320
325
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,76 +1,77 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_label
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 3
|
10
|
+
version: 1.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Coroutine
|
14
14
|
- John Dugan
|
15
|
-
- Rick Branson
|
16
15
|
autorequire:
|
17
16
|
bindir: bin
|
18
17
|
cert_chain: []
|
19
18
|
|
20
|
-
date: 2011-
|
19
|
+
date: 2011-11-16 00:00:00 -06:00
|
21
20
|
default_executable:
|
22
21
|
dependencies:
|
23
22
|
- !ruby/object:Gem::Dependency
|
24
|
-
name:
|
23
|
+
name: rails
|
25
24
|
prerelease: false
|
26
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
27
26
|
none: false
|
28
27
|
requirements:
|
29
28
|
- - ">="
|
30
29
|
- !ruby/object:Gem::Version
|
31
|
-
hash:
|
30
|
+
hash: 7
|
32
31
|
segments:
|
33
|
-
- 2
|
34
32
|
- 3
|
35
|
-
-
|
36
|
-
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
version: 3.0.0
|
37
36
|
type: :runtime
|
38
37
|
version_requirements: *id001
|
39
38
|
- !ruby/object:Gem::Dependency
|
40
|
-
name:
|
39
|
+
name: rspec
|
41
40
|
prerelease: false
|
42
41
|
requirement: &id002 !ruby/object:Gem::Requirement
|
43
42
|
none: false
|
44
43
|
requirements:
|
45
44
|
- - ">="
|
46
45
|
- !ruby/object:Gem::Version
|
47
|
-
hash:
|
46
|
+
hash: 15
|
48
47
|
segments:
|
49
48
|
- 2
|
50
|
-
-
|
51
|
-
-
|
52
|
-
version: 2.
|
49
|
+
- 0
|
50
|
+
- 0
|
51
|
+
version: 2.0.0
|
53
52
|
type: :development
|
54
53
|
version_requirements: *id002
|
55
54
|
description: This acts_as extension implements a system label and a friendly label on a class and centralizes the logic for performing validations and accessing items by system label.
|
56
|
-
email:
|
55
|
+
email:
|
56
|
+
- gems@coroutine.com
|
57
57
|
executables: []
|
58
58
|
|
59
59
|
extensions: []
|
60
60
|
|
61
|
-
extra_rdoc_files:
|
62
|
-
|
61
|
+
extra_rdoc_files: []
|
62
|
+
|
63
63
|
files:
|
64
|
+
- .gitignore
|
64
65
|
- .specification
|
66
|
+
- Gemfile
|
65
67
|
- MIT-LICENSE
|
66
68
|
- README.rdoc
|
67
69
|
- Rakefile
|
68
|
-
- VERSION
|
69
70
|
- acts_as_label.gemspec
|
70
71
|
- init.rb
|
71
72
|
- lib/acts_as_label.rb
|
72
73
|
- lib/acts_as_label/base.rb
|
73
|
-
-
|
74
|
+
- lib/acts_as_label/version.rb
|
74
75
|
- test/acts_as_label_test.rb
|
75
76
|
- test/test_helper.rb
|
76
77
|
has_rdoc: true
|
@@ -102,10 +103,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
103
|
version: "0"
|
103
104
|
requirements: []
|
104
105
|
|
105
|
-
rubyforge_project:
|
106
|
+
rubyforge_project: acts_as_label
|
106
107
|
rubygems_version: 1.6.2
|
107
108
|
signing_key:
|
108
109
|
specification_version: 3
|
109
|
-
summary:
|
110
|
-
test_files:
|
111
|
-
|
110
|
+
summary: This acts_as extension simplifies the process of assigning mutable user-friendly labels to immutable system labels.
|
111
|
+
test_files:
|
112
|
+
- test/acts_as_label_test.rb
|
113
|
+
- test/test_helper.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.1.0
|
data/rails/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require "acts_as_label"
|