Class: DompGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/domp/domp_generator.rb

Instance Method Summary collapse

Instance Method Details

#bundleObject



79
80
81
82
83
# File 'lib/generators/domp/domp_generator.rb', line 79

def bundle
  Bundler.with_clean_env do
    run 'bundle install'
  end
end

#check_if_devise_has_generated_modelObject



13
14
15
16
17
18
# File 'lib/generators/domp/domp_generator.rb', line 13

def check_if_devise_has_generated_model
  unless File.exists?("app/models/#{file_name}.rb")
    puts "Could not find `app/models/#{file_name}.rb`. You need to generate it with Devise first."
    exit
  end
end

#check_if_devise_is_installedObject



6
7
8
9
10
11
# File 'lib/generators/domp/domp_generator.rb', line 6

def check_if_devise_is_installed
  unless File.exists?("config/initializers/devise.rb")
    puts "Devise not found. Install it first."
    exit
  end
end

#copy_templatesObject



71
72
73
74
75
76
77
# File 'lib/generators/domp/domp_generator.rb', line 71

def copy_templates
  template 'authentication_provider.rb', 'app/models/authentication_provider.rb'
  template "model_authentication.rb", "app/models/#{file_name}_authentication.rb"
  template "authentication_providers_migration.rb", "db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_create_authentication_providers.rb"
  template "model_authentications_migration.rb", "db/migrate/#{(Time.now + 1).strftime('%Y%m%d%H%M%S')}_create_#{class_name.downcase}_authentications.rb"
  template "omniauth_callbacks_controller.rb", "app/controllers/#{table_name}/omniauth_callbacks_controller.rb"
end

#migrateObject



85
86
87
# File 'lib/generators/domp/domp_generator.rb', line 85

def migrate
  rake 'db:migrate'
end

#update_devise_configObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/domp/domp_generator.rb', line 20

def update_devise_config
  omniauth_config = []

  providers.each do |provider|
    id = ask("#{provider.capitalize} application ID:")
    secret = ask("#{provider.capitalize} application secret:")
    omniauth_config << "\n  config.omniauth :#{provider.underscore}, '#{id}', '#{secret}'\n"
  end

  inject_into_file 'config/initializers/devise.rb', after: "# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'" do
    omniauth_config.join('')
  end
end

#update_gemfileObject



58
59
60
61
62
63
# File 'lib/generators/domp/domp_generator.rb', line 58

def update_gemfile
  gem 'omniauth'
  providers.each do |provider|
    gem "omniauth-#{provider}"
  end
end

#update_model_classObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/domp/domp_generator.rb', line 34

def update_model_class
  inject_into_class "app/models/#{file_name}.rb", class_name do
    "  has_many :authentications, class_name: '#{class_name}Authentication', dependent: :destroy\n"
  end

  inject_into_class "app/models/#{file_name}.rb", class_name do
    <<-METHOD.gsub(/^ {6}/, '')
      def self.create_from_omniauth(params)
        attributes = {
          email: params['info']['email'],
          password: Devise.friendly_token
        }

        create(attributes)
      end

    METHOD
  end

  inject_into_file "app/models/#{file_name}.rb", after: "  devise" do
    " :omniauthable,"
  end
end

#update_routesObject



65
66
67
68
69
# File 'lib/generators/domp/domp_generator.rb', line 65

def update_routes
  inject_into_file 'config/routes.rb', after: "devise_for :#{table_name}" do
    ", controllers: { omniauth_callbacks: '#{table_name}/omniauth_callbacks' }"
  end
end