Class: Infinum::Omniauth::Generators::ConfigGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/infinum/omniauth/config_generator.rb

Constant Summary collapse

CONFIGURATION =
{}
INITIALIZER_LOCATION =
"config/initializers/omniauth.rb"
OMNIAUTH_CONTROLLER_LOCATION =
"app/controllers/omniauth_controller.rb"

Instance Method Summary collapse

Instance Method Details

#add_omniauth_user_creationObject

create_user_model



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 78

def add_omniauth_user_creation
  method =   "\n"\
        " def self.new_from_omniauth(omniauth)\n"\
            "   user = self.find_by_uid(omniauth['uid']) || self.find_by_email(omniauth[:extra][:email]) || self.new(:uid => omniauth['uid'])\n"\
            "   user.uid = omniauth['uid']\n"\
            "   user.first_name = omniauth['extra']['first_name']\n"\
            "   user.last_name = omniauth['extra']['last_name']\n"\
            "   user.email = omniauth['extra']['email']\n"\
            "   user.avatar_url = omniauth['extra']['avatar_url']\n"\
            "   user.save\n"\
            "   user\n"\
            " end"
      append_to_file   "app/models/#{user_model_name.downcase}.rb", method,
          after: Regexp.new("^.*" + Regexp.escape(" < ActiveRecord::Base") + ".*$", Regexp::IGNORECASE)
end

#ask_user_for_configurationObject

copy_configuration



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 40

def ask_user_for_configuration
  return nil if CONFIGURATION['no_config']
  # User model name
  CONFIGURATION['user_model_name'] = ask "Define user model name:"
  CONFIGURATION['user_model_name'] = "User" if check_option CONFIGURATION['user_model_name']
  # App ID
  CONFIGURATION['app_id'] = ask "App id:"
  CONFIGURATION['app_id'] = "APP_ID" if check_option CONFIGURATION['app_id']
  # App secret
  CONFIGURATION['app_secret'] = ask "App secret:"
  CONFIGURATION['app_secret'] = "APP_SECRET" if check_option CONFIGURATION['app_secret']
end

#check_installationObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 16

def check_installation
  installation_files = [INITIALIZER_LOCATION, OMNIAUTH_CONTROLLER_LOCATION]
  valid_install = true
  installation_files.each do |file_location|
    valid_install = valid_install && File.exists?(file_location)
  end
  if !valid_install
    say "###############", :red
    say "### WARNING ###", :red
    say "###############", :red
    say "None or partial installation was detected!"
    say "Please run the install generator before"
    say "running this generator:"
    say "  rails generate omniauth_infinum:install"
  end
end

#copy_configurationObject

check_installation



33
34
35
36
37
38
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 33

def copy_configuration
  # This is just to make the configuration writeable
  options.each do |key, value|
    CONFIGURATION[key] = value
  end
end

#create_user_modelObject

explane_to_user



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 53

def create_user_model
  if File.exists? "models/#{user_model_name}"
    # Generate migration for existing model
    say "Generating migration files for #{user_model_name} model", :green
    command =   "cd #{Rails.root.to_s};"\
          "rails g migration add_auth_to_#{user_model_name.downcase}"\
          " first_name:string last_name:string email:string avatar_url:string uid:string"
    system command
    # Warn the user
    say ""
    say "############", :red
    say "### NOTE ###", :red
    say "############", :red
    say "Unnecessary columns should be deleted by hand"
    say "from the newly generated migration file!"
    say ""
  else
    # Generate model
    say "Generating #{user_model_name} model", :green
    command =   "cd #{Rails.root.to_s};"\
          "rails g model #{user_model_name} first_name:string last_name:string email:string avatar_url:string uid:string"
    system command
  end
end

#rename_user_model_in_controllersObject

add_omniauth_user_creation



94
95
96
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 94

def rename_user_model_in_controllers
  gsub_file OMNIAUTH_CONTROLLER_LOCATION, "USER_MODEL", user_model_name
end

#set_app_id_and_secretObject

rename_user_model_in_controllers



98
99
100
101
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 98

def set_app_id_and_secret
  gsub_file INITIALIZER_LOCATION, "APP_ID", app_id
  gsub_file INITIALIZER_LOCATION, "APP_SECRET", app_secret
end

#set_user_model_for_authentificationObject

set_app_id_and_secret



103
104
105
# File 'lib/generators/infinum/omniauth/config_generator.rb', line 103

def set_user_model_for_authentification
  gsub_file "app/controllers/application_controller.rb", "USER_MODEL", user_model_name
end