enum_simulator 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -24,6 +24,26 @@ In order to mark an AR attribute as enumerated, make sure the column in question
24
24
  enum :flavor, [:chocolate, :vanilla, :strawberry, :rockyroad]
25
25
  end
26
26
 
27
- enum_simulator will handle validation of inclusion and conversion of the attribute values from symbols to strings and back internally. Honestly, that's really all it does - it's just such a common pattern that I figured we might as well have a more succinct way of using it. It will infer whether or not to allow assignment of nil to your attribute from the column definition.
27
+ You can also pass a hash to enum, which will automatically validate the attribute's inclusion in the keys of that hash. This is useful for attributes that may need longer human-readable descriptions on display. An example of this:
28
+
29
+ class PickupTruck < ActiveRecord::Base
30
+ TRUCK_TYPES = {
31
+ :compact_sb => "Compact Short Bed",
32
+ :compact_lb => "Compact Long Bed",
33
+ :compact_ext_sb => "Compact Extended Cab Short Bed",
34
+ :fullsize_sb => "Full-sized Short Bed",
35
+ :fullsize_lb => "Full-sized Long Bed",
36
+ :fullsize_ext_sb => "Full-sized Extended Cab Short Bed"
37
+ }
38
+ enum :truck_type, TRUCK_TYPES
39
+ end
40
+
41
+ And then in a view you can do something like this:
42
+
43
+ <%= select_tag :truck_type, options_for_select(PickupTruck.enumerated_attributes[:truck_type].map { |k, v| [ v, k ] }) %>
44
+
45
+ You could also have used the PickupTruck::TRUCK_TYPES constant in this example, but the enumerated_attributes hash is a handy way to access whatever values were passed into the enum definition, especially if you haven't put them in a constant somewhere.
46
+
47
+ enum_simulator will handle validation of inclusion and conversion of the attribute values from symbols to strings and back internally. Honestly, that's really all it does - it's just such a common pattern that I figured we might as well have a more succinct way of using it. It will infer whether or not to allow assignment of nil to your attribute from the column definition. If your column allows null, then then enumerated_attributes hash for that column will also include nil. This means you do not need an :include_blank in any form tags related to the attribute in question.
28
48
 
29
49
  Copyright (c) 2012 Centresource, released under the MIT license
data/Rakefile CHANGED
@@ -23,7 +23,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
23
23
  rdoc.rdoc_files.include('lib/**/*.rb')
24
24
  end
25
25
 
26
- Echoe.new('enum_simulator', '1.0.4') do |p|
26
+ Echoe.new('enum_simulator', '1.1.0') do |p|
27
27
  p.description = 'A simple plugin for abstracting out standard ActiveRecord enumerated attributes.'
28
28
  p.url = 'https://github.com/centresource/enum_simulator.git'
29
29
  p.author = ['Jeremy Holland', 'Brandon Valentine']
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "enum_simulator"
5
- s.version = "1.0.4"
5
+ s.version = "1.1.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jeremy Holland, Brandon Valentine"]
@@ -6,10 +6,17 @@ module EnumSimulator
6
6
 
7
7
  module ClassMethods
8
8
  def enum(attr, values)
9
- values << nil if self.columns_hash[attr.to_s].respond_to? :null and self.columns_hash[attr.to_s].null
9
+ if self.columns_hash[attr.to_s].respond_to? :null and self.columns_hash[attr.to_s].null
10
+ if values.is_a? Hash
11
+ values[nil] = ""
12
+ else
13
+ values << nil
14
+ end
15
+ end
16
+ valid = values.is_a?(Hash) ? values.keys : values
10
17
  @enumerated_attributes ||= {}
11
18
  @enumerated_attributes[attr] = values
12
- validates_inclusion_of attr, :in => values
19
+ validates_inclusion_of attr, :in => valid
13
20
 
14
21
  class_eval <<RUBY
15
22
  def #{attr}
@@ -29,6 +29,11 @@ describe EnumSimulator do
29
29
  x.should { validate_inclusion_of :flavor, :in => [:sweet, :sour, :salty, :bitter, :umami] }
30
30
  end
31
31
 
32
+ it "should require the symbolized value of the attribute specified in the first argument to be a key in the hash passed as the second argument" do
33
+ x = Thingy.new
34
+ x.should { validate_inclusion_of :smell, :in => [:floral, :sulphorous, :smoky, :stale, nil] }
35
+ end
36
+
32
37
  it "should require a value for the attribute if the column definition does not allow null" do
33
38
  lambda { x = Thingy.create! }.should raise_error ActiveRecord::RecordInvalid
34
39
  end
@@ -64,10 +69,22 @@ describe EnumSimulator do
64
69
  Thingy.enumerated_attributes[:flavor].should_not include(:corned_beef)
65
70
  end
66
71
 
67
- it "should conditionally include nil in array of possible enumerable values when column allows null" do
72
+ it "should not include nil in array of possible enumerable values even when column allows null" do
68
73
  Thingy.enumerated_attributes[:flavor].should_not include(nil)
69
74
  Thingy.enumerated_attributes[:smell].should include(nil)
70
75
  end
76
+
77
+ describe "when an array is passed to enum" do
78
+ it "should be an array of all possible enumerable values for the key in question" do
79
+ Thingy.enumerated_attributes[:flavor].should be_a(Array)
80
+ end
81
+ end
82
+
83
+ describe "when a hash is passed to enum" do
84
+ it "should be a hash of all possible enumerable values for the key in question" do
85
+ Thingy.enumerated_attributes[:smell].should be_a(Hash)
86
+ end
87
+ end
71
88
  end
72
89
  end
73
90
  end
@@ -1,4 +1,4 @@
1
1
  class Thingy < ActiveRecord::Base
2
2
  enum :flavor, [:salty, :sweet, :bitter, :sour, :umami]
3
- enum :smell, [:floral, :sulphorous, :smoky, :stale]
3
+ enum :smell, {:floral => "Floral", :sulphorous => "Sulphorous", :smoky => "Smoky", :stale => "Stale"}
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_simulator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: