enumitation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in enumitation.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Enumitation
2
+
3
+ <img src="http://upload.wikimedia.org/wikipedia/commons/c/c7/Kanikama.jpg" width="400" height="300"/>
4
+
5
+ ## What is it?
6
+
7
+ **enum + imitation = Enumitation**
8
+
9
+ ## Why?
10
+
11
+ Ever wanted to restrict an ActiveRecord attribute to specific values, but thought a
12
+ join table to contain the values and a foreign key constraint were overkill? Well, you
13
+ could opt for an enum field, but maybe your DBMS doesn't support enums. Or maybe it does,
14
+ but you just aren't in the mood for an enum field today.
15
+
16
+ Also, do you need to provide these values for options in a select field
17
+ in a form?
18
+
19
+ Enumitation exists to allow restrictions on ActiveRecord attribute values without
20
+ the need for a join table or enum field, while also providing those values for
21
+ the purpose of options in a select field in a form.
22
+
23
+ ## Example
24
+
25
+ class Publisher < ActiveRecord::Base
26
+ enumitation :location, %w[US CA]
27
+ end
28
+
29
+ Here we've specified that we want the `location` attribute to be an enum
30
+ with the values *US* and *CA*. The `enumitation` method does two
31
+ important things:
32
+
33
+ * it adds a `validates_inclusion_of` validation to the model, using the
34
+ values specified when declaring the enumitation
35
+ * it provides a `select_options_for` method that can be used to provide
36
+ a select tag with options
37
+
38
+ In the console:
39
+
40
+ publisher = Publisher.new location: 'MX'
41
+ publisher.valid? #=> false
42
+ publisher.errors #=> {:location=>["is not included in the list"]}
43
+ publisher.location = 'US' #=> "US"
44
+ publisher.valid? #=> true
45
+
46
+ Publisher.select_options_for :location #=> [["US", "US"], ["CA", "CA"]]
47
+
48
+ ## Custom Labels (i18n)
49
+
50
+ You can provide alternate labels for your values in your locale files.
51
+ These labels will be returned from the `select_options_for` method.
52
+
53
+ In *config/locales/en.yml*:
54
+
55
+ en:
56
+ enumitation:
57
+ models:
58
+ publisher:
59
+ location:
60
+ US: 'United States'
61
+ CA: 'Canada'
62
+
63
+ Now when calling the `select_options_for` method:
64
+
65
+ Publisher.select_options_for :location #=> [["United States", "US"], ["Canada", "CA"]]
66
+
67
+ So, from your view:
68
+
69
+ = form_for :publisher do |f|
70
+ = f.select :location, Publisher.select_options_for(:location)
71
+
72
+ ### But this is no big deal!
73
+
74
+ I know. It really isn't that big of a deal, and it wouldn't take that
75
+ much effort to reproduce by hand what Enumitation provides. But why do
76
+ all that work when you can get it from a one-liner?
77
+
78
+
79
+ ### There are other gems that practically do the same thing.
80
+
81
+ True, but the ones I found either didn't fit my needs or they did so much more than I needed.
82
+ I wanted something that fit my needs exactly and nothing more.
83
+
84
+ ## Contributing
85
+
86
+ If you think Enumitation could be better, fork away and send a pull
87
+ request!
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "enumitation/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "enumitation"
7
+ s.version = Enumitation::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Chad Boyd"]
10
+ s.email = ["hoverlover@gmail.com"]
11
+ s.homepage = "http://github.com/hoverlover/enumitation"
12
+ s.summary = %q{Fake enums for ActiveModel}
13
+ s.description = <<DESC
14
+ Ever wanted to restrict an attribute to specific values, but not wanted to
15
+ create a join table to hold the values, and didn't want (or couldn't) create
16
+ an enum field? Now you can, with enumitation!
17
+ DESC
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.add_development_dependency('rspec', '> 2')
22
+ s.add_development_dependency('rr', '>= 1.0.2')
23
+ s.add_development_dependency('activerecord', '> 3')
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,39 @@
1
+ module Enumitation
2
+ module ClassMethods
3
+
4
+ def self.extended(base)
5
+ class << base
6
+ attr_accessor :enumitation_values
7
+ end
8
+
9
+ base.enumitation_values = {}
10
+ end
11
+
12
+ def enumitation(attribute, values)
13
+ enumitation_values[attribute] = Array(values)
14
+ add_inclusion_validation(attribute, values)
15
+ end
16
+
17
+ def select_options_for(attribute)
18
+ return [] if enumitation_values.empty?
19
+
20
+ enumitation_values[attribute].map do |val|
21
+ [display_value(attribute, val), val]
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def display_value(attribute, val)
28
+ # Try looking up using i18n. If nothing found, just return the val.
29
+
30
+ I18n.t(val,
31
+ :scope => "enumitation.models.#{self.name.underscore}.#{attribute.to_s.underscore}",
32
+ :default => val)
33
+ end
34
+
35
+ def add_inclusion_validation(attribute, values)
36
+ self.validates_inclusion_of attribute, :in => values
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Enumitation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ module Enumitation
2
+ autoload :ClassMethods, 'enumitation/class_methods'
3
+
4
+ ActiveRecord::Base.class_eval do
5
+ def self.inherited(subclass)
6
+ subclass.extend ClassMethods
7
+ super
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enumitation
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Chad Boyd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-05 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: "2"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rr
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 1.0.2
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: activerecord
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">"
45
+ - !ruby/object:Gem::Version
46
+ version: "3"
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: |
50
+ Ever wanted to restrict an attribute to specific values, but not wanted to
51
+ create a join table to hold the values, and didn't want (or couldn't) create
52
+ an enum field? Now you can, with enumitation!
53
+
54
+ email:
55
+ - hoverlover@gmail.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - enumitation.gemspec
68
+ - lib/enumitation.rb
69
+ - lib/enumitation/class_methods.rb
70
+ - lib/enumitation/version.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/hoverlover/enumitation
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.6.2
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Fake enums for ActiveModel
99
+ test_files: []
100
+