enum_table 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5ae29ffdc710d5a06627e243e256b7a8b46ae55
4
- data.tar.gz: aaa6e9b0c95804dd10760b8b67289f9121d23738
3
+ metadata.gz: 9efb4a2edbe205dac4da12d14820d33a78ddb91f
4
+ data.tar.gz: 8f4aeb6cbc9ae4455bd2efaefbc7e0a16342d31a
5
5
  SHA512:
6
- metadata.gz: 47f827c8bbd52a3102c06df809be0ef29031ec1d194ed5a186da9698127eba253a01efad0d10487949829a019a5ce896969c2e973c92f432b3a49faac1ad4e0b
7
- data.tar.gz: a0ffcf59c9ae8d1c3330eb18909d04c6462b95fa8c659b3f41a1615c01f95584d323358130448f43c65f74985c9f7f284d0d40233aed07929219cdad6d261b87
6
+ metadata.gz: 678a59b0aedd78bcd0ef41b501bf9dfb7e093fee99e348c607353cb812f93ee5d36bcf70827e3b8f291b3eee65fa463c846bbc486e6007c09d19396097eaaa57
7
+ data.tar.gz: 18c93a939a2a712412133779b92a4f39a4154e8c1b9646d41c9da2087c8aac9d824a778cf090b417f64214f98d96c683b6253d18cbdc599e05918f9e6bf20c13
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ bundler_args: --without dev
3
+ script: bundle exec testrb test
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1.0
7
+ gemfile:
8
+ - Gemfile
9
+ - Gemfile.ar3
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.5.0 2014-03-26
2
+
3
+ * Rails 4 support.
4
+
1
5
  == 0.4.0 2014-02-10
2
6
 
3
7
  * Allow missing tables when migrating. [Kenneth Lay]
data/Gemfile CHANGED
@@ -1,6 +1,13 @@
1
1
  source 'https://rubygems.org'
2
+ gem 'activerecord'
2
3
  gem 'sqlite3'
3
4
  gem 'mysql2'
4
5
  gem 'pg'
5
6
  gem 'minitest', '~> 4.0'
6
7
  gemspec
8
+
9
+ group :dev do
10
+ gem 'ruby-debug-base', platform: :jruby
11
+ gem 'debugger', platform: :mri_19
12
+ gem 'byebug', platform: [:mri_20, :mri_21]
13
+ end
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+ gem 'activerecord', '~> 3'
3
+ gem 'sqlite3'
4
+ gem 'mysql2'
5
+ gem 'pg'
6
+ gem 'minitest', '~> 4.0'
7
+ gemspec
8
+
9
+ group :dev do
10
+ gem 'ruby-debug-base', platform: :jruby
11
+ gem 'debugger', platform: :mri_19
12
+ gem 'byebug', platform: [:mri_20, :mri_21]
13
+ end
@@ -1,4 +1,4 @@
1
- ## Enum Table
1
+ ## Enum Table [![Build Status](https://travis-ci.org/howaboutwe/enum_table.svg)](https://travis-ci.org/howaboutwe/enum_table)
2
2
 
3
3
  Table-based enumerations for ActiveRecord.
4
4
 
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  $:.unshift File.expand_path('lib', File.dirname(__FILE__))
3
2
  require 'enum_table/version'
4
3
 
@@ -16,6 +15,6 @@ Gem::Specification.new do |gem|
16
15
  gem.files = `git ls-files`.split("\n")
17
16
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
17
 
19
- gem.add_runtime_dependency 'activerecord', '~> 3.2.0'
18
+ gem.add_runtime_dependency 'activerecord', '>= 3', '< 5'
20
19
  gem.add_development_dependency 'ritual', '~> 0.4.1'
21
20
  end
@@ -106,7 +106,7 @@ module EnumTable
106
106
  (reflection = inheritance_enum) ? super(reflection.value(type_name).to_s) : super
107
107
  end
108
108
 
109
- # Enables .find_by_name(value) for enums.
109
+ # Enables .where(name: value) for enums.
110
110
  def expand_hash_conditions_for_aggregates(attrs) # :nodoc:
111
111
  conditions = super
112
112
  enums.each do |name, reflection|
@@ -127,14 +127,16 @@ module EnumTable
127
127
  conditions
128
128
  end
129
129
 
130
- # Enables .where(name: value) for enums.
131
- def expand_attribute_names_for_aggregates(attribute_names) # :nodoc:
132
- attribute_names = super
133
- enums.each do |name, reflection|
134
- index = attribute_names.index(name) and
135
- attribute_names[index] = reflection.id_name
130
+ if ActiveRecord::VERSION::MAJOR < 4
131
+ # Enables .find_by_name(value) for enums.
132
+ def expand_attribute_names_for_aggregates(attribute_names) # :nodoc:
133
+ attribute_names = super
134
+ enums.each do |name, reflection|
135
+ index = attribute_names.index(name) and
136
+ attribute_names[index] = reflection.id_name
137
+ end
138
+ attribute_names
136
139
  end
137
- attribute_names
138
140
  end
139
141
 
140
142
  # Enables state_machine to set initial values for states. Ick.
@@ -1,5 +1,5 @@
1
1
  module EnumTable
2
- VERSION = [0, 4, 0]
2
+ VERSION = [0, 5, 0]
3
3
 
4
4
  class << VERSION
5
5
  include Comparable
@@ -8,6 +8,7 @@ describe EnumTable do
8
8
  t.integer :gender_id
