dg-enum_column4 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.txt +60 -0
- data/init.rb +1 -0
- data/lib/dg-enum_column4.rb +1 -0
- data/lib/enum/active_record_helper.rb +63 -0
- data/lib/enum/enum_adapter.rb +87 -0
- data/lib/enum/mysql_adapter.rb +17 -0
- data/lib/enum/quoting.rb +17 -0
- data/lib/enum/schema_definitions.rb +12 -0
- data/lib/enum/schema_statements.rb +27 -0
- data/lib/enum/validations.rb +40 -0
- data/lib/enum_column.rb +18 -0
- data/lib/enum_column3.rb +1 -0
- data/test/db/schema.rb +28 -0
- data/test/enum_controller_test.rb +24 -0
- data/test/enum_mysql_test.rb +208 -0
- data/test/fixtures/enum_controller.rb +9 -0
- data/test/fixtures/enumeration.rb +20 -0
- data/test/test_helper.rb +18 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e0286fdc982eb17ec48eeaea7acaadcd6797e8b6
|
4
|
+
data.tar.gz: 53f19ed0dd691d29f888fde68fa61e2419a8913f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 397e7e43c32c5fd776e4875645ad05f6a211944c3014cb55b5f9819f084f78c0cc81de836b437aca7f1c1cca6b38e135b02302d6810722ae51d731488944987a
|
7
|
+
data.tar.gz: ba71897a7b6a3cc0ec0dda69e4e2ed085d65f90a4ace17f36fe43fb681844bda40c9dc0c4fc43a252bce5d8a8cd9e0a909f7eb9f4c373f2048ba45e08b5c0907
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010-2011 Nick Pohodnya
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.txt
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Overview
|
2
|
+
|
3
|
+
This gem is an extension to ActiveRecord which enables native support of enumerations in the database schema using the ENUM type in MySQL.
|
4
|
+
Currently only MySQL is implemented.
|
5
|
+
Tested with Rails 3, for Rails 2 you should better use enum-column plugin (http://rubyforge.org/projects/enum-column/)
|
6
|
+
Works with Scaffolding.
|
7
|
+
|
8
|
+
Supported adapters:
|
9
|
+
mysql
|
10
|
+
mysql2
|
11
|
+
jdbcmysql (by Nilesh Trivedi)
|
12
|
+
|
13
|
+
How to use it.
|
14
|
+
|
15
|
+
In you Gemfile:
|
16
|
+
|
17
|
+
gem 'enum_column3'
|
18
|
+
|
19
|
+
In your schema:
|
20
|
+
|
21
|
+
When you create your schema, specify the constraint as a limit:
|
22
|
+
|
23
|
+
create_table :enumerations, :force => true do |t|
|
24
|
+
t.column :severity, :enum, :limit => [:low, :medium, :high, :critical], :default => :medium
|
25
|
+
t.column :color, :enum, :limit => [:red, :blue, :green, :yellow]
|
26
|
+
...
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
In the model:
|
31
|
+
|
32
|
+
You can then automatically validate this column using:
|
33
|
+
|
34
|
+
validates_columns :severity, :color
|
35
|
+
|
36
|
+
The rest will be handled for you. All enumerated values will be given as symbols.
|
37
|
+
|
38
|
+
@e = Enumeration.new
|
39
|
+
@e.severity = :medium
|
40
|
+
|
41
|
+
You can always use the column reflection to get the list of possible values from the database column.
|
42
|
+
|
43
|
+
Enumeration.columns_hash['color'].limit
|
44
|
+
or
|
45
|
+
@enumeration.column_for_attribute(:color).limit
|
46
|
+
|
47
|
+
Will yield: [:red, :blue, :green, :yellow]
|
48
|
+
|
49
|
+
|
50
|
+
In views:
|
51
|
+
|
52
|
+
You can use enum_select helper to generate input for enumerated attribute as:
|
53
|
+
|
54
|
+
<%= enum_select(@enumeration, 'severity')%>
|
55
|
+
or
|
56
|
+
<%= form_for @enumeration do |f| %>
|
57
|
+
<%= f.label :severity %>
|
58
|
+
<%= f.enum_select :severity %>
|
59
|
+
<% end %>
|
60
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init.rb"
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'enum_column'
|
@@ -0,0 +1,63 @@
|
|
1
|
+
if defined?(Rails::Generator)
|
2
|
+
module Rails
|
3
|
+
module Generator
|
4
|
+
class GeneratedAttribute
|
5
|
+
def field_type_with_enumerated_attribute
|
6
|
+
return (@field_type = :enum_select) if type == :enum
|
7
|
+
field_type_without_enumerated_attribute
|
8
|
+
end
|
9
|
+
alias_method_chain :field_type, :enumerated_attribute
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
if defined?(ActionView::Base)
|
16
|
+
module ActionView
|
17
|
+
module Helpers
|
18
|
+
|
19
|
+
#form_options_helper.rb
|
20
|
+
module FormOptionsHelper
|
21
|
+
#def select
|
22
|
+
def enum_select(object, method, options={}, html_options={})
|
23
|
+
InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class InstanceTag
|
28
|
+
def to_enum_select_tag(options, html_options={})
|
29
|
+
if self.object.respond_to?(method_name.to_sym)
|
30
|
+
column = self.object.column_for_attribute(method_name)
|
31
|
+
if (value = self.object.__send__(method_name.to_sym))
|
32
|
+
options[:selected] ||= value.to_s
|
33
|
+
else
|
34
|
+
options[:include_blank] = column.null if options[:include_blank].nil?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
to_select_tag(column.limit, options, html_options)
|
38
|
+
end
|
39
|
+
|
40
|
+
#initialize record_name, method, self
|
41
|
+
if respond_to?(:to_tag)
|
42
|
+
def to_tag_with_enumerated_attribute(options={})
|
43
|
+
#look for an enum
|
44
|
+
if (column_type == :enum && self.object.class.respond_to?(method_name.to_sym))
|
45
|
+
to_enum_select_tag(options)
|
46
|
+
else
|
47
|
+
to_tag_without_enumerated_attribute(options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
alias_method_chain :to_tag, :enumerated_attribute
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
class FormBuilder
|
56
|
+
def enum_select(method, options={}, html_options={})
|
57
|
+
@template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# This module provides all the column helper methods to deal with the
|
2
|
+
# values and adds the common type management code for the adapters.
|
3
|
+
|
4
|
+
|
5
|
+
# try rails 3.1, then rails 3.2+, mysql column adapters
|
6
|
+
column_class = if defined? ActiveRecord::ConnectionAdapters::Mysql2Column
|
7
|
+
ActiveRecord::ConnectionAdapters::Mysql2Column
|
8
|
+
elsif defined? ActiveRecord::ConnectionAdapters::MysqlColumn
|
9
|
+
ActiveRecord::ConnectionAdapters::MysqlColumn
|
10
|
+
elsif defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
|
11
|
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
|
12
|
+
elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter::Column
|
13
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter::Column
|
14
|
+
else
|
15
|
+
ObviousHint::NoMysqlAdapterFound
|
16
|
+
end
|
17
|
+
|
18
|
+
column_class.module_eval do
|
19
|
+
|
20
|
+
alias __klass_enum klass
|
21
|
+
# The class for enum is Symbol.
|
22
|
+
def klass
|
23
|
+
if type == :enum
|
24
|
+
Symbol
|
25
|
+
else
|
26
|
+
__klass_enum
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
alias __type_cast_enum type_cast
|
31
|
+
# Convert to a symbol.
|
32
|
+
def type_cast(value)
|
33
|
+
if type == :enum
|
34
|
+
self.class.value_to_symbol(value)
|
35
|
+
else
|
36
|
+
__type_cast_enum(value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if respond_to?(:type_cast_code)
|
41
|
+
alias __type_cast_code_enum type_cast_code
|
42
|
+
# Code to convert to a symbol.
|
43
|
+
def type_cast_code(var_name)
|
44
|
+
if type == :enum
|
45
|
+
"#{self.class.name}.value_to_symbol(#{var_name})"
|
46
|
+
else
|
47
|
+
__type_cast_code_enum(var_name)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class << self
|
53
|
+
# Safely convert the value to a symbol.
|
54
|
+
def value_to_symbol(value)
|
55
|
+
case value
|
56
|
+
when Symbol
|
57
|
+
value
|
58
|
+
when String
|
59
|
+
value.empty? ? nil : value.intern
|
60
|
+
else
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
alias __simplified_type_enum simplified_type
|
68
|
+
# The enum simple type.
|
69
|
+
def simplified_type(field_type)
|
70
|
+
if field_type =~ /enum/i
|
71
|
+
:enum
|
72
|
+
else
|
73
|
+
__simplified_type_enum(field_type)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
alias __extract_limit_enum extract_limit
|
78
|
+
def extract_limit(sql_type)
|
79
|
+
if sql_type =~ /^enum/i
|
80
|
+
sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern }
|
81
|
+
else
|
82
|
+
__extract_limit_enum(sql_type)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
adapter_class = if defined? ActiveRecord::ConnectionAdapters::MySQLJdbcConnection
|
2
|
+
ActiveRecord::ConnectionAdapters::MySQLJdbcConnection
|
3
|
+
elsif defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
4
|
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
5
|
+
elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
|
6
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter
|
7
|
+
end
|
8
|
+
|
9
|
+
adapter_class.module_eval do
|
10
|
+
alias __native_database_types_enum native_database_types
|
11
|
+
|
12
|
+
def native_database_types #:nodoc
|
13
|
+
types = __native_database_types_enum
|
14
|
+
types[:enum] = { :name => "enum" }
|
15
|
+
types
|
16
|
+
end
|
17
|
+
end
|
data/lib/enum/quoting.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters # :nodoc:
|
3
|
+
module Quoting
|
4
|
+
alias __quote_enum quote
|
5
|
+
|
6
|
+
# Quote a symbol as a normal string. This will support quoting of
|
7
|
+
# enumerated values.
|
8
|
+
def quote(value, column = nil)
|
9
|
+
if !value.is_a? Symbol
|
10
|
+
__quote_enum(value, column)
|
11
|
+
else
|
12
|
+
ActiveRecord::Base.send(:quote_bound_value, value.to_s)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters # :nodoc:
|
3
|
+
module SchemaStatements
|
4
|
+
alias __type_to_sql_enum type_to_sql
|
5
|
+
|
6
|
+
# Add enumeration support for schema statement creation. This
|
7
|
+
# will have to be adapted for every adapter if the type requires
|
8
|
+
# anything by a list of allowed values. The overrides the standard
|
9
|
+
# type_to_sql method and chains back to the default. This could
|
10
|
+
# be done on a per adapter basis, but is generalized here.
|
11
|
+
#
|
12
|
+
# will generate enum('a', 'b', 'c') for :limit => [:a, :b, :c]
|
13
|
+
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
|
14
|
+
if type == :enum
|
15
|
+
native = native_database_types[type]
|
16
|
+
column_type_sql = (native || {})[:name] || 'enum'
|
17
|
+
|
18
|
+
column_type_sql << "(#{limit.map { |v| quote(v) }.join(',')})"
|
19
|
+
|
20
|
+
column_type_sql
|
21
|
+
else
|
22
|
+
__type_to_sql_enum(type, limit, precision, scale)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
module ActiveRecord
|
3
|
+
module Validations
|
4
|
+
module ClassMethods
|
5
|
+
# Automatically validates the column against the schema definition
|
6
|
+
# for nullability, format, and enumerations. Handles integers, floats,
|
7
|
+
# enumerations, and string limits.
|
8
|
+
#
|
9
|
+
# Usage: validates_columns :severity, :name
|
10
|
+
def validates_columns(*column_names)
|
11
|
+
begin
|
12
|
+
cols = columns_hash
|
13
|
+
column_names.each do |name|
|
14
|
+
col = cols[name.to_s]
|
15
|
+
raise ArgumentError, "Cannot find column #{name}" unless col
|
16
|
+
|
17
|
+
# test for nullability
|
18
|
+
validates_presence_of(name) if !col.null
|
19
|
+
|
20
|
+
# Test various known types.
|
21
|
+
case col.type
|
22
|
+
when :enum
|
23
|
+
validates_inclusion_of name, :in => col.limit, :allow_nil => true
|
24
|
+
|
25
|
+
when :integer, :float
|
26
|
+
validates_numericality_of name, :allow_nil => true
|
27
|
+
|
28
|
+
when :string
|
29
|
+
if col.limit
|
30
|
+
validates_length_of name, :maximum => col.limit, :allow_nil => true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
rescue ActiveRecord::StatementInvalid=>e
|
35
|
+
raise e unless e.message.include?("42S02") # swallow the exception if its for a missing table
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/enum_column.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
if defined?(::Rails::Railtie)
|
2
|
+
class EnumColumnRailtie < Rails::Railtie
|
3
|
+
initializer 'enum_column.initialize', :after => 'active_record.initialize_database' do |app|
|
4
|
+
ActiveSupport.on_load :active_record do
|
5
|
+
require 'enum/mysql_adapter'
|
6
|
+
require 'enum/enum_adapter'
|
7
|
+
require 'enum/schema_statements'
|
8
|
+
require 'enum/schema_definitions'
|
9
|
+
require 'enum/quoting'
|
10
|
+
require 'enum/validations'
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveSupport.on_load :action_view do
|
14
|
+
require 'enum/active_record_helper'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/enum_column3.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'enum_column'
|
data/test/db/schema.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
ActiveRecord::Schema.define do
|
3
|
+
create_table :enumerations, :force => true do |t|
|
4
|
+
t.column :severity, :enum, :limit => [:low, :medium, :high, :critical],
|
5
|
+
:default => :medium
|
6
|
+
t.column :color, :enum, :limit => [:red, :blue, :green, :yellow]
|
7
|
+
t.column :string_field, :string, :limit => 8, :null => false
|
8
|
+
t.column :int_field, :integer
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
create_table :basic_enums, :force => true do |t|
|
13
|
+
t.column :value, :enum, :limit => [:good, :working]
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :basic_default_enums, :force => true do |t|
|
17
|
+
t.column :value, :enum, :limit => [:good, :working],:default => :working
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :nonnull_enums, :force => true do |t|
|
21
|
+
t.column :value, :enum, :limit => [:good, :working],:null => false
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :nonnull_default_enums, :force => true do |t|
|
25
|
+
t.column :value, :enum, :limit => [:good, :working],:null => false,:default => :working
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require 'fixtures/enumeration'
|
3
|
+
require 'fixtures/enum_controller'
|
4
|
+
|
5
|
+
class EnumControllerTest < ActionController::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Enumeration.connection.execute 'DELETE FROM enumerations'
|
9
|
+
|
10
|
+
Rails.application.routes.draw do
|
11
|
+
match '/enum_select' => "enum#enum_select"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
test "should render enum_select" do
|
16
|
+
get :enum_select
|
17
|
+
assert_response :success
|
18
|
+
assert_equal '<select id="test_severity" name="test[severity]"><option value="low">low</option>
|
19
|
+
<option value="medium" selected="selected">medium</option>
|
20
|
+
<option value="high">high</option>
|
21
|
+
<option value="critical">critical</option></select>', @response.body
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,208 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require 'fixtures/enumeration'
|
3
|
+
|
4
|
+
class EnumerationsTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
Enumeration.connection.execute 'DELETE FROM enumerations'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_column_values
|
10
|
+
columns = Enumeration.columns_hash
|
11
|
+
color_column = columns['color']
|
12
|
+
assert color_column
|
13
|
+
assert_equal [:red, :blue, :green, :yellow], color_column.limit
|
14
|
+
|
15
|
+
severity_column = columns['severity']
|
16
|
+
assert severity_column
|
17
|
+
assert_equal [:low, :medium, :high, :critical], severity_column.limit
|
18
|
+
assert_equal :medium, severity_column.default
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_insert_enum
|
22
|
+
row = Enumeration.new
|
23
|
+
row.color = :blue
|
24
|
+
row.string_field = 'test'
|
25
|
+
assert_equal :medium, row.severity
|
26
|
+
assert row.save
|
27
|
+
|
28
|
+
db_row = Enumeration.find(row.id)
|
29
|
+
assert db_row
|
30
|
+
assert_equal :blue, row.color
|
31
|
+
assert_equal :medium, row.severity
|
32
|
+
end
|
33
|
+
|
34
|
+
# Uses the automatic validates_columns to create automatic validation rules
|
35
|
+
# for columns based on the schema information.
|
36
|
+
def test_bad_value
|
37
|
+
row = Enumeration.new
|
38
|
+
row.color = :violet
|
39
|
+
row.string_field = 'test'
|
40
|
+
assert !row.save
|
41
|
+
|
42
|
+
assert row.errors
|
43
|
+
assert_equal ['is not included in the list'], row.errors['color']
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_other_types
|
47
|
+
row = Enumeration.new
|
48
|
+
row.string_field = 'a' * 10
|
49
|
+
assert !row.save
|
50
|
+
assert_equal ['is too long (maximum is 8 characters)'], row.errors['string_field']
|
51
|
+
|
52
|
+
row = Enumeration.new
|
53
|
+
assert !row.save
|
54
|
+
assert_equal ['can\'t be blank'], row.errors['string_field']
|
55
|
+
|
56
|
+
row = Enumeration.new
|
57
|
+
row.string_field = 'test'
|
58
|
+
row.int_field = 'aaaa'
|
59
|
+
assert !row.save
|
60
|
+
assert_equal ['is not a number'], row.errors['int_field']
|
61
|
+
|
62
|
+
row = Enumeration.new
|
63
|
+
row.string_field = 'test'
|
64
|
+
row.int_field = '500'
|
65
|
+
assert row.save
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_view_helper
|
69
|
+
request = ActionController::TestRequest.new
|
70
|
+
response = ActionController::TestResponse.new
|
71
|
+
request.action = 'test1'
|
72
|
+
body = EnumController.process(request, response).body
|
73
|
+
assert_equal '<select id="test_severity" name="test[severity]"><option value="low">low</option><option value="medium" selected="selected">medium</option><option value="high">high</option><option value="critical">critical</option></select>', body
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_radio_helper
|
77
|
+
request = ActionController::TestRequest.new
|
78
|
+
response = ActionController::TestResponse.new
|
79
|
+
request.action = 'test2'
|
80
|
+
body = EnumController.process(request, response).body
|
81
|
+
assert_equal '<label>low: <input id="test_severity_low" name="test[severity]" type="radio" value="low" /></label><label>medium: <input checked="checked" id="test_severity_medium" name="test[severity]" type="radio" value="medium" /></label><label>high: <input id="test_severity_high" name="test[severity]" type="radio" value="high" /></label><label>critical: <input id="test_severity_critical" name="test[severity]" type="radio" value="critical" /></label>', body
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
# Basic tests
|
86
|
+
def test_create_basic_default
|
87
|
+
assert (object = BasicEnum.create)
|
88
|
+
assert_nil object.value,
|
89
|
+
"Enum columns without explicit default, default to null if allowed"
|
90
|
+
assert !object.new_record?
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_create_basic_good
|
94
|
+
assert (object = BasicEnum.create(:value => :good))
|
95
|
+
assert_equal :good, object.value
|
96
|
+
assert !object.new_record?
|
97
|
+
assert (object = BasicEnum.create(:value => :working))
|
98
|
+
assert_equal :working, object.value
|
99
|
+
assert !object.new_record?
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_create_basic_null
|
103
|
+
assert (object = BasicEnum.create(:value => nil))
|
104
|
+
assert_nil object.value
|
105
|
+
assert !object.new_record?
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_create_basic_bad
|
109
|
+
assert (object = BasicEnum.create(:value => :bad))
|
110
|
+
assert object.new_record?
|
111
|
+
end
|
112
|
+
|
113
|
+
# Basic w/ Default
|
114
|
+
|
115
|
+
######################################################################
|
116
|
+
|
117
|
+
def test_create_basic_wd_default
|
118
|
+
assert (object = BasicDefaultEnum.create)
|
119
|
+
assert_equal :working, object.value, "Explicit default ignored columns"
|
120
|
+
assert !object.new_record?
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_create_basic_wd_good
|
124
|
+
assert (object = BasicDefaultEnum.create(:value => :good))
|
125
|
+
assert_equal :good, object.value
|
126
|
+
assert !object.new_record?
|
127
|
+
assert (object = BasicDefaultEnum.create(:value => :working))
|
128
|
+
assert_equal :working, object.value
|
129
|
+
assert !object.new_record?
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_create_basic_wd_null
|
133
|
+
assert (object = BasicDefaultEnum.create(:value => nil))
|
134
|
+
assert_nil object.value
|
135
|
+
assert !object.new_record?
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_create_basic_wd_bad
|
139
|
+
assert (object = BasicDefaultEnum.create(:value => :bad))
|
140
|
+
assert object.new_record?
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
# Nonnull
|
146
|
+
|
147
|
+
######################################################################
|
148
|
+
|
149
|
+
def test_create_nonnull_default
|
150
|
+
assert (object = NonnullEnum.create)
|
151
|
+
# assert_equal :good, object.value,
|
152
|
+
# "Enum columns without explicit default, default to first value if null not allowed"
|
153
|
+
assert object.new_record?
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_create_nonnull_good
|
157
|
+
assert (object = NonnullEnum.create(:value => :good))
|
158
|
+
assert_equal :good, object.value
|
159
|
+
assert !object.new_record?
|
160
|
+
assert (object = NonnullEnum.create(:value => :working))
|
161
|
+
assert_equal :working, object.value
|
162
|
+
assert !object.new_record?
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_create_nonnull_null
|
166
|
+
assert (object = NonnullEnum.create(:value => nil))
|
167
|
+
assert object.new_record?
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_create_nonnull_bad
|
171
|
+
assert (object = NonnullEnum.create(:value => :bad))
|
172
|
+
assert object.new_record?
|
173
|
+
end
|
174
|
+
|
175
|
+
# Nonnull w/ Default
|
176
|
+
|
177
|
+
######################################################################
|
178
|
+
|
179
|
+
def test_create_nonnull_wd_default
|
180
|
+
assert (object = NonnullDefaultEnum.create)
|
181
|
+
assert_equal :working, object.value
|
182
|
+
assert !object.new_record?
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_create_nonnull_wd_good
|
186
|
+
assert (object = NonnullDefaultEnum.create(:value => :good))
|
187
|
+
assert_equal :good, object.value
|
188
|
+
assert !object.new_record?
|
189
|
+
assert (object = NonnullDefaultEnum.create(:value => :working))
|
190
|
+
assert_equal :working, object.value
|
191
|
+
assert !object.new_record?
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_create_nonnull_wd_null
|
195
|
+
assert (object = NonnullDefaultEnum.create(:value => nil))
|
196
|
+
assert object.new_record?
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_create_nonnull_wd_bad
|
200
|
+
assert (object = NonnullDefaultEnum.create(:value => :bad))
|
201
|
+
assert object.new_record?
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_quoting
|
205
|
+
value = Enumeration.send(:sanitize_sql, ["value = ? ", :"'" ] )
|
206
|
+
assert_equal "value = '\\'' ", value
|
207
|
+
end
|
208
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
class Enumeration < ActiveRecord::Base
|
3
|
+
validates_columns :color, :severity, :string_field, :int_field
|
4
|
+
end
|
5
|
+
|
6
|
+
class BasicEnum < ActiveRecord::Base
|
7
|
+
validates_columns :value
|
8
|
+
end
|
9
|
+
|
10
|
+
class BasicDefaultEnum < ActiveRecord::Base
|
11
|
+
validates_columns :value
|
12
|
+
end
|
13
|
+
|
14
|
+
class NonnullEnum < ActiveRecord::Base
|
15
|
+
validates_columns :value
|
16
|
+
end
|
17
|
+
|
18
|
+
class NonnullDefaultEnum < ActiveRecord::Base
|
19
|
+
validates_columns :value
|
20
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# =============================================================================
|
2
|
+
# Include the files required to test Engines.
|
3
|
+
|
4
|
+
# Load the default rails test helper - this will load the environment.
|
5
|
+
require File.dirname(__FILE__) + '/../../../../test/test_helper'
|
6
|
+
|
7
|
+
plugin_path = File::dirname(__FILE__) + '/..'
|
8
|
+
schema_file = plugin_path + "/test/db/schema.rb"
|
9
|
+
load(schema_file) if File.exist?(schema_file)
|
10
|
+
|
11
|
+
# set up the fixtures location to use your engine's fixtures
|
12
|
+
fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
13
|
+
ActiveSupport::TestCase.fixture_path = fixture_path
|
14
|
+
$LOAD_PATH.unshift(ActiveSupport::TestCase.fixture_path)
|
15
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
|
+
# =============================================================================
|
17
|
+
|
18
|
+
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dg-enum_column4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Pohodnya
|
8
|
+
- Gonzalo Fernandez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-05-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Enum column support for Rails 4
|
15
|
+
email:
|
16
|
+
- chalofa@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- LICENSE
|
22
|
+
- README.txt
|
23
|
+
- init.rb
|
24
|
+
- lib/dg-enum_column4.rb
|
25
|
+
- lib/enum/active_record_helper.rb
|
26
|
+
- lib/enum/enum_adapter.rb
|
27
|
+
- lib/enum/mysql_adapter.rb
|
28
|
+
- lib/enum/quoting.rb
|
29
|
+
- lib/enum/schema_definitions.rb
|
30
|
+
- lib/enum/schema_statements.rb
|
31
|
+
- lib/enum/validations.rb
|
32
|
+
- lib/enum_column.rb
|
33
|
+
- lib/enum_column3.rb
|
34
|
+
- test/db/schema.rb
|
35
|
+
- test/enum_controller_test.rb
|
36
|
+
- test/enum_mysql_test.rb
|
37
|
+
- test/fixtures/enum_controller.rb
|
38
|
+
- test/fixtures/enumeration.rb
|
39
|
+
- test/test_helper.rb
|
40
|
+
homepage: http://github.com/devgile/enum_column
|
41
|
+
licenses:
|
42
|
+
- LICENSE
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.4.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Enable enum type for MySQL db.
|
64
|
+
test_files:
|
65
|
+
- test/test_helper.rb
|
66
|
+
- test/db/schema.rb
|
67
|
+
- test/fixtures/enumeration.rb
|
68
|
+
- test/fixtures/enum_controller.rb
|
69
|
+
- test/enum_controller_test.rb
|
70
|
+
- test/enum_mysql_test.rb
|