Class: SearchRails::SearchInstallDevise

Inherits:
Object
  • Object
show all
Defined in:
lib/search_rails.rb

Instance Method Summary collapse

Instance Method Details

#create_filesObject



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
# File 'lib/search_rails.rb', line 269

def create_files
	require "colorize"
	require "fileutils"
	root = Dir.pwd
	Dir.chdir(root + '/app')
	if File.exists?('search')
		puts 'Already Exists'.bold.green + ' /app/search'.bold
	else
		FileUtils::mkdir_p "search"
		puts "Created ".green.bold + '/app/search'.bold
	end
	Dir.chdir(root + '/app/search')
	if File.exists?('search_module.rb')
		puts 'Already Exists'.bold.green + ' /app/search/search_module.rb'.bold
	else
		File.new "search_module.rb", "w"
		puts "Created ".green.bold + '/app/search/search_module.rb'.bold
	end
	Dir.chdir(root + '/app/controllers')
	if File.exists?('searches_controller.rb')
		puts 'Already Exists'.bold.green + ' /app/controllers/searches_controller.rb'.bold
	else
		File.new "searches_controller.rb", "w"
		puts "Created ".green.bold + '/app/controllers/searches_controller.rb'.bold
	end
	Dir.chdir(root + '/app/models')
	if File.exists?('search.rb')
		puts 'Already Exists'.bold.green +  ' /app/models/search.rb'.bold
	else
		File.new "search.rb", "w"
		puts 'Created '.green.bold + '/app/models/search.rb'.bold
	end
	Dir.chdir(root + '/db')
	if File.exists?('migrate')
		puts 'Already Exists'.bold.green + ' /db/migrate'.bold
	else
		FileUtils::mkdir_p "migrate"
		puts "Created ".green.bold + '/db/migrate'.bold
	end
	Dir.chdir(root)
end

#runObject



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/search_rails.rb', line 502

def run
	require 'colorize'
	root = Dir.pwd

	puts "Syntax: 'install OBJECT ATTRIBUTE:TYPE'".bold.on_red
	command = gets.chomp
	command = command.split(" ")
	if command[0] == 'install'
		puts ''
		puts 'Creating Files...'.bold
		object = command[1]
		attributes = command[2..10000000]
	end

	SearchInstallDevise.new.create_files
	SearchInstallDevise.new.write_search_controller
	SearchInstallDevise.new.write_search_model
	SearchInstallDevise.new.write_search_module(object, attributes)
	SearchInstallDevise.new.write_application_controller
	SearchInstallDevise.new.write_migration
	SearchInstallDevise.new.update_routes
	
	puts 'Done'.bold
	puts ''
	

end

#update_routesObject



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/search_rails.rb', line 478

def update_routes
	require "fileutils"
	root = Dir.pwd
	Dir.chdir(root + '/config')
	first_line = IO.readlines("routes.rb")[0]
	other_lines = IO.readlines("routes.rb")[1..1000000000]
	second_line = IO.readlines("routes.rb")[1]
	if second_line.chomp == 'resources :searches do'
		puts 'Already Exists'.bold.green + ' /config/routes.rb'.bold
	else
		File.open('routes.rb', 'w') do |line|
			line.puts first_line
			line.puts 'resources :searches do'
   		line.puts '	member do'
    	line.puts "		get 'clear'"
    	line.puts '	end'
  		line.puts 'end'
  		line.puts other_lines
		end
		puts 'Updated '.bold.green + '/config/routes.rb'.bold
	end
	Dir.chdir(root)
end

#write_application_controllerObject



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/search_rails.rb', line 438

def write_application_controller
	require "fileutils"
	root = Dir.pwd
	Dir.chdir(root + '/app/controllers')
	first_line = IO.readlines("application_controller.rb")[0]
	other_lines = IO.readlines("application_controller.rb")[1..1000000000]
	second_line = IO.readlines("application_controller.rb")[1]

	if second_line.chomp == '	before_action :check_for_search'
		puts 'Already Exists'.bold.green + ' /app/controllers/application_controller.rb'.bold
	else
		FileUtils::mkdir_p "migrate"
		puts "Updated ".green.bold + '/app/controllers/application_controller.rb'.bold
		File.open("application_controller.rb", "w") do |line|
			line.puts first_line
			line.puts '	before_action :check_for_search'
			line.puts ''
			line.puts '	def check_for_search'
			line.puts "		$LOAD_PATH.unshift(File.dirname('../app/search'))"
			line.puts '		extend SearchModule'
			line.puts '		check_for_search'
			line.puts '	end'
			line.puts ''
			line.puts other_lines
		end
	end
	Dir.chdir(root)
end

#write_migrationObject



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/search_rails.rb', line 410

def write_migration
	require "fileutils"
	require 'colorize'
	root = Dir.pwd
	Dir.chdir(root + '/db/migrate')
	require 'date'
	@time = DateTime.now.strftime('%Y%m%d%H%M%S')
	if Dir[root + '/db/migrate/*_create_searches.rb'].count > 0
		file = Dir[root + '/db/migrate/*_create_searches.rb'].first
		puts 'Already Exists '.bold.green + file[38..100].bold
	else
		File.new @time + '_create_searches.rb', "w"
		puts "Created ".green.bold + '/db/migrate/'.bold + @time.bold + '_create_searches.rb'.bold
		File.open(@time + "_create_searches.rb", "a") do |line|
			line.puts 'class CreateSearches < ActiveRecord::Migration'
			line.puts '	def change'
			line.puts '		create_table :searches do |t|'
			line.puts '			t.string :search'
			line.puts '			t.integer :user_id'
			line.puts '			t.timestamps null: false'
			line.puts '		end'
			line.puts '	end'
			line.puts 'end'
		end
	end
	Dir.chdir(root)
