active_enum 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ [0.8.2] : 2010-08-01
2
+ - Fix method name clash of enum_for which is defined on Object
3
+ - Refactor extension method definitions
4
+
5
+ [0.8.1] : 2010-07-19
6
+ - Enumerated attribute read method can access meta data using key as argument
7
+
1
8
  [0.8.0] : 2010-07-19
2
9
  - Added pluggable storage API
3
10
  - Added memory store as default storage
@@ -3,7 +3,7 @@
3
3
  Define enum classes in Rails and use them to enumerate ActiveRecord attributes. Brings together some ideas
4
4
  of similar plugins that I liked and does it they way I prefer.
5
5
 
6
- Enum values are stored in memory at the moment but I plan to add database and yaml.
6
+ Enum values are stored in memory at the moment but I plan to add database and possibly others.
7
7
 
8
8
  == Install
9
9
 
@@ -135,7 +135,7 @@ You can check if the attribute value matches a particular enum value by passing
135
135
 
136
136
  A convenience method on the class is available to the enum class of any enumerated attribute
137
137
 
138
- User.enum_for(:sex) # => Sex
138
+ User.active_enum_for(:sex) # => Sex
139
139
 
140
140
  === Bulk definition
141
141
 
@@ -163,15 +163,15 @@ All defined enum classes are stored in ActiveEnum.enum_classes array if you need
163
163
 
164
164
  You can make an existing model class behave like an enum class with acts_as_enum
165
165
 
166
- class User < ActiveRecord::Base
167
- acts_as_enum :name_column => 'first_name'
166
+ class Country < ActiveRecord::Base
167
+ acts_as_enum :name_column => 'short_name'
168
168
  end
169
169
 
170
170
  Giving you the familiar enum methods
171
171
 
172
- User[1]
173
- User['Dave']
174
- User.to_select
172
+ Country[1]
173
+ Country['Australia']
174
+ Country.to_select
175
175
 
176
176
 
177
177
  === Form Helpers
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{active_enum}
5
- s.version = "0.8.1"
5
+ s.version = "0.8.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Adam Meehan"]
9
9
  s.autorequire = %q{active_enum}
10
- s.date = %q{2010-07-19}
10
+ s.date = %q{2010-08-01}
11
11
  s.description = %q{Define enum classes in Rails and use them to enumerate ActiveRecord attributes}
12
12
  s.email = %q{adam.meehan@gmail.com}
13
13
  s.extra_rdoc_files = ["README.rdoc"]
@@ -17,11 +17,7 @@ module ActiveEnum
17
17
  class Configuration
18
18
  def enum(name, &block)
19
19
  class_name = name.to_s.camelize
20
- class_def = <<-end_eval
21
- class #{class_name} < ActiveEnum::Base
22
- end
23
- end_eval
24
- eval(class_def, TOPLEVEL_BINDING)
20
+ eval("class #{class_name} < ActiveEnum::Base; end", TOPLEVEL_BINDING)
25
21
  new_enum = Module.const_get(class_name)
26
22
  new_enum.class_eval(&block)
27
23
  end
@@ -21,9 +21,9 @@ module ActiveEnum
21
21
 
22
22
  # Order enum values. Allowed values are :asc, :desc or :as_defined
23
23
  #
24
- def order(order)
25
- @order = order
26
- end
24
+ def order(order)
25
+ @order = order
26
+ end
27
27
 
28
28
  def all
29
29
  store.values
@@ -50,7 +50,7 @@ module ActiveEnum
50
50
  end
51
51
  end
52
52
 
53
- def enum_for(attribute)
53
+ def active_enum_for(attribute)
54
54
  self.enumerated_attributes[attribute.to_sym]
55
55
  end
56
56
 
@@ -63,24 +63,26 @@ module ActiveEnum
63
63
  # user.sex(:meta_key)
64
64
  #
65
65
  def define_active_enum_read_method(attribute)
66
- define_method("#{attribute}") do |*arg|
67
- arg = arg.first
68
- value = super()
69
- enum = self.class.enum_for(attribute)
66
+ class_eval <<-DEF
67
+ def #{attribute}(arg=nil)
68
+ value = super()
69
+ return if value.nil? && arg.nil?
70
70
 
