acts_as_label 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.specification CHANGED
@@ -5,10 +5,11 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
+ - Coroutine
12
13
  - John Dugan
13
14
  autorequire:
14
15
  bindir: bin
@@ -39,10 +40,12 @@ extra_rdoc_files:
39
40
  - README.rdoc
40
41
  files:
41
42
  - .gitignore
43
+ - .specification
42
44
  - MIT-LICENSE
43
45
  - README.rdoc
44
46
  - Rakefile
45
47
  - VERSION
48
+ - acts_as_label-0.1.2.gem
46
49
  - acts_as_label.gemspec
47
50
  - init.rb
48
51
  - lib/acts_as_label.rb
data/README.rdoc CHANGED
@@ -6,6 +6,13 @@
6
6
  This acts_as extension implements a system label and a friendly label on a class and centralizes
7
7
  the logic for performing validations and accessing items by system label.
8
8
 
9
+ The extension is particularly useful for tabled, enumerated lists.
10
+
11
+ The system label is declared as a read-only attribute, allowing models and controllers to code
12
+ to the value safely, secure in the knowledge that the value will never change.
13
+
14
+ The friendly value can be changed to any value required by the end-user without affecting the
15
+ model/controller code in any way.
9
16
 
10
17
 
11
18
  == Usage
@@ -42,6 +49,28 @@ record is by implementing a class method on the subclass.
42
49
  end
43
50
 
44
51
 
52
+ The extension also allows records to be access by system label as though the system label were
53
+ a class method. The default option can be accessed in a similar manner using the class
54
+ method +default+. This yields more expressive code.
55
+
56
+ class Role < ActiveRecord::Base
57
+ has_many :users
58
+
59
+ acts_as_label :default => :guest
60
+ end
61
+
62
+ class User < ActiveRecord::Base
63
+ belongs_to :role
64
+ end
65
+
66
+ Role.create!({ :system_label => "SUPERUSER", :label => "Superuser"} )
67
+ Role.create!({ :system_label => "GUEST", :label => "Guest"} )
68
+
69
+ User.create!({ :name => "John Dugan", :role => Role.superuser })
70
+ User.create!({ :name => "Anonymous Dude", :role => Role.default })
71
+
72
+
73
+
45
74
 
46
75
  == License
47
76
 
data/Rakefile CHANGED
@@ -33,7 +33,7 @@ begin
33
33
  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."
34
34
  gemspec.email = "jdugan@coroutine.com"
35
35
  gemspec.homepage = "http://github.com/coroutine/acts_as_label"
36
- gemspec.authors = ["John Dugan"]
36
+ gemspec.authors = ["Coroutine", "John Dugan"]
37
37
  gemspec.add_dependency "activesupport"
38
38
  end
39
39
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -5,10 +5,10 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_label}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["John Dugan"]
11
+ s.authors = ["Coroutine", "John Dugan"]
12
12
  s.date = %q{2010-03-10}
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}
data/lib/acts_as_label.rb CHANGED
@@ -88,6 +88,10 @@ module Coroutine #:nodoc:
88
88
  class_inheritable_reader :acts_as_label_default_system_label
89
89
 
90
90
 
91
+ # protect attributes
92
+ attr_readonly system_label
93
+
94
+
91
95
  # Add validations
92
96
  validates_presence_of system_label
93
97
  validates_length_of system_label, :maximum => 255
@@ -241,6 +241,39 @@ class ActsAsLabelTest < ActiveSupport::TestCase
241
241
 
242
242
 
243
243
 
244
+ #---------------------------------------------
245
+ # test validations
246
+ #---------------------------------------------
247
+
248
+ def test_system_label_is_readonly
249
+
250
+ # build valid record
251
+ record = Role.new({ :system_label => "CUSTOMER", :label => "Client" })
252
+ assert record.valid?
253
+
254
+ # save it and remember id and system label
255
+ record.save
256
+ id = record.id
257
+ system_label = record.system_label
258
+
259
+ # system label unchanged on safe update
260
+ record.label = "Customer"
261
+ record.save
262
+ record = Role.find(id) # we have to get the record again to verify what's in the db
263
+ assert_equal system_label, record.system_label
264
+
265
+ # system_label unchanged on unsafe update
266
+ record.system_label = "CLIENT"
267
+ record.label = "Client"
268
+ record.save
269
+ record = Role.find(id) # we have to get the record again to verify what's in the db
270
+ assert_equal "Client", record.label
271
+ assert_equal system_label, record.system_label
272
+
273
+ end
274
+
275
+
276
+
244
277
  #---------------------------------------------
245
278
  # test instance methods
246
279
  #---------------------------------------------
metadata CHANGED
@@ -5,10 +5,11 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
+ - Coroutine
12
13
  - John Dugan
13
14
  autorequire:
14
15
  bindir: bin