acts_as_label 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.specification CHANGED
@@ -1,22 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_label
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 1
9
- version: 1.0.1
4
+ prerelease:
5
+ version: 1.1.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Coroutine
13
9
  - John Dugan
10
+ - Rick Branson
14
11
  autorequire:
15
12
  bindir: bin
16
13
  cert_chain: []
17
14
 
18
- date: 2010-12-13 00:00:00 -06:00
19
- default_executable:
15
+ date: 2011-05-12 00:00:00 Z
20
16
  dependencies:
21
17
  - !ruby/object:Gem::Dependency
22
18
  name: activerecord
@@ -26,10 +22,6 @@ dependencies:
26
22
  requirements:
27
23
  - - ">="
28
24
  - !ruby/object:Gem::Version
29
- segments:
30
- - 2
31
- - 3
32
- - 4
33
25
  version: 2.3.4
34
26
  type: :runtime
35
27
  version_requirements: *id001
@@ -41,10 +33,6 @@ dependencies:
41
33
  requirements:
42
34
  - - ">="
43
35
  - !ruby/object:Gem::Version
44
- segments:
45
- - 2
46
- - 3
47
- - 4
48
36
  version: 2.3.4
49
37
  type: :development
50
38
  version_requirements: *id002
@@ -57,7 +45,6 @@ extensions: []
57
45
  extra_rdoc_files:
58
46
  - README.rdoc
59
47
  files:
60
- - .gitignore
61
48
  - .specification
62
49
  - MIT-LICENSE
63
50
  - README.rdoc
@@ -70,13 +57,12 @@ files:
70
57
  - rails/init.rb
71
58
  - test/acts_as_label_test.rb
72
59
  - test/test_helper.rb
73
- has_rdoc: true
74
60
  homepage: http://github.com/coroutine/acts_as_label
75
61
  licenses: []
76
62
 
77
63
  post_install_message:
78
- rdoc_options:
79
- - --charset=UTF-8
64
+ rdoc_options: []
65
+
80
66
  require_paths:
81
67
  - lib
82
68
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -84,25 +70,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
70
  requirements:
85
71
  - - ">="
86
72
  - !ruby/object:Gem::Version
87
- segments:
88
- - 0
89
73
  version: "0"
90
74
  required_rubygems_version: !ruby/object:Gem::Requirement
91
75
  none: false
92
76
  requirements:
93
77
  - - ">="
94
78
  - !ruby/object:Gem::Version
95
- segments:
96
- - 0
97
79
  version: "0"
98
80
  requirements: []
99
81
 
100
82
  rubyforge_project:
101
- rubygems_version: 1.3.7
83
+ rubygems_version: 1.8.2
102
84
  signing_key:
103
85
  specification_version: 3
104
86
  summary: Gem version of acts_as_label Rails plugin.
105
- test_files:
106
- - test/acts_as_label_test.rb
107
- - test/test_helper.rb
87
+ test_files: []
88
+
108
89
 
data/Rakefile CHANGED
@@ -28,7 +28,7 @@ end
28
28
 
29
29
  begin
30
30
  Jeweler::Tasks.new do |gemspec|
31
- gemspec.authors = ["Coroutine", "John Dugan"]
31
+ gemspec.authors = ["Coroutine", "John Dugan", "Rick Branson"]
32
32
  gemspec.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."
33
33
  gemspec.email = "jdugan@coroutine.com"
34
34
  gemspec.homepage = "http://github.com/coroutine/acts_as_label"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.0
@@ -1,47 +1,40 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_label}
8
- s.version = "1.0.2"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Coroutine", "John Dugan"]
12
- s.date = %q{2010-12-16}
11
+ s.authors = [%q{Coroutine}, %q{John Dugan}, %q{Rick Branson}]
12
+ s.date = %q{2011-05-12}
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
14
  s.email = %q{jdugan@coroutine.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
- ".gitignore",
20
- ".specification",
21
- "MIT-LICENSE",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION",
25
- "acts_as_label.gemspec",
26
- "init.rb",
27
- "lib/acts_as_label.rb",
28
- "lib/acts_as_label/base.rb",
29
- "rails/init.rb",
30
- "test/acts_as_label_test.rb",
31
- "test/test_helper.rb"
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"
32
31
  ]
