Class: Turbolog::Generators::ConfigGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blue(mytext) ⇒ Object



9
10
11
# File 'lib/generators/turbolog/config_generator.rb', line 9

def self.blue(mytext) 
   "\e[34m#{mytext}\e[0m".center(40)
end

.source_rootObject



5
6
7
# File 'lib/generators/turbolog/config_generator.rb', line 5

def self.source_root
  File.dirname(__FILE__) + "/templates"
end

Instance Method Details

#add_specObject



125
126
127
128
129
# File 'lib/generators/turbolog/config_generator.rb', line 125

def add_spec
  directory "spec"
  # Added sample calculator test
  directory "lib"
end

#authenticate_userObject



61
62
63
64
65
66
# File 'lib/generators/turbolog/config_generator.rb', line 61

def authenticate_user
  puts Color.blue("..............Authenticate user in application..........\n")
  inject_into_file 'app/controllers/application_controller.rb', :after => 'class ApplicationController < ActionController::Base' do
    "\n  before_action :authenticate_user!\n"
  end
end

#config_deviseObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/turbolog/config_generator.rb', line 26

def config_devise
  puts Color.blue("..................rails g devise:install................\n")
  run "rails generate devise:install"
  puts Color.blue(" ..............Remove devise from routes.............\n")
  gsub_file 'config/routes.rb',/devise_for.*\n/,''
  puts Color.blue("....................rails g devise User.................\n")
  run "rails generate devise User"
  gsub_file 'config/initializers/devise.rb',/# config.secret_key/,'config.secret_key'
  gsub_file 'config/initializers/devise.rb',/config.sign_out_via = :delete/,'config.sign_out_via = :get'
end

#config_mongoidObject



16
17
18
# File 'lib/generators/turbolog/config_generator.rb', line 16

def config_mongoid
  run "rails g mongoid:config"
end

#config_rootObject



46
47
48
49
50
# File 'lib/generators/turbolog/config_generator.rb', line 46

def config_root
  # set root to Welcome
  puts Color.blue(".................config routes for user ................\n")
  copy_file "routes.rb","config/routes.rb"
end

#create_welcomeObject



39
40
41
42
43
44
# File 'lib/generators/turbolog/config_generator.rb', line 39

def create_welcome

  run "rails g scaffold welcome greeting:text"
  #copy_file "welcomes_controller_spec.rb","spec/controllers"
  #copy_file "welcomes_spec.rb","spec/models"
end

#dot_optionObject



52
53
54
55
56
57
58
# File 'lib/generators/turbolog/config_generator.rb', line 52

def dot_option
  # set root to Welcome
  puts Color.blue("......................config .rspec ....................\n")
  copy_file ".rspec",".rspec"
  copy_file ".env",".env"
  copy_file ".gitignore",".gitignore" #protect .env
end

#finishObject



131
132
133
134
135
136
137
138
# File 'lib/generators/turbolog/config_generator.rb', line 131

def finish
  puts "\n"
  puts Color.blue("________________________________________________________\n")
  puts Color.blue("                 Finished Step 3/3\n")
  puts Color.red("       HOT FIX: PLEASE REMOVE DIRECTORY \"spec\"  \n")
  puts Color.blue("________________________________________________________\n")

end

#log_in_outObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/turbolog/config_generator.rb', line 97

def 
 inject_into_file 'app/views/layouts/application.html.erb', :after => '<body>' do
  "\n                                           "+
  "\n    <p class=\"notice\"><%= notice %></p>" +
  "\n    <p class=\"alert\"><%= alert %></p>" +
  "\n    <% if user_signed_in? %>" +
  "\n      <%= link_to \"Log Out\", destroy_user_session_path, method: :delete %>" +
  "\n    <% else %>" +
  "\n      <%= link_to(\"Log In\", new_user_session_path) %>" +
  "\n    <% end %>\n" 
  end
end

#set_default_mailerObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/generators/turbolog/config_generator.rb', line 112

def set_default_mailer
 inject_into_file 'config/environments/development.rb', :after => 'ails.application.configure do' do
  "\n  ## config default mail server \n" +
  "  config.action_mailer.default_url_options = { host\: \"localhost\:3000\" }\n"
  end
  inject_into_file 'config/environments/test.rb', :after => 'ails.application.configure do' do
  "\n  ## config default mail server \n" +
  "  config.action_mailer.default_url_options = { host\: \"localhost\:3000\" }\n"
  end 
end

#sethost_mongoidObject



20
21
22
23
# File 'lib/generators/turbolog/config_generator.rb', line 20

def sethost_mongoid
  puts Color.blue("..............change localhost to 127.0.0.1.............\n")
  gsub_file 'config/mongoid.yml','localhost:27017','127.0.0.1:27017'
end

#setup_omniauthObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/generators/turbolog/config_generator.rb', line 69

def setup_omniauth
  puts Color.blue("...............config devise for facebook...............\n")
  inject_into_file 'config/initializers/devise.rb', :after => 'Devise.setup do |config|' do
    "\n  config.omniauth :facebook, ENV['FACEBOOK_API'], ENV['FACEBOOK_SECRET']\n"
  end
  puts Color.blue("..............config user model for facebook............\n")
  inject_into_file 'app/models/user.rb', :after => 'include Mongoid::Document' do
  "\n\n  include Mongoid::Attributes::Dynamic" +
    "\n  devise :omniauthable, :omniauth_providers => [:facebook]" +
    "\n  def self.from_omniauth(auth)" +
    "\n     where(provider: auth.provider, uid: auth.uid).first_or_create do |user|" +
    "\n     user.email = auth.info.email" +
    "\n     user.password = Devise.friendly_token[0,20]" +
    "\n    end" + 
    "\n  end\n" 
  end

  puts Color.blue("...........Add field [provider] to user model...........\n")
  inject_into_file 'app/models/user.rb', :after => '  field :encrypted_password, type: String, default: ""' do
    "\n  field :provider,           type: String, default: \"\"\n"
    "\n  field :admin,              type: Boolean, default: false \n"
  end

end