Class: SearchRails::SearchInstall

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

Instance Method Summary collapse

Instance Method Details

#create_filesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/search_rails.rb', line 6

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



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

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

#update_routesObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/search_rails.rb', line 212

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



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

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



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

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

#write_search_controllerObject



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

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

#write_search_modelObject



201
202
203
204
205
206
207
208
209
210
# File 'lib/search_rails.rb', line 201

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



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

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