search_rails 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2552884dd2669c3765432f904680f388d929b034
4
- data.tar.gz: c16792b509fcb29849b84003b4be5deea2e19489
3
+ metadata.gz: cb884217f51c0fe72ecfd70e0e0ea014c9424059
4
+ data.tar.gz: 249bda3f506a9faf9ccbed16df5db9756da90d6a
5
5
  SHA512:
6
- metadata.gz: 710a713cd55d7e7ccc0607d959cbbcf66dd4948809db89a87c2de1e352cf4a438cea6dda690ed33b25ad4e59d9055cfad49244e27f4355fbb95c308e985d65ff
7
- data.tar.gz: 62aaab1d67c283084e708da0fcdf42b1a6ae273c3da888b1e89124d064aa63bce5fd6127f522818cedead8e2b084205821afa916f7f5eaa923ed26a98ac9bbfc
6
+ metadata.gz: c55532232f14852cc8c31828111a006ffe19a414f2d61103ff6185b3d02b78bcb788a75203e90bd9ea48d89c0a40e39810093d8a1644a1cfc061dc358cba6c2f
7
+ data.tar.gz: 7d68e141b042cb72ecb482ecc7f92de659c38fb944682c6366438df801e54aa1a2128f4c7918f354f0689ae01db2080bf605c51664b0bd535d45b06140275ceb
data/README.md CHANGED
@@ -13,14 +13,18 @@ gem 'colorize'
13
13
 
14
14
  And then execute:
15
15
 
16
- $ bundle
16
+ $ bundle install
17
17
 
18
18
  ## Usage
19
19
 
20
- To install the search functionality, type the command:
20
+ To install the search functionality not using Devise, type the command:
21
21
 
22
22
  $ search:install
23
23
 
24
+ To install the search functionality using Devise, type the command:
25
+
26
+ $ search:install:devise
27
+
24
28
  Then follow the steps below.
25
29
 
26
30
  The syntax for installing the search function is shown in the example below,
@@ -42,7 +46,7 @@ Then insert the following form on whatever page you have set up for the search f
42
46
  ```
43
47
 
44
48
 
45
- Then add the following lines to whatever controller action you want to have the search functionality in.
49
+ If you are not using Devise, add the following lines to whatever controller action you want to have the search functionality in.
46
50
 
47
51
  ```ruby
48
52
  $LOAD_PATH.unshift(File.dirname('../app/search'))
@@ -53,9 +57,18 @@ if @search.search != nil
53
57
  end
