Last active
April 11, 2017 07:27
-
-
Save nalabjp/84bc693f468b7763aea85a99ac86e305 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless File.exists?('./Gemfile') | |
File.write('./Gemfile', <<~GEMFILE) | |
source 'http://rubygems.org' | |
gem 'activerecord' | |
gem 'sqlite3' | |
GEMFILE | |
system 'bundle install --path=vendor/bundle' | |
end | |
require 'bundler/setup' | |
require 'active_record' | |
CONFIG = { | |
adapter: 'sqlite3', | |
database: './ar_enum_development.sqlite3' | |
}.freeze | |
ActiveRecord::Base.configurations['ar_enum_development'] = CONFIG | |
ActiveRecord::Base.establish_connection(:ar_enum_development) | |
ActiveRecord::Schema.define do | |
self.verbose = false | |
create_table :users, force: true do |t| | |
t.string :gender, default: 'male' | |
end | |
end | |
module Encryptable | |
def self.included(base) | |
base.class_eval do | |
around_save :encrypt_before_save_and_decrypt_after_save | |
end | |
end | |
def encrypt_before_save_and_decrypt_after_save | |
encrypt_gender | |
yield | |
decrypt_gender | |
end | |
def encrypt_gender | |
v = read_attribute(:gender) | |
write_attribute(:gender, v.dup.reverse) if v | |
end | |
def dectypt_gender | |
v = read_attribute(:gender) | |
write_attribute(:gender, v.dup.reverse) if v | |
end | |
end | |
class User < ActiveRecord::Base | |
# include Encryptable | |
enum gender: { male: 'male', female: 'female', other: 'other' } | |
end | |
u1 = User.create | |
puts u1.male? | |
puts u1.gender | |
u2 = User.create(gender: :other) | |
puts u2.other? | |
puts u2.gender | |
system("sqlite3 -header -column ./ar_enum_development.sqlite3 'select * from users;'") |
Author
nalabjp
commented
Apr 10, 2017
Encryptable
module is a simple pseudo-encryption.
If User
enables include Encryptable
, it will raise ArgumentError
.
$ ruby ar_enum.rb
/Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/enum.rb:139:in `assert_valid_value': 'elam' is not a valid gender (ArgumentError)
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute.rb:67:in `with_value_from_user'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_set.rb:53:in `write_from_user'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods/write.rb:50:in `write_attribute_with_type_cast'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods/write.rb:32:in `write_attribute'
from ar_enum.rb:43:in `encrypt_gender'
from ar_enum.rb:36:in `encrypt_before_save_and_decrypt_after_save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:382:in `block in make_lambda'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:285:in `block in halting'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:447:in `block in around'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:455:in `call'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:101:in `__run_callbacks__'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.2/lib/active_support/callbacks.rb:750:in `_run_save_callbacks'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/callbacks.rb:298:in `create_or_update'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/persistence.rb:125:in `save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/validations.rb:44:in `save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:319:in `block (2 levels) in save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:211:in `transaction'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:319:in `block in save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:334:in `rollback_active_record_state!'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:318:in `save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/suppressor.rb:41:in `save'
from /Users/kimura/work/ar_enum/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.2/lib/active_record/persistence.rb:34:in `create'
from ar_enum.rb:57:in `<main>'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment