has_enum 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format documentation
3
+
data/Gemfile CHANGED
@@ -1,12 +1,12 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "rails", "~> 3.0.3"
3
+ gem "rails", "~> 3.0.0"
4
4
 
5
5
  # Add dependencies to develop your gem here.
6
6
  # Include everything needed to run rake, tests, features, etc.
7
7
  group :development do
8
8
  gem "rspec", ">= 2.2.0"
9
- gem "rails", "~> 3.0.3"
9
+ gem "rails", "~> 3.0.0"
10
10
  gem "bundler", ">= 1.0.0"
11
11
  gem "jeweler", ">= 1.5.1"
12
12
  gem "sqlite3-ruby", "~> 1.3.2"
data/Gemfile.lock CHANGED
@@ -86,7 +86,7 @@ PLATFORMS
86
86
  DEPENDENCIES
87
87
  bundler (>= 1.0.0)
88
88
  jeweler (>= 1.5.1)
89
- rails (~> 3.0.3)
89
+ rails (~> 3.0.0)
90
90
  rcov
91
91
  rspec (>= 2.2.0)
92
92
  sqlite3-ruby (~> 1.3.2)
data/README.md CHANGED
@@ -18,7 +18,7 @@ in your Rails 3 Gemfile. Then do
18
18
  Here's example model:
19
19
  class TestModel < ActiveRecord::Base
20
20
  has_enum :category, %w( stuff things misc )
21
- has_enum :color , %w( red green blue )
21
+ has_enum :color , %w( red green blue ) , :scopes => true
22
22
  has_enum :size , %w( small medium large ) , :query_methods => false
23
23
  has_enum :status , [:pending, :failed, :done]
24
24
  end
@@ -26,6 +26,10 @@ Query methods are available by default. For example, query methods for color enu
26
26
  color_green?
27
27
  color_red?
28
28
  color_blue?
29
+ If you set *:scopes => true* these scopes will be available:
30
+ color_green
31
+ color_red
32
+ color_blue
29
33
  See [sample usage in specs](https://github.com/openteam/has_enum/blob/master/spec/has_enum_spec.rb).
30
34
 
31
35
  ### Views
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
data/has_enum.gemspec CHANGED
@@ -5,17 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_enum}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andreas Korth", "Konstantin Shabanov"]
12
- s.date = %q{2010-12-23}
12
+ s.date = %q{2011-01-19}
13
13
  s.email = %q{kes.eclipse@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
16
16
  "README.md"
17
17
  ]
18
18
  s.files = [
19
+ ".rspec",
19
20
  "Gemfile",
20
21
  "Gemfile.lock",
21
22
  "LICENSE.txt",
@@ -29,7 +30,7 @@ Gem::Specification.new do |s|
29
30
  "spec/has_enum_spec.rb",
30
31
  "spec/helpers/form_helper_spec.rb",
31
32
  "spec/rcov.opts",
32
- "spec/spec.opts",
33
+ "spec/ru.yml",
33
34
  "spec/spec_helper.rb",
34
35
  "spec/test_model.rb"
35
36
  ]
@@ -50,26 +51,26 @@ Gem::Specification.new do |s|
50
51
  s.specification_version = 3
51
52
 
52
53
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
- s.add_runtime_dependency(%q<rails>, ["~> 3.0.3"])
54
+ s.add_runtime_dependency(%q<rails>, ["~> 3.0.0"])
54
55
  s.add_development_dependency(%q<rspec>, [">= 2.2.0"])
55
- s.add_development_dependency(%q<rails>, ["~> 3.0.3"])
56
+ s.add_development_dependency(%q<rails>, ["~> 3.0.0"])
56
57
  s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
57
58
  s.add_development_dependency(%q<jeweler>, [">= 1.5.1"])
58
59
  s.add_development_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
59
60
  s.add_development_dependency(%q<rcov>, [">= 0"])
60
61
  else
61
- s.add_dependency(%q<rails>, ["~> 3.0.3"])
62
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
62
63
  s.add_dependency(%q<rspec>, [">= 2.2.0"])
63
- s.add_dependency(%q<rails>, ["~> 3.0.3"])
64
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
64
65
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
65
66
  s.add_dependency(%q<jeweler>, [">= 1.5.1"])
66
67
  s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
67
68
  s.add_dependency(%q<rcov>, [">= 0"])
