classy_enum 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -18,33 +18,33 @@ The most common use for ClassyEnum is to replace database lookup tables where th
18
18
 
19
19
  The fastest way to get up and running with ClassyEnum is to use the built-in Rails generator like so:
20
20
 
21
- @script/generate classy_enum AlarmPriority low medium high@
21
+ <pre>
22
+ script/generate classy_enum AlarmPriority low medium high
23
+ </pre>
22
24
 
23
25
  A new file will be created at app/enums/alarm_priority.rb that will look like:
24
26
 
25
27
  <pre>
26
- module AlarmPriority
27
-
28
- OPTIONS = [:low, :medium, :high]
28
+ module AlarmPriority
29
+ OPTIONS = [:low, :medium, :high]
29
30
 
30
- module Defaults
31
-
32
- end
33
-
34
- include ClassyEnum
31
+ module InstanceMethods
35
32
  end
36
33
 
37
- class AlarmPriorityLow
38
-
34
+ module ClassMethods
39
35
  end
40
36
 
41
- class AlarmPriorityMedium
37
+ include ClassyEnum
38
+ end
42
39
 
43
- end
40
+ class AlarmPriorityLow
41
+ end
44
42
 
45
- class AlarmPriorityHigh
43
+ class AlarmPriorityMedium
44
+ end
46
45
 
47
- end
46
+ class AlarmPriorityHigh
47
+ end
48
48
  </pre>
49
49
 
50
50
  That is the default setup, but can be changed to fit your needs, like so...
@@ -54,24 +54,23 @@ Using the OPTIONS constant, I have defined three priority levels: low, medium, a
54
54
  *It is important that you include ClassyEnum AFTER declaring your OPTIONS and Default methods because they are used when creating the enum classes*
55
55
 
56
56
  <pre>
57
- module AlarmPriority
58
-
59
- OPTIONS = [:low, :medium, :high]
57
+ module AlarmPriority
58
+ OPTIONS = [:low, :medium, :high]
60
59
 
61
- module Defaults
62
- def email?
63
- false
64
- end
60
+ module InstanceMethods
61
+ def email?
62
+ false
65
63
  end
66
-
67
- include ClassyEnum
68
64
  end
69
65
 
70
- class AlarmPriorityHigh
71
- def email?
72
- true
73
- end
66
+ include ClassyEnum
67
+ end
68
+
69
+ class AlarmPriorityHigh
70
+ def email?
71
+ true
74
72
  end
73
+ end
75
74
  </pre>
76
75
 
77
76
  Then in my ActiveRecord model, Alarm, I've added a line that calls @classy_enum_attr@. The first argument is required, and is the name of the module defined above. The second argument is optional and specifies which Alarm attribute will be used as an enumerable.
@@ -79,27 +78,37 @@ Then in my ActiveRecord model, Alarm, I've added a line that calls @classy_enum_
79
78
  In this case, I am using the module AlarmPriority, but the name of my attribute is priority. By default, it will use the name of module as the attribute name. If I wanted to do @alarm.alarm_priority@, I would not have included the second argument.
80
79
 
81
80
  <pre>
82
- class Alarm < ActiveRecord::Base
83
- classy_enum_attr :alarm_priority, :priority
81
+ class Alarm < ActiveRecord::Base
82
+ classy_enum_attr :alarm_priority, :priority
84
83
 
85
- delegate :email?, :to => :priority
86
- end
84
+ delegate :email?, :to => :priority
85
+ end
87
86
  </pre>
88
87
 
89
88
  With this setup, I can now do the following:
90
89
 
91
90
  <pre>
92
- @alarm = Alarm.create(:priority => :medium)
91
+ @alarm = Alarm.create(:priority => :medium)
93
92
 
94
- @alarm.priority => AlarmPriorityMedium
93
+ @alarm.priority => AlarmPriorityMedium
95
94
 
96
- @alarm.email? => false
95
+ @alarm.email? => false
97
96
 
98
- @alarm.update_attribute(:priority, :high)
97
+ @alarm.update_attribute(:priority, :high)
99
98
 
100
- @alarm.email? => true
99
+ @alarm.email? => true
100
+ </pre>
101
+
102
+ h2. Formtastic Support
103
+
104
+ To add ClassyEnum support to Formtastic, add the following to your formtastic.rb initializer (config/initializers/formtastic.rb):
105
+
106
+ <pre>
107
+ Formtastic::SemanticFormHelper.builder = ClassyEnumHelper::SemanticFormBuilder
101
108
  </pre>
102
109
 
110
+ Then in your Formtastic view forms, use this syntax: @<%= f.input :priority, :as => :enum_select %>@
111
+
103
112
  h2. Notes
104
113
 
105
114
  An ActiveRecord validator @validates_inclusion_of :field, :in => ENUM.all@ is automatically added to your model when you use @classy_enum_attr@.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/classy_enum.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{classy_enum}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
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 = %q{2010-10-01}
12
+ s.date = %q{2010-10-04}
13
13
  s.description = %q{A utility that adds class based enum functionaltiy to ActiveRecord attributes}
14
14
  s.email = %q{github@lette.us}
15
15
  s.extra_rdoc_files = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "init.rb",
30
30
  "lib/classy_enum.rb",
31
31
  "lib/classy_enum/classy_enum_attributes.rb",
32
+ "lib/classy_enum/classy_enum_helper.rb",
32
33
  "spec/classy_enum_attributes_spec.rb",
