acts_as_label 0.1.3 → 0.1.4
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.
- data/.specification +9 -19
- data/Rakefile +6 -4
- data/VERSION +1 -1
- data/acts_as_label.gemspec +4 -3
- data/init.rb +1 -5
- data/lib/acts_as_label.rb +6 -164
- data/lib/acts_as_label/base.rb +168 -0
- metadata +10 -18
data/.specification
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_label
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 3
|
9
|
-
version: 0.1.3
|
4
|
+
version: 0.1.4
|
10
5
|
platform: ruby
|
11
6
|
authors:
|
12
7
|
- Coroutine
|
@@ -15,21 +10,19 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2010-03-
|
13
|
+
date: 2010-03-25 00:00:00 -05:00
|
19
14
|
default_executable:
|
20
15
|
dependencies:
|
21
16
|
- !ruby/object:Gem::Dependency
|
22
17
|
name: activesupport
|
23
|
-
|
24
|
-
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
21
|
requirements:
|
26
22
|
- - ">="
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
24
|
version: "0"
|
31
|
-
|
32
|
-
version_requirements: *id001
|
25
|
+
version:
|
33
26
|
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
27
|
email: jdugan@coroutine.com
|
35
28
|
executables: []
|
@@ -45,7 +38,6 @@ files:
|
|
45
38
|
- README.rdoc
|
46
39
|
- Rakefile
|
47
40
|
- VERSION
|
48
|
-
- acts_as_label-0.1.2.gem
|
49
41
|
- acts_as_label.gemspec
|
50
42
|
- init.rb
|
51
43
|
- lib/acts_as_label.rb
|
@@ -64,20 +56,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
56
|
requirements:
|
65
57
|
- - ">="
|
66
58
|
- !ruby/object:Gem::Version
|
67
|
-
segments:
|
68
|
-
- 0
|
69
59
|
version: "0"
|
60
|
+
version:
|
70
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
62
|
requirements:
|
72
63
|
- - ">="
|
73
64
|
- !ruby/object:Gem::Version
|
74
|
-
segments:
|
75
|
-
- 0
|
76
65
|
version: "0"
|
66
|
+
version:
|
77
67
|
requirements: []
|
78
68
|
|
79
69
|
rubyforge_project:
|
80
|
-
rubygems_version: 1.3.
|
70
|
+
rubygems_version: 1.3.5
|
81
71
|
signing_key:
|
82
72
|
specification_version: 3
|
83
73
|
summary: Gem version of acts_as_label Rails plugin.
|
data/Rakefile
CHANGED
@@ -28,13 +28,15 @@ end
|
|
28
28
|
|
29
29
|
begin
|
30
30
|
Jeweler::Tasks.new do |gemspec|
|
31
|
-
gemspec.
|
32
|
-
gemspec.summary = "Gem version of acts_as_label Rails plugin."
|
31
|
+
gemspec.authors = ["Coroutine", "John Dugan"]
|
33
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."
|
34
33
|
gemspec.email = "jdugan@coroutine.com"
|
35
34
|
gemspec.homepage = "http://github.com/coroutine/acts_as_label"
|
36
|
-
gemspec.
|
37
|
-
gemspec.
|
35
|
+
gemspec.name = "acts_as_label"
|
36
|
+
gemspec.summary = "Gem version of acts_as_label Rails plugin."
|
37
|
+
|
38
|
+
gemspec.add_dependency("activesupport")
|
39
|
+
gemspec.files.include("lib/**/*.rb")
|
38
40
|
end
|
39
41
|
Jeweler::GemcutterTasks.new
|
40
42
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/acts_as_label.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{acts_as_label}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Coroutine", "John Dugan"]
|
12
|
-
s.date = %q{2010-03-
|
12
|
+
s.date = %q{2010-03-25}
|
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 = [
|
@@ -25,13 +25,14 @@ Gem::Specification.new do |s|
|
|
25
25
|
"acts_as_label.gemspec",
|
26
26
|
"init.rb",
|
27
27
|
"lib/acts_as_label.rb",
|
28
|
+
"lib/acts_as_label/base.rb",
|
28
29
|
"test/acts_as_label_test.rb",
|
29
30
|
"test/test_helper.rb"
|
30
31
|
]
|
31
32
|
s.homepage = %q{http://github.com/coroutine/acts_as_label}
|
32
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
33
34
|
s.require_paths = ["lib"]
|
34
|
-
s.rubygems_version = %q{1.3.
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
35
36
|
s.summary = %q{Gem version of acts_as_label Rails plugin.}
|
36
37
|
s.test_files = [
|
37
38
|
"test/acts_as_label_test.rb",
|
data/init.rb
CHANGED
data/lib/acts_as_label.rb
CHANGED
@@ -1,168 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module Label #:nodoc:
|
1
|
+
# external gems
|
2
|
+
require "active_record"
|
4
3
|
|
5
|
-
def self.included(base) #:nodoc:
|
6
|
-
base.extend(ClassMethods)
|
7
|
-
end
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# == Description
|
14
|
-
#
|
15
|
-
# This +acts_as+ extension implements a system label and a friendly label on a class and centralizes
|
16
|
-
# the logic for performing validations and accessing items by system label.
|
17
|
-
#
|
18
|
-
#
|
19
|
-
# == Usage
|
20
|
-
#
|
21
|
-
# Simple Example
|
22
|
-
#
|
23
|
-
# class BillingFrequency < ActiveRecord::Base
|
24
|
-
# has_many :subscriptions
|
25
|
-
# acts_as_label :default => :monthly
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
# class Subscription < ActiveRecord::Base
|
29
|
-
# belongs_to :billing_frequency
|
30
|
-
# end
|
31
|
-
#
|
32
|
-
# subscription.billing_frequency = BillingFrequency.monthly
|
33
|
-
# subscription.billing_frequency = BillingFrequency.default
|
34
|
-
#
|
35
|
-
#
|
36
|
-
# STI Example:
|
37
|
-
#
|
38
|
-
# class Label < ActiveRecord::Base
|
39
|
-
# acts_as_label :scoped_to => :type
|
40
|
-
# end
|
41
|
-
#
|
42
|
-
# class BillingFrequency < Label
|
43
|
-
# has_many :subscriptions
|
44
|
-
# def self.default
|
45
|
-
# BillingFrequency.monthly
|
46
|
-
# end
|
47
|
-
# end
|
48
|
-
#
|
49
|
-
# class Subscription < ActiveRecord::Base
|
50
|
-
# belongs_to :billing_frequency
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
# subscription.billing_frequency = BillingFrequency.monthly
|
54
|
-
# subscription.billing_frequency = BillingFrequency.default
|
55
|
-
#
|
56
|
-
#
|
57
|
-
# == Configuration
|
58
|
-
#
|
59
|
-
# * +system_label_cloumn+ - specifies the column name to use for storing the system label (default: +system_label+)
|
60
|
-
# * +label_column+ - specifies the column name to use for storing the label (default: +label+)
|
61
|
-
# * +default+ - specifies the system label value of the default instance (default: the first record in the default scope)
|
62
|
-
#
|
63
|
-
def acts_as_label(options = {})
|
64
|
-
|
65
|
-
#-------------------------------------------
|
66
|
-
# scrub options
|
67
|
-
#-------------------------------------------
|
68
|
-
options = {} unless options.is_a?(Hash)
|
69
|
-
system_label = options.key?(:system_label_column) ? options[:system_label_column].to_sym : :system_label
|
70
|
-
label = options.key?(:label_column) ? options[:label_column].to_sym : :label
|
71
|
-
scope = options.key?(:scope) ? options[:scope] : "1 = 1"
|
72
|
-
default = options.key?(:default) ? options[:default].to_sym : nil
|
73
|
-
|
74
|
-
|
75
|
-
#--------------------------------------------
|
76
|
-
# mix methods into class definition
|
77
|
-
#--------------------------------------------
|
78
|
-
class_eval do
|
79
|
-
|
80
|
-
# Add inheritable accessors
|
81
|
-
write_inheritable_attribute :acts_as_label_system_label_column, system_label
|
82
|
-
class_inheritable_reader :acts_as_label_system_label_column
|
83
|
-
write_inheritable_attribute :acts_as_label_label_column, label
|
84
|
-
class_inheritable_reader :acts_as_label_label_column
|
85
|
-
write_inheritable_attribute :acts_as_label_scope, scope
|
86
|
-
class_inheritable_reader :acts_as_label_scope
|
87
|
-
write_inheritable_attribute :acts_as_label_default_system_label, default
|
88
|
-
class_inheritable_reader :acts_as_label_default_system_label
|
89
|
-
|
90
|
-
|
91
|
-
# protect attributes
|
92
|
-
attr_readonly system_label
|
93
|
-
|
94
|
-
|
95
|
-
# Add validations
|
96
|
-
validates_presence_of system_label
|
97
|
-
validates_length_of system_label, :maximum => 255
|
98
|
-
validates_format_of system_label, :with => /^[A-Z][_A-Z0-9]*$/
|
99
|
-
validates_presence_of label
|
100
|
-
validates_length_of label, :maximum => 255
|
101
|
-
|
102
|
-
|
103
|
-
# Add method missing, if needed
|
104
|
-
unless self.method_defined? :method_missing
|
105
|
-
def self.method_missing(method, *args, &block)
|
106
|
-
super
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
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.
|
112
|
-
def self.method_missing_with_label(method, *args, &block)
|
113
|
-
record = self.find(:first, :conditions => ["#{acts_as_label_system_label_column} = ?", method.to_s.upcase])
|
114
|
-
if record
|
115
|
-
return record
|
116
|
-
else
|
117
|
-
method_missing_without_label(method, *args, &block)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
# Add method missing alias
|
122
|
-
class << self
|
123
|
-
alias_method_chain :method_missing, :label
|
124
|
-
end
|
125
|
-
|
126
|
-
# Add class method to return default record, if needed
|
127
|
-
unless self.method_defined? :default
|
128
|
-
if default.nil?
|
129
|
-
def self.default
|
130
|
-
self.first
|
131
|
-
end
|
132
|
-
else
|
133
|
-
def self.default
|
134
|
-
self.send("#{acts_as_label_default_system_label}")
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
|
140
|
-
# Add all the instance methods
|
141
|
-
include Coroutine::Acts::Label::InstanceMethods
|
5
|
+
# acts_as_label extension
|
6
|
+
require File.dirname(__FILE__) + "/acts_as_label/base"
|
142
7
|
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
8
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
# This method updates the system label attribute writer to ensure it is uppercase.
|
151
|
-
#
|
152
|
-
def system_label=(value)
|
153
|
-
value = value.to_s.strip.upcase unless value.nil?
|
154
|
-
write_attribute("#{acts_as_label_system_label_column}", value)
|
155
|
-
end
|
156
|
-
|
157
|
-
|
158
|
-
# This method overrides the to_s method to return the friendly label value.
|
159
|
-
#
|
160
|
-
def to_s
|
161
|
-
self.send("#{acts_as_label_label_column}")
|
162
|
-
end
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
9
|
+
# add extensions to active record
|
10
|
+
::ActiveRecord::Base.send :include, Coroutine::ActsAsLabel::Base
|
@@ -0,0 +1,168 @@
|
|
1
|
+
module Coroutine #:nodoc:
|
2
|
+
module ActsAsLabel #:nodoc:
|
3
|
+
module Base #:nodoc:
|
4
|
+
|
5
|
+
def self.included(base) #:nodoc:
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
|
13
|
+
# == Description
|
14
|
+
#
|
15
|
+
# This +acts_as+ extension implements a system label and a friendly label on a class and centralizes
|
16
|
+
# the logic for performing validations and accessing items by system label.
|
17
|
+
#
|
18
|
+
#
|
19
|
+
# == Usage
|
20
|
+
#
|
21
|
+
# Simple Example
|
22
|
+
#
|
23
|
+
# class BillingFrequency < ActiveRecord::Base
|
24
|
+
# has_many :subscriptions
|
25
|
+
# acts_as_label :default => :monthly
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# class Subscription < ActiveRecord::Base
|
29
|
+
# belongs_to :billing_frequency
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# subscription.billing_frequency = BillingFrequency.monthly
|
33
|
+
# subscription.billing_frequency = BillingFrequency.default
|
34
|
+
#
|
35
|
+
#
|
36
|
+
# STI Example:
|
37
|
+
#
|
38
|
+
# class Label < ActiveRecord::Base
|
39
|
+
# acts_as_label :scoped_to => :type
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# class BillingFrequency < Label
|
43
|
+
# has_many :subscriptions
|
44
|
+
# def self.default
|
45
|
+
# BillingFrequency.monthly
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# class Subscription < ActiveRecord::Base
|
50
|
+
# belongs_to :billing_frequency
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# subscription.billing_frequency = BillingFrequency.monthly
|
54
|
+
# subscription.billing_frequency = BillingFrequency.default
|
55
|
+
#
|
56
|
+
#
|
57
|
+
# == Configuration
|
58
|
+
#
|
59
|
+
# * +system_label_cloumn+ - specifies the column name to use for storing the system label (default: +system_label+)
|
60
|
+
# * +label_column+ - specifies the column name to use for storing the label (default: +label+)
|
61
|
+
# * +default+ - specifies the system label value of the default instance (default: the first record in the default scope)
|
62
|
+
#
|
63
|
+
def acts_as_label(options = {})
|
64
|
+
|
65
|
+
#-------------------------------------------
|
66
|
+
# scrub options
|
67
|
+
#-------------------------------------------
|
68
|
+
options = {} unless options.is_a?(Hash)
|
69
|
+
system_label = options.key?(:system_label_column) ? options[:system_label_column].to_sym : :system_label
|
70
|
+
label = options.key?(:label_column) ? options[:label_column].to_sym : :label
|
71
|
+
scope = options.key?(:scope) ? options[:scope] : "1 = 1"
|
72
|
+
default = options.key?(:default) ? options[:default].to_sym : nil
|
73
|
+
|
74
|
+
|
75
|
+
#--------------------------------------------
|
76
|
+
# mix methods into class definition
|
77
|
+
#--------------------------------------------
|
78
|
+
class_eval do
|
79
|
+
|
80
|
+
# Add inheritable accessors
|
81
|
+
write_inheritable_attribute :acts_as_label_system_label_column, system_label
|
82
|
+
class_inheritable_reader :acts_as_label_system_label_column
|
83
|
+
write_inheritable_attribute :acts_as_label_label_column, label
|
84
|
+
class_inheritable_reader :acts_as_label_label_column
|
85
|
+
write_inheritable_attribute :acts_as_label_scope, scope
|
86
|
+
class_inheritable_reader :acts_as_label_scope
|
87
|
+
write_inheritable_attribute :acts_as_label_default_system_label, default
|
88
|
+
class_inheritable_reader :acts_as_label_default_system_label
|
89
|
+
|
90
|
+
|
91
|
+
# protect attributes
|
92
|
+
attr_readonly system_label
|
93
|
+
|
94
|
+
|
95
|
+
# Add validations
|
96
|
+
validates_presence_of system_label
|
97
|
+
validates_length_of system_label, :maximum => 255
|
98
|
+
validates_format_of system_label, :with => /^[A-Z][_A-Z0-9]*$/
|
99
|
+
validates_presence_of label
|
100
|
+
validates_length_of label, :maximum => 255
|
101
|
+
|
102
|
+
|
103
|
+
# Add method missing, if needed
|
104
|
+
unless self.method_defined? :method_missing
|
105
|
+
def self.method_missing(method, *args, &block)
|
106
|
+
super
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
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.
|
112
|
+
def self.method_missing_with_label(method, *args, &block)
|
113
|
+
record = self.find(:first, :conditions => ["#{acts_as_label_system_label_column} = ?", method.to_s.upcase])
|
114
|
+
if record
|
115
|
+
return record
|
116
|
+
else
|
117
|
+
method_missing_without_label(method, *args, &block)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Add method missing alias
|
122
|
+
class << self
|
123
|
+
alias_method_chain :method_missing, :label
|
124
|
+
end
|
125
|
+
|
126
|
+
# Add class method to return default record, if needed
|
127
|
+
unless self.method_defined? :default
|
128
|
+
if default.nil?
|
129
|
+
def self.default
|
130
|
+
self.first
|
131
|
+
end
|
132
|
+
else
|
133
|
+
def self.default
|
134
|
+
self.send("#{acts_as_label_default_system_label}")
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
# Add all the instance methods
|
141
|
+
include Coroutine::ActsAsLabel::Base::InstanceMethods
|
142
|
+
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
module InstanceMethods
|
149
|
+
|
150
|
+
# This method updates the system label attribute writer to ensure it is uppercase.
|
151
|
+
#
|
152
|
+
def system_label=(value)
|
153
|
+
value = value.to_s.strip.upcase unless value.nil?
|
154
|
+
write_attribute("#{acts_as_label_system_label_column}", value)
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
# This method overrides the to_s method to return the friendly label value.
|
159
|
+
#
|
160
|
+
def to_s
|
161
|
+
self.send("#{acts_as_label_label_column}")
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_label
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 3
|
9
|
-
version: 0.1.3
|
4
|
+
version: 0.1.4
|
10
5
|
platform: ruby
|
11
6
|
authors:
|
12
7
|
- Coroutine
|
@@ -15,21 +10,19 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2010-03-
|
13
|
+
date: 2010-03-25 00:00:00 -05:00
|
19
14
|
default_executable:
|
20
15
|
dependencies:
|
21
16
|
- !ruby/object:Gem::Dependency
|
22
17
|
name: activesupport
|
23
|
-
|
24
|
-
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
21
|
requirements:
|
26
22
|
- - ">="
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
24
|
version: "0"
|
31
|
-
|
32
|
-
version_requirements: *id001
|
25
|
+
version:
|
33
26
|
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
27
|
email: jdugan@coroutine.com
|
35
28
|
executables: []
|
@@ -48,6 +41,7 @@ files:
|
|
48
41
|
- acts_as_label.gemspec
|
49
42
|
- init.rb
|
50
43
|
- lib/acts_as_label.rb
|
44
|
+
- lib/acts_as_label/base.rb
|
51
45
|
- test/acts_as_label_test.rb
|
52
46
|
- test/test_helper.rb
|
53
47
|
has_rdoc: true
|
@@ -63,20 +57,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
57
|
requirements:
|
64
58
|
- - ">="
|
65
59
|
- !ruby/object:Gem::Version
|
66
|
-
segments:
|
67
|
-
- 0
|
68
60
|
version: "0"
|
61
|
+
version:
|
69
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
63
|
requirements:
|
71
64
|
- - ">="
|
72
65
|
- !ruby/object:Gem::Version
|
73
|
-
segments:
|
74
|
-
- 0
|
75
66
|
version: "0"
|
67
|
+
version:
|
76
68
|
requirements: []
|
77
69
|
|
78
70
|
rubyforge_project:
|
79
|
-
rubygems_version: 1.3.
|
71
|
+
rubygems_version: 1.3.5
|
80
72
|
signing_key:
|
81
73
|
specification_version: 3
|
82
74
|
summary: Gem version of acts_as_label Rails plugin.
|