active_enum 0.6.6 → 0.7.0
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/README.rdoc +25 -10
- data/Rakefile +5 -9
- data/lib/active_enum/acts_as_enum.rb +4 -5
- data/lib/active_enum/extensions.rb +11 -23
- data/lib/active_enum/form_helpers/formtastic.rb +14 -0
- data/lib/active_enum/form_helpers/simple_form.rb +18 -0
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/extensions_spec.rb +6 -6
- data/spec/active_enum/form_helpers/formtastic_spec.rb +37 -0
- data/spec/active_enum/form_helpers/simple_form_spec.rb +38 -0
- data/spec/spec_helper.rb +3 -2
- metadata +9 -7
- data/lib/active_enum/formtastic.rb +0 -12
- data/spec/active_enum/formtastic_spec.rb +0 -59
data/README.rdoc
CHANGED
@@ -7,13 +7,13 @@ Enum values are stored in memory at the moment but I plan to add database and ya
|
|
7
7
|
|
8
8
|
== Install
|
9
9
|
|
10
|
-
|
10
|
+
From version 0.7 onwards this plugin will only have Rails 3 support. If you need Rails 2 compatibility, check the 0.6 branch.
|
11
11
|
|
12
|
-
gem install active_enum
|
12
|
+
gem install active_enum
|
13
13
|
|
14
|
-
|
14
|
+
Put this in your Gemfile
|
15
15
|
|
16
|
-
|
16
|
+
gem 'active_enum'
|
17
17
|
|
18
18
|
|
19
19
|
== Example
|
@@ -124,7 +124,7 @@ You can check if the attribute value matches a particular enum value by passing
|
|
124
124
|
|
125
125
|
A convenience method on the class is available to the enum class of any enumerated attribute
|
126
126
|
|
127
|
-
User.
|
127
|
+
User.enum_for(:sex) # => Sex
|
128
128
|
|
129
129
|
=== Bulk definition
|
130
130
|
|
@@ -162,21 +162,36 @@ Giving you the familiar enum methods
|
|
162
162
|
User['Dave']
|
163
163
|
User.to_select
|
164
164
|
|
165
|
-
=== Formtastic
|
166
165
|
|
167
|
-
|
166
|
+
=== Form Helpers
|
167
|
+
|
168
|
+
There is support for Formtastic[http://github.com/justinfrench/formtastic] and SimpleForm[http://github.com/plataformatec/simple_form]
|
169
|
+
to make it easier to use the enum values as select options. The plugins have an almost identical API which is handy.
|
170
|
+
Instructions for each are below.
|
171
|
+
|
172
|
+
==== Formtastic
|
168
173
|
|
169
174
|
In an initialiser:
|
170
175
|
|
171
|
-
require 'active_enum/formtastic'
|
176
|
+
require 'active_enum/form_helpers/formtastic'
|
172
177
|
|
173
|
-
Then
|
178
|
+
Then declare the input for an enumerated attribute as the :enum type.
|
174
179
|
|
175
180
|
<% semantic_form_for(@person) do |f| %>
|
176
181
|
<%= f.input :sex, :as => :enum %>
|
177
182
|
<% end %>
|
178
183
|
|
179
|
-
|
184
|
+
==== SimpleForm
|
185
|
+
|
186
|
+
In an initialiser:
|
187
|
+
|
188
|
+
require 'active_enum/form_helpers/simple_form'
|
189
|
+
|
190
|
+
Then declare the input for an enumerated attribute as the :enum type.
|
191
|
+
|
192
|
+
<% simple_form_for(@person) do |f| %>
|
193
|
+
<%= f.input :sex, :as => :enum %>
|
194
|
+
<% end %>
|
180
195
|
|
181
196
|
|
182
197
|
== TODO
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake/rdoctask'
|
3
3
|
require 'rake/gempackagetask'
|
4
4
|
require 'rubygems/specification'
|
5
|
-
require '
|
5
|
+
require 'rspec/core/rake_task'
|
6
6
|
require 'lib/active_enum/version'
|
7
7
|
|
8
8
|
GEM_NAME = "active_enum"
|
@@ -29,19 +29,15 @@ end
|
|
29
29
|
desc 'Default: run specs.'
|
30
30
|
task :default => :spec
|
31
31
|
|
32
|
-
spec_files = Rake::FileList["spec/**/*_spec.rb"]
|
33
|
-
|
34
32
|
desc "Run specs"
|
35
|
-
|
36
|
-
t.
|
37
|
-
t.spec_opts = ["-c"]
|
33
|
+
RSpec::Core::RakeTask.new do |t|
|
34
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
38
35
|
end
|
39
36
|
|
40
37
|
desc "Generate code coverage"
|
41
|
-
|
42
|
-
t.spec_files = spec_files
|
38
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
43
39
|
t.rcov = true
|
44
|
-
t.rcov_opts = ['--exclude', 'spec
|
40
|
+
t.rcov_opts = ['--exclude', 'spec']
|
45
41
|
end
|
46
42
|
|
47
43
|
desc 'Generate documentation for plugin.'
|
@@ -12,10 +12,9 @@ module ActiveEnum
|
|
12
12
|
extend ClassMethods
|
13
13
|
class_inheritable_accessor :active_enum_options
|
14
14
|
self.active_enum_options = options.reverse_merge(:name_column => 'name')
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
:order => "#{primary_key} #{active_enum_options[:order]}"
|
15
|
+
scope :enum_values, select("#{primary_key}, #{active_enum_options[:name_column]}").
|
16
|
+
where(active_enum_options[:conditions]).
|
17
|
+
order("#{primary_key} #{active_enum_options[:order]}")
|
19
18
|
end
|
20
19
|
|
21
20
|
end
|
@@ -51,7 +50,7 @@ module ActiveEnum
|
|
51
50
|
end
|
52
51
|
|
53
52
|
def lookup_by_name(index)
|
54
|
-
enum_values.
|
53
|
+
enum_values.where("#{active_enum_options[:name_column]} like ?", index.to_s).first
|
55
54
|
end
|
56
55
|
|
57
56
|
end
|
@@ -50,7 +50,7 @@ module ActiveEnum
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
53
|
+
def enum_for(attribute)
|
54
54
|
self.enumerated_attributes[attribute.to_sym]
|
55
55
|
end
|
56
56
|
|
@@ -62,13 +62,11 @@ module ActiveEnum
|
|
62
62
|
# user.sex(:enum)
|
63
63
|
#
|
64
64
|
def define_active_enum_read_method(attribute)
|
65
|
-
|
66
|
-
old_method = "#{attribute}_without_enum"
|
67
|
-
define_method("#{attribute}_with_enum") do |*arg|
|
65
|
+
define_method("#{attribute}") do |*arg|
|
68
66
|
arg = arg.first
|
69
|
-
value =
|
67
|
+
value = super()
|
70
68
|
|
71
|
-
enum = self.class.
|
69
|
+
enum = self.class.enum_for(attribute)
|
72
70
|
case arg
|
73
71
|
when :id
|
74
72
|
value if enum[value]
|
@@ -80,8 +78,6 @@ module ActiveEnum
|
|
80
78
|
ActiveEnum.use_name_as_value ? enum[value] : value
|
81
79
|
end
|
82
80
|
end
|
83
|
-
|
84
|
-
alias_method_chain attribute, :enum
|
85
81
|
end
|
86
82
|
|
87
83
|
# Define write method to also handle enum value
|
@@ -90,19 +86,15 @@ module ActiveEnum
|
|
90
86
|
# user.sex = :male
|
91
87
|
#
|
92
88
|
def define_active_enum_write_method(attribute)
|
93
|
-
|
94
|
-
|
95
|
-
define_method("#{attribute}_with_enum=") do |arg|
|
96
|
-
enum = self.class.active_enum_for(attribute)
|
89
|
+
define_method("#{attribute}=") do |arg|
|
90
|
+
enum = self.class.enum_for(attribute)
|
97
91
|
if arg.is_a?(Symbol)
|
98
92
|
value = enum[arg]
|
99
|
-
|
93
|
+
super(value)
|
100
94
|
else
|
101
|
-
|
95
|
+
super(arg)
|
102
96
|
end
|
103
97
|
end
|
104
|
-
|
105
|
-
alias_method_chain :"#{attribute}=", :enum
|
106
98
|
end
|
107
99
|
|
108
100
|
# Define question method to check enum value against attribute value
|
@@ -110,18 +102,14 @@ module ActiveEnum
|
|
110
102
|
# user.sex?(:male)
|
111
103
|
#
|
112
104
|
def define_active_enum_question_method(attribute)
|
113
|
-
|
114
|
-
|
115
|
-
old_method = "#{attribute}_without_enum?"
|
116
|
-
define_method("#{attribute}_with_enum?") do |*arg|
|
105
|
+
define_method("#{attribute}?") do |*arg|
|
117
106
|
arg = arg.first
|
118
107
|
if arg
|
119
|
-
send(attribute) == self.class.
|
108
|
+
send(attribute) == self.class.enum_for(attribute)[arg]
|
120
109
|
else
|
121
|
-
|
110
|
+
super
|
122
111
|
end
|
123
112
|
end
|
124
|
-
alias_method_chain :"#{attribute}?", :enum
|
125
113
|
end
|
126
114
|
|
127
115
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ActiveEnum
|
2
|
+
module FormHelpers
|
3
|
+
module Formtastic
|
4
|
+
|
5
|
+
def enum_input(method, options)
|
6
|
+
raise "Attribute '#{method}' has no enum class" unless enum = @object.class.enum_for(method)
|
7
|
+
select_input(method, options.merge(:collection => enum.to_select))
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Formtastic::SemanticFormBuilder.send :include, ActiveEnum::FormHelpers::Formtastic
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveEnum
|
2
|
+
module FormHelpers
|
3
|
+
class SimpleForm < SimpleForm::Inputs::CollectionInput
|
4
|
+
|
5
|
+
def initialize(builder)
|
6
|
+
super
|
7
|
+
raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.enum_for(attribute_name)
|
8
|
+
builder.options[:collection] = enum.to_select
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
SimpleForm::FormBuilder.class_eval do
|
16
|
+
map_type :enum, :to => ActiveEnum::FormHelpers::SimpleForm
|
17
|
+
alias_method :collection_enum, :collection_select
|
18
|
+
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -17,23 +17,23 @@ describe ActiveEnum::Extensions do
|
|
17
17
|
ActiveRecord::Base.should respond_to(:enumerate)
|
18
18
|
end
|
19
19
|
|
20
|
-
it 'should add class :
|
21
|
-
ActiveRecord::Base.should respond_to(:
|
20
|
+
it 'should add class :enum_for method to ActiveRecord' do
|
21
|
+
ActiveRecord::Base.should respond_to(:enum_for)
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'should allow multiple attributes to be enumerated with same enum' do
|
25
25
|
Person.class_eval do
|
26
26
|
enumerate :attending, :staying, :with => Accepted
|
27
27
|
end
|
28
|
-
Person.
|
29
|
-
Person.
|
28
|
+
Person.enum_for(:attending).should == Accepted
|
29
|
+
Person.enum_for(:staying).should == Accepted
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'should allow implicit enumeration class from attribute name' do
|
33
33
|
Person.class_eval do
|
34
34
|
enumerate :sex
|
35
35
|
end
|
36
|
-
Person.
|
36
|
+
Person.enum_for(:sex).should == Sex
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should create enum namespaced enum class from block' do
|
@@ -42,7 +42,7 @@ describe ActiveEnum::Extensions do
|
|
42
42
|
value :id => 1, :name => 'Male'
|
43
43
|
end
|
44
44
|
end
|
45
|
-
Person.
|
45
|
+
Person.enum_for(:sex).should == Person::Sex
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'should raise error if implicit enumeration class cannot be found' do
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'formtastic'
|
4
|
+
require 'active_enum/form_helpers/formtastic'
|
5
|
+
|
6
|
+
describe ActiveEnum::FormHelpers::Formtastic do
|
7
|
+
include RSpec::Rails::HelperExampleGroup
|
8
|
+
include Formtastic::SemanticFormHelper
|
9
|
+
|
10
|
+
before do
|
11
|
+
reset_class Person do
|
12
|
+
enumerate :sex do
|
13
|
+
value :id => 1, :name => 'Male'
|
14
|
+
value :id => 2, :name => 'Female'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should use enum class for select option values for enum input type" do
|
20
|
+
output = semantic_form_for(Person.new, :url => people_path) do |f|
|
21
|
+
concat f.input(:sex, :as => :enum)
|
22
|
+
end
|
23
|
+
output.should have_selector('select#person_sex')
|
24
|
+
output.should have_xpath('//option[@value=1]', :content => 'Male')
|
25
|
+
output.should have_xpath('//option[@value=2]', :content => 'Female')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise error if attribute for enum input is not enumerated" do
|
29
|
+
lambda {
|
30
|
+
semantic_form_for(Person.new, :url => people_path) {|f| f.input(:attending, :as => :enum) }
|
31
|
+
}.should raise_error "Attribute 'attending' has no enum class"
|
32
|
+
end
|
33
|
+
|
34
|
+
def people_path
|
35
|
+
'/people'
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
require 'simple_form'
|
4
|
+
require 'active_enum/form_helpers/simple_form'
|
5
|
+
|
6
|
+
describe ActiveEnum::FormHelpers do
|
7
|
+
include RSpec::Rails::HelperExampleGroup
|
8
|
+
include SimpleForm::ActionViewExtensions::FormHelper
|
9
|
+
|
10
|
+
before do
|
11
|
+
controller.stub!(:action_name).and_return('new')
|
12
|
+
reset_class Person do
|
13
|
+
enumerate :sex do
|
14
|
+
value :id => 1, :name => 'Male'
|
15
|
+
value :id => 2, :name => 'Female'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should use enum class for select option values for enum input type" do
|
21
|
+
output = simple_form_for(Person.new, :url => people_path) do |f|
|
22
|
+
concat f.input(:sex, :as => :enum)
|
23
|
+
end
|
24
|
+
output.should have_selector('select#person_sex')
|
25
|
+
output.should have_xpath('//option[@value=1]', :content => 'Male')
|
26
|
+
output.should have_xpath('//option[@value=2]', :content => 'Female')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise error if attribute for enum input is not enumerated" do
|
30
|
+
lambda {
|
31
|
+
simple_form_for(Person.new, :url => people_path) {|f| f.input(:attending, :as => :enum) }
|
32
|
+
}.should raise_error(StandardError, "Attribute 'attending' has no enum class")
|
33
|
+
end
|
34
|
+
|
35
|
+
def people_path
|
36
|
+
'/people'
|
37
|
+
end
|
38
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,7 +2,8 @@ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
2
2
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'spec'))
|
3
3
|
|
4
4
|
require 'rubygems'
|
5
|
-
require '
|
5
|
+
require 'rspec/autorun'
|
6
|
+
require 'rspec/rails'
|
6
7
|
|
7
8
|
require 'active_record'
|
8
9
|
require 'active_enum'
|
@@ -27,6 +28,6 @@ module SpecHelper
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
+
RSpec.configure do |config|
|
31
32
|
config.include SpecHelper
|
32
33
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 0.7.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Meehan
|
@@ -15,7 +15,7 @@ autorequire: active_enum
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-06-24 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -34,13 +34,15 @@ files:
|
|
34
34
|
- lib/active_enum/acts_as_enum.rb
|
35
35
|
- lib/active_enum/base.rb
|
36
36
|
- lib/active_enum/extensions.rb
|
37
|
-
- lib/active_enum/formtastic.rb
|
37
|
+
- lib/active_enum/form_helpers/formtastic.rb
|
38
|
+
- lib/active_enum/form_helpers/simple_form.rb
|
38
39
|
- lib/active_enum/version.rb
|
39
40
|
- lib/active_enum.rb
|
40
41
|
- spec/active_enum/acts_as_enum_spec.rb
|
41
42
|
- spec/active_enum/base_spec.rb
|
42
43
|
- spec/active_enum/extensions_spec.rb
|
43
|
-
- spec/active_enum/formtastic_spec.rb
|
44
|
+
- spec/active_enum/form_helpers/formtastic_spec.rb
|
45
|
+
- spec/active_enum/form_helpers/simple_form_spec.rb
|
44
46
|
- spec/active_enum_spec.rb
|
45
47
|
- spec/schema.rb
|
46
48
|
- spec/spec_helper.rb
|
@@ -1,12 +0,0 @@
|
|
1
|
-
module ActiveEnum
|
2
|
-
module Formtastic
|
3
|
-
|
4
|
-
def enum_input(method, options)
|
5
|
-
raise "Attribute '#{method}' has no enum class" unless enum = @object.class.active_enum_for(method)
|
6
|
-
select_input(method, options.merge(:collection => enum.to_select))
|
7
|
-
end
|
8
|
-
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
Formtastic::SemanticFormBuilder.send :include, ActiveEnum::Formtastic
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
require 'action_controller'
|
4
|
-
require 'action_view'
|
5
|
-
require 'formtastic'
|
6
|
-
require 'rspec_tag_matchers'
|
7
|
-
require 'active_enum/formtastic'
|
8
|
-
|
9
|
-
describe ActiveEnum::Formtastic do
|
10
|
-
include ActionView::Helpers::FormHelper
|
11
|
-
include ActionView::Helpers::FormTagHelper
|
12
|
-
include ActionView::Helpers::FormOptionsHelper
|
13
|
-
include ActionView::Helpers::UrlHelper
|
14
|
-
include ActionView::Helpers::TagHelper
|
15
|
-
include ActionView::Helpers::TextHelper
|
16
|
-
include ActionView::Helpers::ActiveRecordHelper
|
17
|
-
include ActionView::Helpers::RecordIdentificationHelper
|
18
|
-
include ActionView::Helpers::CaptureHelper
|
19
|
-
include ActionController::PolymorphicRoutes
|
20
|
-
include Formtastic::SemanticFormHelper
|
21
|
-
include RspecTagMatchers
|
22
|
-
|
23
|
-
attr_accessor :output_buffer
|
24
|
-
|
25
|
-
before do
|
26
|
-
reset_class Person do
|
27
|
-
enumerate :sex do
|
28
|
-
value :id => 1, :name => 'Male'
|
29
|
-
value :id => 2, :name => 'Female'
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
@output_buffer = ''
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should use enum class for select option values for enum input type" do
|
37
|
-
semantic_form_for(Person.new) do |f|
|
38
|
-
concat f.input(:sex, :as => :enum)
|
39
|
-
end
|
40
|
-
output_buffer.should have_tag('select#person_sex') do |inner|
|
41
|
-
inner.should have_tag('//option[@value=1]', 'Male')
|
42
|
-
inner.should have_tag('//option[@value=2]', 'Female')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should raise error if attribute for enum input is not enumerated" do
|
47
|
-
lambda do
|
48
|
-
semantic_form_for(Person.new) {|f| f.input(:attending, :as => :enum) }
|
49
|
-
end.should raise_error "Attribute 'attending' has no enum class"
|
50
|
-
end
|
51
|
-
|
52
|
-
def protect_against_forgery?
|
53
|
-
false
|
54
|
-
end
|
55
|
-
|
56
|
-
def people_path
|
57
|
-
'/people'
|
58
|
-
end
|
59
|
-
end
|