Browse Source

Clean up the way user corp checking is handled.

old-ruby
Ben Kurtovic 9 years ago
parent
commit
bfdbaa3ad9
5 changed files with 41 additions and 13 deletions
  1. +4
    -1
      app/models/user.rb
  2. +5
    -0
      db/migrate/20150316234044_remove_is_corp_from_user.rb
  3. +1
    -2
      db/schema.rb
  4. +1
    -1
      db/seeds.rb
  5. +30
    -9
      lib/tasks/calefaction.rake

+ 4
- 1
app/models/user.rb View File

@@ -3,7 +3,6 @@ require 'eaal'
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_secure_password has_secure_password
alias_attribute :admin?, :is_admin alias_attribute :admin?, :is_admin
alias_attribute :corp?, :is_corp


def char_names def char_names
ensure_api_user ensure_api_user
@@ -14,6 +13,10 @@ class User < ActiveRecord::Base
end end
end end


def in_corp?
member_of? AdminSettings.get(:corp_id)
end

def member_of?(corp_id) def member_of?(corp_id)
ensure_api_user ensure_api_user
@api.scope = 'char' @api.scope = 'char'


+ 5
- 0
db/migrate/20150316234044_remove_is_corp_from_user.rb View File

@@ -0,0 +1,5 @@
class RemoveIsCorpFromUser < ActiveRecord::Migration
def change
remove_column :users, :is_corp, :boolean
end
end

+ 1
- 2
db/schema.rb View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "admin_settings", force: :cascade do |t|
t.string "key" t.string "key"
@@ -25,7 +25,6 @@ ActiveRecord::Schema.define(version: 20150314013929) do
t.string "api_key" t.string "api_key"
t.string "api_verify" t.string "api_verify"
t.boolean "is_admin" t.boolean "is_admin"
t.boolean "is_corp"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end


+ 1
- 1
db/seeds.rb View File

@@ -8,7 +8,7 @@


AdminSetting.add(:site_name, 'Your Corporation\'s Site') AdminSetting.add(:site_name, 'Your Corporation\'s Site')
AdminSetting.add(:corp_name, 'Your Corporation') 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(:copyright, 'Your Corporation/Your Name')
AdminSetting.add(:description, AdminSetting.add(:description,
'Welcome to your corporation\'s website! This message can be changed in the admin settings panel.') 'Welcome to your corporation\'s website! This message can be changed in the admin settings panel.')


+ 30
- 9
lib/tasks/calefaction.rake View File

@@ -1,8 +1,29 @@
require 'io/console' require 'io/console'


namespace :calefaction do 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 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: " print "Enter your corporation's name: "
corp_name = STDIN.gets.chomp corp_name = STDIN.gets.chomp


@@ -16,13 +37,12 @@ namespace :calefaction do


print "\nEnter your character's API key ID (create one at\n"\ print "\nEnter your character's API key ID (create one at\n"\
"https://community.eveonline.com/support/api-key/createpredefined?accessMask=8): " "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: " 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 chars = user.char_names
if chars.empty? if chars.empty?
puts "The given API key is invalid, has no characters, or something "\ puts "The given API key is invalid, has no characters, or something "\
@@ -32,6 +52,7 @@ namespace :calefaction do


if chars.length == 1 if chars.length == 1
user.name = chars.first user.name = chars.first
puts "\nUsing character: #{user.name}"
else else
puts "\nChoose a character:" puts "\nChoose a character:"
chars.each_with_index do |name, i| chars.each_with_index do |name, i|
@@ -40,7 +61,7 @@ namespace :calefaction do
puts "Enter the number next to your chosen character: " puts "Enter the number next to your chosen character: "
loop do loop do
index = STDIN.gets.to_i index = STDIN.gets.to_i
break unless index >= chars.length || index < 0
break if index >= 0 && index < chars.length
puts "Bad input; try again: " puts "Bad input; try again: "
end end
user.name = chars[index] user.name = chars[index]
@@ -66,10 +87,10 @@ namespace :calefaction do
end end
puts 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 user.save

puts "\nDone!" puts "\nDone!"
end end
end end

Loading…
Cancel
Save