rails_string_enum 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -12
- data/lib/rails_string_enum.rb +12 -8
- data/lib/rails_string_enum/{string_enum.rb → active_record_string_enum.rb} +2 -2
- data/lib/rails_string_enum/mixin_string_enum.rb +27 -0
- data/lib/rails_string_enum/pg_enum_migration.rb +20 -22
- data/lib/rails_string_enum/version.rb +2 -2
- data/rails_string_enum.gemspec +1 -4
- metadata +5 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a89d7aa241c25ea1f29072f16f75d0fe4a36897
|
4
|
+
data.tar.gz: de151b0233565f9d35b2f6794660df13c681a25a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c85604c44e26365ecdf8742fbb68666529cd20c800864f98eeff60e7741e0425fa5747827a811c89747472ae724815df09f174284a864a9025512e8c911c0fe
|
7
|
+
data.tar.gz: 542eac4498a6ad9661d996831d295f4736843cbf8c840a0940769fdc1d39d0d9e7720707513be4133b43038f36867fa3713640896764ecc7f4f504e4f0d46c7b
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
## rails_string_enum
|
2
|
-
support in rails
|
2
|
+
support in rails native postgresql enums or string (using as flexible enum)
|
3
3
|
|
4
4
|
This gem inspired rails native enum and https://github.com/zmbacker/enum_help
|
5
5
|
## Installation
|
@@ -11,11 +11,7 @@ Add this line to your application's Gemfile:
|
|
11
11
|
|
12
12
|
Tested on Rails 4.2, ruby 2.2
|
13
13
|
|
14
|
-
#### Broken changes from 0.1 to 0.2
|
15
|
-
`Product::COLOR::GREEN -> Product::GREEN`
|
16
14
|
|
17
|
-
#### Broken changes from 0.2 to 0.3
|
18
|
-
`added prefix for scopes: Product.red -> Product.only_red or Product.only_reds`
|
19
15
|
|
20
16
|
#### Native postgresql enum (migrations)
|
21
17
|
```ruby
|
@@ -28,18 +24,20 @@ add_enum_value :color, 'black', after: 'red'
|
|
28
24
|
add_enum_value :color, 'black', before: 'blue'
|
29
25
|
add_enum_value :color, 'black', schema: 'public'
|
30
26
|
|
27
|
+
# add_enum_value cannot run inside a transaction block, using outside change method
|
28
|
+
|
31
29
|
drop_enum :color
|
32
30
|
drop_enum :color, schema: 'cmyk'
|
33
31
|
|
34
32
|
rename_enum_value :color, 'gray', 'black'
|
35
|
-
reorder_enum_values :
|
33
|
+
reorder_enum_values :color, %w(black white) # other color will be latest
|
36
34
|
|
37
35
|
delete_enum_value :color, 'black'
|
38
36
|
# you should delete record with deleting value
|
39
37
|
# if exists index with condition, method raise exception
|
40
|
-
# "ERROR: operator does not exist:
|
38
|
+
# "ERROR: operator does not exist: color <> color_new"
|
41
39
|
# you should first remove and then create an index
|
42
|
-
Product.where(color: 'black').delete_all # or Product.where(color: 'black').update_all(
|
40
|
+
Product.where(color: 'black').delete_all # or Product.where(color: 'black').update_all(color: nil)
|
43
41
|
remove_index :product, :color, where: "color NOT IN ('pink')"
|
44
42
|
delete_enum_value :color, 'black'
|
45
43
|
add_index :product, :color, where: "color NOT IN ('pink')"
|
@@ -61,9 +59,9 @@ class CreateTables < ActiveRecord::Migration
|
|
61
59
|
create_table :products do |t|
|
62
60
|
t.string :color
|
63
61
|
end
|
64
|
-
|
62
|
+
|
65
63
|
create_enum :enum_color, %w(red green blue)
|
66
|
-
|
64
|
+
|
67
65
|
create_table :pages do |t|
|
68
66
|
t.column :color, :enum_color, default: 'red'
|
69
67
|
end
|
@@ -72,7 +70,7 @@ end
|
|
72
70
|
|
73
71
|
class Product < ActiveRecord::Base
|
74
72
|
string_enum :color, %w(red green yellow black white), scopes: true # default false
|
75
|
-
|
73
|
+
|
76
74
|
def self.colored
|
77
75
|
where.not(color: [BLACK, WHITE])
|
78
76
|
end
|
@@ -101,6 +99,15 @@ Product.only_red # if scopes: true
|
|
101
99
|
Product.only_reds # if scopes: { pluralize: true }
|
102
100
|
```
|
103
101
|
|
102
|
+
Using constants to any `Class` or `Module`
|
103
|
+
```ruby
|
104
|
+
module ValidateStatesFromApi
|
105
|
+
|
106
|
+
extend MixinStringEnum
|
107
|
+
string_enum :states, %w(accept obtain canceled lost)
|
108
|
+
|
109
|
+
end
|
110
|
+
```
|
104
111
|
|
105
112
|
I18n local file example (compatible with https://github.com/zmbacker/enum_help):
|
106
113
|
|
@@ -141,4 +148,3 @@ Calculating -------------------------------------
|
|
141
148
|
rails_string_enum_c 38.159k (± 5.3%) i/s - 192.128k
|
142
149
|
enum_help_c 15.939k (± 4.5%) i/s - 80.272k
|
143
150
|
```
|
144
|
-
|
data/lib/rails_string_enum.rb
CHANGED
@@ -1,20 +1,24 @@
|
|
1
1
|
require 'rails_string_enum/version'
|
2
|
+
require 'rails_string_enum/mixin_string_enum'
|
2
3
|
|
3
4
|
|
4
|
-
|
5
|
-
require '
|
6
|
-
ActiveRecord::
|
7
|
-
|
5
|
+
if defined? ActiveRecord::Base
|
6
|
+
require 'rails_string_enum/active_record_string_enum'
|
7
|
+
ActiveRecord::Base.send :extend, ActiveRecordStringEnum
|
8
|
+
end
|
8
9
|
|
9
|
-
require 'rails_string_enum/string_enum'
|
10
|
-
ActiveRecord::Base.send :extend, RailsStringEnum
|
11
10
|
|
11
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
12
|
+
if defined? ActiveRecord::ConnectionAdapters::PostgreSQL
|
13
|
+
require 'rails_string_enum/pg_enum_migration'
|
14
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PgEnumMigrations
|
12
15
|
|
13
|
-
require 'rails_string_enum/patch_enum_null' if Rails.version <= "4.2.1"
|
16
|
+
require 'rails_string_enum/patch_enum_null' if Rails.version <= "4.2.1"
|
17
|
+
end
|
14
18
|
|
15
19
|
|
16
20
|
begin
|
17
21
|
require 'simple_form'
|
18
22
|
require 'rails_string_enum/simple_form'
|
19
23
|
rescue LoadError
|
20
|
-
end
|
24
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module MixinStringEnum
|
2
|
+
|
3
|
+
# this is a simplified version ActiveRecordStringEnum, for any Class or Module, support only constants
|
4
|
+
|
5
|
+
def string_enum(name, enums)
|
6
|
+
|
7
|
+
const_name_all_values = name.to_s.pluralize.upcase
|
8
|
+
const_set const_name_all_values, enums.map(&:to_s)
|
9
|
+
|
10
|
+
klass = self
|
11
|
+
enums.each do |value|
|
12
|
+
const_set value.to_s.upcase, value.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def const_missing(name)
|
21
|
+
name_s = name.to_s
|
22
|
+
return super unless name_s.include?('__')
|
23
|
+
|
24
|
+
const_set name_s, name_s.split('__').map { |i| const_get(i) }
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -2,7 +2,7 @@ module PgEnumMigrations
|
|
2
2
|
|
3
3
|
# create_enum :color, %w(red green blue) # default schema is 'public'
|
4
4
|
# create_enum :color, %w(red green blue), schema: 'cmyk'
|
5
|
-
def create_enum(enum_name, values, schema:
|
5
|
+
def create_enum(enum_name, values, schema: 'public')
|
6
6
|
execute "CREATE TYPE #{enum_name(enum_name, schema)} AS ENUM (#{escape_enum_values(values)})"
|
7
7
|
end
|
8
8
|
|
@@ -11,12 +11,13 @@ module PgEnumMigrations
|
|
11
11
|
# add_enum_value :color, 'purple', after: 'red'
|
12
12
|
# add_enum_value :color, 'pink', before: 'purple'
|
13
13
|
# add_enum_value :color, 'white', schema: 'public'
|
14
|
-
|
14
|
+
# **WARN** cannot run inside a transaction block
|
15
|
+
def add_enum_value(enum_name, value, before: nil, after: nil, schema: 'public')
|
15
16
|
opts = if before then "BEFORE #{escape_enum_value(before)}"
|
16
17
|
elsif after then "AFTER #{escape_enum_value(after)}"
|
17
18
|
else ''
|
18
19
|
end
|
19
|
-
execute "ALTER TYPE #{enum_name(enum_name, schema)} ADD VALUE #{escape_enum_value(value)} #{opts}"
|
20
|
+
execute "ALTER TYPE #{enum_name(enum_name, schema)} ADD VALUE IF NOT EXISTS #{escape_enum_value(value)} #{opts}"
|
20
21
|
end
|
21
22
|
|
22
23
|
# drop_enum :color
|
@@ -27,15 +28,15 @@ module PgEnumMigrations
|
|
27
28
|
|
28
29
|
|
29
30
|
# you should delete record with deleting value
|
30
|
-
#
|
31
|
+
# Product.only_purple.delete_all or Product.purple.update_all(color: nil)
|
31
32
|
#
|
32
|
-
# if exists index with condition - add_index :
|
33
|
-
# this method show exeption ERROR: operator does not exist:
|
33
|
+
# if exists index with condition - add_index :products, :color, where: "color NOT IN ('white', 'black')"
|
34
|
+
# this method show exeption ERROR: operator does not exist: color <> color_new
|
34
35
|
# you must first remove and then create an index
|
35
36
|
#
|
36
|
-
# delete_enum_value :
|
37
|
-
def delete_enum_value(enum_name, value_name, scheme:
|
38
|
-
old_values = select_values("SELECT enumlabel FROM pg_catalog.pg_enum WHERE enumtypid = #{
|
37
|
+
# delete_enum_value :color, 'black'
|
38
|
+
def delete_enum_value(enum_name, value_name, scheme: 'public')
|
39
|
+
old_values = select_values("SELECT enumlabel FROM pg_catalog.pg_enum WHERE enumtypid = '#{scheme}.#{enum_name}'::regtype::oid")
|
39
40
|
new_values = old_values - Array(value_name)
|
40
41
|
|
41
42
|
execute <<-SQL
|
@@ -43,8 +44,11 @@ module PgEnumMigrations
|
|
43
44
|
CREATE TYPE #{enum_name} AS enum (#{escape_enum_values(new_values)});
|
44
45
|
SQL
|
45
46
|
|
46
|
-
cols_using_enum = select_rows("SELECT table_name, column_name FROM information_schema.columns WHERE udt_name = '#{enum_name}_old'")
|
47
|
-
cols_using_enum.each do |table_name, column_name|
|
47
|
+
cols_using_enum = select_rows("SELECT table_name, column_name, column_default FROM information_schema.columns WHERE udt_name = '#{enum_name}_old'")
|
48
|
+
cols_using_enum.each do |table_name, column_name, column_default|
|
49
|
+
unless column_default.nil?
|
50
|
+
raise "column #{table_name}.#{column_name} has default value #{column_default}, you must manually drop default"
|
51
|
+
end
|
48
52
|
execute <<-SQL
|
49
53
|
ALTER TABLE #{table_name}
|
50
54
|
ALTER COLUMN #{column_name} TYPE #{enum_name} USING #{column_name}::text::#{enum_name};
|
@@ -58,7 +62,7 @@ module PgEnumMigrations
|
|
58
62
|
|
59
63
|
|
60
64
|
|
61
|
-
# rename_enum_value :
|
65
|
+
# rename_enum_value :color, 'white', 'pale'
|
62
66
|
def rename_enum_value(enum_name, old_value_name, new_value_name, scheme: 'public')
|
63
67
|
execute <<-SQL
|
64
68
|
UPDATE pg_catalog.pg_enum
|
@@ -69,8 +73,8 @@ module PgEnumMigrations
|
|
69
73
|
|
70
74
|
|
71
75
|
|
72
|
-
# reorder_enum_values :
|
73
|
-
def reorder_enum_values(enum_name, ordered_values, scheme:
|
76
|
+
# reorder_enum_values :color, %w(green pale red blue)
|
77
|
+
def reorder_enum_values(enum_name, ordered_values, scheme: 'public')
|
74
78
|
all_values = select_values("SELECT enumlabel FROM pg_catalog.pg_enum WHERE enumtypid = '#{scheme}.#{enum_name}'::regtype::oid")
|
75
79
|
max_order = select_value("SELECT max(enumsortorder) FROM pg_catalog.pg_enum WHERE enumtypid = '#{scheme}.#{enum_name}'::regtype::oid").to_i + 1
|
76
80
|
|
@@ -79,7 +83,7 @@ module PgEnumMigrations
|
|
79
83
|
execute <<-SQL
|
80
84
|
UPDATE pg_catalog.pg_enum
|
81
85
|
SET enumsortorder = CASE enumlabel #{ordered_sql} END
|
82
|
-
WHERE enumtypid = #{
|
86
|
+
WHERE enumtypid = '#{scheme}.#{enum_name}'::regtype::oid
|
83
87
|
SQL
|
84
88
|
end
|
85
89
|
|
@@ -110,12 +114,6 @@ module PgEnumMigrations
|
|
110
114
|
}.join('.')
|
111
115
|
end
|
112
116
|
|
113
|
-
def enum_name_as_srt(name, schema)
|
114
|
-
[schema || 'public', name].map { |s|
|
115
|
-
%Q{'#{s}'}
|
116
|
-
}.join('.')
|
117
|
-
end
|
118
|
-
|
119
117
|
def escape_enum_value(value)
|
120
118
|
escaped_value = value.to_s.sub("'", "''")
|
121
119
|
"'#{escaped_value}'"
|
@@ -140,4 +138,4 @@ module PgEnumMigrations
|
|
140
138
|
SQL
|
141
139
|
end
|
142
140
|
|
143
|
-
end
|
141
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module RailsStringEnum
|
2
|
-
VERSION = "0.
|
3
|
-
end
|
2
|
+
VERSION = "0.5.0"
|
3
|
+
end
|
data/rails_string_enum.gemspec
CHANGED
@@ -20,9 +20,6 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
-
|
25
|
-
spec.add_dependency "activerecord", "~> 4.2"
|
26
23
|
|
27
24
|
spec.required_ruby_version = '~> 2.0'
|
28
|
-
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_string_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ermolaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,34 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.9'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '10.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '10.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: activerecord
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '4.2'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '4.2'
|
55
27
|
description: migration methods for postgresql enum, internationalization, simple_form
|
56
28
|
email:
|
57
29
|
- andrey@ermolaev.me
|
@@ -65,10 +37,11 @@ files:
|
|
65
37
|
- README.md
|
66
38
|
- Rakefile
|
67
39
|
- lib/rails_string_enum.rb
|
40
|
+
- lib/rails_string_enum/active_record_string_enum.rb
|
41
|
+
- lib/rails_string_enum/mixin_string_enum.rb
|
68
42
|
- lib/rails_string_enum/patch_enum_null.rb
|
69
43
|
- lib/rails_string_enum/pg_enum_migration.rb
|
70
44
|
- lib/rails_string_enum/simple_form.rb
|
71
|
-
- lib/rails_string_enum/string_enum.rb
|
72
45
|
- lib/rails_string_enum/version.rb
|
73
46
|
- rails_string_enum.gemspec
|
74
47
|
homepage: https://github.com/ermolaev/rails_string_enum
|
@@ -91,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
64
|
version: '0'
|
92
65
|
requirements: []
|
93
66
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.4.
|
67
|
+
rubygems_version: 2.4.8
|
95
68
|
signing_key:
|
96
69
|
specification_version: 4
|
97
70
|
summary: support in rails db enums or string (using as flexible enum)
|