require "search_rails/version" module SearchRails class SearchInstall 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 Dir.chdir(root + '/app/search') File.new "search_module.rb", "w" puts "Created ".green.bold + '/app/search/search_module.rb'.bold Dir.chdir(root + '/app/controllers') File.new "searches_controller.rb", "w" puts "Created ".green.bold + '/app/controllers/searches_controller.rb'.bold Dir.chdir(root + '/app/models') File.new "search.rb", "w" puts 'Created '.green.bold + 'app/models/search.rb'.bold Dir.chdir(root + '/db') if File.exists?('/migrate') 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] + " }" 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' 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' 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') 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' 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 end puts "Updated ".green.bold + '/app/controllers/application_controller.rb'.bold 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] 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 Dir.chdir(root) end def run require 'colorize' 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_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