68
69
  end
69
70
  else
70
- s.add_dependency(%q<rails>, ["~> 3.0.3"])
71
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
71
72
  s.add_dependency(%q<rspec>, [">= 2.2.0"])
72
- s.add_dependency(%q<rails>, ["~> 3.0.3"])
73
+ s.add_dependency(%q<rails>, ["~> 3.0.0"])
73
74
  s.add_dependency(%q<bundler>, [">= 1.0.0"])
74
75
  s.add_dependency(%q<jeweler>, [">= 1.5.1"])
75
76
  s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
@@ -12,13 +12,17 @@ module HasEnum
12
12
  end
13
13
 
14
14
  def has_enum(attribute, values, options = {})
15
+ options.assert_valid_keys(:query_methods, :scopes)
15
16
  enum[attribute] = values.freeze
16
17
 
17
- if options[:query_methods] != false
18
- values.each do |val|
19
- define_method(:"#{attribute}_#{val}?") { self.send(attribute) == val }
20
- end
21
- end
18
+ values.each do |val|
19
+ scope :"#{attribute}_#{val}", where(:"#{attribute}" => "#{val}")
20
+ end if options[:scopes]
21
+
22
+ values.each do |val|
23
+ define_method(:"#{attribute}_#{val}?") { self.send(attribute) == val }
24
+ end if options[:query_methods] != false
25
+
22
26
 
23
27
  define_method(:"#{attribute}=") do |value|
24
28
  if value.nil? or values.find{ |val| val == value }
@@ -36,25 +40,28 @@ module HasEnum
36
40
  end
37
41
 
38
42
  define_method "human_#{attribute}" do
39
- begin
40
- return nil unless self.send(attribute)
41
- key = "activerecord.attributes.#{self.class.model_name.i18n_key}.#{attribute}_enum.#{self.send(attribute)}"
42
- translation = I18n.translate(key, :raise => true)
43
- rescue I18n::MissingTranslationData
44
- self.send(attribute).humanize
45
- end
43
+ return nil unless self.send(attribute)
44
+
45
+ klass = self.class
46
+ klass_key = klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore
47
+ defaults = ["activerecord.attributes.#{klass_key}.#{attribute}_enum.#{self.send(attribute)}"]
48
+
49
+ defaults << self.send(attribute).to_s.humanize
50
+ I18n.translate(defaults.shift, :default => defaults, :raise => true)
46
51
  end
47
52
  end
48
53
 
49
54
  def values_for_select_tag(enum)
50
55
  values = enum(enum)
51
56
  begin
52
- translation = I18n.translate("activerecord.attributes.#{self.model_name.i18n_key}.#{enum}_enum", :raise => true)
57
+ klass_key = self.model_name.respond_to?(:i18n_key) ? self.model_name.i18n_key : self.name.underscore
58
+ translation = I18n.translate("activerecord.attributes.#{klass_key}.#{enum}_enum", :raise => true)
59
+
53
60
  values.map { |value| [translation[value.to_sym], value] }
54
61
  rescue I18n::MissingTranslationData
55
- values.map { |value| [value.humanize, value] }
62
+ values.map { |value| [value.to_s.humanize, value] }
56
63
  end
57
64
  end
58
65
  end
59
66
  end
60
- end
67
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
4
 
3
5
  describe HasEnum::ActiveRecord do
@@ -26,6 +28,12 @@ describe HasEnum::ActiveRecord do
26
28
  @model.category = 'objects'
27
29
  @model.errors[:category].size.should eql(1)
28
30
  end
31
+
32
+ it "should define query methods for enum values" do
33
+ %w( stuff things misc ).each do |value|
34
+ @model.should respond_to(:"category_#{value}?")
35
+ end
36
+ end
29
37
  end
30
38
 
31
39
  describe "color enum" do
@@ -49,6 +57,16 @@ describe HasEnum::ActiveRecord do
49
57
  @model.should_not be_color_red
50
58
  @model.should_not be_color_blue
51
59
  end
60
+
61
+ it "should define a scope for each enum value" do
62
+ @model.color = 'red'
63
+ @model.save
64
+ @model2 = TestModel.new(:color => 'red')
65
+ @model2.save
66
+
67
+ TestModel.color_red.all.should eql TestModel.where(:color => 'red').all
68
+ TestModel.color_green.all.should be_empty
69
+ end
52
70
  end
