active_enum 0.9.12 → 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +0,0 @@
1
- module ActiveEnum
2
- module FormHelpers
3
- module Formtastic
4
- def default_input_type_with_active_enum(method, options)
5
- return :enum if @object.class.respond_to?(:active_enum_for) && @object.class.active_enum_for(method)
6
- default_input_type_without_active_enum(method, options)
7
- end
8
-
9
- def enum_input(method, options)
10
- raise "Attribute '#{method}' has no enum class" unless enum = @object.class.active_enum_for(method)
11
- select_input(method, options.merge(:collection => enum.to_select))
12
- end
13
-
14
- end
15
- end
16
- end
17
-
18
- Formtastic::SemanticFormBuilder.class_eval do
19
- include ActiveEnum::FormHelpers::Formtastic
20
- alias_method_chain :default_input_type, :active_enum
21
- end
@@ -1,29 +0,0 @@
1
- module Formtastic
2
- module Inputs
3
- class EnumInput < Formtastic::Inputs::SelectInput
4
-
5
- def raw_collection
6
- @raw_collection ||= begin
7
- raise "Attribute '#{@method}' has no enum class" unless enum = @object.class.active_enum_for(@method)
8
- enum.to_select
9
- end
10
- end
11
-
12
- end
13
- end
14
- end
15
-
16
- module ActiveEnum
17
- module FormHelpers
18
- module Formtastic2
19
-
20
- def default_input_type(method, options)
21
- return :enum if @object.class.respond_to?(:active_enum_for) && @object.class.active_enum_for(method)
22
- super
23
- end
24
-
25
- end
26
- end
27
- end
28
-
29
- Formtastic::FormBuilder.send :include, ActiveEnum::FormHelpers::Formtastic2
@@ -1,80 +0,0 @@
1
- require "spec_helper"
2
-
3
- require 'formtastic'
4
-
5
- if Formtastic.const_defined?(:VERSION) && Formtastic::VERSION =~ /^2\./
6
- require 'active_enum/form_helpers/formtastic2'
7
-
8
- describe ActiveEnum::FormHelpers::Formtastic2, :type => :helper do
9
- include Rails.application.routes.url_helpers
10
- include Formtastic::Helpers::FormHelper
11
-
12
- before do
13
- reset_class Person do
14
- enumerate :sex do
15
- value :id => 1, :name => 'Male'
16
- value :id => 2, :name => 'Female'
17
- end
18
- end
19
- end
20
-
21
- it "should use enum input type for enumerated attribute" do
22
- output = semantic_form_for(Person.new, :url => people_path) do |f|
23
- concat f.input(:sex)
24
- end
25
- output.should have_selector('select#person_sex')
26
- output.should have_xpath('//option[@value=1]', :content => 'Male')
27
- output.should have_xpath('//option[@value=2]', :content => 'Female')
28
- end
29
-
30
- it "should use explicit :enum input type" do
31
- output = semantic_form_for(Person.new, :url => people_path) do |f|
32
- concat f.input(:sex, :as => :enum)
33
- end
34
- output.should have_selector('select#person_sex')
35
- output.should have_xpath('//option[@value=1]', :content => 'Male')
36
- output.should have_xpath('//option[@value=2]', :content => 'Female')
37
- end
38
-
39
- it "should not use enum input type if :as option indicates other type" do
40
- output = semantic_form_for(Person.new, :url => people_path) do |f|
41
- concat f.input(:sex, :as => :string)
42
- end
43
- output.should have_selector('input#person_sex')
44
- end
45
-
46
- it "should raise error if attribute for enum input is not enumerated" do
47
- expect {
48
- semantic_form_for(Person.new, :url => people_path) do |f|
49
- f.input(:attending, :as => :enum)
50
- end
51
- }.should raise_error "Attribute 'attending' has no enum class"
52
- end
53
-
54
- it "should not use enum input type if class does not support ActiveEnum" do
55
- output = semantic_form_for(NotActiveRecord.new, :as => :not_active_record, :url => people_path) do |f|
56
- concat f.input(:name)
57
- end
58
- output.should have_selector('input#not_active_record_name')
59
- end
60
-
61
- it "should allow non-enum fields to use default input determination" do
62
- output = semantic_form_for(Person.new, :url => people_path) do |f|
63
- concat f.input(:first_name)
64
- end
65
- output.should have_selector('input#person_first_name')
66
- end
67
-
68
- it "should allow models without enumerated attributes to behave normally" do
69
- output = semantic_form_for(NoEnumPerson.new, :url => people_path) do |f|
70
- concat f.input(:first_name)
71
- end
72
- output.should have_selector('input#no_enum_person_first_name')
73
- end
74
-
75
- def people_path
76
- '/people'
77
- end
78
- end
79
-
80
- end
@@ -1,83 +0,0 @@
1
- require "spec_helper"
2
-
3
- begin
4
- require 'formtastic'
5
- require 'formtastic/version'
6
- rescue LoadError
7
- end
8
-
9
- unless Formtastic.const_defined?(:VERSION)
10
- require 'active_enum/form_helpers/formtastic'
11
-
12
- describe ActiveEnum::FormHelpers::Formtastic, :type => :helper do
13
- include Formtastic::SemanticFormHelper
14
-
15
- before do
16
- reset_class Person do
17
- enumerate :sex do
18
- value :id => 1, :name => 'Male'
19
- value :id => 2, :name => 'Female'
20
- end
21
- end
22
- end
23
-
24
- it "should use enum input type for enumerated attribute" do
25
- output = semantic_form_for(Person.new, :url => people_path) do |f|
26
- concat f.input(:sex)
27
- end
28
- output.should have_selector('select#person_sex')
29
- output.should have_xpath('//option[@value=1]', :content => 'Male')
30
- output.should have_xpath('//option[@value=2]', :content => 'Female')
31
- end
32
-
33
- it "should use explicit :enum input type" do
34
- output = semantic_form_for(Person.new, :url => people_path) do |f|
35
- concat f.input(:sex, :as => :enum)
36
- end
37
- output.should have_selector('select#person_sex')
38
- output.should have_xpath('//option[@value=1]', :content => 'Male')
39
- output.should have_xpath('//option[@value=2]', :content => 'Female')
40
- end
41
-
42
- it "should not use enum input type if :as option indicates other type" do
43
- output = semantic_form_for(Person.new, :url => people_path) do |f|
44
- concat f.input(:sex, :as => :string)
45
- end
46
- output.should have_selector('input#person_sex')
47
- end
48
-
49
- it "should raise error if attribute for enum input is not enumerated" do
50
- expect {
51
- semantic_form_for(Person.new, :url => people_path) do |f|
52
- f.input(:attending, :as => :enum)
53
- end
54
- }.should raise_error "Attribute 'attending' has no enum class"
55
- end
56
-
57
- it "should not use enum input type if class does not support ActiveEnum" do
58
- output = semantic_form_for(NotActiveRecord.new, :as => :not_active_record, :url => people_path) do |f|
59
- concat f.input(:name)
60
- end
61
- output.should have_selector('input#not_active_record_name')
62
- end
63
-
64
- it "should allow non-enum fields to use default input determination" do
65
- output = semantic_form_for(Person.new, :url => people_path) do |f|
66
- concat f.input(:first_name)
67
- end
68
- output.should have_selector('input#person_first_name')
69
- end
70
-
71
- it "should allow models without enumerated attributes to behave normally" do
72
- output = semantic_form_for(NoEnumPerson.new, :url => people_path) do |f|
73
- concat f.input(:first_name)
74
- end
75
- output.should have_selector('input#no_enum_person_first_name')
76
- end
77
-
78
- def people_path
79
- '/people'
80
- end
81
- end
82
-
83
- end