end

#write_search_controllerObject



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
# File 'lib/search_rails.rb', line 374

def write_search_controller
	root = Dir.pwd
	Dir.chdir(root + '/app/controllers')
	File.open("searches_controller.rb", "a") do |line|
		line.puts 'class SearchesController < ApplicationController'
		line.puts '	before_action :set_search, only: [:show, :edit, :update, :destroy]'
		line.puts '	before_action :include_search_module, only: [ :clear, :edit, :update ]'
		line.puts ''
		line.puts '	def clear'
	  line.puts '		clear'
	  line.puts '	end'
	  line.puts ''
	  line.puts '	def update'
	  line.puts '		update_search'
	  line.puts '	end'
	  line.puts ''
	  line.puts '	private'
	  line.puts ''
	  line.puts '		def set_search'
	  line.puts '			@search = Search.find_by(user_id: current_user.id)'
	  line.puts '		end'
	  line.puts ''
	  line.puts '		def include_search_module'
	  line.puts "			$LOAD_PATH.unshift(File.dirname('../app/search'))"
	  line.puts '			extend SearchModule'
	  line.puts '		end'
	  line.puts ''
	  line.puts '		def search_params'
	  line.puts '			params.require(:search).permit(:search, :user_id)'
	  line.puts '		end'
	  line.puts ''
		line.puts 'end'
	end
	Dir.chdir(root)
end

#write_search_modelObject



467
468
469
470
471
472
473
474
475
476
# File 'lib/search_rails.rb', line 467

def write_search_model
	require "fileutils"
	root = Dir.pwd
	Dir.chdir(root + '/app/models')
	File.open("search.rb", "w") do |line|
		line.puts 'class Search < ActiveRecord::Base'
		line.puts 'end'
	end
	Dir.chdir(root)
end

#write_search_module(object, attributes) ⇒ Object



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
# File 'lib/search_rails.rb', line 311

def write_search_module(object, attributes)
	root = Dir.pwd
	require 'active_support/inflector'
	Dir.chdir(root + '/app/search')
	File.open("search_module.rb", "a") do |line|
		line.puts 'module SearchModule'
		line.puts '	def search'
		attri_one = attributes[0]
		attribute = attri_one.split(':')
		line.puts "		@" + object.downcase.pluralize + " = " + object.capitalize.singularize + ".where('lower(" + attribute[0] + ") LIKE ?', '%' + @search.search.downcase + '%')"
		attributes = attributes.reject { |attri| attri == attributes[0] }
		attributes.each do |attri|
			attribute = attri.split(':')
			if attribute[1] == 'integer' || attribute[1] == 'decimal'
				line.puts "		" + object.capitalize.singularize + ".where('cast(" + attribute[0] + " AS VARCHAR) LIKE ?', '%' + @search.search + '%').each { |" + attribute[0] + "| @" + object.downcase.pluralize + ".any? { |f| f.id == " + attribute[0] + ".id } ? true : @" + object.downcase.pluralize + " << " + attribute[0] + " }"
			elsif attribute[1] == 'boolean'
				line.puts "		if @search.search.downcase == 'true' || @search.search.downcase == 'tru' || @search.search.downcase == 'tr' || @search.search.downcase == 't'"
	      line.puts "			"	+ object.capitalize.singularize + ".where(" + attribute[0] + ": true).each { |" + attribute[0] + "| @" + object.downcase.pluralize + ".any? { |f| f.id == " + attribute[0] + ".id } ? true : @" + object.downcase.pluralize + " << " + attribute[0] + " }"
	      line.puts "		end"
	      line.puts "		if @search.search.downcase == 'false' || @search.search.downcase == 'fals' || @search.search.downcase == 'fal' || @search.search.downcase == 'fa' || @search.search.downcase == 'f'"
	      line.puts "			" + object.capitalize.singularize + ".where(" + attribute[0] + ": false).each { |" + attribute[0] + "| @" + object.downcase.pluralize + ".any? { |f| f.id == " + attribute[0] + ".id } ? true : @" + object.downcase.pluralize + " << " + attribute[0] + " }"
	      line.puts "		end"
			else
				line.puts "		" + object.capitalize.singularize + ".where('lower(" + attribute[0] + ") LIKE ?', '%' + @search.search + '%').each { |" + attribute[0] + "| @" + object.downcase.pluralize + ".any? { |f| f.id == " + attribute[0] + ".id } ? true : @" + object.downcase.pluralize + " << " + attribute[0] + " }"
			end
		end
		line.puts '	end'
		line.puts ''
		line.puts '	def clear'
		line.puts '		@search = Search.find_by(user_id: current_user.id)'
		line.puts '		@search.search = nil'
		line.puts '		@search.save'
		line.puts '		redirect_to ' + object.pluralize.downcase + '_path'
		line.puts '	end'
		line.puts ''
		line.puts '	def check_for_search'
		line.puts '		if user_signed_in?'
		line.puts '			if Search.find_by(user_id: current_user.id) == nil'
		line.puts '				@search = Search.new'
		line.puts '				@search.search = nil'
		line.puts '				@search.user_id = current_user.id'
		line.puts '				@search.save'
		line.puts '			end'
		line.puts '		end'
		line.puts '	end'
		line.puts ''
		line.puts '	def update_search'
		line.puts '		respond_to do |format|'
		line.puts '			if @search.update(search_params)'
		line.puts '				format.html { redirect_to :back }'
		line.puts '				format.json { render :show, status: :ok, location: @search }'
		line.puts '			else'
		line.puts '				format.html { render :edit }'
		line.puts '				format.json { render json: @search.errors, status: :unprocessable_entity }'
		line.puts '			end'
		line.puts '		end'
		line.puts '	end'

		line.puts 'end'
	end
	Dir.chdir(root)
end