From bfdbaa3ad9dad8d11aa244e7a8e1f519c8d2f7f9 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 16 Mar 2015 19:23:51 -0500 Subject: [PATCH] Clean up the way user corp checking is handled. --- app/models/user.rb | 5 ++- .../20150316234044_remove_is_corp_from_user.rb | 5 +++ db/schema.rb | 3 +- db/seeds.rb | 2 +- lib/tasks/calefaction.rake | 39 +++++++++++++++++----- 5 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20150316234044_remove_is_corp_from_user.rb diff --git a/app/models/user.rb b/app/models/user.rb index ea08488..bb081bc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,7 +3,6 @@ require 'eaal' class User < ActiveRecord::Base has_secure_password alias_attribute :admin?, :is_admin - alias_attribute :corp?, :is_corp def char_names ensure_api_user @@ -14,6 +13,10 @@ class User < ActiveRecord::Base end end + def in_corp? + member_of? AdminSettings.get(:corp_id) + end + def member_of?(corp_id) ensure_api_user @api.scope = 'char' diff --git a/db/migrate/20150316234044_remove_is_corp_from_user.rb b/db/migrate/20150316234044_remove_is_corp_from_user.rb new file mode 100644 index 0000000..110fc76 --- /dev/null +++ b/db/migrate/20150316234044_remove_is_corp_from_user.rb @@ -0,0 +1,5 @@ +class RemoveIsCorpFromUser < ActiveRecord::Migration + def change + remove_column :users, :is_corp, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 32785a9..09c6889 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150314013929) do +ActiveRecord::Schema.define(version: 20150316234044) do create_table "admin_settings", force: :cascade do |t| t.string "key" @@ -25,7 +25,6 @@ ActiveRecord::Schema.define(version: 20150314013929) do t.string "api_key" t.string "api_verify" t.boolean "is_admin" - t.boolean "is_corp" t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/db/seeds.rb b/db/seeds.rb index 37a7ea3..7828b47 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -8,7 +8,7 @@ AdminSetting.add(:site_name, 'Your Corporation\'s Site') AdminSetting.add(:corp_name, 'Your Corporation') -AdminSetting.add(:corp_id, '1') +AdminSetting.add(:corp_id, '0') AdminSetting.add(:copyright, 'Your Corporation/Your Name') AdminSetting.add(:description, 'Welcome to your corporation\'s website! This message can be changed in the admin settings panel.') diff --git a/lib/tasks/calefaction.rake b/lib/tasks/calefaction.rake index 6d0dd0f..5536fb5 100644 --- a/lib/tasks/calefaction.rake +++ b/lib/tasks/calefaction.rake @@ -1,8 +1,29 @@ require 'io/console' namespace :calefaction do - desc "Sets some initial database values and creates an admin user" + desc "Set some initial database values and creates an admin user" task setup: :environment do + begin + AdminSetting.get(:test) + rescue ActiveRecord::StatementInvalid + puts "The database has not been set up properly. You need to run\n"\ + "`rake db:setup` first." + next + end + + if AdminSetting.get(:corp_id).nil? + puts "The database does not contain the correct seed values. You need "\ + "to run\n`rake db:reset db:setup` first." + next + end + + if AdminSetting.get(:corp_id).to_i > 0 + puts "The database is not empty; you should change settings from "\ + "within the\napplication. Alternatively, you can start over with\n"\ + "`rake db:reset db:setup calefaction:setup`." + next + end + print "Enter your corporation's name: " corp_name = STDIN.gets.chomp @@ -16,13 +37,12 @@ namespace :calefaction do print "\nEnter your character's API key ID (create one at\n"\ "https://community.eveonline.com/support/api-key/createpredefined?accessMask=8): " - user_api_key = STDIN.gets.chomp + api_key = STDIN.gets.chomp print "\nEnter the verification code for the key you just entered: " - user_api_verify = STDIN.gets.chomp + api_verify = STDIN.gets.chomp - user = User.new(api_key: user_api_key, api_verify: user_api_verify, - is_admin: true, is_corp: true) + user = User.new(api_key: api_key, api_verify: api_verify, admin?: true) chars = user.char_names if chars.empty? puts "The given API key is invalid, has no characters, or something "\ @@ -32,6 +52,7 @@ namespace :calefaction do if chars.length == 1 user.name = chars.first + puts "\nUsing character: #{user.name}" else puts "\nChoose a character:" chars.each_with_index do |name, i| @@ -40,7 +61,7 @@ namespace :calefaction do puts "Enter the number next to your chosen character: " loop do index = STDIN.gets.to_i - break unless index >= chars.length || index < 0 + break if index >= 0 && index < chars.length puts "Bad input; try again: " end user.name = chars[index] @@ -66,10 +87,10 @@ namespace :calefaction do end puts - AdminSetting.where(key: %w(corp_name site_name)).update_all(value: corp_name) - AdminSetting.find_by(key: 'corp_id').update(value: corp_id) + AdminSetting.set(:corp_name, corp_name) + AdminSetting.set(:site_name, corp_name) + AdminSetting.set(:corp_id, corp_id) user.save - puts "\nDone!" end end