Class: Rtrain::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#gem_infoObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/rtrain/install_generator.rb', line 16

def gem_info
return if options.copy_css? || options.add_nav? || options.add_homepage? || options.add_user_sessions? || options.ajaxify?
      puts "
      ----------------------------------------------------
      Rtrain needs an argument to continue.
      Please re-run the install generator with one of the
      following options:
      \n
         --copy_css
         --add_nav
         --add_homepage
         --add_user_sessions
         --ajaxify
      \n
      Refer to the README at https://github.com/lynnd335/rtrain
      for details and illustrations of each option's executions
      ----------------------------------------------------
      "
end

#require_gemsObject



11
12
13
14
# File 'lib/generators/rtrain/install_generator.rb', line 11

def require_gems
  gem('authlogic')
  gem('ffaker')
end

#run_rtrainObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/generators/rtrain/install_generator.rb', line 36

def run_rtrain


  dir = File.dirname(__FILE__)

  ##create rtrain logfile or open it if it already exists
    if !File.exist?("rtrain_log.txt")
      rtrain_log = File.new("rtrain_log.txt", "a")
      puts "\n
      ----------------------------------------------------
      Rtrain logfile created in main directory of app
      ----------------------------------------------------
      \n
      "
    else
      rtrain_log = open("rtrain_log.txt", "a")
    end
  rtrain_log_read = open(rtrain_log).read  
  ##end create rtrain logfile  
  
  ##add lines to config/routes.rb for auth
  routes_rb = open("config/routes.rb").read
    if !routes_rb.match("#session routes here")
      routes_rb['#'] = "\n#session routes here\n#"
    end 
  routes_rb_write = open("config/routes.rb", "w")
  routes_rb_write.write(routes_rb)
  routes_rb_write.close          
  ##end 

  ##Begin Copy CSS

  if options[:copy_css]
    stylesheets = "app/assets/stylesheets/"
    new_css = ["../templates/rtrain_scaffold.scss","../templates/forms.scss"]
    
    new_css.each do |n|
      new_css_path = File.join(dir, n)  
      FileUtils.cp(new_css_path, stylesheets)
    end  
    if !rtrain_log_read.match('_copy_css_run') 
      scaffold = open("app/assets/stylesheets/scaffolds.scss").read
      old_css = open("app/assets/stylesheets/scaffolds.scss", "w")
      old_css.write("/*\n" + scaffold + "\n*/")
      old_css.close
    end 
     puts "
        ----------------------------------------------------
        Rtrain Scaffold and forms css documents now active in app/assets/stylesheets/
        ----------------------------------------------------
      "
      
      open(rtrain_log, 'a') { |f| f.puts Time.now().to_s() + "_copy_css_run\n"}
  end

  ##end copy css
  
  ##begin add nvigation bar  

  if options[:add_nav]
    read_applayout = open("app/views/layouts/application.html.erb").read
    app_name = read_applayout.split("<title>")[1].split("</title>")[0]
    app_name = "\n<h1>"+app_name+"</h1>\n"
    tables = ActiveRecord::Base.connection.tables[1..-1]
    list_items = []
    tables.each do |t|
      if t != 'users'
        link = "<li><%=link_to '" + t.titleize + "'," + t + "_path%></li>"
        list_items.push(link)
      end 
    end

    nav = "\n<div id='main-navbar'>\n\t<ul id='menu'>\n\t\t" + list_items.join("\n\t\t") + "\n\t</ul>\n</div>\t\n\n\n"
    

    ##check to see if add_nav has already been run, update via overwrite if true, add if else

    if rtrain_log_read.match('_nav_added')
      old_menu = read_applayout.split("<ul id='menu'>")[1].split("</ul>")[0]
        if rtrain_log_read.match('_main_view_controller_added')
          list_items.unshift("<li><%=link_to 'Main', '/'%></li>")
        end
      new_menu = list_items.join("\n\t\t")
      read_applayout [old_menu] = new_menu
      new_applayout = open("app/views/layouts/application.html.erb", "w")
      new_applayout.write(read_applayout)
      new_applayout.close  
      open(rtrain_log, 'a') { |f| f.puts Time.now().to_s() + "_nav_updated-" + tables.to_s() + "\n"} 
      puts "
          ----------------------------------------------------
          Rtrain Nav Bar updated app/views/layouts/application.html.erb\n
          ----------------------------------------------------
        "     
    else  
      new_layout = read_applayout.split("<body>")
      read_applayout = open("app/views/layouts/application.html.erb", "w")
      read_applayout.write(new_layout[0] + "<body>" + app_name + nav + new_layout[1])
      read_applayout.close
      ###
      nav_css = "../templates/nav-bar.scss"
      nav_css_path = File.join(dir, nav_css)
      stylesheets = "app/assets/stylesheets/"
      FileUtils.cp(nav_css_path, stylesheets)
       puts "
          ----------------------------------------------------
          Rtrain Nav Bar and App Title now active in app/views/layouts/application.html.erb\n

          IMPORTANT! Be extra careful re-running this command, as it will overwrite to the default styling
          ----------------------------------------------------
        "
       open(rtrain_log, 'a') { |f| f.puts Time.now().to_s() + "_nav_added-" + tables.to_s() + "\n"}
    end
    # new_applayout.close  
  end  

  ##end add navigation bar

  ##begin add main page

  if options[:add_homepage]
    FileUtils.mkdir("app/views/main/")

    home_page = "../templates/home.html.erb"
    home_page_path = File.join(dir, home_page)
    main_view_dir = "app/views/main/"
    FileUtils.cp(home_page_path, main_view_dir)

    main = "../controllers/main_controller.rb"
    main_path = File.join(dir, main)
    controllers = "app/controllers"
    FileUtils.cp(main_path, controllers)

    root = open("config/routes.rb").read
    root ["# root 'welcome#index'"] = "root 'main#show', page: 'home'
    get '/main/:page' => 'pages#show'"
    routes = open("config/routes.rb","w")
    routes.write(root)
    routes.close
   
    puts "
        ----------------------------------------------------
        Rtrain Homepage Now Active and set as root URL, re-run '--add_nav' to have link appear in nav bar
        ----------------------------------------------------
      "
    open(rtrain_log, 'a') { |f| f.puts Time.now().to_s() + "_main_view_controller_added" + "\n"}
  end  

  ##end add homepage

  ##begin add sessions
  

  if options[:add_user_sessions]
    if !rtrain_log_read.match('_user_sessions_added')

      FileUtils.mkdir("app/views/users/")
      new_users = "../users/new.html.erb"
      new_users_path = File.join(dir, new_users)
      new_users_dir = "app/views/users/"
      FileUtils.cp(new_users_path, new_users_dir)

      FileUtils.mkdir("app/views/user_sessions/")
      new_user_sessions = "../user_sessions/new.html.erb"
      new_user_sessions_path = File.join(dir, new_user_sessions)
      new_user_sessions_dir = "app/views/user_sessions/"
      FileUtils.cp(new_user_sessions_path, new_user_sessions_dir)

      FileUtils.mkdir("app/views/shared/")
      errors = "../shared/_errors.html.erb"
      errors_path = File.join(dir, errors)
      errors_dir = "app/views/shared/"
      FileUtils.cp(errors_path, errors_dir)

      models = ["../models/user.rb","../models/user_session.rb"]
      controllers = ["../controllers/users_controller.rb","../controllers/user_sessions_controller.rb","../controllers/sessions_app_controller.rb"]
      
      models.each do |m|
        models_path = File.join(dir, m)
        models_dir = "app/models/"
        FileUtils.cp(models_path, models_dir)
      end  
      
      controllers.each do |c|
        controllers_path = File.join(dir, c)
        controllers_dir = "app/controllers/"
        FileUtils.cp(controllers_path, controllers_dir)
      end
      
      sessions_app_controller = open("app/controllers/sessions_app_controller.rb").read
      app_controller = open("app/controllers/application_controller.rb").read

      if app_controller.match("private")
        app_controller ["private"] = "private" + "\n" + sessions_app_controller
      else
        app_controller[app_controller.rindex('end')...(app_controller.rindex('end') + 'end'.length)] = app_controller + "private" + sessions_app_controller
      end

      current_app_controller = open("app/controllers/application_controller.rb", "w")
      current_app_controller.write(app_controller)
      current_app_controller.close


      session_css = "../templates/sign-in.scss"
      session_css_path = File.join(dir, session_css)
      stylesheets = "app/assets/stylesheets/"
      FileUtils.cp(session_css_path, stylesheets)
      applayout = open("app/views/layouts/application.html.erb").read
      new_layout = applayout.split("<body>")
      session_links = "
          <div id='signin-bar'>
              <% if current_user %>
                <span><%= current_user.email %></span>&nbsp;&nbsp;|&nbsp;&nbsp;
                <%= link_to 'Sign Out', sign_out_path, method: :delete %>
              <% else %>
                <%=link_to 'Sign Up', new_user_path%>&nbsp;&nbsp;|&nbsp;&nbsp;
                <%= link_to 'Sign In', sign_in_path %>
              <% end %>
          </div>\n"
      new_applayout = open("app/views/layouts/application.html.erb", "w")    
      new_applayout.write(new_layout[0] + "<body>\n" + session_links + new_layout[1])
      new_applayout.close
      forms = Dir.glob("app/**/_form.html.erb")
      session_check = "
          <%if !current_user%>
            <div id='blank'>
              <div id='alert'>
               <h3>Must be SIGNED IN to add item!</h3>
              </div>
             </div> 
          <%end%>\n"
      
      forms.each do |f|
        content = open(f).read
        form_erb = open(f,"w")
        form_erb.write(session_check + content)
        form_erb.close  
      end
      
      sesh = "
      resources :users, only: [:new, :create]
      
      resources :user_sessions, only: [:create, :destroy]

      delete '/sign_out', to: 'user_sessions#destroy', as: :sign_out
      get '/sign_in', to: 'user_sessions#new', as: :sign_in
      "

      sesh_route = open("config/routes.rb").read
      sesh_route ["#session routes here"] = sesh
      routes = open("config/routes.rb","w")
      routes.write(sesh_route)
      routes.close

      system("rails g migration CreateUser email:string crypted_password:string password_salt:string persistence_token:string")
      mig = Dir.glob("db/migrate/**.rb")[-1]
      mig_read = open(mig).read
      mig_read ["end"] = "\nt.timestamps null: false
                end
      add_index :users, :email, unique: true"
      mig_write = open(mig, 'w')
      mig_write.write(mig_read)
      mig_write.close


      system("bundle exec rake db:migrate")
       puts "
          ----------------------------------------------------
          Rtrain User Sessions Now Active
          ----------------------------------------------------
        "
      open(rtrain_log, 'a') { |f| f.puts Time.now().to_s() + "_user_sessions_added" + "\n"}  
     else
      puts "
          ----------------------------------------------------
          Rtrain User Sessions Already Active - Cannot re-run!
          ----------------------------------------------------
        "
     end 
    end

     ##end users sessions install

     ##begin ajaxify

    if options[:ajaxify]

      ajax_css = "../templates/ajax.scss"
      ajax_css_path = File.join(dir, ajax_css)
      stylesheets = "app/assets/stylesheets/"
      FileUtils.cp(ajax_css_path, stylesheets)
      
      ajax_files = ["../ajax_files/_delete.html.erb", "../ajax_files/_edit.html.erb", "../ajax_files/_form.html.erb", "../ajax_files/_index.html.erb", "../ajax_files/_new.html.erb", "../ajax_files/_save.js.erb", "../ajax_files/create.js.erb", "../ajax_files/delete.js.erb", "../ajax_files/destroy.js.erb", "../ajax_files/edit.js.erb", "../ajax_files/index.html.erb", "../ajax_files/new.js.erb", "../ajax_files/update.js.erb"]

      tables = ActiveRecord::Base.connection.tables.map{|x|x.classify.safe_constantize}.compact
      
      controllers = (tables.map{|x|x.name.downcase.pluralize + "_controller.rb"} - ["users_controller.rb"]).join(", ")

      stamp = (Time.now).to_s
      FileUtils.mkdir("rtrain_temp-" + stamp + "/")
      ajax_controller = "../controllers/ajax_controller.rb"
      ajax_controller_path = File.join(dir, ajax_controller)
      rtrain_temp = "rtrain_temp-" + stamp + "/"
      FileUtils.cp(ajax_controller_path, rtrain_temp)
      
      tables.each_with_index do |t, i|
        if t.name != "User"
          
          t_cols = tables[i].columns.map{|x|x.name} 
          t_cols -= ["id", "created_at", "updated_at"]
          view_dir = 'app/views/' + t.name.downcase.pluralize + '/'

          FileUtils.rm_rf(Dir.glob(view_dir + "*"))

          ajax_files.each do |a|
            ajax_files_path = File.join(dir, a)
            FileUtils.cp(ajax_files_path, view_dir)
          end

          docs = Dir[view_dir + "*"]

          def replacer(txt,filler,replace)
            if txt.match(filler)
              txt.gsub! filler, replace  
            end
          end


          new_controller_read = open("rtrain_temp-" + stamp + "/ajax_controller.rb").read
          controller_read = open("app/controllers/" + t.name.downcase.pluralize + "_controller.rb").read
          old_controller = "app/controllers/" + t.name.downcase.pluralize + "_controller.rb"

          FileUtils.cp(old_controller, rtrain_temp)
          

          controller_write = open("app/controllers/" + t.name.downcase.pluralize + "_controller.rb", "w")

          replacer(new_controller_read,"rt_mod_cap_plur",t.name.capitalize.pluralize)
          replacer(new_controller_read,"rt_mod_low_plur",t.name.downcase.pluralize)
          replacer(new_controller_read,"rt_mod_cap",t.name.capitalize)
          replacer(new_controller_read,"rt_mod_low",t.name.downcase)
          replacer(new_controller_read,"<-- permit_cols -->",t_cols.map{|c|":"+c}.join(", "))

          controller_write.write(new_controller_read)  
          controller_write.close

          docs.each do |doc|
            doc_read = open(doc).read
            doc_write = open(doc,"w")

            replacer(doc_read,"rt_mod_cap_plur",t.name.capitalize.pluralize)
            replacer(doc_read,"rt_mod_low_plur",t.name.downcase.pluralize)
            replacer(doc_read,"rt_mod_cap",t.name.capitalize)
            replacer(doc_read,"rt_mod_low",t.name.downcase)
            replacer(doc_read,"<!-- rt_mod_cols -->",t_cols.map{|c|"<td><%= "+t.name.downcase + "." + c.downcase + " %></td>"}.join("\n\t\t\t"))
            replacer(doc_read,"<!-- rt_mod_hdr_cols -->",t_cols.map{|c|"<td>" + c.capitalize + "</td>"}.join("\n\t\t\t"))
            replacer(doc_read,"<!-- rt_mod_fields -->",t_cols.map{|c|"<div class='field'>\n\t<%= f.label :" + c.downcase + " %><br>\n<%= f.text_field :" + c.downcase + '%></div>'}.join("\n\t\t\t"))
            replacer(doc_read,"rt_mod_1st_col",t_cols[0].capitalize)

            doc_write.write(doc_read)
            doc_write.close

          end 

        end 
      end
      puts "
          ----------------------------------------------------
          Rails App Views and Controllers Ajaxified

          NOTE: " + controllers + " have each been overwritten. 
          
          The prior existing versions of those files have been 
          copied into the directory named " + rtrain_temp + "in 
          the main directory of this app and can be found there.
          ----------------------------------------------------
        "
      open(rtrain_log, 'a') { |f| f.puts Time.now().to_s() + "_ajaxified" + "\n"}  
    end  
  
    ##end ajaxify

  rtrain_log.close
end