enumerate_it 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumerate_it (0.7.4)
4
+ enumerate_it (0.7.5)
5
+ activesupport (>= 2.3.2)
5
6
 
6
7
  GEM
7
8
  remote: http://rubygems.org/
@@ -135,6 +135,26 @@ This will create:
135
135
  p.married? #=> true
136
136
  p.divorced? #=> false
137
137
 
138
+ * If you pass the :create_scopes option as 'true', it will create a scope method for each enumeration option (this option defaults to false):
139
+
140
+ class Person < ActiveRecord::Base
141
+ has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_scopes => true
142
+ end
143
+
144
+ Person.married.to_sql # => SELECT "people".* FROM "people" WHERE "people"."relationship_status" = 1
145
+
146
+ NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
147
+
148
+ * If you pass the :create_scopes option as 'true', it will create a scope method for each enumeration option (this option defaults to false):
149
+
150
+ class Person < ActiveRecord::Base
151
+ has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_scopes => true
152
+ end
153
+
154
+ Person.married.to_sql # => SELECT "people".* FROM "people" WHERE "people"."relationship_status" = 1
155
+
156
+ NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
157
+
138
158
  * If your class can manage validations and responds to :validates_inclusion_of, it will create this validation:
139
159
 
140
160
  class Person < ActiveRecord::Base
@@ -234,8 +234,12 @@ module EnumerateIt
234
234
  create_enumeration_humanize_method options[:with], attribute
235
235
  store_enumeration options[:with], attribute
236
236
  if options[:create_helpers]
237
- create_helper_methods(options[:with], attribute)
238
- create_mutator_methods(options[:with], attribute)
237
+ create_helper_methods options[:with], attribute
238
+ create_mutator_methods options[:with], attribute
239
+ end
240
+
241
+ if options[:create_scopes]
242
+ create_scopes options[:with], attribute
239
243
  end
240
244
  end
241
245
 
@@ -269,6 +273,18 @@ module EnumerateIt
269
273
  end
270
274
  end
271
275
 
276
+ def create_scopes(klass, attribute_name)
277
+ klass.enumeration.keys.each do |option|
278
+ if defined?(Rails)
279
+ if Rails.version >= "3.0.0"
280
+ scope option, where(attribute_name => klass.enumeration[option].first)
281
+ else
282
+ raise StandardError, "EnumerateIt cannot create scopes if Rails.version < 3.0.0"
283
+ end
284
+ end
285
+ end
286
+ end
287
+
272
288
  def create_mutator_methods(klass, attribute_name)
273
289
  class_eval do
274
290
  klass.enumeration.each_pair do |key, values|
@@ -1,4 +1,4 @@
1
1
  module EnumerateIt
2
- VERSION = "0.7.4"
2
+ VERSION = "0.7.5"
3
3
  end
4
4
 
@@ -130,6 +130,58 @@ describe EnumerateIt do
130
130
  end
131
131
  end
132
132
 
133
+ describe "using the :create_scopes option" do
134
+ def setup_enumeration
135
+ TestClass.send(:has_enumeration_for, :foobar, :with => TestEnumeration, :create_scopes => true)
136
+ end
137
+
138
+ context "when using Rails" do
139
+ before { module Rails; end }
140
+
141
+ context "with version >= 3.0.0" do
142
+ before do
143
+ module Rails
144
+ def self.version; "3.0.0"; end
145
+ end
146
+
147
+ class TestClass
148
+ def self.where(whatever); end
149
+ def self.scope(name, whatever); end
150
+ end
151
+
152
+ setup_enumeration
153
+ end
154
+
155
+ it "creates a scope for each enumeration value" do
156
+ TestEnumeration.enumeration do |symbol, pair|
157
+ TestClass.should respond_to(symbol)
158
+ end
159
+ end
160
+
161
+ it "when called, the scopes create the correct query" do
162
+ TestEnumeration.enumeration do |symbol, pair|
163
+ TestClass.should_receive(:where).with(:foobar => pair.firs)
164
+ TestClass.send symbol
165
+ end
166
+ end
167
+ end
168
+
169
+ context "with version < 3.0.0" do
170
+ before :each do
171
+ module Rails
172
+ def self.version; "2.3.9"; end
173
+ end
174
+ end
175
+
176
+ it "raises an error telling that scopes creation are ony supported for Rails >= 3.0.0" do
177
+ expect {
178
+ setup_enumeration
179
+ }.to raise_error("EnumerateIt cannot create scopes if Rails.version < 3.0.0")
180
+ end
181
+ end
182
+ end
183
+ end
184
+
133
185
  describe EnumerateIt::Base do
134
186
  it "creates constants for each enumeration value" do
135
187
  [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.7.4
5
+ version: 0.7.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - "C\xC3\xA1ssio Marques"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-11 00:00:00 -03:00
13
+ date: 2011-04-24 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -36,15 +36,15 @@ dependencies:
36
36
  type: :development
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
39
- name: activerecord
39
+ name: activesupport
40
40
  prerelease: false
41
41
  requirement: &id003 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
- - - "="
44
+ - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 3.0.5
47
- type: :development
46
+ version: 2.3.2
47
+ type: :runtime
48
48
  version_requirements: *id003
49
49
  description: 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.
50
50
  email: cassiommc@gmail.com
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  requirements: []
96
96
 
97
97
  rubyforge_project:
98
- rubygems_version: 1.5.2
98
+ rubygems_version: 1.6.1
99
99
  signing_key:
100
100
  specification_version: 3
101
101
  summary: Ruby Enumerations