enumerate_it 0.5.0 → 0.6.0

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.rdoc CHANGED
@@ -40,7 +40,7 @@ Enter EnumerateIt.
40
40
  == Creating enumerations
41
41
 
42
42
  Enumerations are created as models, but you can put then anywhere in your application. In Rails
43
- applications, I put them inside models/.
43
+ applications, you can put them inside models/.
44
44
 
45
45
  class RelationshipStatus < EnumerateIt::Base
46
46
  associate_values(
@@ -126,6 +126,17 @@ This will create:
126
126
  p.valid? # => false
127
127
  p.errors[:relationship_status] # => "is not included in the list"
128
128
 
129
+ * If your class can manage validations and responds to :validates_presence_of, you can pass the :required options as true and this validation will be created for you (this option defaults to false):
130
+
131
+ class Person < ActiveRecord::Base
132
+ has_enumeration_for :relationship_status, :required => true
133
+ end
134
+
135
+ p = Person.new :relationship_status => nil
136
+ p.valid? # => false
137
+ p.errors[:relationship_status] # => "can't be blank"
138
+
139
+
129
140
  Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
130
141
  ActiveRecord::Base.
131
142
 
@@ -170,6 +181,14 @@ located on enumerations.'enumeration_name'.'key' :
170
181
 
171
182
  * Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler)
172
183
 
184
+ An interesting approach to use it in Rails apps is to create an app/models/enumerations folder and add it to your autoload path in config/application.rb:
185
+
186
+ module YourApp
187
+ class Application < Rails::Application
188
+ config.autoload_paths << "#{Rails.root}/app/models/enumerations"
189
+ end
190
+ end
191
+
173
192
  == Ruby 1.9
174
193
 
175
194
  EnumerateIt is fully compatible with Ruby 1.9.1 (all tests pass)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
