has_enum 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "~> 3.0.0"
4
+ gem "sqlite3-ruby", "~> 1.3.2"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.2.0"
10
+ gem "rails", "~> 3.0.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.1"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,93 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.1)
6
+ actionpack (= 3.0.1)
7
+ mail (~> 2.2.5)
8
+ actionpack (3.0.1)
9
+ activemodel (= 3.0.1)
10
+ activesupport (= 3.0.1)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4.1)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.12)
16
+ rack-test (~> 0.5.4)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.1)
19
+ activesupport (= 3.0.1)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4.1)
22
+ activerecord (3.0.1)
23
+ activemodel (= 3.0.1)
24
+ activesupport (= 3.0.1)
25
+ arel (~> 1.0.0)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.1)
28
+ activemodel (= 3.0.1)
29
+ activesupport (= 3.0.1)
30
+ activesupport (3.0.1)
31
+ arel (1.0.1)
32
+ activesupport (~> 3.0.0)
33
+ builder (2.1.2)
34
+ diff-lcs (1.1.2)
35
+ erubis (2.6.6)
36
+ abstract (>= 1.0.0)
37
+ git (1.2.5)
38
+ i18n (0.4.2)
39
+ jeweler (1.5.1)
40
+ bundler (~> 1.0.0)
41
+ git (>= 1.2.5)
42
+ rake
43
+ mail (2.2.9.1)
44
+ activesupport (>= 2.3.6)
45
+ i18n (>= 0.4.1)
46
+ mime-types (~> 1.16)
47
+ treetop (~> 1.4.8)
48
+ mime-types (1.16)
49
+ polyglot (0.3.1)
50
+ rack (1.2.1)
51
+ rack-mount (0.6.13)
52
+ rack (>= 1.0.0)
53
+ rack-test (0.5.6)
54
+ rack (>= 1.0)
55
+ rails (3.0.1)
56
+ actionmailer (= 3.0.1)
57
+ actionpack (= 3.0.1)
58
+ activerecord (= 3.0.1)
59
+ activeresource (= 3.0.1)
60
+ activesupport (= 3.0.1)
61
+ bundler (~> 1.0.0)
62
+ railties (= 3.0.1)
63
+ railties (3.0.1)
64
+ actionpack (= 3.0.1)
65
+ activesupport (= 3.0.1)
66
+ rake (>= 0.8.4)
67
+ thor (~> 0.14.0)
68
+ rake (0.8.7)
69
+ rcov (0.9.9)
70
+ rspec (2.2.0)
71
+ rspec-core (~> 2.2)
72
+ rspec-expectations (~> 2.2)
73
+ rspec-mocks (~> 2.2)
74
+ rspec-core (2.2.1)
75
+ rspec-expectations (2.2.0)
76
+ diff-lcs (~> 1.1.2)
77
+ rspec-mocks (2.2.0)
78
+ sqlite3-ruby (1.3.2)
79
+ thor (0.14.4)
80
+ treetop (1.4.9)
81
+ polyglot (>= 0.3.1)
82
+ tzinfo (0.3.23)
83
+
84
+ PLATFORMS
85
+ ruby
86
+
87
+ DEPENDENCIES
88
+ bundler (~> 1.0.0)
89
+ jeweler (~> 1.5.1)
90
+ rails (~> 3.0.0)
91
+ rcov
92
+ rspec (~> 2.2.0)
93
+ sqlite3-ruby (~> 1.3.2)
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Andreas Korth, Konstantin Shabanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # has_enum
2
+
3
+ Gem for Rails 3 app to easily handle enumeration attributes in models.
4
+
5
+ ## Installation and configuration
6
+
7
+ This gem has been designed for Rails 3 only.
8
+
9
+ ### Install gem
10
+
11
+ Insert
12
+ gem 'has_enum' :git => 'git://github.com/openteam/has_enum.git'
13
+ in your Rails 3 Gemfile. Then do
14
+ $ bundle install
15
+
16
+ ## Usage
17
+ ### Models
18
+ Here's example model:
19
+ class TestModel < ActiveRecord::Base
20
+ has_enum :category, %w( stuff things misc )
21
+ has_enum :color , %w( red green blue )
22
+ has_enum :size , %w( small medium large ) , :query_methods => false
23
+ has_enum :status , [:pending, :failed, :done]
24
+ end
25
+ See [sample usage in specs](https://github.com/openteam/has_enum/blob/master/spec/has_enum_spec.rb).
26
+
27
+ ### Views
28
+ radio_button_enum(object_name, method, options = {})
29
+ select_enum(object_name, method, options = {})
30
+ #### Usage with Formtastic (Monkeypatch)
31
+ If you are using another gems that inherited from formtastic the most simple method will be to add this code
32
+ in your *config/initializers/formtastic.rb*:
33
+ class Formtastic::SemanticFormBuilder
34
+ def enum_input(method, options = {})
35
+ value = @object.send(method)
36
+ additional_params = {:as => :select, :collection => @object.class.values_for_select_tag(method)}
37
+ self.input(method, options.merge(additional_params)).gsub(/class="select/, 'class="enum')
38
+ end
39
+ end
40
+ Or if you sure you don't use any the proper method is to inherit from it:
41
+ class CustomBuilder < Formtastic::SemanticFormBuilder
42
+ def enum_input(method, options = {})
43
+ value = @object.send(method)
44
+ additional_params = {:as => :select, :collection => @object.class.values_for_select_tag(method)}
45
+ self.input(method, options.merge(additional_params)).gsub(/class="select/, 'class="enum')
46
+ end
47
+ end
48
+ And uncomment
49
+ Formtastic::SemanticFormHelper.builder = CustomBuilder
50
+
51
+ ## Running Tests
52
+
53
+ Run the tests
54
+ rake spec
55
+ View specs are still failing. I'm working on it.
56
+
57
+ ## Contributing to has_enum
58
+
59
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
60
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
61
+ * Fork the project
62
+ * Start a feature/bugfix branch
63
+ * Commit and push until you are happy with your contribution
64
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
65
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
66
+
67
+ ## Copyright
68
+
69
+ Copyright (c) 2010 Andreas Korth, Konstantin Shabanov. See LICENSE.txt for
70
+ further details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "has_enum"
16
+ gem.homepage = "https://github.com/etehtsea/has_enum"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Gem for Rails to easily handle enumeration attributes in models}
19
+ gem.email = "kes.eclipse@gmail.com"
20
+ gem.authors = ["Andreas Korth", "Konstantin Shabanov"]
21
+ gem.add_runtime_dependency 'rails', '> 3.0.0'
22
+ gem.add_development_dependency 'rspec', '> 2.2.0'
23
+ gem.add_development_dependency 'rails', '> 3.0.0'
24
+ gem.add_development_dependency 'sqlite3-ruby', '> 1.3.2'
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "has_enum #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,91 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{has_enum}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andreas Korth", "Konstantin Shabanov"]
12
+ s.date = %q{2010-12-08}
13
+ s.email = %q{kes.eclipse@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ "Gemfile",
20
+ "Gemfile.lock",
21
+ "LICENSE.txt",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "has_enum.gemspec",
26
+ "lib/has_enum.rb",
27
+ "lib/has_enum/active_record.rb",
28
+ "lib/has_enum/helpers.rb",
29
+ "spec/has_enum_spec.rb",
30
+ "spec/helpers/form_helper_spec.rb",
31
+ "spec/rcov.opts",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb",
34
+ "spec/test_model.rb"
35
+ ]
36
+ s.homepage = %q{https://github.com/etehtsea/has_enum}
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Gem for Rails to easily handle enumeration attributes in models}
41
+ s.test_files = [
42
+ "spec/has_enum_spec.rb",
43
+ "spec/helpers/form_helper_spec.rb",
44
+ "spec/spec_helper.rb",
45
+ "spec/test_model.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<rails>, ["~> 3.0.0"])
54
+ s.add_runtime_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
55
+ s.add_development_dependency(%q<rspec>, ["~> 2.2.0"])
56
+ s.add_development_dependency(%q<rails>, ["~> 3.0.0"])
57
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
59
+ s.add_development_dependency(%q<rcov>, [">= 0"])
60
+ s.add_runtime_dependency(%q<rails>, ["> 3.0.0"])
61
+ s.add_development_dependency(%q<rspec>, ["> 2.2.0"])
62
+ s.add_development_dependency(%q<rails>, ["> 3.0.0"])
63
+ s.add_development_dependency(%q<sqlite3-ruby>, ["> 1.3.2"])
64
+ else
65
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
66
+ s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
67
+ s.add_dependency(%q<rspec>, ["~> 2.2.0"])
68
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
69
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
70
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
71
+ s.add_dependency(%q<rcov>, [">= 0"])
72
+ s.add_dependency(%q<rails>, ["> 3.0.0"])
73
+ s.add_dependency(%q<rspec>, ["> 2.2.0"])
74
+ s.add_dependency(%q<rails>, ["> 3.0.0"])
75
+ s.add_dependency(%q<sqlite3-ruby>, ["> 1.3.2"])
76
+ end
77
+ else
78
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
79
+ s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
80
+ s.add_dependency(%q<rspec>, ["~> 2.2.0"])
81
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
82
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
83
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
84
+ s.add_dependency(%q<rcov>, [">= 0"])
85
+ s.add_dependency(%q<rails>, ["> 3.0.0"])
86
+ s.add_dependency(%q<rspec>, ["> 2.2.0"])
87
+ s.add_dependency(%q<rails>, ["> 3.0.0"])
88
+ s.add_dependency(%q<sqlite3-ruby>, ["> 1.3.2"])
89
+ end
90
+ end
91
+
@@ -0,0 +1,4 @@
1
+ require 'has_enum/active_record'
2
+ require 'has_enum/helpers'
3
+
4
+ ActiveRecord::Base.send(:include, HasEnum::ActiveRecord)
@@ -0,0 +1,78 @@
1
+ module HasEnum
2
+ module ActiveRecord
3
+ def self.included(base)
4
+ base.write_inheritable_hash(:enum, {})
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def enum(attribute = nil)
10
+ @enum ||= read_inheritable_attribute(:enum)
11
+ attribute ? @enum[attribute.to_sym] : @enum
12
+ end
13
+
14
+
15
+ def has_enum(attributes, values, options = {})
16
+ [*attributes].each do |attribute|
17
+ attribute = attribute.to_sym
18
+ if values[0].is_a?(Symbol)
19
+ symbols = true
20
+ end
21
+ if symbols
22
+ values = values.map{|value| value.to_s.insert(0, ':')}
23
+ else
24
+ values = values.map(&:to_s)
25
+ end
26
+ enum[attribute] = values.freeze
27
+
28
+ if options[:query_methods] != false
29
+ enum[attribute].each do |val|
30
+ val = val[1..-1] if symbols
31
+ define_method(:"#{val}?") do
32
+ self.send(attribute).to_s == val
33
+ end
34
+ end
35
+ end
36
+
37
+ define_method(:"#{attribute}=") do |value|
38
+ value = value.to_s.insert(0, ':') if symbols
39
+
40
+ if values.find{ |val| val == value }
41
+ write_attribute(attribute, value.blank? ? nil : value.to_s)
42
+ else
43
+ errors.add(:"#{attribute}", "#{value} is not in enum")
44
+ end
45
+ end
46
+
47
+ define_method(:"#{attribute}") do
48
+ if symbols
49
+ read_attribute(attribute.to_sym).tap{|str| str.slice!(0)}.to_sym
50
+ else
51
+ read_attribute(attribute.to_sym)
52
+ end
53
+ end
54
+
55
+ define_method "human_#{attribute}" do
56
+ begin
57
+ key = "activerecord.attributes.#{self.class.name.underscore}.#{attribute}_enum.#{self.send(attribute)}"
58
+ translation = I18n.translate(key, :raise => true)
59
+ rescue I18n::MissingTranslationData
60
+ self.send(attribute).humanize
61
+ end
62
+ end
63
+
64
+ end
65
+ end
66
+
67
+ def values_for_select_tag(enum)
68
+ values = enum(enum)
69
+ begin
70
+ translation = I18n.translate("activerecord.attributes.#{self.name.underscore}.#{enum}_enum", :raise => true)
71
+ values.map { |value| [translation[value.to_sym], value] }
72
+ rescue I18n::MissingTranslationData
73
+ values.map { |value| [value.humanize, value] }
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,49 @@
1
+ module ActionView::Helpers::FormHelper
2
+
3
+ def radio_button_enum(object_name, method, options = {})
4
+ ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_radio_button_enum_tag(options)
5
+ end
6
+
7
+ def select_enum(object_name, method, options = {})
8
+ ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_select_enum_tag(options)
9
+ end
10
+
11
+ end
12
+
13
+
14
+ class ActionView::Helpers::InstanceTag
15
+
16
+ def to_radio_button_enum_tag(options = {})
17
+ values_for_enum_tag.map do |val|
18
+ radio_button = to_radio_button_tag(val.last, options)
19
+ [ radio_button, to_label_tag(val.first, :for => radio_button.match(/ id="(.*?)"/)[1]) ] * $/
20
+ end.join($/)
21
+ end
22
+
23
+ def to_select_enum_tag(options = {})
24
+ html_options = options.delete(:html) || {}
25
+ to_select_tag(values_for_enum_tag, options, html_options)
26
+ end
27
+
28
+ def values_for_enum_tag
29
+ values = object.class.enum(method_name.to_sym)
30
+ begin
31
+ translation = I18n.translate("activerecord.attributes.#{object.class.name.i18nize}.#{method_name}_enum", :raise => true)
32
+ values.map { |val| [translation[val.to_sym], val] }
33
+ rescue I18n::MissingTranslationData
34
+ values.map { |val| [val.humanize, val] }
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+ class ActionView::Helpers::FormBuilder
41
+
42
+ def radio_button_enum(method, options = {})
43
+ @template.radio_button_enum(@object_name, method, objectify_options(options))
44
+ end
45
+
46
+ def select_enum(method, options = {})
47
+ @template.select_enum(@object_name, method, objectify_options(options.merge(:include_blank => true)))
48
+ end
49
+ end
@@ -0,0 +1,84 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe HasEnum::ActiveRecord do
4
+ before :each do
5
+ @model = TestModel.new(:category => 'misc', :color => 'blue', :foo => 'bar', :status => :pending)
6
+ end
7
+
8
+ it "should return the values for a given enum attribute" do
9
+ TestModel.enum[:category].should eql(%w(stuff things misc))
10
+ end
11
+
12
+ describe "category enum" do
13
+ it "should accept enum values for the attribute" do
14
+ %w(stuff things misc).each do |value|
15
+ @model.category = value
16
+ @model.should be_valid
17
+ end
18
+ end
19
+
20
+ it "should reject non enum values for the attribute" do
21
+ @model.category = 'objects'
22
+ @model.errors[:category].size.should eql(1)
23
+ end
24
+ end
25
+
26
+ describe "color enum" do
27
+ it "should accept any value for the attribute" do
28
+ %w(red orange yellow).each do |value|
29
+ @model.color = value
30
+ @model.should be_valid
31
+ end
32
+ end
33
+
34
+ it "should reject an empty value for the attribute" do
35
+ @model.color = ''
36
+ @model.errors[:color].size.should eql(1)
37
+ end
38
+
39
+ it "should define a query method for each enum value" do
40
+ @model.color = 'green'
41
+ @model.category = 'stuff'
42
+ @model.should be_stuff
43
+ @model.should be_green
44
+ @model.should_not be_red
45
+ @model.should_not be_blue
46
+ end
47
+ end
48
+
49
+ describe "size enum" do
50
+ it "should accept any value for the attribute" do
51
+ ['orange', 'medium', 'misc', '', nil].each do |value|
52
+ @model.size = value
53
+ @model.should be_valid
54
+ end
55
+ end
56
+
57
+ it "should not define query methods for enum values" do
58
+ %w( small? medium? large? ).each do |method|
59
+ @model.should_not respond_to(method)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "status enum" do
65
+ it "should accept symbols as symbols" do
66
+ @model.status = :pending
67
+ @model.status.should eql(:pending)
68
+ end
69
+
70
+ it "should define query methods for enum values" do
71
+ @model.status = :pending
72
+ @model.should be_pending
73
+ end
74
+ end
75
+
76
+ describe "foo enum" do
77
+ it "should accept attribute values starting with 'bar'" do
78
+ ['bar', 'barcode', 'bargain'].each do |value|
79
+ @model.foo = value
80
+ @model.should be_valid
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe ActionView::Helpers::FormHelper do
4
+ before :each do
5
+ @model = TestModel.new
6
+ I18n.reload!
7
+ end
8
+
9
+ it "should produce a select_enum" do
10
+ html = [
11
+ %(<select id="model_category" name="model[category]"><option value="stuff">stuff</option>),
12
+ %(<option value="things">things</option>),
13
+ %(<option value="misc">misc</option></select>)
14
+ ].join($/)
15
+ form_for @model do |f|
16
+ f.fields_for :model do |fields|
17
+ fields.select_enum(:category).should eql(html)
18
+ end
19
+ end
20
+ end
21
+
22
+ it "should produce a radio_button_enum" do
23
+ html = [
24
+ %(<input id="model_status_pending" name="model[status]" type="radio" value="pending" />),
25
+ %(<label for="model_status_pending">pending</label>),
26
+ %(<input id="model_status_failed" name="model[status]" type="radio" value="failed" />),
27
+ %(<label for="model_status_failed">failed</label>),
28
+ %(<input id="model_status_done" name="model[status]" type="radio" value="done" />),
29
+ %(<label for="model_status_done">done</label>)
30
+ ].join($/)
31
+
32
+ fields_for(:model) do |fields|
33
+ fields.radio_button_enum(:status).should eql(html)
34
+ end
35
+ end
36
+
37
+ describe "I18n" do
38
+ before :each do
39
+ I18n.locale = :de
40
+ I18n.backend.store_translations(:de, YAML::load(<<-YAML
41
+ activerecord:
42
+ attributes:
43
+ model:
44
+ category_enum:
45
+ stuff: 'Zeug'
46
+ things: 'Sachen'
47
+ misc: 'Diverses'
48
+ status_enum:
49
+ pending: 'In Arbeit'
50
+ failed: 'Fehlgeschlagen'
51
+ done: 'Erledigt'
52
+ YAML
53
+ ))
54
+ end
55
+
56
+ it "should produce translated options for a select_enum" do
57
+ html = fields_for(:model) do |fields|
58
+ fields.select_enum(:category)
59
+ end
60
+ html.should include(%(<option value="stuff">Zeug</option>))
61
+ html.should include(%(<option value="things">Sachen</option>))
62
+ html.should include(%(<option value="misc">Diverses</option>))
63
+ end
64
+
65
+ it "should translated labels for produce a radio_button_enum" do
66
+ html = fields_for(:model) do |fields|
67
+ fields.radio_button_enum(:status)
68
+ end
69
+ html.should include(%(<label for="model_status_pending">In Arbeit</label>))
70
+ html.should include(%(<label for="model_status_failed">Fehlgeschlagen</label>))
71
+ html.should include(%(<label for="model_status_done">Erledigt</label>))
72
+ end
73
+ end
74
+ end
75
+
@@ -0,0 +1,3 @@
1
+ --exclude "spec/*,gems/*"
2
+ --rails
3
+ --sort coverage
@@ -0,0 +1,5 @@
1
+ --colour
2
+ --format progress
3
+ --format failing_examples:tmp/failed_examples.txt
4
+ --loadby mtime
5
+ --reverse
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'rails/all'
5
+ require 'has_enum'
6
+
7
+ ActiveRecord::Base.establish_connection(
8
+ :adapter => 'sqlite3',
9
+ :database => 'spec/rspec.db'
10
+ )
11
+
12
+ ActiveRecord::Schema.define(:version => 0) do
13
+ create_table "test_models", :force => true do |t|
14
+ t.string "category"
15
+ t.string "color"
16
+ t.string "size"
17
+ t.string "status"
18
+ t.string "foo"
19
+ end
20
+
21
+ create_table "schema_info", :id => false, :force => true do |t|
22
+ t.integer "version"
23
+ end
24
+ end
25
+
26
+ require File.dirname(__FILE__) + "/test_model" unless defined?(Model)
@@ -0,0 +1,6 @@
1
+ class TestModel < ActiveRecord::Base
2
+ has_enum :category, %w( stuff things misc )
3
+ has_enum :color , %w( red green blue )
4
+ has_enum :size , %w( small medium large ) , :query_methods => false
5
+ has_enum :status , [:pending, :failed, :done]
6
+ end
metadata ADDED
@@ -0,0 +1,247 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_enum
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
+ platform: ruby
11
+ authors:
12
+ - Andreas Korth
13
+ - Konstantin Shabanov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-08 00:00:00 +06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3-ruby
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 3
46
+ - 2
47
+ version: 1.3.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 2
60
+ - 2
61
+ - 0
62
+ version: 2.2.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: rails
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 3
75
+ - 0
76
+ - 0
77
+ version: 3.0.0
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: bundler
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 1
90
+ - 0
91
+ - 0
92
+ version: 1.0.0
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: *id005
96
+ - !ruby/object:Gem::Dependency
97
+ name: jeweler
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 1
105
+ - 5
106
+ - 1
107
+ version: 1.5.1
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: *id006
111
+ - !ruby/object:Gem::Dependency
112
+ name: rcov
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *id007
124
+ - !ruby/object:Gem::Dependency
125
+ name: rails
126
+ requirement: &id008 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">"
130
+ - !ruby/object:Gem::Version
131
+ segments:
132
+ - 3
133
+ - 0
134
+ - 0
135
+ version: 3.0.0
136
+ type: :runtime
137
+ prerelease: false
138
+ version_requirements: *id008
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: &id009 !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">"
145
+ - !ruby/object:Gem::Version
146
+ segments:
147
+ - 2
148
+ - 2
149
+ - 0
150
+ version: 2.2.0
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: *id009
154
+ - !ruby/object:Gem::Dependency
155
+ name: rails
156
+ requirement: &id010 !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ">"
160
+ - !ruby/object:Gem::Version
161
+ segments:
162
+ - 3
163
+ - 0
164
+ - 0
165
+ version: 3.0.0
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: *id010
169
+ - !ruby/object:Gem::Dependency
170
+ name: sqlite3-ruby
171
+ requirement: &id011 !ruby/object:Gem::Requirement
172
+ none: false
173
+ requirements:
174
+ - - ">"
175
+ - !ruby/object:Gem::Version
176
+ segments:
177
+ - 1
178
+ - 3
179
+ - 2
180
+ version: 1.3.2
181
+ type: :development
182
+ prerelease: false
183
+ version_requirements: *id011
184
+ description:
185
+ email: kes.eclipse@gmail.com
186
+ executables: []
187
+
188
+ extensions: []
189
+
190
+ extra_rdoc_files:
191
+ - LICENSE.txt
192
+ - README.md
193
+ files:
194
+ - Gemfile
195
+ - Gemfile.lock
196
+ - LICENSE.txt
197
+ - README.md
198
+ - Rakefile
199
+ - VERSION
200
+ - has_enum.gemspec
201
+ - lib/has_enum.rb
202
+ - lib/has_enum/active_record.rb
203
+ - lib/has_enum/helpers.rb
204
+ - spec/has_enum_spec.rb
205
+ - spec/helpers/form_helper_spec.rb
206
+ - spec/rcov.opts
207
+ - spec/spec.opts
208
+ - spec/spec_helper.rb
209
+ - spec/test_model.rb
210
+ has_rdoc: true
211
+ homepage: https://github.com/etehtsea/has_enum
212
+ licenses:
213
+ - MIT
214
+ post_install_message:
215
+ rdoc_options: []
216
+
217
+ require_paths:
218
+ - lib
219
+ required_ruby_version: !ruby/object:Gem::Requirement
220
+ none: false
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ hash: -2399266451107921661
225
+ segments:
226
+ - 0
227
+ version: "0"
228
+ required_rubygems_version: !ruby/object:Gem::Requirement
229
+ none: false
230
+ requirements:
231
+ - - ">="
232
+ - !ruby/object:Gem::Version
233
+ segments:
234
+ - 0
235
+ version: "0"
236
+ requirements: []
237
+
238
+ rubyforge_project:
239
+ rubygems_version: 1.3.7
240
+ signing_key:
241
+ specification_version: 3
242
+ summary: Gem for Rails to easily handle enumeration attributes in models
243
+ test_files:
244
+ - spec/has_enum_spec.rb
245
+ - spec/helpers/form_helper_spec.rb
246
+ - spec/spec_helper.rb
247
+ - spec/test_model.rb