enum_table 0.3.0 → 0.4.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.
- checksums.yaml +7 -0
- data/.gitignore +2 -1
- data/CHANGELOG +4 -0
- data/Gemfile +1 -0
- data/README.markdown +18 -0
- data/lib/enum_table/railtie.rb +14 -0
- data/lib/enum_table/record.rb +1 -0
- data/lib/enum_table/version.rb +1 -1
- data/lib/enum_table.rb +31 -0
- data/test/enum_table/test_record.rb +12 -1
- data/test/test_helper.rb +8 -3
- metadata +12 -24
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a5ae29ffdc710d5a06627e243e256b7a8b46ae55
|
4
|
+
data.tar.gz: aaa6e9b0c95804dd10760b8b67289f9121d23738
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 47f827c8bbd52a3102c06df809be0ef29031ec1d194ed5a186da9698127eba253a01efad0d10487949829a019a5ce896969c2e973c92f432b3a49faac1ad4e0b
|
7
|
+
data.tar.gz: a0ffcf59c9ae8d1c3330eb18909d04c6462b95fa8c659b3f41a1615c01f95584d323358130448f43c65f74985c9f7f284d0d40233aed07929219cdad6d261b87
|
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -123,6 +123,24 @@ it less disruptive to use strings instead. Do that with the `:type` option:
|
|
123
123
|
|
124
124
|
enum :genders, type: :string
|
125
125
|
|
126
|
+
### I can't set up my Rails app!
|
127
|
+
|
128
|
+
If you load any models during Rails' initialization phase (e.g. from
|
129
|
+
`config/application.rb`, or an initializer), and these models depend on the
|
130
|
+
existence of enum tables, you may wind up with a circular dependency: your app
|
131
|
+
startup depends on an enum table existing, and creating the enum table requires
|
132
|
+
loading the app (via `db:schema:load`). This might only manifest itself when you
|
133
|
+
try to load your schema on an empty database.
|
134
|
+
|
135
|
+
It is strongly recommended to avoid loading models during
|
136
|
+
initialization. However if you cannot avoid this there is an escape hatch:
|
137
|
+
|
138
|
+
config.enum_tables.allow_missing_tables = true
|
139
|
+
|
140
|
+
This will silently return an empty enum mapping while this flag is true. Of
|
141
|
+
course, this may have knock on effects if your model expects certain values to
|
142
|
+
be present. Consider yourself warned!
|
143
|
+
|
126
144
|
## Contributing
|
127
145
|
|
128
146
|
* [Bug reports](https://github.com/howaboutwe/enum_table/issues)
|
data/lib/enum_table/railtie.rb
CHANGED
@@ -1,13 +1,27 @@
|
|
1
1
|
module EnumTable
|
2
2
|
class Railtie < Rails::Railtie
|
3
|
+
config.enum_table = ActiveSupport::OrderedOptions.new
|
4
|
+
|
3
5
|
rake_tasks do
|
4
6
|
namespace :enum_table do
|
5
7
|
task :load_schema_dumper do
|
6
8
|
require 'enum_table/schema_dumper'
|
7
9
|
end
|
10
|
+
|
11
|
+
task :allow_missing_tables do
|
12
|
+
EnumTable.missing_tables_allowed
|
13
|
+
end
|
8
14
|
end
|
9
15
|
|
10
16
|
Rake::Task['db:schema:dump'].prerequisites << 'enum_table:load_schema_dumper'
|
17
|
+
|
18
|
+
if config.enum_table.allow_missing_tables
|
19
|
+
%w'db:schema:load db:migrate db:migrate:up'.each do |task|
|
20
|
+
task = Rake::Task[task]
|
21
|
+
task.prerequisites.insert 0, 'enum_table:allow_missing_tables'
|
22
|
+
task.enhance { EnumTable.missing_tables_disallowed }
|
23
|
+
end
|
24
|
+
end
|
11
25
|
end
|
12
26
|
end
|
13
27
|
end
|
data/lib/enum_table/record.rb
CHANGED
@@ -62,6 +62,7 @@ module EnumTable
|
|
62
62
|
else
|
63
63
|
map = {}
|
64
64
|
table_name = table || "#{self.table_name.singularize}_#{name.to_s.pluralize}"
|
65
|
+
return {} if EnumTable.missing_tables_allowed? && !connection.tables.include?(table_name)
|
65
66
|
connection.execute("SELECT id, value FROM #{connection.quote_table_name table_name}").each do |row|
|
66
67
|
map[row[1]] = row[0]
|
67
68
|
end
|
data/lib/enum_table/version.rb
CHANGED
data/lib/enum_table.rb
CHANGED
@@ -4,6 +4,37 @@ module EnumTable
|
|
4
4
|
autoload :Reflection, 'enum_table/reflection'
|
5
5
|
autoload :SchemaDumper, 'enum_table/schema_dumper'
|
6
6
|
autoload :SchemaStatements, 'enum_table/schema_statements'
|
7
|
+
|
8
|
+
# Allow enum tables to be missing until missing_tables_disallowed is
|
9
|
+
# called. This is invoked from rake tasks that create the enum tables, such as
|
10
|
+
# Rails migration tasks.
|
11
|
+
#
|
12
|
+
# Missing table allowance is implemented as a thread-local stack to handle
|
13
|
+
# nested invocations in multi-threaded programs.
|
14
|
+
class << self
|
15
|
+
def missing_tables_allowed
|
16
|
+
missing_tables_allowances.push true
|
17
|
+
end
|
18
|
+
|
19
|
+
def missing_tables_disallowed
|
20
|
+
missing_tables_allowances.pop
|
21
|
+
end
|
22
|
+
|
23
|
+
def missing_tables_allowed?
|
24
|
+
!missing_tables_allowances.empty?
|
25
|
+
end
|
26
|
+
|
27
|
+
# Reset our state. Intended for testing Enum Table.
|
28
|
+
def reset
|
29
|
+
Thread.current[:enum_table_missing_tables_allowed] = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def missing_tables_allowances
|
35
|
+
Thread.current[:enum_table_missing_tables_allowed] ||= []
|
36
|
+
end
|
37
|
+
end
|
7
38
|
end
|
8
39
|
|
9
40
|
require 'enum_table/railtie' if defined?(Rails)
|
@@ -9,9 +9,9 @@ describe EnumTable do
|
|
9
9
|
t.integer :status_id
|
10
10
|
t.string :user_type
|
11
11
|
end
|
12
|
+
Object.const_set :User, Class.new(ActiveRecord::Base)
|
12
13
|
end
|
13
14
|
|
14
|
-
before { Object.const_set :User, Class.new(ActiveRecord::Base) }
|
15
15
|
after { Object.send :remove_const, :User }
|
16
16
|
|
17
17
|
describe '.enum' do
|
@@ -90,6 +90,17 @@ describe EnumTable do
|
|
90
90
|
exception.must_be_kind_of StandardError
|
91
91
|
end
|
92
92
|
|
93
|
+
describe "when missing tables are allowed" do
|
94
|
+
before { EnumTable.missing_tables_allowed }
|
95
|
+
after { EnumTable.reset }
|
96
|
+
|
97
|
+
it "does not cry when you first use the enum if the table does not exist" do
|
98
|
+
User.enum(:status)
|
99
|
+
User.enum_id(:status, 'awesome').must_equal nil
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
|
93
104
|
describe "on a subclass" do
|
94
105
|
before do
|
95
106
|
User.inheritance_column = :user_type
|
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
ROOT = File.expand_path('..', File.dirname(__FILE__))
|
2
2
|
$:.unshift "#{ROOT}/lib"
|
3
3
|
|
4
|
-
require 'minitest/
|
4
|
+
require 'minitest/autorun'
|
5
5
|
require 'active_record'
|
6
6
|
require 'enum_table'
|
7
7
|
|
@@ -47,7 +47,12 @@ MiniTest::Spec.class_eval do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def self.use_database
|
50
|
-
|
51
|
-
|
50
|
+
mod = Module.new do
|
51
|
+
extend Minitest::Spec::DSL
|
52
|
+
|
53
|
+
before { recreate_database }
|
54
|
+
after { drop_database }
|
55
|
+
end
|
56
|
+
include mod
|
52
57
|
end
|
53
58
|
end
|
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.4.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- George Ogata
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-02-10 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 3.2.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 3.2.0
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: ritual
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: 0.4.1
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: 0.4.1
|
46
41
|
description:
|
@@ -50,7 +45,7 @@ executables: []
|
|
50
45
|
extensions: []
|
51
46
|
extra_rdoc_files: []
|
52
47
|
files:
|
53
|
-
- .gitignore
|
48
|
+
- ".gitignore"
|
54
49
|
- CHANGELOG
|
55
50
|
- Gemfile
|
56
51
|
- LICENSE
|
@@ -73,33 +68,26 @@ files:
|
|
73
68
|
homepage: http://github.com/howaboutwe/enum_table
|
74
69
|
licenses:
|
75
70
|
- MIT
|
71
|
+
metadata: {}
|
76
72
|
post_install_message:
|
77
73
|
rdoc_options: []
|
78
74
|
require_paths:
|
79
75
|
- lib
|
80
76
|
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
77
|
requirements:
|
83
|
-
- -
|
78
|
+
- - ">="
|
84
79
|
- !ruby/object:Gem::Version
|
85
80
|
version: '0'
|
86
|
-
segments:
|
87
|
-
- 0
|
88
|
-
hash: 932469567180106626
|
89
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
82
|
requirements:
|
92
|
-
- -
|
83
|
+
- - ">="
|
93
84
|
- !ruby/object:Gem::Version
|
94
85
|
version: '0'
|
95
|
-
segments:
|
96
|
-
- 0
|
97
|
-
hash: 932469567180106626
|
98
86
|
requirements: []
|
99
87
|
rubyforge_project:
|
100
|
-
rubygems_version:
|
88
|
+
rubygems_version: 2.2.0
|
101
89
|
signing_key:
|
102
|
-
specification_version:
|
90
|
+
specification_version: 4
|
103
91
|
summary: Enumeration tables for ActiveRecord
|
104
92
|
test_files:
|
105
93
|
- test/database.yml
|