activerecord-enumerated_model 0.1.2
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/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +67 -0
- data/Rakefile +9 -0
- data/activerecord-enumerated_model.gemspec +23 -0
- data/lib/active_record-enumerated_model.rb +5 -0
- data/lib/active_record-enumerated_model/enumerated_model.rb +74 -0
- data/lib/active_record-enumerated_model/version.rb +5 -0
- data/test/helper.rb +7 -0
- data/test/test_bypass.rb +43 -0
- data/test/test_enumeration.rb +60 -0
- metadata +126 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# ActiveRecord::EnumeratedModel
|
2
|
+
|
3
|
+
ActiveRecord::EnumeratedModel is an ActiveRecord extensions that allows
|
4
|
+
you to create enumerated constants out of a model containing static
|
5
|
+
data.
|
6
|
+
|
7
|
+
Sometimes you have a model where values will not change. Consider the
|
8
|
+
following:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
class Role
|
12
|
+
ADMIN = 1
|
13
|
+
EDITOR = 2
|
14
|
+
VIEWER = 3
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
We'll often do something like this in order to easily check values in
|
19
|
+
our application.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
unless @current_user.has?(Role::ADMIN)
|
23
|
+
# some admin stuff
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
Keeping this out of ActiveRecord prevents us from doing any kind of
|
28
|
+
joining in the database and limits what `Role` can do. Wouldn't be cool
|
29
|
+
if you could `Role::ADMIN` returned an ActiveRecord row?
|
30
|
+
|
31
|
+
Role::ADMIN == #<Role name: "Admin">
|
32
|
+
|
33
|
+
This is what ActiveRecord::EnumeratedModel provides. Since we're dealing
|
34
|
+
with constants here, ActiveRecord::EnumeratedModel also makes your model
|
35
|
+
readonly.
|
36
|
+
|
37
|
+
## Installation
|
38
|
+
|
39
|
+
Add this line to your application's Gemfile:
|
40
|
+
|
41
|
+
gem 'activerecord-enumerated_model'
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
$ bundle
|
46
|
+
|
47
|
+
Or install it yourself as:
|
48
|
+
|
49
|
+
$ gem install activerecord-enumerated_model
|
50
|
+
|
51
|
+
## Usage
|
52
|
+
|
53
|
+
Simply include `ActiveRecord::EnumeratedModel` in your model:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class StaticThing < ActiveRecord::Base
|
57
|
+
include ActiveRecord::EnumeratedModel
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/active_record-enumerated_model/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Christian Dinger"]
|
6
|
+
gem.email = ["cdinger@gmail.com"]
|
7
|
+
gem.description = %q{Creates an enumeration of constants that contain ActiveRecord rows for a static model}
|
8
|
+
gem.summary = %q{Creates an enumeration of constants that contain ActiveRecord rows for a static model}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "activerecord-enumerated_model"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActiveRecord::EnumeratedModel::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency('activerecord', '>= 3.0.0')
|
19
|
+
gem.add_dependency('activerecord-readonly_model', '>= 0.0.1')
|
20
|
+
|
21
|
+
gem.add_development_dependency('sqlite3')
|
22
|
+
gem.add_development_dependency('shoulda')
|
23
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module EnumeratedModel
|
3
|
+
class UnknownAttributeError < StandardError; end
|
4
|
+
class NilConstantError < StandardError; end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
def create_enumeration_constants(enumeration_attribute = :name, options = {:force => false})
|
9
|
+
@enumeration_attribute = enumeration_attribute
|
10
|
+
@enumerated_constants = [] if @enumerated_constants.blank?
|
11
|
+
clear_existing_constants if options[:force]
|
12
|
+
attribute = enumeration_attribute.to_s
|
13
|
+
|
14
|
+
self.all.each do |instance|
|
15
|
+
unless instance.attributes.include?(attribute)
|
16
|
+
raise UnknownAttributeError.new("Tried to create constants on the #{attribute} attribute, but it doesn't exist on this model")
|
17
|
+
end
|
18
|
+
value = instance.attributes[attribute]
|
19
|
+
if value.blank?
|
20
|
+
raise NilConstantError.new("Encountered a nil value on the #{attribute} attribute and can't create a constant")
|
21
|
+
end
|
22
|
+
const = ActiveRecord::EnumeratedModel.constant_friendly_string(value)
|
23
|
+
if self.const_defined?(const)
|
24
|
+
if options[:force]
|
25
|
+
self.send(:remove_const, const)
|
26
|
+
else
|
27
|
+
raise RuntimeError.new("Constant #{self.to_s}::#{const} has already been defined")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
self.const_set(const, instance)
|
31
|
+
@enumerated_constants << const
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def clear_existing_constants
|
36
|
+
unless @enumerated_constants.blank?
|
37
|
+
@enumerated_constants.each do |existing_constant|
|
38
|
+
if self.const_defined?(existing_constant)
|
39
|
+
self.send(:remove_const, existing_constant)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def bypass_readonly(&block)
|
46
|
+
ActiveRecord::ReadonlyModel.bypass do
|
47
|
+
yield
|
48
|
+
end
|
49
|
+
# regenerate constants
|
50
|
+
create_enumeration_constants(@enumeration_attribute, :force => true)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.included(caller)
|
55
|
+
caller.extend(ClassMethods)
|
56
|
+
caller.send(:include, ActiveRecord::ReadonlyModel)
|
57
|
+
if caller.attribute_names.include? 'name'
|
58
|
+
caller.create_enumeration_constants :name
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.constant_friendly_string(str)
|
63
|
+
const = str.upcase
|
64
|
+
const.gsub!(/\s+/, '_') # replace whitespace with an underscore
|
65
|
+
const.gsub!(/^[^A-Z]+/, '') # remove leading numbers, symbols, etc
|
66
|
+
const.gsub!(/[-\/\\]/, '_') # turn these symbols into underscores
|
67
|
+
const.gsub!(/[^A-Z_]/, '') # remove anything that's not a letter or underscore
|
68
|
+
const.gsub!(/_+/, '_') # collapse multiple adjacent underscores
|
69
|
+
const.gsub!(/[^A-Z0-9]$/, '') # always end with a non-symbol
|
70
|
+
const
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_bypass.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestBypass < Test::Unit::TestCase
|
4
|
+
context "Some static model" do
|
5
|
+
setup do
|
6
|
+
ActiveRecord::Base.connection.create_table :static_things, :force => true do |t|
|
7
|
+
t.string :name
|
8
|
+
end
|
9
|
+
ActiveRecord::Base.connection.execute("insert into static_things (name) values ('asdf')")
|
10
|
+
class StaticThing < ActiveRecord::Base
|
11
|
+
include ActiveRecord::EnumeratedModel
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
teardown do
|
16
|
+
ActiveRecord::Base.connection.drop_table :static_things
|
17
|
+
end
|
18
|
+
|
19
|
+
should "bypass readonly to destroy" do
|
20
|
+
before_count = StaticThing.count
|
21
|
+
StaticThing.bypass_readonly do
|
22
|
+
t = StaticThing.first
|
23
|
+
t.destroy
|
24
|
+
end
|
25
|
+
assert_not_equal(before_count, StaticThing.count)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "bypass readonly to update" do
|
29
|
+
StaticThing.bypass_readonly do
|
30
|
+
thing = StaticThing.first
|
31
|
+
thing.name = 'lkjasdflkjasdf'
|
32
|
+
assert(thing.save)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
should "reload constants after bypass" do
|
37
|
+
StaticThing.bypass_readonly do
|
38
|
+
StaticThing.create(:name => 'new thing')
|
39
|
+
end
|
40
|
+
assert(StaticThing::NEW_THING)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestEnumeration < Test::Unit::TestCase
|
4
|
+
context "Some static model" do
|
5
|
+
setup do
|
6
|
+
ActiveRecord::Base.connection.create_table :static_things, :force => true do |t|
|
7
|
+
t.string :name
|
8
|
+
end
|
9
|
+
ActiveRecord::Base.connection.execute("insert into static_things (name) values ('Blah thing')")
|
10
|
+
ActiveRecord::Base.connection.execute("insert into static_things (name) values ('''nother blah\" thing. !')")
|
11
|
+
class StaticThing < ActiveRecord::Base
|
12
|
+
include ActiveRecord::EnumeratedModel
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
teardown do
|
17
|
+
ActiveRecord::Base.connection.drop_table :static_things
|
18
|
+
end
|
19
|
+
|
20
|
+
should "create constants" do
|
21
|
+
assert(ActiveRecord::EnumeratedModel)
|
22
|
+
assert_equal(StaticThing::BLAH_THING, StaticThing.first)
|
23
|
+
assert_equal(StaticThing::NOTHER_BLAH_THING, StaticThing.last)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "Some static model without a name attribute" do
|
28
|
+
setup do
|
29
|
+
ActiveRecord::Base.connection.create_table :static_thingies, :force => true do |t|
|
30
|
+
t.string :blah
|
31
|
+
end
|
32
|
+
ActiveRecord::Base.connection.execute("insert into static_thingies (blah) values ('ahoy hoy')")
|
33
|
+
class StaticThingy < ActiveRecord::Base
|
34
|
+
include ActiveRecord::EnumeratedModel
|
35
|
+
create_enumeration_constants :blah
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
teardown do
|
40
|
+
ActiveRecord::Base.connection.drop_table :static_thingies
|
41
|
+
end
|
42
|
+
|
43
|
+
should "create constants" do
|
44
|
+
assert(ActiveRecord::EnumeratedModel)
|
45
|
+
assert_equal(StaticThingy::AHOY_HOY, StaticThingy.first)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "constant_friendly_string method" do
|
50
|
+
should "properly format a string for use as a constant" do
|
51
|
+
assert_equal("SOME_CONSTANT", ActiveRecord::EnumeratedModel.constant_friendly_string('Some constant'))
|
52
|
+
assert_equal("THIS_THAT", ActiveRecord::EnumeratedModel.constant_friendly_string('this/that'))
|
53
|
+
assert_equal("BLAH", ActiveRecord::EnumeratedModel.constant_friendly_string('123blah'))
|
54
|
+
assert_equal("BLAH", ActiveRecord::EnumeratedModel.constant_friendly_string('___123blah'))
|
55
|
+
assert_equal("BLAH", ActiveRecord::EnumeratedModel.constant_friendly_string('blah!!!'))
|
56
|
+
assert_equal("BLAH_BLAH", ActiveRecord::EnumeratedModel.constant_friendly_string('blah blah'))
|
57
|
+
assert_equal("THIS_THAT_THE_OTHER", ActiveRecord::EnumeratedModel.constant_friendly_string('This, that, & the other.'))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-enumerated_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christian Dinger
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord-readonly_model
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.0.1
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.0.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: shoulda
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Creates an enumeration of constants that contain ActiveRecord rows for
|
79
|
+
a static model
|
80
|
+
email:
|
81
|
+
- cdinger@gmail.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- activerecord-enumerated_model.gemspec
|
92
|
+
- lib/active_record-enumerated_model.rb
|
93
|
+
- lib/active_record-enumerated_model/enumerated_model.rb
|
94
|
+
- lib/active_record-enumerated_model/version.rb
|
95
|
+
- test/helper.rb
|
96
|
+
- test/test_bypass.rb
|
97
|
+
- test/test_enumeration.rb
|
98
|
+
homepage: ''
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.24
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Creates an enumeration of constants that contain ActiveRecord rows for a
|
122
|
+
static model
|
123
|
+
test_files:
|
124
|
+
- test/helper.rb
|
125
|
+
- test/test_bypass.rb
|
126
|
+
- test/test_enumeration.rb
|