71
- case arg
72
- when :id
73
- value if enum[value]
74
- when :name
75
- enum[value]
76
- when :enum
77
- enum
78
- when Symbol
79
- (enum.meta(value) || {})[arg]
80
- else
81
- ActiveEnum.use_name_as_value ? enum[value] : value
71
+ enum = self.class.active_enum_for(:#{attribute})
72
+ case arg
73
+ when :id
74
+ value if enum[value]
75
+ when :name
76
+ enum[value]
77
+ when :enum
78
+ enum
79
+ when Symbol
80
+ (enum.meta(value) || {})[arg]
81
+ else
82
+ #{ActiveEnum.use_name_as_value ? 'enum[value]' : 'value' }
83
+ end
82
84
  end
83
- end
85
+ DEF
84
86
  end
85
87
 
86
88
  # Define write method to also handle enum value
@@ -89,15 +91,16 @@ module ActiveEnum
89
91
  # user.sex = :male
90
92
  #
91
93
  def define_active_enum_write_method(attribute)
92
- define_method("#{attribute}=") do |arg|
93
- enum = self.class.enum_for(attribute)
94
- if arg.is_a?(Symbol)
95
- value = enum[arg]
96
- super(value)
97
- else
98
- super(arg)
94
+ class_eval <<-DEF
95
+ def #{attribute}=(arg)
96
+ if arg.is_a?(Symbol)
97
+ value = self.class.active_enum_for(:#{attribute})[arg]
98
+ super(value)
99
+ else
100
+ super(arg)
101
+ end
99
102
  end
100
- end
103
+ DEF
101
104
  end
102
105
 
103
106
  # Define question method to check enum value against attribute value
@@ -108,7 +111,7 @@ module ActiveEnum
108
111
  define_method("#{attribute}?") do |*arg|
109
112
  arg = arg.first
110
113
  if arg
111
- send(attribute) == self.class.enum_for(attribute)[arg]
114
+ send(attribute) == self.class.active_enum_for(attribute)[arg]
112
115
  else
113
116
  super
114
117
  end
@@ -6,12 +6,12 @@ module ActiveEnum
6
6
  end
7
7
 
8
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
9
+ return :enum if @object.class.respond_to?(:active_enum_for) && @object.class.active_enum_for(method)
10
+ default_input_type_without_active_enum(method, options)
11
11
  end
12
12
 
13
13
  def enum_input(method, options)
14
- raise "Attribute '#{method}' has no enum class" unless enum = @object.class.enum_for(method)
14
+ raise "Attribute '#{method}' has no enum class" unless enum = @object.class.active_enum_for(method)
15
15
  select_input(method, options.merge(:collection => enum.to_select))
16
16
  end
17
17
 
@@ -7,7 +7,9 @@ module ActiveEnum
7
7
  end
8
8
 
9
9
  def default_input_type_with_active_enum
10
- return :enum if @options[:as].nil? && object.class.enum_for(attribute_name)
10
+ return :enum if @options[:as].nil? &&
11
+ object.class.respond_to?(:active_enum_for) &&
12
+ object.class.active_enum_for(attribute_name)
11
13
  default_input_type_without_active_enum
12
14
  end
13
15
  end
@@ -16,7 +18,7 @@ module ActiveEnum
16
18
 
17
19
  def initialize(builder)
18
20
  super
19
- raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.enum_for(attribute_name)
21
+ raise "Attribute '#{attribute_name}' has no enum class" unless enum = object.class.active_enum_for(attribute_name)
20
22
  builder.options[:collection] = enum.to_select
21
23
  end
22
24
 
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.2'
3
3
  end
@@ -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 :enum_for method to ActiveRecord' do
21
- ActiveRecord::Base.should respond_to(:enum_for)
20
+ it 'should add class :active_enum_for method to ActiveRecord' do
21
+ ActiveRecord::Base.should respond_to(:active_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.enum_for(:attending).should == Accepted
29
- Person.enum_for(:staying).should == Accepted
28
+ Person.active_enum_for(:attending).should == Accepted
29
+ Person.active_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.enum_for(:sex).should == Sex
36
+ Person.active_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.enum_for(:sex).should == Person::Sex
45
+ Person.active_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
@@ -198,6 +198,13 @@ describe ActiveEnum::Extensions do
198
198
  ActiveEnum.use_name_as_value = true
199
199
  end
200
200
 
201
+ before do
202
+ reset_class Person do
203
+ enumerate :sex, :with => Sex
204
+ end
205
+ @person = Person.new(:sex =>1)
206
+ end
207
+
201
208
  it 'should return text name value for attribute' do
202
209
  @person.sex.should == 'Male'
203
210
  end
@@ -38,6 +38,13 @@ describe 'ActiveEnum::FormHelpers::Formtastic' do
38
38
  }.should raise_error "Attribute 'attending' has no enum class"
39
39
  end
40
40
 
41
+ it "should not use enum input type if class does not support ActiveEnum" do
42
+ output = semantic_form_for(:not_active_record, NotActiveRecord.new, :url => people_path) do |f|
43
+ concat f.input(:name)
44
+ end
45
+ output.should have_selector('input#not_active_record_name')
46
+ end
47
+
41
48
  def people_path
42
49
  '/people'
43
50
  end
@@ -39,6 +39,13 @@ describe 'ActiveEnum::FormHelpers::Simple' do
39
39
  }.should raise_error(StandardError, "Attribute 'attending' has no enum class")
40
40
  end
41
41
 
42
+ it "should not use enum input type if class does not support ActiveEnum" do
43
+ output = simple_form_for(:not_active_record, NotActiveRecord.new, :url => people_path) do |f|
44
+ concat f.input(:name)
45
+ end
46
+ output.should have_selector('input#not_active_record_name')
47
+ end
48
+
42
49
  def people_path
43
50
  '/people'
44
51
  end
@@ -23,6 +23,11 @@ require 'schema'
23
23
 
24
24
  class Person < ActiveRecord::Base; end
25
25
 
26
+ class NotActiveRecord
27
+ include ActiveModel::Validations
28
+ attr_accessor :name
29
+ end
30
+
26
31
  module SpecHelper
27
32
  def reset_class(klass, &block)
28
33
  name = klass.name.to_sym
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: 61
4
+ hash: 59
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 1
10
- version: 0.8.1
9
+ - 2
10
+ version: 0.8.2
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-07-19 00:00:00 +10:00
18
+ date: 2010-08-01 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies: []
21
21