33
34
  "spec/classy_enum_spec.rb",
34
35
  "spec/spec.opts",
data/lib/classy_enum.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  require "classy_enum/classy_enum_attributes"
2
+ require 'classy_enum/classy_enum_helper'
2
3
 
3
4
  class ClassyEnumValue < Object
4
-
5
- attr_reader :to_s, :index
6
5
 
7
- def initialize(option, index)
6
+ attr_reader :to_s, :to_sym, :index, :base_class
7
+
8
+ def initialize(base_class, option, index)
8
9
  @to_s = option.to_s.downcase
10
+ @to_sym = @to_s.to_sym
9
11
  @index = index + 1
12
+ @base_class = base_class
10
13
  end
11
14
 
12
15
  def name
@@ -61,7 +64,7 @@ module ClassyEnum
61
64
 
62
65
  Object.const_set("#{other}#{option.to_s.camelize}", klass)
63
66
 
64
- instance = klass.new(option, other::OPTIONS.index(option))
67
+ instance = klass.new(other, option, other::OPTIONS.index(option))
65
68
 
66
69
  other::OPTION_HASH[option] = other::OPTION_HASH[option.to_s.downcase] = instance
67
70
 
@@ -0,0 +1,14 @@
1
+ module ClassyEnumHelper
2
+ class SemanticFormBuilder < Formtastic::SemanticFormBuilder
3
+ def enum_select_input(method, options)
4
+ enum_class = object.send(method)
5
+
6
+ raise "#{method} does not refer to a defined ClassyEnum object" unless enum_class.respond_to? :base_class
7
+
8
+ options[:collection] = enum_class.base_class.all_with_name
9
+ options[:selected] = enum_class.to_s
10
+
11
+ select_input(method, options)
12
+ end
13
+ end
14
+ end
@@ -1,29 +1,20 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
3
+ class Dog < ActiveRecord::Base
4
+ classy_enum_attr :breed
5
+ end
4
6
 
5
- ActiveRecord::Schema.define(:version => 1) do
6
- create_table :dogs, :force => true do |t|
7
- t.string :breed
8
- end
7
+ describe "A Dog Collection" do
8
+ before(:each) do
9
+ dog1 = Dog.new(:breed => :golden_retriever)
10
+ dog2 = Dog.new(:breed => :snoop)
9
11
 
10
- create_table :things, :force => true do |t|
11
- t.string :dog_breed
12
+ @dogs = [dog1, dog2]
12
13
  end
13
- end
14
14
 
15
- module Breed
16
- OPTIONS = [:golden_retriever, :snoop]
17
-
18
- module Defaults
19
-
15
+ it "should sort by breed" do
16
+ @dogs.sort_by(&:breed).should == @dogs
20
17
  end
21
-
22
- include ClassyEnum
23
- end
24
-
25
- class Dog < ActiveRecord::Base
26
- classy_enum_attr :breed
27
18
  end
28
19
 
29
20
  describe "A Dog" do
@@ -33,6 +24,10 @@ describe "A Dog" do
33
24
  it "should have an enumerable breed" do
34
25
  @dog.breed.class.should == BreedGoldenRetriever
35
26
  end
27
+
28
+ it "should have a base class of Breed" do
29
+ @dog.breed.base_class.should == Breed
30
+ end
36
31
 
37
32
  end
38
33
 
@@ -46,4 +41,8 @@ describe "A Thing" do
46
41
  it "should have an enumerable dog breed as breed" do
47
42
  @thing.dog_breed.class.should == BreedSnoop
48
43
  end
44
+
45
+ it "should have a base class of Breed" do
46
+ @thing.dog_breed.base_class.should == Breed
47
+ end
49
48
  end
@@ -79,6 +79,14 @@ end
79
79
  describe "An ClassyEnumValue" do
80
80
  before(:each) { @enum = TestEnum.new(:one) }
81
81
 
82
+ it "should convert to a string" do
83
+ @enum.to_s.should == "one"
84
+ end
85
+
86
+ it "should convert to a symbol" do
87
+ @enum.to_sym.should == :one
88
+ end
89
+
82
90
  it "should have a name" do
83
91
  @enum.name.should == "One"
84
92
  end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,27 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
4
  require 'rubygems'
5
+ require 'spec'
6
+
5
7
  require 'active_record'
8
+ require 'action_view'
9
+ require 'formtastic'
6
10
  require 'classy_enum'
7
- require 'spec'
11
+
12
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
13
+
14
+ ActiveRecord::Schema.define(:version => 1) do
15
+ create_table :dogs, :force => true do |t|
16
+ t.string :breed
17
+ end
18
+
19
+ create_table :things, :force => true do |t|
20
+ t.string :dog_breed
21
+ end
22
+ end
23
+
24
+ module Breed
25
+ OPTIONS = [:golden_retriever, :snoop]
26
+
27
+ include ClassyEnum
28
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classy_enum
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Peter Brown
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-01 00:00:00 -04:00
18
+ date: 2010-10-04 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -71,6 +71,7 @@ files:
71
71
  - init.rb
72
72
  - lib/classy_enum.rb
73
73
  - lib/classy_enum/classy_enum_attributes.rb
74
+ - lib/classy_enum/classy_enum_helper.rb
74
75
  - spec/classy_enum_attributes_spec.rb
75
76
  - spec/classy_enum_spec.rb
76
77
  - spec/spec.opts