Skip to content

Commit

Permalink
It's finally done. Multi-column search is here to stay. Currently onl…
Browse files Browse the repository at this point in the history
…y supports one keyword but will search all columns in the Search Group. Commenting is very ugly so I'll personally take care of cleaning that up by tomorrow. Test it out and report bugs.
  • Loading branch information
eburch2 committed Aug 11, 2016
1 parent 1ed8d47 commit 4cee148
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ gem 'spring', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'tzinfo-data'
gem 'tzinfo-data'
72 changes: 63 additions & 9 deletions app/controllers/data_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,78 @@ class DataController < ApplicationController
# GET /data
# GET /data.json
def index
@search_params = ""

# broken code for dynamic searching of all columns
=begin
@sql_query = "SELECT * FROM data WHERE ("
@columns = Datum.column_names
@results = ""
# Builds the query one column at a time for the 1 search parameter
# SELECT * FROM data WHERE (col LIKE '%search%' OR col2 LIKE '%search%' OR ...)
@columns.each do |col|
col = "\'#{col}\'"
@search_params = @search_params + 'OR ' + col + ' LIKE ? '
col = "\"#{col}\""
#@sql_query = @sql_query + 'OR ' + col + ' LIKE ? '
@sql_query = @sql_query + "#{col} LIKE '%#{params[:search]}%' OR "
end
# chops off the first OR from the string after the joined search parameters are done
# adds "OR column_name LIKE ?" for each column
@search_params = @search_params[3, -1]
# chops off the last OR from the string after the entire query is done
# adds the final closing parenthesis on the end
@sql_query = @sql_query[0, -4]
key = "%#{params[:search]}%"
=end


@data = Datum.where(['"School Name" LIKE ?', "%#{params[:search]}%"])

@columns = Datum.column_names

# Credits to http://www.korenlc.com/creating-a-simple-search-in-rails-4/ for getting us started

@data = nil

if params[:search]

if params[:Search_Group]

columns_string = params[:Search_Group].to_s
@columns = columns_string

if columns_string.length > 15
col_array = []
columns_string = columns_string[12..-3]

@columns = columns_string.split(", ")
#@columns = col_array

else
@columns = Datum.column_names
end

else
@columns = Datum.column_names
end

# we never want to show the id, this is strictly for our database
index = @columns.find_index("id")
if index
@columns.delete_at(index)
end

@data = Datum.search(params[:search], @columns)
else
@data = Datum.all
end



# Code to test

#search = "\"School Name\""
#@data = Datum.where([search + ' LIKE ?', "%#{params[:search]}%"])
#@data = Datum.where(['"School Name" LIKE ?', "%#{params[:search]}%"])

#@search_params = "\"School Name\" LIKE ? OR \"School Number\" LIKE ?"

#@data = Datum.where(@search_params, search: key)
#@data = Datum.where([@search_params,"%#{params[:search]}%"])

Expand Down
40 changes: 40 additions & 0 deletions app/models/datum.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
class Datum < ActiveRecord::Base

def self.search(search, columns)

columns_string = ""

columns.each do |col|
columns_string = columns_string + '' + "\"#{col}\"" + ' LIKE :test OR '
end

#columns_string = columns.to_s

#columns_string = "\"" + columns_string

#columns_string.gsub! ", ", "\" || \""

# cuts off the last ' OR '
columns_string = columns_string[0..-5]
#columns_string = columns_string[2..-3]


# cuts off the last ' || '
#columns_string = columns_string[0..-5].to_s

#columns_string = "\"School Name\" || \"CLN\" || \"Grades Served\""

name = "\"School Name\""
number = "\"CLN\""

#columns_string = "\"School Name\""

#where(name + " LIKE ?", "%#{search}%")
#where(number + " LIKE ?", "%#{search}%")

#where(name + " LIKE ? OR " + number + " LIKE ?", "%#{search}%")

key = "%#{search}%"
#where(name + ' LIKE :search OR ' + number + ' LIKE :search', search: key)
where(columns_string, test: key)
end

end
49 changes: 23 additions & 26 deletions app/views/data/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ header {
<h2>School Information Search</h2>
</header>

<%= form_tag data_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
<% end %>

<center>
<!-- Code for the category combobox, search bar, and search button -->
<%= form_tag data_path, :method => 'get', enforce_utf8: true do %>
<%= collection_select :Search_Group, :select, SearchGroup.all.order(:group_name), :children_columns, :group_name, { include_blank: 'School Profile'} %>
<%= text_field_tag :search, params[:search], maxlength: 300, placeholder: "Search by any keyword or school number", id: "searchBar" %>
<%= submit_tag "Search" %>
<% end %>

<%= form_tag data_path, :method => 'get', enforce_utf8: true do %>
<%= collection_select :Search_Group, :select, SearchGroup.all.order(:group_name), :children_columns, :group_name, { include_blank: 'School Profile'} %>
<%= text_field_tag :search, params[:search], maxlength: 300, placeholder: "Search by any keyword or school number", id: "searchBar" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</center>
<br>

<script>
function checkStatus() {
Expand Down Expand Up @@ -86,29 +83,29 @@ header {
<!-- Code for the table displaying the search results -->
<table id="resultsTable" border ="1" width =100% style="visibility:hidden" script="onload='checkStatus()'">

<!-- prints the column names for top of the table -->
<tr>
<% @columns = @data.column_names %>
<% @columns.each do |col| %>
<th>
<%= col %>
</th>
<% end %>
</tr>
<% @data = @data.order('"School Number"') %>
<%= @columns %>
<% @data.each do |datum| %>
<tr>
<% @columns = @data.column_names %>
<% @columns.each do |col| %>
<td>
<%= datum[col] %>
</td>
<% end %>

<!-- Appends the Show/Edit/Destroy options as hyperlinks -->
<td><%= link_to 'Show', datum %></td>
<td><%= link_to 'Edit', edit_datum_path(datum) %></td>
<td><%= link_to 'Destroy', datum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>

<% @columns.each do |cell| %>
<td>
<%= datum[cell] %>
</td>
<% end %>

<!-- Appends the Show/Edit/Destroy options as hyperlinks -->
<td><%= link_to 'Show', datum %></td>
<td><%= link_to 'Edit', edit_datum_path(datum) %></td>
<td><%= link_to 'Destroy', datum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
</body>
Expand Down

0 comments on commit 4cee148

Please sign in to comment.