enum_for_what 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 442291e3d675a7c3a17027f60fc49262a957aa13
4
+ data.tar.gz: f12a0d4fc21fa53f71ded0ec41ce9257478e8203
5
+ SHA512:
6
+ metadata.gz: 3d6aac074f5934a13dda17ea4fb1c369ebeb103b2f42a65941d071f470172f4c454f26e6a228839fc19035589b1470cfeb7acbc2dbca745f0f80d864fb071aef
7
+ data.tar.gz: 2f76ea7ed227971bd8627b721d376c9b0b798c24d60d917088f6a16444138a2ff28e9e2f5e7346a8318500585694d31fa8917c565f709ec246f0e0ff85512a45
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2010-2011 Nick Pohodnya
4
+ Copyright (c) 2015 Sport Ngin
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ # Enum For What!
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
+
5
+ Originally forked from [eletronick/enum_column][1] with several enhancements written and curated from the unenumerable forks of this
6
+ gem (see what I did there?) with love by [Sport Ngin][2].
7
+
8
+ Supported adapters:
9
+ - mysql
10
+ - mysql2
11
+ - jdbcmysql (by Nilesh Trivedi)
12
+
13
+ ## Installation
14
+
15
+ In your Gemfile:
16
+
17
+ ```ruby
18
+ gem 'enum_for_what'
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ In your schema(s), specify the constraint as a limit:
24
+
25
+ ```ruby
26
+ create_table :enumerations, :force => true do |t|
27
+ t.column :severity, :enum, :limit => [:low, :medium, :high, :critical], :default => :medium
28
+ t.column :color, :enum, :limit => [:red, :blue, :green, :yellow]
29
+ end
30
+ ```
31
+
32
+ In the model, you can then automatically validate this column using:
33
+
34
+ ```ruby
35
+ validates_columns :severity, :color
36
+ ```
37
+
38
+ The rest will be handled for you. All enumerated values will be given as symbols.
39
+
40
+ ```ruby
41
+ @e = Enumeration.new
42
+ @e.severity = :medium
43
+ ```
44
+
45
+ You can always use the column reflection to get the list of possible values from the database column.
46
+
47
+ ```ruby
48
+ Enumeration.columns_hash['color'].limit
49
+ # or
50
+ @enumeration.column_for_attribute(:color).limit
51
+
52
+ # Will yield => [:red, :blue, :green, :yellow]
53
+ ```
54
+
55
+ If you assign a string to the column, it will be converted to a symbol if it's valid and `nil` otherwise,
56
+ so if this is the only way you populate color, `validates_presence_of` may be the only validation you need.
57
+
58
+ ```ruby
59
+ Enumeration.new(:color => "red") (color will be :red)
60
+ Enumeration.new(:color => "infrared") (color will be nil)
61
+ ```
62
+
63
+ In views, you can use the enum_select helper to generate input for enumerated attributes:
64
+
65
+ ```ruby
66
+ <%= enum_select(@enumeration, 'severity')%>
67
+ # or
68
+ <%= form_for @enumeration do |f| %>
69
+ <%= f.label :severity %>
70
+ <%= f.enum_select :severity %>
71
+ <% end %>
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
81
+
82
+ [1]: https://github.com/electronick/enum_column
83
+ [2]: http://www.codinginthecrease.com/
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'enum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "enum_for_what"
8
+ spec.version = EnumForWhat::VERSION
9
+ spec.authors = ["Chris Arcand", "Nick Pohodnya", "Aaron Weiner"]
10
+ spec.email = ["chris.arcand@sportngin.com"]
11
+
12
+ spec.summary = %q{MySQL's native ENUM type for Rails}
13
+ spec.description = %q{Allows your Rails models and views to take advantage of MySQL's' native ENUM type
14
+ Originally forked from various forks of https://github.com/electronick/enum_column}
15
+ spec.homepage = "https://github.com/sportngin/enum_for_what"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
26
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init.rb"
@@ -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,25 @@
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
+ adapter_class = if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
5
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter
6
+ elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
7
+ ActiveRecord::ConnectionAdapters::MysqlAdapter
8
+ end
9
+
10
+ if adapter_class
11
+ adapter_class.class_eval do
12
+
13
+ protected
14
+ if instance_methods.include?(:initialize_type_map)
15
+ def initialize_type_map_with_enum_types(m)
16
+ initialize_type_map_without_enum_types(m)
17
+ m.register_type(%r(enum)i) do |sql_type|
18
+ limit = sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern }
19
+ ActiveRecord::Type::Enum.new(limit: limit)
20
+ end
21
+ end
22
+ alias_method_chain :initialize_type_map, :enum_types
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,105 @@
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.class_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
+ def __enum_type_cast(value)
31
+ if type == :enum
32
+ self.class.value_to_symbol(value)
33
+ else
34
+ __type_cast_enum(value)
35
+ end
36
+ end
37
+
38
+ if instance_methods.include?(:type_cast_from_database)
39
+ alias __type_cast_enum type_cast_from_database
40
+ # Convert to a symbol.
41
+ def type_cast_from_database(value)
42
+ __enum_type_cast(value)
43
+ end
44
+ elsif instance_methods.include?(:type_cast)
45
+ alias __type_cast_enum type_cast
46
+ def type_cast(value)
47
+ __enum_type_cast(value)
48
+ end
49
+ end
50
+
51
+ # Deprecated in Rails 4.1
52
+ if instance_methods.include?(:type_cast_code)
53
+ alias __type_cast_code_enum type_cast_code
54
+ # Code to convert to a symbol.
55
+ def type_cast_code(var_name)
56
+ if type == :enum
57
+ "#{self.class.name}.value_to_symbol(#{var_name})"
58
+ else
59
+ __type_cast_code_enum(var_name)
60
+ end
61
+ end
62
+ end
63
+
64
+ class << self
65
+ # Safely convert the value to a symbol.
66
+ def value_to_symbol(value)
67
+ case value
68
+ when Symbol
69
+ value
70
+ when String
71
+ value.empty? ? nil : value.intern
72
+ else
73
+ nil
74
+ end
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ # Deprecated in Rails 4.2
81
+ if private_instance_methods.include?(:simplified_type)
82
+ alias __simplified_type_enum simplified_type
83
+ # The enum simple type.
84
+ def simplified_type(field_type)
85
+ if field_type =~ /enum/i
86
+ :enum
87
+ else
88
+ __simplified_type_enum(field_type)
89
+ end
90
+ end
91
+ end
92
+
93
+ # Deprecated in Rails 4.2
94
+ if private_instance_methods.include?(:extract_limit)
95
+ alias __extract_limit_enum extract_limit
96
+ def extract_limit(sql_type)
97
+ if sql_type =~ /^enum/i
98
+ sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern }
99
+ else
100
+ __extract_limit_enum(sql_type)
101
+ end
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,29 @@
1
+ if defined? ActiveRecord::Type::Value
2
+ module ActiveRecord
3
+ module Type
4
+ class Enum < Value # :nodoc:
5
+ def type
6
+ :enum
7
+ end
8
+
9
+ def type_cast_for_database(value)
10
+ if value.nil? || value == ''
11
+ nil
12
+ else
13
+ value.to_s
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def cast_value(value)
20
+ if value.nil? || value == ''
21
+ nil
22
+ else
23
+ value.to_sym
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ 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
@@ -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,11 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class TableDefinition
4
+ def enum(*args)
5
+ options = args.extract_options!
6
+ column_names = args
7
+ column_names.each { |name| column(name, 'enum', options) }
8
+ end
9
+ end
10
+ end
11
+ 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,39 @@
1
+ module ActiveRecord
2
+ module Validations
3
+ module ClassMethods
4
+ # Automatically validates the column against the schema definition
5
+ # for nullability, format, and enumerations. Handles integers, floats,
6
+ # enumerations, and string limits.
7
+ #
8
+ # Usage: validates_columns :severity, :name
9
+ def validates_columns(*column_names)
10
+ begin
11
+ cols = columns_hash
12
+ column_names.each do |name|
13
+ col = cols[name.to_s]
14
+ raise ArgumentError, "Cannot find column #{name}" unless col
15
+
16
+ # test for nullability
17
+ validates_presence_of(name) if !col.null
18
+
19
+ # Test various known types.
20
+ case col.type
21
+ when :enum
22
+ validates_inclusion_of name, :in => col.limit, :allow_nil => true
23
+
24
+ when :integer, :float
25
+ validates_numericality_of name, :allow_nil => true
26
+
27
+ when :string
28
+ if col.limit
29
+ validates_length_of name, :maximum => col.limit, :allow_nil => true
30
+ end
31
+ end
32
+ end
33
+ rescue ActiveRecord::StatementInvalid=>e
34
+ raise e unless e.message.include?("42S02") # swallow the exception if its for a missing table
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module EnumForWhat
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,20 @@
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_type'
7
+ require 'enum/enum_adapter'
8
+ require 'enum/enum_column_adapter'
9
+ require 'enum/schema_statements'
10
+ require 'enum/schema_definitions'
11
+ require 'enum/quoting'
12
+ require 'enum/validations'
13
+ end
14
+
15
+ ActiveSupport.on_load :action_view do
16
+ require 'enum/active_record_helper'
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1 @@
1
+ require 'enum_column'
@@ -0,0 +1 @@
1
+ require 'enum_column'
@@ -0,0 +1 @@
1
+ require 'enum_column'
@@ -0,0 +1 @@
1
+ require 'enum_column'
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum_for_what
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Arcand
8
+ - Nick Pohodnya
9
+ - Aaron Weiner
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2015-09-03 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: '1.10'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: '10.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: '10.0'
43
+ description: |-
44
+ Allows your Rails models and views to take advantage of MySQL's' native ENUM type
45
+ Originally forked from various forks of https://github.com/electronick/enum_column
46
+ email:
47
+ - chris.arcand@sportngin.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - .gitignore
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - enum_for_what.gemspec
58
+ - init.rb
59
+ - lib/enum/active_record_helper.rb
60
+ - lib/enum/enum_adapter.rb
61
+ - lib/enum/enum_column_adapter.rb
62
+ - lib/enum/enum_type.rb
63
+ - lib/enum/mysql_adapter.rb
64
+ - lib/enum/quoting.rb
65
+ - lib/enum/schema_definitions.rb
66
+ - lib/enum/schema_statements.rb
67
+ - lib/enum/validations.rb
68
+ - lib/enum/version.rb
69
+ - lib/enum_column.rb
70
+ - lib/enum_column3.rb
71
+ - lib/enum_column_strict.rb
72
+ - lib/enum_for_what.rb
73
+ - rails/init.rb
74
+ homepage: https://github.com/sportngin/enum_for_what
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: MySQL's native ENUM type for Rails
98
+ test_files: []