lib/search_rails.rb in search_rails-1.0.9 vs lib/search_rails.rb in search_rails-1.1.0

- old
+ new

@@ -6,23 +6,43 @@ def create_files require 'colorize' require "fileutils" root = Dir.pwd Dir.chdir(root + '/app') - FileUtils::mkdir_p "search" - puts "Created ".green.bold + '/app/search'.bold + 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') - File.new "search_module.rb", "w" - puts "Created ".green.bold + '/app/search/search_module.rb'.bold + if File.exists?('search_module.rb') + File.open(root + '/app/search/search_module.rb', 'w') {|file| file.truncate(0) } + 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') - File.new "searches_controller.rb", "w" - puts "Created ".green.bold + '/app/controllers/searches_controller.rb'.bold + if File.exists?('searches_controller.rb') + File.open(root + '/app/controllers/searches_controller.rb', 'w') {|file| file.truncate(0) } + 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') - File.new "search.rb", "w" - puts 'Created '.green.bold + 'app/models/search.rb'.bold + if File.exists?('search.rb') + File.open(root + '/app/models/search.rb', 'w') {|file| file.truncate(0) } + 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') + 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) @@ -129,45 +149,57 @@ require 'colorize' root = Dir.pwd Dir.chdir(root + '/db/migrate') require 'date' @time = DateTime.now.strftime('%Y%m%d%H%M%S') - File.new @time + '_create_searches.rb', "w" - 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 :search_id' - line.puts ' t.timestamps null: false' - line.puts ' end' - line.puts ' end' - line.puts 'end' + 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 :search_id' + line.puts ' t.timestamps null: false' + line.puts ' end' + line.puts ' end' + line.puts 'end' + end end - puts "Created ".green.bold + '/db/migrate/'.bold + @time.bold + '_create_searches.rb'.bold Dir.chdir(root) end 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] - 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 + 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 - puts "Updated ".green.bold + '/app/controllers/application_controller.rb'.bold Dir.chdir(root) end def write_search_model require "fileutils" @@ -184,24 +216,31 @@ require "fileutils" root = Dir.pwd Dir.chdir(root + '/config') first_line = IO.readlines("routes.rb")[0] other_lines = IO.readlines("routes.rb")[1..1000000000] - 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 + 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 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' @@ -210,22 +249,32 @@ object = command[1] attributes = command[2..10000000] end SearchInstall.new.create_files - SearchInstall.new.write_search_controller + Dir.chdir(root + '/app/controllers') + if File.exists?('searches_controller.rb') + else + SearchInstall.new.write_search_controller + end + Dir.chdir(root + '/app/models') + if File.exists?('search.rb') + else + SearchInstall.new.write_search_model + end + Dir.chdir(root + '/app/search') + if File.exists?('search_module.rb') + else + SearchInstall.new.write_search_module(object, attributes) + end + Dir.chdir(root) SearchInstall.new.write_application_controller - SearchInstall.new.write_search_model SearchInstall.new.write_migration SearchInstall.new.update_routes - SearchInstall.new.write_search_module(object, attributes) + puts 'Done'.bold puts '' - rescue - puts '' - puts 'Something Has Gone Wrong'.bold.red - puts 'Make Sure You Used The Correct Syntax'.bold.red - puts '' + end end end