33
32
  s.homepage = %q{http://github.com/coroutine/acts_as_label}
34
- s.rdoc_options = ["--charset=UTF-8"]
35
- s.require_paths = ["lib"]
36
- s.rubygems_version = %q{1.3.7}
33
+ s.require_paths = [%q{lib}]
34
+ s.rubygems_version = %q{1.8.2}
37
35
  s.summary = %q{Gem version of acts_as_label Rails plugin.}
38
- s.test_files = [
39
- "test/acts_as_label_test.rb",
40
- "test/test_helper.rb"
41
- ]
42
36
 
43
37
  if s.respond_to? :specification_version then
44
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
38
  s.specification_version = 3
46
39
 
47
40
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -77,7 +77,7 @@ module Coroutine #:nodoc:
77
77
  #--------------------------------------------
78
78
  class_eval do
79
79
 
80
- # Add inheritable accessors
80
+ # inheritable accessors
81
81
  write_inheritable_attribute :acts_as_label_system_label_column, system_label
82
82
  class_inheritable_reader :acts_as_label_system_label_column
83
83
  write_inheritable_attribute :acts_as_label_label_column, label
@@ -92,7 +92,7 @@ module Coroutine #:nodoc:
92
92
  attr_readonly system_label
93
93
 
94
94
 
95
- # Add validations
95
+ # validations
96
96
  validates_presence_of system_label
97
97
  validates_length_of system_label, :maximum => 255
98
98
  validates_format_of system_label, :with => /^[A-Z][_A-Z0-9]*$/
@@ -100,36 +100,66 @@ module Coroutine #:nodoc:
100
100
  validates_length_of label, :maximum => 255
101
101
 
102
102
 
103
- # Add method missing, if needed
104
- unless self.method_defined? :method_missing
105
- def self.method_missing(method, *args, &block)
103
+
104
+ # This method catches all undefined method calls. It first sees if any ancestor
105
+ # understands the request. If not, it tries to match the method call to an
106
+ # existing system label. If that is found, it lazily manufacturers a method on the
107
+ # class of the same name. Otherwise, it throws the NoMethodError.
108
+ #
109
+ def self.method_missing(method, *args, &block)
110
+ begin
106
111
  super
112
+ rescue NoMethodError => e
113
+ if has_acts_as_label_method?(method)
114
+ self.__send__(method)
115
+ elsif method.to_s == "to_ary"
116
+ # do nothing
117
+ else
118
+ throw e
119
+ end
107
120
  end
108
121
  end
109
122
 
110
- # Add custom method missing functionality to perform find by system label lookup. If
111
- # nothing is found, it delegates the call to the original method_missing.
123
+
124
+ # This method determines whether or not the class has an instance with
125
+ # the given system label. If it does, it also lazily creates a method
126
+ # that can be accessed without all this method missing nonsense.
112
127
  #
113
- # The method attempts rails 3 syntax. If that fails, it reverts to rails 2 syntax.
114
- def self.method_missing_with_label(method, *args, &block)
115
- if self.respond_to?(:where)
116
- record = self.where("#{acts_as_label_system_label_column} = ?", method.to_s.upcase).first
117
- else
118
- record = self.find(:first, :conditions => ["#{acts_as_label_system_label_column} = ?", method.to_s.upcase])
119
- end
120
- if record
121
- return record
122
- else
123
- method_missing_without_label(method, *args, &block)
128
+ def self.has_acts_as_label_method?(method_name)
129
+ mn = method_name.to_s.underscore
130
+ sl = mn.upcase
131
+
132
+ if record = by_acts_as_label_system_label(sl)
133
+ eval %Q{
134
+ class << self
135
+ def #{mn}
136
+ by_acts_as_label_system_label('#{sl}')
137
+ end
138
+ end
139
+ }
124
140
  end
141
+
142
+ !!record
125
143
  end
126
144
 
127
- # Add method missing alias
128
- class << self
129
- alias_method_chain :method_missing, :label
145
+
146
+ # 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
+ #
150
+ def self.by_acts_as_label_system_label(system_label)
151
+ sl = system_label.to_s.upcase
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
130
158
  end
131
-
132
- # Add class method to return default record, if needed
159
+
160
+
161
+ # This block adds a class method to return the default record.
162
+ #
133
163
  unless self.method_defined? :default
134
164
  if default.nil?
135
165
  def self.default
@@ -141,7 +171,16 @@ module Coroutine #:nodoc:
141
171
  end
142
172
  end
143
173
  end
144
-
174
+
175
+
176
+ # This method overrides the system label column writer to force
177
+ # upcasing of the value.
178
+ #
179
+ define_method("#{acts_as_label_system_label_column}=") do |value|
180
+ value = value.to_s.strip.upcase unless value.nil?
181
+ write_attribute("#{acts_as_label_system_label_column}", value)
182
+ end
183
+
145
184
 
146
185
  # Add all the instance methods
147
186
  include Coroutine::ActsAsLabel::Base::InstanceMethods
@@ -153,14 +192,6 @@ module Coroutine #:nodoc:
153
192
 
154
193
  module InstanceMethods
155
194
 
156
- # This method updates the system label attribute writer to ensure it is uppercase.
157
- #
158
- def system_label=(value)
159
- value = value.to_s.strip.upcase unless value.nil?
160
- write_attribute("#{acts_as_label_system_label_column}", value)
161
- end
162
-
163
-
164
195
  # This method overrides the to_s method to return the friendly label value.
165
196
  #
166
197
  def to_s
@@ -169,15 +200,25 @@ module Coroutine #:nodoc:
169
200
 
170
201
 
171
202
  # This method overrides the to_sym method to return the downcased symbolized
172
- # system label value. This method is particularly useful in conjunction with
203
+ # system label value. This method is particularly useful in conjunction with
173
204
  # role-based authorization systems.
174
205
  #
175
206
  def to_sym
176
- self.send("#{acts_as_label_system_label_column}").downcase.to_sym
207
+ self.send("#{acts_as_label_system_label_column}").underscore.to_sym
208
+ end
209
+
210
+
211
+ # This method compares two values by running to_sym on both sides. This allows
212
+ # comparisons like the following:
213
+ # u.role == Role.superuser
214
+ # u.role == :superuser
215
+ #
216
+ def ==(other)
217
+ self.to_sym == other.to_sym
177
218
  end
178
219
 
179
220
  end
180
221
 
181
222
  end
182
223
  end
183
- end
224
+ end
data/lib/acts_as_label.rb CHANGED
@@ -1,10 +1,10 @@
1
- # external gems
2
- require "active_record"
3
-
4
-
5
1
  # acts_as_label extension
6
2
  require File.dirname(__FILE__) + "/acts_as_label/base"
7
3
 
8
4
 
9
5
  # add extensions to active record
10
- ::ActiveRecord::Base.send(:include, Coroutine::ActsAsLabel::Base)
6
+ begin
7
+ ::ActiveRecord::Base.send(:include, Coroutine::ActsAsLabel::Base)
8
+ rescue
9
+ # do nothing
10
+ end
data/rails/init.rb CHANGED
@@ -1 +1 @@
1
- require 'acts_as_label'
1
+ require "acts_as_label"
@@ -6,7 +6,6 @@
6
6
  require "test_helper"
7
7
 
8
8
 
9
-
10
9
  #---------------------------------------------------------
11
10
  # Database config
12
11
  #---------------------------------------------------------
@@ -14,7 +13,6 @@ require "test_helper"
14
13
  # establish db connection
15
14
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
16
15
 
17
-
18
16
  # define and seed tables
19
17
  def setup_db
20
18
  ActiveRecord::Schema.define(:version => 1) do
@@ -59,7 +57,6 @@ def teardown_db
59
57
  end
60
58
 
61
59
 
62
-
63
60
  #---------------------------------------------------------
64
61
  # Model definitions
65
62
  #---------------------------------------------------------
@@ -96,7 +93,6 @@ class Framework < ActiveRecord::Base
96
93
  end
97
94
 
98
95
 
99
-
100
96
  #---------------------------------------------------------
101
97
  # Tests
102
98
  #---------------------------------------------------------
@@ -114,8 +110,6 @@ class ActsAsLabelTest < ActiveSupport::TestCase
114
110
  teardown_db
115
111
  end
116
112
 
117
-
118
-
119
113
  #---------------------------------------------
120
114
  # test validations
121
115
  #---------------------------------------------
@@ -216,6 +210,7 @@ class ActsAsLabelTest < ActiveSupport::TestCase
216
210
  begin
217
211
  role_superuser = Role.where("system_label = ?", "SUPERUSER").first
218
212
  role_guest = Role.where("system_label = ?", "GUEST").first
213
+ role_fresh = Role.create!(:system_label => "FRESH", :label => "Fresh")
219
214
  framework_rails = Framework.where("system_name = ?", "RUBY_ON_RAILS").first
220
215
  rescue
221
216
  role_superuser = Role.find(:first, :conditions => ["system_label = ?", "SUPERUSER"])
@@ -223,8 +218,14 @@ class ActsAsLabelTest < ActiveSupport::TestCase
223
218
  framework_rails = Framework.find(:first, :conditions => ["system_name = ?", "RUBY_ON_RAILS"])
224
219
  end
225
220
 
221
+ # Won't have a method now
222
+ assert !Role.methods.map(&:to_s).include?("fresh") # some rubies report strings, some symbols
223
+
226
224
  # test lookup by system label
227
- assert_equal role_superuser, Role.superuser
225
+ assert_equal role_fresh, Role.fresh
226
+
227
+ # should have a method now
228
+ assert Role.methods.map(&:to_s).include?("fresh") # some rubies report strings, some symbols
228
229
 
229
230
  # test default with implemented method
230
231
  assert_equal role_guest, Role.default
@@ -234,10 +235,8 @@ class ActsAsLabelTest < ActiveSupport::TestCase
234
235
 
235
236
  # test default with specified system label
236
237
  assert_equal framework_rails, Framework.default
237
-
238
238
  end
239
239
 
240
-
241
240
  def test_method_missing_finders
242
241
 
243
242
  # dynamic find on stand-alone model
@@ -289,6 +288,13 @@ class ActsAsLabelTest < ActiveSupport::TestCase
289
288
  # test instance methods
290
289
  #---------------------------------------------
291
290
 
291
+ def test_equality
292
+ r1 = Role.superuser
293
+
294
+ assert (r1 == Role.superuser)
295
+ assert (r1 == :superuser)
296
+ end
297
+
292
298
  def test_to_s
293
299
  role = Role.first
294
300
  assert_equal role.label, role.to_s
@@ -300,8 +306,15 @@ class ActsAsLabelTest < ActiveSupport::TestCase
300
306
  end
301
307
 
302
308
  def test_upcase_system_label_value
309
+
310
+ # default system label column name
303
311
  record = Role.create!({ :system_label => "Customer", :label => "Client" })
304
312
  assert_equal record.system_label, "CUSTOMER"
313
+
314
+ # custom system label column name
315
+ record = Framework.create!( :system_name => "example", :name => "Example")
316
+ assert_equal record.system_name, "EXAMPLE"
317
+
305
318
  end
306
319
 
307
320
  end
data/test/test_helper.rb CHANGED
@@ -2,13 +2,12 @@
2
2
  require "rubygems"
3
3
  require "active_support"
4
4
  require "active_support/test_case"
5
+ require "active_record"
5
6
  require "test/unit"
6
7
 
7
8
  # require plugin
8
9
  require "#{File.dirname(__FILE__)}/../init"
9
10
 
10
-
11
-
12
11
  #----------------------------------------------------------
13
12
  # Define global methods
14
13
  #----------------------------------------------------------
metadata CHANGED
@@ -1,21 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_label
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 19
5
+ prerelease:
5
6
  segments:
6
7
  - 1
8
+ - 1
7
9
  - 0
8
- - 2
9
- version: 1.0.2
10
+ version: 1.1.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Coroutine
13
14
  - John Dugan
15
+ - Rick Branson
14
16
  autorequire:
15
17
  bindir: bin
16
18
  cert_chain: []
17
19
 
18
- date: 2010-12-16 00:00:00 -06:00
20
+ date: 2011-05-12 00:00:00 -05:00
19
21
  default_executable:
20
22
  dependencies:
21
23
  - !ruby/object:Gem::Dependency
@@ -26,6 +28,7 @@ dependencies:
26
28
  requirements:
27
29
  - - ">="
28
30
  - !ruby/object:Gem::Version
31
+ hash: 11
29
32
  segments:
30
33
  - 2
31
34
  - 3
@@ -41,6 +44,7 @@ dependencies:
41
44
  requirements:
42
45
  - - ">="
43
46
  - !ruby/object:Gem::Version
47
+ hash: 11
44
48
  segments:
45
49
  - 2
46
50
  - 3
@@ -57,7 +61,6 @@ extensions: []
57
61
  extra_rdoc_files:
58
62
  - README.rdoc
59
63
  files:
60
- - .gitignore
61
64
  - .specification
62
65
  - MIT-LICENSE
63
66
  - README.rdoc
@@ -75,8 +78,8 @@ homepage: http://github.com/coroutine/acts_as_label
75
78
  licenses: []
76
79
 
77
80
  post_install_message:
78
- rdoc_options:
79
- - --charset=UTF-8
81
+ rdoc_options: []
82
+
80
83
  require_paths:
81
84
  - lib
82
85
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -84,6 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
87
  requirements:
85
88
  - - ">="
86
89
  - !ruby/object:Gem::Version
90
+ hash: 3
87
91
  segments:
88
92
  - 0
89
93
  version: "0"
@@ -92,16 +96,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
96
  requirements:
93
97
  - - ">="
94
98
  - !ruby/object:Gem::Version
99
+ hash: 3
95
100
  segments:
96
101
  - 0
97
102
  version: "0"
98
103
  requirements: []
99
104
 
100
105
  rubyforge_project:
101
- rubygems_version: 1.3.7
106
+ rubygems_version: 1.6.2
102
107
  signing_key:
103
108
  specification_version: 3
104
109
  summary: Gem version of acts_as_label Rails plugin.
105
- test_files:
106
- - test/acts_as_label_test.rb
107
- - test/test_helper.rb
110
+ test_files: []
111
+
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- .DS_Store
2
- *.gem