haz_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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/haz_enum.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{haz_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 = ["thyphoon"]
data/lib/haz_enum/enum.rb CHANGED
@@ -18,7 +18,11 @@ module HazEnum
18
18
  end
19
19
 
20
20
  define_method "#{enum_name}=" do |enum_to_set|
21
- self["#{enum_column}"] = enum_to_set.name
21
+ if enum_to_set.respond_to?(:name)
22
+ self["#{enum_column}"] = enum_to_set.name
23
+ else
24
+ self["#{enum_column}"] = enum_class.const_get(enum_to_set).name
25
+ end
22
26
  end
23
27
 
24
28
  define_method "#{enum_name}_changed?" do
@@ -30,7 +34,12 @@ module HazEnum
30
34
  begin
31
35
  enum_class.const_get(self["#{enum_column}"])
32
36
  rescue NameError => e
33
- self.errors.add("#{enum_name}".to_sym, I18n.t("activerecord.errors.messages.enum_value_invalid", :value => self["#{enum_column}"], :name => enum_name))
37
+
38
+ message = I18n.translate("activerecord.errors.models.#{self.class.name.underscore}.attributes.#{enum_name}.enum_value_invalid", :value => self["#{enum_column}"], :name => enum_name) ||
39
+ I18n.translate("activerecord.errors.models.#{self.class.name.underscore}.enum_value_invalid", :value => self["#{enum_column}"], :name => enum_name) ||
40
+ I18n.translate(:'activerecord.errors.messages.enum_value_invalid', :value => self["#{enum_column}"], :name => enum_name)
41
+
42
+ self.errors.add(enum_name.to_sym, message)
34
43
  end
35
44
  end
36
45
  end
data/spec/enum_spec.rb CHANGED
@@ -7,29 +7,33 @@ describe "HazEnum" do
7
7
  before(:all) do
8
8
  setup_db
9
9
  end
10
-
10
+
11
11
  it "should have class method has_enum" do
12
12
  ClassWithEnum.should respond_to(:has_enum)
13
13
  end
14
-
14
+
15
15
  it "should be able to set enum-attribute via hash in initializer" do
16
16
  ClassWithEnum.new(:product => Products::Silver).product.should be(Products::Silver)
17
17
  ClassWithCustomNameEnum.new(:product => Products::Silver).product.should be(Products::Silver)
18
18
  end
19
-
19
+
20
+ it "should be able to set enum-value as string in initializer" do
21
+ ClassWithEnum.new(:product => "Silver").product.should be(Products::Silver)
22
+ end
23
+
20
24
  it "should not be able to set enum-attribute by colum-name via hash in initializer" do
21
25
  ClassWithCustomNameEnum.new(:custom_name => Products::Silver).product.should_not be(Products::Silver)
22
26
  ClassWithCustomNameEnum.new(:custom_name => Products::Silver.name).product.should_not be(Products::Silver)
23
27
  end
24
-
28
+
25
29
  it "should have a enum_value attribute" do
26
30
  ClassWithCustomNameEnum.new(:product => Products::Gold).custom_name.should be(Products::Gold.name)
27
31
  end
28
-
32
+
29
33
  it "colum_name should be configurable" do
30
- ClassWithCustomNameEnum.new(:product => Products::Gold).custom_name.should be(Products::Gold.name)
34
+ ClassWithCustomNameEnum.new(:product => Products::Gold).product.should be(Products::Gold)
31
35
  end
32
-
36
+
33
37
  it "should know if enum-attribute has changed" do
34
38
  product_enum = ClassWithEnum.new(:product => Products::Silver)
35
39
  product_enum.product_changed?.should be(true)
@@ -38,14 +42,14 @@ describe "HazEnum" do
38
42
  product_enum.product = Products::Gold
39
43
  product_enum.product_changed?.should be(true)
40
44
  end
41
-
45
+
42
46
  it "should know if enum-attribute has not really changed" do
43
47
  product_enum = ClassWithEnum.new(:product => Products::Silver)
44
48
  product_enum.save
45
49
  product_enum.product = Products::Silver
46
50
  product_enum.product_changed?.should be(false)
47
51
  end
48
-
52
+
49
53
  it "should validate enum-value" do
50
54
  enum_mixin = ClassWithEnum.new
51
55
  platin = "Platin"
@@ -54,7 +58,7 @@ describe "HazEnum" do
54
58
  enum_mixin.valid?.should_not be(true)
55
59
  enum_mixin.errors[:product].size.should > 0
56
60
  end
57
-
61
+
58
62
  after(:all) do
59
63
  teardown_db
60
64
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - thyphoon