has_enum 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -16,28 +16,45 @@ in your Rails 3 Gemfile. Then do
16
16
  ## Usage
17
17
  ### Models
18
18
  Here's example model:
19
- class TestModel < ActiveRecord::Base
20
- has_enum :category, %w( stuff things misc )
21
- has_enum :color , %w( red green blue ) , :scopes => true
22
- has_enum :size , %w( small medium large ) , :query_methods => false
23
- has_enum :status , [:pending, :failed, :done]
24
- end
19
+
20
+ ```ruby
21
+ class TestModel < ActiveRecord::Base
22
+ has_enum :category, %w( stuff things misc )
23
+ has_enum :color , %w( red green blue ) , :scopes => true
24
+ has_enum :size , %w( small medium large ) , :query_methods => false
25
+ has_enum :status , [:pending, :failed, :done]
26
+ end
27
+ ```
28
+
25
29
  Query methods are available by default. For example, query methods for color enum this methods will be available:
30
+
26
31
  color_green?
27
32
  color_red?
28
33
  color_blue?
34
+
29
35
  If you set *:scopes => true* these scopes will be available:
30
- color_green
31
- color_red
32
- color_blue
36
+
37
+ ```ruby
38
+ color_green
39
+ color_red
40
+ color_blue
41
+ ```
42
+
33
43
  See [sample usage in specs](https://github.com/openteam/has_enum/blob/master/spec/has_enum_spec.rb).
34
44
 
35
45
  ### Views
46
+
47
+ ```ruby
36
48
  radio_button_enum(object_name, method, options = {})
37
49
  select_enum(object_name, method, options = {})
50
+ ```
51
+
38
52
  #### Usage with Formtastic (Monkeypatch)
53
+
39
54
  If you are using another gems that inherited from formtastic the most simple method will be to add this code
40
55
  in your *config/initializers/formtastic.rb*:
56
+
57
+ ```ruby
41
58
  class Formtastic::SemanticFormBuilder
42
59
  def enum_input(method, options = {})
43
60
  value = @object.send(method)
@@ -45,7 +62,11 @@ in your *config/initializers/formtastic.rb*:
45
62
  self.input(method, options.merge(additional_params)).gsub(/class="select/, 'class="enum')
46
63
  end
47
64
  end
65
+ ```
66
+
48
67
  Or if you sure you don't use any the proper method is to inherit from it:
68
+
69
+ ```ruby
49
70
  class CustomBuilder < Formtastic::SemanticFormBuilder
50
71
  def enum_input(method, options = {})
51
72
  value = @object.send(method)
@@ -53,13 +74,19 @@ Or if you sure you don't use any the proper method is to inherit from it:
53
74
  self.input(method, options.merge(additional_params)).gsub(/class="select/, 'class="enum')
54
75
  end
55
76
  end
77
+ ```
78
+
56
79
  And uncomment
80
+ ```ruby
57
81
  Formtastic::SemanticFormHelper.builder = CustomBuilder
82
+ ```
58
83
 
59
84
  ## Running Tests
60
85
 
61
86
  Run the tests
87
+
62
88
  rake spec
89
+
63
90
  View specs are still failing. I'm working on it.
64
91
 
65
92
  ## Contributing to has_enum
@@ -31,7 +31,7 @@ module HasEnum::ClassMethods
31
31
 
32
32
  def has_enum(*params)
33
33
  options = params.extract_options!
34
- options.assert_valid_keys(:query_methods, :scopes, :presence, :multiple)
34
+ options.assert_valid_keys(:query_methods, :scopes, :presence, :multiple, :validates)
35
35
 
36
36
  raise ArgumentError, "Empty arguments list for has_enum call" if params.empty?
37
37
 
@@ -54,7 +54,7 @@ module HasEnum::ClassMethods
54
54
  if options[:multiple]
55
55
  serialize attribute, Array
56
56
  else
57
- validates attribute, :inclusion => { :in => values + [nil]}
57
+ validates attribute, :inclusion => { :in => values + [nil]} unless options[:validates] == false
58
58
  end
59
59
 
60
60
  validates attribute, :presence => options[:presence] if options[:presence]
@@ -1,3 +1,3 @@
1
1
  module HasEnum
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
@@ -198,6 +198,11 @@ describe HasEnum do
198
198
  model.should_not respond_to(:"size_#{value}?")
199
199
  end
200
200
  end
201
+
202
+ it "should accept not enum values" do
203
+ model.size = 'stuff'
204
+ model.should be_valid
205
+ end
201
206
  end
202
207
 
203
208
  describe "status enum" do
data/spec/test_model.rb CHANGED
@@ -4,7 +4,7 @@ class TestModel < ActiveRecord::Base
4
4
 
5
5
  has_enum :color, %w[red green blue], :scopes => true
6
6
 
7
- has_enum :size, :query_methods => false
7
+ has_enum :size, :query_methods => false, :validates => false
8
8
 
9
9
  has_enum ["status", :state], [:pending, :failed, :done], :presence => true
10
10
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 1
9
- version: 0.9.1
8
+ - 2
9
+ version: 0.9.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andreas Korth
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-04-20 00:00:00 +07:00
19
+ date: 2011-05-12 00:00:00 +07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency