active_enum 0.7.0 → 0.7.1

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.
@@ -1,6 +1,14 @@
1
1
  module ActiveEnum
2
2
  module FormHelpers
3
3
  module Formtastic
4
+ def self.included(base)
5
+ base.alias_method_chain :default_input_type, :active_enum
6
+ end
7
+
8
+ def default_input_type_with_active_enum(method, options)
9
+ return :enum if @object.class.enum_for(method)
10
+ default_input_type_without_active_enum
11
+ end
4
12
 
5
13
  def enum_input(method, options)
6
14
  raise "Attribute '#{method}' has no enum class" unless enum = @object.class.enum_for(method)
@@ -1,18 +1,33 @@
1
1
  module ActiveEnum
2
2
  module FormHelpers
3
- class SimpleForm < SimpleForm::Inputs::CollectionInput
3
+ module SimpleForm
4
+ module BuilderExtension
5
+ def self.included(base)
6
+ base.alias_method_chain :default_input_type, :active_enum
7
+ end
4
8
 
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
+ def default_input_type_with_active_enum
10
+ return :enum if @options[:as].nil? && object.class.enum_for(attribute_name)
11
+ default_input_type_without_active_enum
12
+ end
13
+ end
14
+
15
+ class EnumInput < ::SimpleForm::Inputs::CollectionInput
16
+
17
+ def initialize(builder)
18
+ super
19
+ raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.enum_for(attribute_name)
20
+ builder.options[:collection] = enum.to_select
21
+ end
22
+
9
23
  end
10
-
11
24
  end
12
25
  end
13
26
  end
14
27
 
15
28
  SimpleForm::FormBuilder.class_eval do
16
- map_type :enum, :to => ActiveEnum::FormHelpers::SimpleForm
29
+ include ActiveEnum::FormHelpers::SimpleForm::BuilderExtension
30
+
31
+ map_type :enum, :to => ActiveEnum::FormHelpers::SimpleForm::EnumInput
17
32
  alias_method :collection_enum, :collection_select
18
33
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '0.7.0'
2
+ VERSION = '0.7.1'
3
3
  end
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
3
3
  require 'formtastic'
4
4
  require 'active_enum/form_helpers/formtastic'
5
5
 
6
- describe ActiveEnum::FormHelpers::Formtastic do
6
+ describe 'ActiveEnum::FormHelpers::Formtastic' do
7
7
  include RSpec::Rails::HelperExampleGroup
8
8
  include Formtastic::SemanticFormHelper
9
9
 
@@ -16,15 +16,22 @@ describe ActiveEnum::FormHelpers::Formtastic do
16
16
  end
17
17
  end
18
18
 
19
- it "should use enum class for select option values for enum input type" do
19
+ it "should use enum input type for enumerated attribute" do
20
20
  output = semantic_form_for(Person.new, :url => people_path) do |f|
21
- concat f.input(:sex, :as => :enum)
21
+ concat f.input(:sex)
22
22
  end
23
23
  output.should have_selector('select#person_sex')
24
24
  output.should have_xpath('//option[@value=1]', :content => 'Male')
25
25
  output.should have_xpath('//option[@value=2]', :content => 'Female')
26
26
  end
27
27
 
28
+ it "should not use enum input type if :as option indicates other type" do
29
+ output = semantic_form_for(Person.new, :url => people_path) do |f|
30
+ concat f.input(:sex, :as => :string)
31
+ end
32
+ output.should have_selector('input#person_sex')
33
+ end
34
+
28
35
  it "should raise error if attribute for enum input is not enumerated" do
29
36
  lambda {
30
37
  semantic_form_for(Person.new, :url => people_path) {|f| f.input(:attending, :as => :enum) }
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
3
3
  require 'simple_form'
4
4
  require 'active_enum/form_helpers/simple_form'
5
5
 
6
- describe ActiveEnum::FormHelpers do
6
+ describe 'ActiveEnum::FormHelpers::Simple' do
7
7
  include RSpec::Rails::HelperExampleGroup
8
8
  include SimpleForm::ActionViewExtensions::FormHelper
9
9
 
@@ -17,15 +17,22 @@ describe ActiveEnum::FormHelpers do
17
17
  end
18
18
  end
19
19
 
20
- it "should use enum class for select option values for enum input type" do
20
+ it "should use enum input type for enumerated attribute" do
21
21
  output = simple_form_for(Person.new, :url => people_path) do |f|
22
- concat f.input(:sex, :as => :enum)
22
+ concat f.input(:sex)
23
23
  end
24
24
  output.should have_selector('select#person_sex')
25
25
  output.should have_xpath('//option[@value=1]', :content => 'Male')
26
26
  output.should have_xpath('//option[@value=2]', :content => 'Female')
27
27
  end
28
28
 
29
+ it "should not use enum input type if :as option indicates other type" do
30
+ output = simple_form_for(Person.new, :url => people_path) do |f|
31
+ concat f.input(:sex, :as => :string)
32
+ end
33
+ output.should have_selector('input#person_sex')
34
+ end
35
+
29
36
  it "should raise error if attribute for enum input is not enumerated" do
30
37
  lambda {
31
38
  simple_form_for(Person.new, :url => people_path) {|f| f.input(:attending, :as => :enum) }
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: 3
4
+ hash: 1
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 0
10
- version: 0.7.0
9
+ - 1
10
+ version: 0.7.1
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-06-24 00:00:00 +10:00
18
+ date: 2010-07-02 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies: []
21
21