require "search_rails/version" module SearchRails class SearchInstall 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 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 " else @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_search_id(1)' 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 Search.find_by_search_id(1) == nil' line.puts ' @search = Search.new' line.puts ' @search.search = nil' line.puts ' @search.search_id = 1' line.puts ' @search.save' 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 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_search_id(1)' 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)' line.puts ' end' line.puts '' line.puts 'end' end Dir.chdir(root) end 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 :search_id' line.puts ' t.timestamps null: false' line.puts ' end' line.puts ' end' line.puts 'end' end end 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] 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 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 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 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 SearchInstall.new.create_files SearchInstall.new.write_search_controller SearchInstall.new.write_search_model SearchInstall.new.write_search_module(object, attributes) SearchInstall.new.write_application_controller SearchInstall.new.write_migration SearchInstall.new.update_routes puts 'Done'.bold puts '' end end class SearchInstallDevise 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 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 " else @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 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 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 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 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 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 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 end end