classy_enum 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # ClassyEnum Changelog
2
2
 
3
+ ## 2.0.3
4
+
5
+ * Fixes issue with validates_uniqueness_of when using an enum field as
6
+ the scope.
7
+
8
+ ## 2.0.1 & 2.0.2
9
+
10
+ * Resolving gem release conflicts, no changes
11
+
3
12
  ## 2.0.0
4
13
 
5
14
  * Enum class definitions are no longer defined implicity and must be explicitly subclassed from children of ClassyEnum::Base
data/README.md CHANGED
@@ -8,8 +8,7 @@ ClassyEnum is a Ruby on Rails gem that adds class-based enumerator functionality
8
8
 
9
9
  *Rails:*
10
10
 
11
- * 3.0.x - 3.2.x: Fully tested in a production application. See below
12
- for known issues.
11
+ * 3.0.x - 3.2.x: Fully tested in a production application.
13
12
  * 2.3.x: If you need support for Rails 2.3.x, please install [version 0.9.1](https://rubygems.org/gems/classy_enum/versions/0.9.1)
14
13
 
15
14
  *Ruby:* Ruby 1.8.7, 1.9.2, and 1.9.3 are tested and supported
@@ -30,9 +29,6 @@ generator, there are no changes to the existing structure.
30
29
  Built-in Formtastic support has been removed. See the note at the
31
30
  bottom of this readme for more information how how to enable it.
32
31
 
33
- validates_uniqueness_of will no longer work with Rails 3.0.x when using
34
- a scope that is the enum field.
35
-
36
32
  ## Example Usage
37
33
 
38
34
  The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". In this example, I have an ActiveRecord model called `Alarm` with an attribute called `priority`. Priority is stored as a string (VARCHAR) type in the database and is converted to an enum value when requested.
@@ -81,10 +77,10 @@ class Priority < ClassyEnum::Base
81
77
  end
82
78
  end
83
79
 
84
- class prioritylow < priority
80
+ class PriorityLow < priority
85
81
  end
86
82
 
87
- class prioritymedium < priority
83
+ class PriorityMedium < priority
88
84
  end
89
85
 
90
86
  class PriorityHigh < Priority
@@ -148,10 +144,10 @@ class Priority < ClassyEnum::Base
148
144
  owner :alarm
149
145
  end
150
146
 
151
- class prioritylow < priority
147
+ class PriorityLow < priority
152
148
  end
153
149
 
154
- class prioritymedium < priority
150
+ class PriorityMedium < priority
155
151
  end
156
152
 
157
153
  class PriorityHigh < Priority
@@ -192,7 +188,7 @@ end
192
188
  @alarm.to_json.should == "{\"alarm\":{\"priority\":{}}}"
193
189
  ```
194
190
 
195
- ## Special Cases and Known Issue
191
+ ## Special Cases
196
192
 
197
193
  What if your enum class name is not the same as your model's attribute name? No problem! Just use a second arugment in `classy_enum_attr` to declare the attribute name. In this case, the model's attribute is called *alarm_priority*.
198
194
 
@@ -205,11 +201,8 @@ end
205
201
  @alarm.alarm_priority # => PriorityMedium
206
202
  ```
207
203
 
208
- There is an [issue](https://github.com/beerlington/classy_enum/issues/8)
209
- with Rails 3.0 and higher when using validates_uniqueness_of
210
- and a scope that is the enum field. This issue also occurs when using
211
- `composed_of` and is not a bug with ClassyEnum. As a workaround to this
212
- problem, you can use the reader suffix option when declaring your field:
204
+ If you would like the default getter method to return a string, you can
205
+ use the optional *:suffix* option for the enum getter:
213
206
 
214
207
  ```ruby
215
208
  class Alarm < ActiveRecord::Base
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.2
1
+ 2.0.3
data/classy_enum.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "classy_enum"
8
- s.version = "2.0.2"
8
+ s.version = "2.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Peter Brown"]
12
- s.date = "2012-04-22"
12
+ s.date = "2012-04-24"
13
13
  s.description = "A utility that adds class based enum functionality to ActiveRecord attributes"
14
14
  s.email = "github@lette.us"
15
15
  s.extra_rdoc_files = [
@@ -33,6 +33,11 @@ module ClassyEnum
33
33
  def inherited(klass)
34
34
  return if self == ClassyEnum::Base
35
35
 
36
+ # Add visit_EnumMember methods to support validates_uniqueness_of with enum field
37
+ Arel::Visitors::ToSql.class_eval do
38
+ define_method "visit_#{klass.name}", lambda {|value| quote(value.to_s) }
39
+ end
40
+
36
41
  enum = klass.name.gsub(klass.base_class.name, '').underscore.to_sym
37
42
  index = self.enum_options.index(enum) + 1
38
43
 
@@ -1,12 +1,23 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ class Color < ClassyEnum::Base
4
+ enum_classes :white, :black
5
+ end
6
+
7
+ class ColorWhite < Color; end;
8
+
9
+ class ColorBlack < Color; end;
10
+
3
11
  class ActiveDog < ActiveRecord::Base
4
12
  classy_enum_attr :breed, :suffix => 'type'
13
+ classy_enum_attr :color
5
14
 
6
15
  validates :name,
7
16
  :presence => true,
8
17
  :uniqueness => { :scope => [:breed] }
9
18
 
19
+ validates_uniqueness_of :name, :scope => :color
20
+
10
21
  scope :goldens, where(:breed => 'golden_retriever')
11
22
 
12
23
  end
@@ -14,22 +25,35 @@ end
14
25
  describe ActiveDog do
15
26
 
16
27
  context 'valid instance' do
17
- subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retriever) }
28
+ subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retriever, :color => :black) }
18
29
 
19
30
  it { should have(:no).errors_on(:breed) }
20
31
  its(:breed_type) { should be_a_golden_retriever }
21
32
  its(:breed) { should == 'golden_retriever' }
22
33
  end
23
34
 
35
+ context 'uniqueness on name' do
36
+ subject { ActiveDog.new(:name => 'Kitteh', :breed => :golden_retriever, :color => :black) }
37
+ it { should be_valid }
38
+
39
+ context 'with existing kitteh' do
40
+ before do
41
+ ActiveDog.create!(:name => 'Kitteh', :breed => :husky, :color => :black)
42
+ end
43
+
44
+ it { should have(1).error_on(:name) }
45
+ end
46
+ end
47
+
24
48
  context 'invalid instance' do
25
- subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retrievers) }
49
+ subject { ActiveDog.new(:name => 'sirius', :breed => :golden_retrievers, :color => :white) }
26
50
 
27
51
  it { should have(1).error_on(:breed) }
28
52
  end
29
53
 
30
54
  context 'scopes' do
31
- let!(:golden) { ActiveDog.create!(:name => 'Sebastian', :breed => :golden_retriever) }
32
- let!(:husky) { ActiveDog.create!(:name => 'Sirius', :breed => :husky) }
55
+ let!(:golden) { ActiveDog.create!(:name => 'Sebastian', :breed => :golden_retriever, :color => :white) }
56
+ let!(:husky) { ActiveDog.create!(:name => 'Sirius', :breed => :husky, :color => :black) }
33
57
 
34
58
  after { ActiveDog.destroy_all }
35
59
 
data/spec/spec_helper.rb CHANGED
@@ -33,6 +33,7 @@ ActiveRecord::Schema.define(:version => 1) do
33
33
 
34
34
  create_table :active_dogs, :force => true do |t|
35
35
  t.string :breed
36
+ t.string :color
36
37
  t.string :name
37
38
  t.integer :age
38
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classy_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-22 00:00:00.000000000Z
12
+ date: 2012-04-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -141,7 +141,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  segments:
143
143
  - 0
144
- hash: -624822224793334700
144
+ hash: 3374934930458401417
145
145
  required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  none: false
147
147
  requirements: