From 7a2fc528badd386d3a150388eb92cfd8cffd5136 Mon Sep 17 00:00:00 2001 From: Nicholas Shook Date: Wed, 24 Jun 2015 14:49:18 -0700 Subject: [PATCH] Added json support for tokens In the installation generator script, the migration template checks to see if the app is using postgres by seeing what sub-class of ``ActiveRecord::ConnectionAdapter`` the app is using. If it is postgres, or mysql with the correct version, the app will use a token column instead of a text one Postgres has JSON types as of 9.3 Mysql has JSON types as of 5.7.7 http://mysqlserverteam.com/json-labs-release-native-json-data-type-and-binary-format/ teeny refactoring --- .../devise_token_auth/install_generator.rb | 28 +++++++++++++++++++ .../devise_token_auth_create_users.rb.erb | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/generators/devise_token_auth/install_generator.rb b/lib/generators/devise_token_auth/install_generator.rb index a9b8cdea4..5a7521ba7 100644 --- a/lib/generators/devise_token_auth/install_generator.rb +++ b/lib/generators/devise_token_auth/install_generator.rb @@ -115,5 +115,33 @@ def parse_file_for_line(filename, str) end match end + + def json_supported_database? + (postgres? && postgres_correct_version?) || (mysql? && mysql_correct_version?) + end + + def postgres? + database_name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter' + end + + def postgres_correct_version? + database_version > '9.3' + end + + def mysql? + database_name == 'ActiveRecord::ConnectionAdapters::MysqlAdapter' + end + + def mysql_correct_version? + database_version > '5.7.7' + end + + def database_name + ActiveRecord::Base.connection.class.name + end + + def database_version + ActiveRecord::Base.connection.select_value('SELECT VERSION()') + end end end diff --git a/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb b/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb index a384daace..20358cad8 100644 --- a/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +++ b/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb @@ -40,7 +40,7 @@ class DeviseTokenAuthCreate<%= user_class.pluralize %> < ActiveRecord::Migration t.string :email ## Tokens - t.text :tokens + <%= json_supported_database? ? 't.json :tokens' : 't.text :tokens' %> t.timestamps end