data/enumerate_it.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{enumerate_it}
8
- s.version = "0.5.0"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["C\303\241ssio Marques"]
12
- s.date = %q{2010-04-14}
12
+ s.date = %q{2010-10-20}
13
13
  s.description = %q{Have a legacy database and need some enumerations in your models to match those stupid '4 rows/2 columns' tables with foreign keys and stop doing joins just to fetch a simple description? Or maybe use some integers instead of strings as the code for each value of your enumerations? Here's EnumerateIt.}
14
14
  s.email = %q{cassiommc@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
34
34
  s.homepage = %q{http://github.com/cassiomarques/enumerate_it}
35
35
  s.rdoc_options = ["--charset=UTF-8"]
36
36
  s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.3.6}
37
+ s.rubygems_version = %q{1.3.7}
38
38
  s.summary = %q{Ruby Enumerations}
39
39
  s.test_files = [
40
40
  "spec/enumerate_it_spec.rb",
@@ -45,7 +45,7 @@ Gem::Specification.new do |s|
45
45
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
46
  s.specification_version = 3
47
47
 
48
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
49
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
50
  else
51
51
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
data/lib/enumerate_it.rb CHANGED
@@ -130,6 +130,13 @@
130
130
  # p.valid? # => false
131
131
  # p.errors[:relationship_status] # => "is not included in the list"
132
132
  #
133
+ # - Also, if your class responds to :validates_presence_of, you can pass an :required option and this validation
134
+ # will be added to your attribute:
135
+ #
136
+ # class Person < ActiveRecord::Base
137
+ # has_enumeration_for :relationship_status, :required => true # => defaults to false
138
+ # end
139
+ #
133
140
  # Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from
134
141
  # ActiveRecord::Base.
135
142
  #
@@ -206,9 +213,7 @@ module EnumerateIt
206
213
  module ClassMethods
207
214
  def has_enumeration_for(attribute, options = {})
208
215
  define_enumeration_class attribute, options
209
- if self.respond_to? :validates_inclusion_of
210
- validates_inclusion_of attribute, :in => options[:with].list, :allow_blank => true
211
- end
216
+ set_validations attribute, options
212
217
  create_enumeration_humanize_method options[:with], attribute
213
218
  if options[:create_helpers]
214
219
  create_helper_methods options[:with], attribute
@@ -241,6 +246,11 @@ module EnumerateIt
241
246
  options[:with] = attribute.to_s.camelize.constantize
242
247
  end
243
248
  end
249
+
250
+ def set_validations(attribute, options)
251
+ validates_inclusion_of(attribute, :in => options[:with].list, :allow_blank => true) if self.respond_to?(:validates_inclusion_of)
252
+ validates_presence_of(attribute) if options[:required] and self.respond_to?(:validates_presence_of)
253
+ end
244
254
  end
245
255
 
246
256
  def self.included(receiver)
@@ -90,7 +90,7 @@ describe EnumerateIt do
90
90
  end
91
91
  end
92
92
 
93
- context "using the option :create_helpers option" do
93
+ context "using the :create_helpers option" do
94
94
  before :each do
95
95
  class TestClass
96
96
  has_enumeration_for :foobar, :with => TestEnumeration, :create_helpers => true
@@ -137,8 +137,15 @@ describe EnumerateIt do
137
137
 
138
138
  context "when included in ActiveRecord::Base" do
139
139
  before :each do
140
- class ActiveRecordStub; attr_accessor :bla; end
141
- ActiveRecordStub.stub!(:respond_to?).with(:validates_inclusion_of).and_return(true)
140
+ class ActiveRecordStub
141
+ attr_accessor :bla
142
+
143
+ class << self
144
+ def validates_inclusion_of(options); true; end
145
+ def validates_presence_of; true; end
146
+ end
147
+ end
148
+
142
149
  ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
143
150
  ActiveRecordStub.send :include, EnumerateIt
144
151
  end
@@ -149,6 +156,26 @@ describe EnumerateIt do
149
156
  has_enumeration_for :bla, :with => TestEnumeration
150
157
  end
151
158
  end
159
+
160
+ context "using the :required option" do
161
+ before :each do
162
+ ActiveRecordStub.stub!(:validates_presence_of).and_return(true)
163
+ end
164
+
165
+ it "creates a validation for presence" do
166
+ ActiveRecordStub.should_receive(:validates_presence_of)
167
+ class ActiveRecordStub
168
+ has_enumeration_for :bla, :with => TestEnumeration, :required => true
169
+ end
170
+ end
171
+
172
+ it "do not require the attribute by default" do
173
+ ActiveRecordStub.should_not_receive(:validates_presence_of)
174
+ class ActiveRecordStub
175
+ has_enumeration_for :bla, :with => TestEnumeration
176
+ end
177
+ end
178
+ end
152
179
  end
153
180
  end
154
181
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 7
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 5
8
+ - 6
8
9
  - 0
9
- version: 0.5.0
10
+ version: 0.6.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - "C\xC3\xA1ssio Marques"
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-04-14 00:00:00 -03:00
18
+ date: 2010-10-20 00:00:00 -02:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rspec
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 13
27
30
  segments:
28
31
  - 1
29
32
  - 2
@@ -64,23 +67,27 @@ rdoc_options:
64
67
  require_paths:
65
68
  - lib
66
69
  required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
67
71
  requirements:
68
72
  - - ">="
69
73
  - !ruby/object:Gem::Version
74
+ hash: 3
70
75
  segments:
71
76
  - 0
72
77
  version: "0"
73
78
  required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
74
80
  requirements:
75
81
  - - ">="
76
82
  - !ruby/object:Gem::Version
83
+ hash: 3
77
84
  segments:
78
85
  - 0
79
86
  version: "0"
80
87
  requirements: []
81
88
 
82
89
  rubyforge_project:
83
- rubygems_version: 1.3.6
90
+ rubygems_version: 1.3.7
84
91
  signing_key:
85
92
  specification_version: 3
86
93
  summary: Ruby Enumerations