53
71
 
54
72
  describe "size enum" do
@@ -60,10 +78,15 @@ describe HasEnum::ActiveRecord do
60
78
  end
61
79
 
62
80
  it "should not define query methods for enum values" do
63
- %w( small? medium? large? ).each do |method|
64
- @model.should_not respond_to(method)
81
+ %w(small medium large).each do |value|
82
+ @model.should_not respond_to(:"size_#{value}?")
65
83
  end
66
84
  end
85
+
86
+ it "should return humanized translation if not localized" do
87
+ @model.size = 'medium'
88
+ @model.human_size.should eql("Medium")
89
+ end
67
90
  end
68
91
 
69
92
  describe "status enum" do
@@ -81,5 +104,11 @@ describe HasEnum::ActiveRecord do
81
104
  @model.status = nil
82
105
  @model.status.should be_nil
83
106
  end
107
+
108
+ it "should translate values for the enum" do
109
+ I18n.reload!
110
+ @model.status = :pending
111
+ @model.human_status.should eql("На рассмотрении")
112
+ end
84
113
  end
85
114
  end
@@ -71,5 +71,4 @@ describe ActionView::Helpers::FormHelper do
71
71
  html.should include(%(<label for="model_status_done">Erledigt</label>))
72
72
  end
73
73
  end
74
- end
75
-
74
+ end
data/spec/ru.yml ADDED
@@ -0,0 +1,8 @@
1
+ ru:
2
+ activerecord:
3
+ attributes:
4
+ test_model:
5
+ status_enum:
6
+ pending: "На рассмотрении"
7
+ failed: "Обработано с ошибкой"
8
+ done: "Завершено"
data/spec/spec_helper.rb CHANGED
@@ -23,4 +23,7 @@ ActiveRecord::Schema.define(:version => 0) do
23
23
  end
24
24
  end
25
25
 
26
- require File.dirname(__FILE__) + "/test_model" unless defined?(Model)
26
+ I18n.load_path << 'spec/ru.yml'
27
+ I18n.default_locale = :ru
28
+
29
+ require File.dirname(__FILE__) + "/test_model" unless defined?(Model)
data/spec/test_model.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  class TestModel < ActiveRecord::Base
2
2
  has_enum :category, %w( stuff things misc )
3
- has_enum :color , %w( red green blue )
3
+ has_enum :color , %w( red green blue ) , :scopes => true
4
4
  has_enum :size , %w( small medium large ) , :query_methods => false
5
5
  has_enum :status , [:pending, :failed, :done]
6
6
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
7
+ - 5
8
8
  - 0
9
- version: 0.4.0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andreas Korth
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-23 00:00:00 +06:00
18
+ date: 2011-01-19 00:00:00 +06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -28,8 +28,8 @@ dependencies:
28
28
  segments:
29
29
  - 3
30
30
  - 0
31
- - 3
32
- version: 3.0.3
31
+ - 0
32
+ version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: *id001
@@ -58,8 +58,8 @@ dependencies:
58
58
  segments:
59
59
  - 3
60
60
  - 0
61
- - 3
62
- version: 3.0.3
61
+ - 0
62
+ version: 3.0.0
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: *id003
@@ -131,6 +131,7 @@ extra_rdoc_files:
131
131
  - LICENSE.txt
132
132
  - README.md
133
133
  files:
134
+ - .rspec
134
135
  - Gemfile
135
136
  - Gemfile.lock
136
137
  - LICENSE.txt
@@ -144,7 +145,7 @@ files:
144
145
  - spec/has_enum_spec.rb
145
146
  - spec/helpers/form_helper_spec.rb
146
147
  - spec/rcov.opts
147
- - spec/spec.opts
148
+ - spec/ru.yml
148
149
  - spec/spec_helper.rb
149
150
  - spec/test_model.rb
150
151
  has_rdoc: true
@@ -161,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
162
  requirements:
162
163
  - - ">="
163
164
  - !ruby/object:Gem::Version
164
- hash: 3267787670941739246
165
+ hash: -956477031175016980
165
166
  segments:
166
167
  - 0
167
168
  version: "0"
data/spec/spec.opts DELETED
@@ -1,5 +0,0 @@
1
- --colour
2
- --format progress
3
- --format failing_examples:tmp/failed_examples.txt
4
- --loadby mtime
5
- --reverse