enumitation 0.0.1 → 0.0.3
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/.rspec +1 -0
- data/README.md +6 -3
- data/enumitation.gemspec +3 -1
- data/lib/enumitation.rb +6 -4
- data/lib/enumitation/class_methods.rb +3 -5
- data/lib/enumitation/version.rb +1 -1
- data/spec/enumitation_spec.rb +42 -0
- data/spec/lib/class_methods_spec.rb +42 -0
- data/spec/spec_helper.rb +19 -0
- metadata +33 -5
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format doc
|
data/README.md
CHANGED
@@ -13,8 +13,7 @@ join table to contain the values and a foreign key constraint were overkill? We
|
|
13
13
|
could opt for an enum field, but maybe your DBMS doesn't support enums. Or maybe it does,
|
14
14
|
but you just aren't in the mood for an enum field today.
|
15
15
|
|
16
|
-
Also, do you need to provide these values
|
17
|
-
in a form?
|
16
|
+
Also, do you need to provide these values as options in a select form field?
|
18
17
|
|
19
18
|
Enumitation exists to allow restrictions on ActiveRecord attribute values without
|
20
19
|
the need for a join table or enum field, while also providing those values for
|
@@ -81,7 +80,11 @@ all that work when you can get it from a one-liner?
|
|
81
80
|
True, but the ones I found either didn't fit my needs or they did so much more than I needed.
|
82
81
|
I wanted something that fit my needs exactly and nothing more.
|
83
82
|
|
83
|
+
## Testing
|
84
|
+
|
85
|
+
bundle exec rspec spec
|
86
|
+
|
84
87
|
## Contributing
|
85
88
|
|
86
89
|
If you think Enumitation could be better, fork away and send a pull
|
87
|
-
request!
|
90
|
+
request! Please make sure to include specs for your changes.
|
data/enumitation.gemspec
CHANGED
@@ -20,7 +20,9 @@ DESC
|
|
20
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
21
|
s.add_development_dependency('rspec', '> 2')
|
22
22
|
s.add_development_dependency('rr', '>= 1.0.2')
|
23
|
-
s.add_development_dependency('activerecord', '
|
23
|
+
s.add_development_dependency('activerecord', '~> 3')
|
24
|
+
s.add_development_dependency('ap')
|
25
|
+
s.add_development_dependency('ruby-debug19')
|
24
26
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
25
27
|
s.require_paths = ["lib"]
|
26
28
|
end
|
data/lib/enumitation.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
module Enumitation
|
2
2
|
autoload :ClassMethods, 'enumitation/class_methods'
|
3
3
|
|
4
|
-
ActiveRecord::Base.
|
5
|
-
def
|
6
|
-
|
7
|
-
|
4
|
+
ActiveRecord::Base.instance_eval do
|
5
|
+
def enumitation(attribute, values)
|
6
|
+
extend ClassMethods
|
7
|
+
|
8
|
+
self.enumitation_values[attribute] = Array(values)
|
9
|
+
add_inclusion_validation(attribute, values)
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
@@ -2,6 +2,9 @@ module Enumitation
|
|
2
2
|
module ClassMethods
|
3
3
|
|
4
4
|
def self.extended(base)
|
5
|
+
# If we've already been extended, don't do it again
|
6
|
+
return if defined? base.enumitation_values
|
7
|
+
|
5
8
|
class << base
|
6
9
|
attr_accessor :enumitation_values
|
7
10
|
end
|
@@ -9,11 +12,6 @@ module Enumitation
|
|
9
12
|
base.enumitation_values = {}
|
10
13
|
end
|
11
14
|
|
12
|
-
def enumitation(attribute, values)
|
13
|
-
enumitation_values[attribute] = Array(values)
|
14
|
-
add_inclusion_validation(attribute, values)
|
15
|
-
end
|
16
|
-
|
17
15
|
def select_options_for(attribute)
|
18
16
|
return [] if enumitation_values.empty?
|
19
17
|
|
data/lib/enumitation/version.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Enumitation do
|
4
|
+
before(:all) do
|
5
|
+
class MyModel < ActiveRecord::Base
|
6
|
+
enumitation :number, %w[1 2]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:all) do
|
11
|
+
Object.send :remove_const, :MyModel
|
12
|
+
end
|
13
|
+
|
14
|
+
it "adds the enumitation method to the model" do
|
15
|
+
MyModel.methods.should include(:enumitation)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#enumitation" do
|
19
|
+
it "adds the select_options_for method to the model" do
|
20
|
+
MyModel.methods.should include(:select_options_for)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "adds the validates_inclusion_of validator to the model" do
|
24
|
+
MyModel.should have(1).validators_on :number
|
25
|
+
|
26
|
+
validator = MyModel.validators.first
|
27
|
+
validator.should be_a_kind_of ActiveModel::Validations::InclusionValidator
|
28
|
+
|
29
|
+
validator.options[:in].should == %w[1 2]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "can be called more than once without reinitializing the enumitation values" do
|
33
|
+
# Validate that the original enumitation still exists
|
34
|
+
MyModel.enumitation_values.keys.should include(:number)
|
35
|
+
|
36
|
+
MyModel.enumitation :other_field, %w[one two]
|
37
|
+
|
38
|
+
# Should still include the :number enumitation
|
39
|
+
MyModel.enumitation_values.keys.should include(:number)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Enumitation::ClassMethods do
|
4
|
+
before(:all) do
|
5
|
+
class MyModel < ActiveRecord::Base
|
6
|
+
enumitation :number, %w[1 2]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:all) do
|
11
|
+
Object.send :remove_const, :MyModel
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "select_options_for" do
|
15
|
+
context "when no label translation is present" do
|
16
|
+
it "returns select options with label and value of equal values" do
|
17
|
+
MyModel.select_options_for(:number).should == [%w[1 1], %w[2 2]]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when label translation is present" do
|
22
|
+
before do
|
23
|
+
%w[1 2].each do |num|
|
24
|
+
mock(I18n).t(num,
|
25
|
+
:scope => "enumitation.models.my_model.number",
|
26
|
+
:default => num) do
|
27
|
+
|
28
|
+
if num == '1'
|
29
|
+
'one'
|
30
|
+
elsif num == '2'
|
31
|
+
'two'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns select options with the label translated" do
|
38
|
+
MyModel.select_options_for(:number).should == [%w[one 1], %w[two 2]]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'active_record'
|
5
|
+
require 'enumitation'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.mock_with :rr
|
9
|
+
end
|
10
|
+
|
11
|
+
# Stub out AR
|
12
|
+
ActiveRecord::Base.class_eval do
|
13
|
+
alias_method :save, :valid?
|
14
|
+
def self.columns() @columns ||= []; end
|
15
|
+
|
16
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
17
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: enumitation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chad Boyd
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-07-01 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -41,11 +41,33 @@ dependencies:
|
|
41
41
|
requirement: &id003 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - ~>
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: "3"
|
47
47
|
type: :development
|
48
48
|
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: ap
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: ruby-debug19
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id005
|
49
71
|
description: |
|
50
72
|
Ever wanted to restrict an attribute to specific values, but not wanted to
|
51
73
|
create a join table to hold the values, and didn't want (or couldn't) create
|
@@ -61,6 +83,7 @@ extra_rdoc_files: []
|
|
61
83
|
|
62
84
|
files:
|
63
85
|
- .gitignore
|
86
|
+
- .rspec
|
64
87
|
- Gemfile
|
65
88
|
- README.md
|
66
89
|
- Rakefile
|
@@ -68,6 +91,9 @@ files:
|
|
68
91
|
- lib/enumitation.rb
|
69
92
|
- lib/enumitation/class_methods.rb
|
70
93
|
- lib/enumitation/version.rb
|
94
|
+
- spec/enumitation_spec.rb
|
95
|
+
- spec/lib/class_methods_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
71
97
|
has_rdoc: true
|
72
98
|
homepage: http://github.com/hoverlover/enumitation
|
73
99
|
licenses: []
|
@@ -96,5 +122,7 @@ rubygems_version: 1.6.2
|
|
96
122
|
signing_key:
|
97
123
|
specification_version: 3
|
98
124
|
summary: Fake enums for ActiveModel
|
99
|
-
test_files:
|
100
|
-
|
125
|
+
test_files:
|
126
|
+
- spec/enumitation_spec.rb
|
127
|
+
- spec/lib/class_methods_spec.rb
|
128
|
+
- spec/spec_helper.rb
|