9
9
  t.integer :status_id
10
10
  t.string :user_type
11
+ t.integer :lock_version
11
12
  end
12
13
  Object.const_set :User, Class.new(ActiveRecord::Base)
13
14
  end
@@ -186,9 +187,13 @@ describe EnumTable do
186
187
  attributes.must_equal('gender_id' => 1)
187
188
  end
188
189
 
189
- it "does not prevent optimistic locking from working, which also uses this internal method" do
190
- attributes = User.initialize_attributes('lock_version' => nil)
191
- attributes.must_equal('lock_version' => 0)
190
+ it "does not prevent optimistic locking from working, which also uses this internal method in AR < 4" do
191
+ user = User.create(gender: :male)
192
+ user.lock_version.must_equal 0
193
+
194
+ user = User.find(user.id)
195
+ user.update_attributes(gender: :female)
196
+ user.lock_version.must_equal 1
192
197
  end
193
198
 
194
199
  it "should favor the ID if both the id and value are present in the attributes hash (so enums override columns)" do
@@ -513,14 +518,14 @@ describe EnumTable do
513
518
  User.enum :gender, table: {female: 1, male: 2}
514
519
  female = User.create(gender_id: 1)
515
520
  male = User.create(gender_id: 2)
516
- User.where(gender: :female).all.must_equal [female]
521
+ User.where(gender: :female).to_a.must_equal [female]
517
522
  end
518
523
 
519
524
  it "supports filtering by enums with string keys" do
520
525
  User.enum :gender, table: {female: 1, male: 2}
521
526
  female = User.create(gender_id: 1)
522
527
  male = User.create(gender_id: 2)
523
- User.where('gender' => :female).all.must_equal [female]
528
+ User.where('gender' => :female).to_a.must_equal [female]
524
529
  end
525
530
 
526
531
  it "supports filtering by multiple values" do
@@ -528,7 +533,7 @@ describe EnumTable do
528
533
  female = User.create(gender_id: 1)
529
534
  male = User.create(gender_id: 2)
530
535
  other = User.create(gender_id: 3)
531
- User.where(gender: [:female, :male]).all.must_equal [female, male]
536
+ User.where(gender: [:female, :male]).to_a.must_equal [female, male]
532
537
  end
533
538
 
534
539
  it "still supports filtering by other attributes" do
@@ -536,18 +541,41 @@ describe EnumTable do
536
541
  female1 = User.create(gender_id: 1, status_id: 1)
537
542
  male1 = User.create(gender_id: 2, status_id: 1)
538
543
  male2 = User.create(gender_id: 2, status_id: 2)
539
- User.where(gender: :male, status_id: 1).all.must_equal [male1]
544
+ User.where(gender: :male, status_id: 1).to_a.must_equal [male1]
540
545
  end
541
546
  end
542
547
 
543
- describe "dynamic finders" do
544
- it "support retrieval by enums" do
545
- User.enum :gender, table: {female: 1, male: 2}
546
- female = User.create(gender_id: 1)
547
- male = User.create(gender_id: 2)
548
+ if ActiveRecord::VERSION::MAJOR < 4
549
+ describe "dynamic finders" do
550
+ it "support retrieval by enums" do
551
+ User.enum :gender, table: {female: 1, male: 2}
552
+ female = User.create(gender_id: 1)
553
+ male = User.create(gender_id: 2)
548
554
 
549
- User.find_by_gender(:female).must_equal female
550
- User.find_all_by_gender(:female).must_equal [female]
555
+ User.find_by_gender(:female).must_equal female
556
+ User.find_all_by_gender(:female).must_equal [female]
557
+ end
558
+ end
559
+ else
560
+ describe ".find_or_initialize_by" do
561
+ it "supports building with enums" do
562
+ User.enum :gender, table: {female: 1, male: 2}
563
+
564
+ user = User.find_or_initialize_by(gender: :female)
565
+ user.gender_id.must_equal 1
566
+ user.gender.must_equal :female
567
+ user.save!
568
+ end
569
+
570
+ it "supports finding by enums" do
571
+ female = User.create(gender_id: 1)
572
+ male = User.create(gender_id: 2)
573
+
574
+ User.enum :gender, table: {female: 1, male: 2}
575
+
576
+ user = User.find_or_initialize_by(gender: :female)
577
+ user.must_equal female
578
+ end
551
579
  end
552
580
  end
553
581
 
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Ogata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-10 00:00:00.000000000 Z
11
+ date: 2014-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ - - "<"
18
21
  - !ruby/object:Gem::Version
19
- version: 3.2.0
22
+ version: '5'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - - "<"
25
31
  - !ruby/object:Gem::Version
26
- version: 3.2.0
32
+ version: '5'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: ritual
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -46,8 +52,10 @@ extensions: []
46
52
  extra_rdoc_files: []
47
53
  files:
48
54
  - ".gitignore"
55
+ - ".travis.yml"
49
56
  - CHANGELOG
50
57
  - Gemfile
58
+ - Gemfile.ar3
51
59
  - LICENSE
52
60
  - README.markdown
53
61
  - Rakefile
@@ -85,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
93
  version: '0'
86
94
  requirements: []
87
95
  rubyforge_project:
88
- rubygems_version: 2.2.0
96
+ rubygems_version: 2.2.2
89
97
  signing_key:
90
98
  specification_version: 4
91
99
  summary: Enumeration tables for ActiveRecord