54
58
  ```
55
59
 
56
- This is still a work in progress, so if there are any issues, please report them on the GitHub page.
60
+ If you are using Devise, add the following lines to whatever controller action you want to have the search functionality in.
61
+
62
+ ```ruby
63
+ $LOAD_PATH.unshift(File.dirname('../app/search'))
64
+ extend SearchModule
65
+ @search = Search.find_by_user_id(current_user.id)
66
+ if @search.search != nil
67
+ search()
68
+ end
69
+ ```
57
70
 
58
- #### Hopefully in the future, automatic support for Devise will be included, but for now, you will have to modify the files 'search:install' makes on your own for devise or multi-user use.
71
+ This is still a work in progress, so if there are any issues, please report them on the GitHub page.
59
72
 
60
73
 
61
74
  ## Contributing
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'search_rails'
4
+
5
+ run = SearchRails::SearchInstallDevise.new.run
@@ -1,3 +1,3 @@
1
1
  module SearchRails
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
data/lib/search_rails.rb CHANGED
@@ -262,4 +262,271 @@ module SearchRails
262
262
  end
263
263
 
264
264
  end
265
+
266
+
267
+ class SearchInstallDevise
268
+
269
+ def create_files
270
+ require "colorize"
271
+ require "fileutils"
272
+ root = Dir.pwd
273
+ Dir.chdir(root + '/app')
274
+ if File.exists?('search')
275
+ puts 'Already Exists'.bold.green + ' /app/search'.bold
276
+ else
277
+ FileUtils::mkdir_p "search"
278
+ puts "Created ".green.bold + '/app/search'.bold
279
+ end
280
+ Dir.chdir(root + '/app/search')
281
+ if File.exists?('search_module.rb')
282
+ puts 'Already Exists'.bold.green + ' /app/search/search_module.rb'.bold
283
+ else
284
+ File.new "search_module.rb", "w"
285
+ puts "Created ".green.bold + '/app/search/search_module.rb'.bold
286
+ end
287
+ Dir.chdir(root + '/app/controllers')
288
+ if File.exists?('searches_controller.rb')
289
+ puts 'Already Exists'.bold.green + ' /app/controllers/searches_controller.rb'.bold
290
+ else
291
+ File.new "searches_controller.rb", "w"
292
+ puts "Created ".green.bold + '/app/controllers/searches_controller.rb'.bold
293
+ end
294
+ Dir.chdir(root + '/app/models')
295
+ if File.exists?('search.rb')
296
+ puts 'Already Exists'.bold.green + ' /app/models/search.rb'.bold
297
+ else
298
+ File.new "search.rb", "w"
299
+ puts 'Created '.green.bold + '/app/models/search.rb'.bold
300
+ end
301
+ Dir.chdir(root + '/db')
302
+ if File.exists?('migrate')
303
+ puts 'Already Exists'.bold.green + ' /db/migrate'.bold
304
+ else
305
+ FileUtils::mkdir_p "migrate"
306
+ puts "Created ".green.bold + '/db/migrate'.bold
307
+ end
308
+ Dir.chdir(root)
309
+ end
310
+
311
+ def write_search_module(object, attributes)
312
+ root = Dir.pwd
313
+ require 'active_support/inflector'
314
+ Dir.chdir(root + '/app/search')
315
+ File.open("search_module.rb", "a") do |line|
316
+ line.puts 'module SearchModule'
317
+ line.puts ' def search'
318
+ attri_one = attributes[0]
319
+ attribute = attri_one.split(':')
320
+ line.puts " @" + object.downcase.pluralize + " = " + object.capitalize.singularize + ".where('lower(" + attribute[0] + ") LIKE ?', '%' + @search.search.downcase + '%')"
321
+ attributes = attributes.reject { |attri| attri == attributes[0] }
322
+ attributes.each do |attri|
323
+ attribute = attri.split(':')
324
+ if attribute[1] == 'integer' || attribute[1] == 'decimal'
325
+ 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] + " }"
326
+ elsif attribute[1] == 'boolean'
327
+ line.puts " if @search.search.downcase == 'true' || @search.search.downcase == 'tru' || @search.search.downcase == 'tr' || @search.search.downcase == 't'"
328
+ 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] + " }"
329
+ line.puts " else @search.search.downcase == 'false' || @search.search.downcase == 'fals' || @search.search.downcase == 'fal' || @search.search.downcase == 'fa' || @search.search.downcase == 'f'"
330
+ 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] + " }"
331
+ line.puts " end"
332
+ else
333
+ 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] + " }"
334
+ end
335
+ end
336
+ line.puts ' end'
337
+ line.puts ''
338
+ line.puts ' def clear'
339
+ line.puts ' @search = Search.find_by(user_id: current_user.id)'
340
+ line.puts ' @search.search = nil'
341
+ line.puts ' @search.save'
342
+ line.puts ' redirect_to ' + object.pluralize.downcase + '_path'
343
+ line.puts ' end'
344
+ line.puts ''
345
+ line.puts ' def check_for_search'
346
+ line.puts ' if user_signed_in?'
347
+ line.puts ' if Search.find_by(user_id: current_user.id) == nil'
348
+ line.puts ' @search = Search.new'
349
+ line.puts ' @search.search = nil'
350
+ line.puts ' @search.user_id = current_user.id'
351
+ line.puts ' @search.save'
352
+ line.puts ' end'
353
+ line.puts ' end'
354
+ line.puts ' end'
355
+ line.puts ''
356
+ line.puts ' def update_search'
357
+ line.puts ' respond_to do |format|'
358
+ line.puts ' if @search.update(search_params)'
359
+ line.puts ' format.html { redirect_to :back }'
360
+ line.puts ' format.json { render :show, status: :ok, location: @search }'
361
+ line.puts ' else'
362
+ line.puts ' format.html { render :edit }'
363
+ line.puts ' format.json { render json: @search.errors, status: :unprocessable_entity }'
364
+ line.puts ' end'
365
+ line.puts ' end'
366
+ line.puts ' end'
367
+
368
+ line.puts 'end'
369
+ end
370
+ Dir.chdir(root)
371
+ end
372
+
373
+ def write_search_controller
374
+ root = Dir.pwd
375
+ Dir.chdir(root + '/app/controllers')
376
+ File.open("searches_controller.rb", "a") do |line|
377
+ line.puts 'class SearchesController < ApplicationController'
378
+ line.puts ' before_action :set_search, only: [:show, :edit, :update, :destroy]'
379
+ line.puts ' before_action :include_search_module, only: [ :clear, :edit, :update ]'
380
+ line.puts ''
381
+ line.puts ' def clear'
382
+ line.puts ' clear'
383
+ line.puts ' end'
384
+ line.puts ''
385
+ line.puts ' def update'
386
+ line.puts ' update_search'
387
+ line.puts ' end'
388
+ line.puts ''
389
+ line.puts ' private'
390
+ line.puts ''
391
+ line.puts ' def set_search'
392
+ line.puts ' @search = Search.find_by(user_id: current_user.id)'
393
+ line.puts ' end'
394
+ line.puts ''
395
+ line.puts ' def include_search_module'
396
+ line.puts " $LOAD_PATH.unshift(File.dirname('../app/search'))"
397
+ line.puts ' extend SearchModule'
398
+ line.puts ' end'
399
+ line.puts ''
400
+ line.puts ' def search_params'
401
+ line.puts ' params.require(:search).permit(:search, :user_id)'
402
+ line.puts ' end'
403
+ line.puts ''
404
+ line.puts 'end'
405
+ end
406
+ Dir.chdir(root)
407
+ end
408
+
409
+ def write_migration
410
+ require "fileutils"
411
+ require 'colorize'
412
+ root = Dir.pwd
413
+ Dir.chdir(root + '/db/migrate')
414
+ require 'date'
415
+ @time = DateTime.now.strftime('%Y%m%d%H%M%S')
416
+ if Dir[root + '/db/migrate/*_create_searches.rb'].count > 0
417
+ file = Dir[root + '/db/migrate/*_create_searches.rb'].first
418
+ puts 'Already Exists '.bold.green + file[38..100].bold
419
+ else
420
+ File.new @time + '_create_searches.rb', "w"
421
+ puts "Created ".green.bold + '/db/migrate/'.bold + @time.bold + '_create_searches.rb'.bold
422
+ File.open(@time + "_create_searches.rb", "a") do |line|
423
+ line.puts 'class CreateSearches < ActiveRecord::Migration'
424
+ line.puts ' def change'
425
+ line.puts ' create_table :searches do |t|'
426
+ line.puts ' t.string :search'
427
+ line.puts ' t.integer :user_id'
428
+ line.puts ' t.timestamps null: false'
429
+ line.puts ' end'
430
+ line.puts ' end'
431
+ line.puts 'end'
432
+ end
433
+ end
434
+ Dir.chdir(root)
435
+ end
436
+
437
+ def write_application_controller
438
+ require "fileutils"
439
+ root = Dir.pwd
440
+ Dir.chdir(root + '/app/controllers')
441
+ first_line = IO.readlines("application_controller.rb")[0]
442
+ other_lines = IO.readlines("application_controller.rb")[1..1000000000]
443
+ second_line = IO.readlines("application_controller.rb")[1]
444
+
445
+ if second_line.chomp == ' before_action :check_for_search'
446
+ puts 'Already Exists'.bold.green + ' /app/controllers/application_controller.rb'.bold
447
+ else
448
+ FileUtils::mkdir_p "migrate"
449
+ puts "Updated ".green.bold + '/app/controllers/application_controller.rb'.bold
450
+ File.open("application_controller.rb", "w") do |line|
451
+ line.puts first_line
452
+ line.puts ' before_action :check_for_search'
453
+ line.puts ''
454
+ line.puts ' def check_for_search'
455
+ line.puts " $LOAD_PATH.unshift(File.dirname('../app/search'))"
456
+ line.puts ' extend SearchModule'
457
+ line.puts ' check_for_search'
458
+ line.puts ' end'
459
+ line.puts ''
460
+ line.puts other_lines
461
+ end
462
+ end
463
+ Dir.chdir(root)
464
+ end
465
+
466
+ def write_search_model
467
+ require "fileutils"
468
+ root = Dir.pwd
469
+ Dir.chdir(root + '/app/models')
470
+ File.open("search.rb", "w") do |line|
471
+ line.puts 'class Search < ActiveRecord::Base'
472
+ line.puts 'end'
473
+ end
474
+ Dir.chdir(root)
475
+ end
476
+
477
+ def update_routes
478
+ require "fileutils"
479
+ root = Dir.pwd
480
+ Dir.chdir(root + '/config')
481
+ first_line = IO.readlines("routes.rb")[0]
482
+ other_lines = IO.readlines("routes.rb")[1..1000000000]
483
+ second_line = IO.readlines("routes.rb")[1]
484
+ if second_line.chomp == 'resources :searches do'
485
+ puts 'Already Exists'.bold.green + ' /config/routes.rb'.bold
486
+ else
487
+ File.open('routes.rb', 'w') do |line|
488
+ line.puts first_line
489
+ line.puts 'resources :searches do'
490
+ line.puts ' member do'
491
+ line.puts " get 'clear'"
492
+ line.puts ' end'
493
+ line.puts 'end'
494
+ line.puts other_lines
495
+ end
496
+ puts 'Updated '.bold.green + '/config/routes.rb'.bold
497
+ end
498
+ Dir.chdir(root)
499
+ end
500
+
501
+ def run
502
+ require 'colorize'
503
+ root = Dir.pwd
504
+
505
+ puts "Syntax: 'install OBJECT ATTRIBUTE:TYPE'".bold.on_red
506
+ command = gets.chomp
507
+ command = command.split(" ")
508
+ if command[0] == 'install'
509
+ puts ''
510
+ puts 'Creating Files...'.bold
511
+ object = command[1]
512
+ attributes = command[2..10000000]
513
+ end
514
+
515
+ SearchInstallDevise.new.create_files
516
+ SearchInstallDevise.new.write_search_controller
517
+ SearchInstallDevise.new.write_search_model
518
+ SearchInstallDevise.new.write_search_module(object, attributes)
519
+ SearchInstallDevise.new.write_application_controller
520
+ SearchInstallDevise.new.write_migration
521
+ SearchInstallDevise.new.update_routes
522
+
523
+ puts 'Done'.bold
524
+ puts ''
525
+
526
+
527
+ end
528
+
529
+ end
530
+
531
+
265
532
  end
data/search_rails.gemspec CHANGED
@@ -10,8 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["micahnicodemus22@gmail.com"]
11
11
 
12
12
  spec.summary = "A Simple Search Gem"
13
- spec.description = "A Simple Search Gem For Searching In Rails Projects.
14
- As Of Now, Devise or multi-user use is not included, so you will have to modify the files yourself if you want multi-user support."
13
+ spec.description = "A Simple Search Gem For Searching In Rails Projects."
15
14
  spec.homepage = "https://github.com/micahnico/search_rails"
16
15
  spec.license = "MIT"
17
16
 
@@ -20,11 +19,11 @@ Gem::Specification.new do |spec|
20
19
  f.match(%r{^(test|spec|features)/})
21
20
  end
22
21
  spec.bindir = "bin"
23
- spec.executables = "search:install"
22
+ spec.executables = ["search:install", "search:install:devise"]
24
23
  spec.require_paths = ["lib"]
25
24
 
26
25
  spec.add_development_dependency "bundler", "~> 1.13"
27
26
  spec.add_development_dependency "rake", "~> 10.0"
28
27
  spec.add_development_dependency "rspec", "~> 3.0"
29
- spec.add_development_dependency "colorize", "~> 0.8.1"
28
+ spec.add_development_dependency "colorize"
30
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - micahnico22
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-29 00:00:00.000000000 Z
11
+ date: 2016-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,23 +56,22 @@ dependencies:
56
56
  name: colorize
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 0.8.1
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 0.8.1
69
- description: |-
70
- A Simple Search Gem For Searching In Rails Projects.
71
- As Of Now, Devise or multi-user use is not included, so you will have to modify the files yourself if you want multi-user support.
68
+ version: '0'
69
+ description: A Simple Search Gem For Searching In Rails Projects.
72
70
  email:
73
71
  - micahnicodemus22@gmail.com
74
72
  executables:
75
73
  - search:install
74
+ - search:install:devise
76
75
  extensions: []
77
76
  extra_rdoc_files: []
78
77
  files:
@@ -86,6 +85,7 @@ files:
86
85
  - Rakefile
87
86
  - bin/console
88
87
  - bin/search:install
88
+ - bin/search:install:devise
89
89
  - bin/setup
90
90
  - lib/search_rails.rb
91
91
  - lib/search_rails/version.rb