Permalink
Browse files

Fixes search in dashboard's features table

  • Loading branch information...
1 parent ce2b20b commit 7ae38f8cabeb59157431fe1092cfc9a3626445d9 @Ferdev Ferdev committed Sep 1, 2011
View
29 app/controllers/admin/dashboard_controller.rb
@@ -1,13 +1,38 @@
class Admin::DashboardController < Admin::AdminController
def index
+ @search = OpenStruct.new params[:search]
+
+ search_where = generate_search_where(@search)
+
@page = (params[:page] || 1).to_i
@features_columns = Feature.data_columns
- @features_count = CartoDB::Connection.query("SELECT COUNT(cartodb_id) as features_count FROM #{Cartoset::Config['features_table']}").rows.first.features_count
- @features = CartoDB::Connection.query("SELECT * FROM #{Cartoset::Config['features_table']}", :page => @page, :rows_per_page => 10).rows
+ @features_count = CartoDB::Connection.query("SELECT COUNT(cartodb_id) as features_count FROM #{Cartoset::Config['features_table']} #{search_where}").rows.first.features_count
+ @features = CartoDB::Connection.query(<<-SQL
+ SELECT *
+ FROM #{Cartoset::Config['features_table']}
+ #{search_where}
+ LIMIT 10
+ OFFSET #{(@page - 1) * 10}
+ SQL
+ ).rows
@pages = Cartoset::Config['pages'].select{|p| p['id'].present? } || []
@cartodb_host = CartoDB::Settings['host']
@table_id = CartoDB::Connection.table(Cartoset::Config['features_table']).try(:id)
end
+
+ def generate_search_where(search_params)
+ where = ''
+ if search_params && search_params.q.present?
+ where = []
+ fields = Feature.non_common_fields.select{|f| f[:type] == 'string'}
+ fields.each do |f|
+ where << "#{f[:name]} ilike '%#{search_params.q.sanitize_sql!}%'"
+ end
+ where = "WHERE #{where.join(' OR ')}"
+ end
+ where
+ end
+
end
View
2 app/helpers/admin/admin_helper.rb
@@ -1,6 +1,6 @@
module Admin::AdminHelper
def build_field_for_column(column, value = nil)
-puts value
+
case column[:type]
when 'boolean'
ul_content = ''
View
11 app/helpers/application_helper.rb
@@ -1,6 +1,11 @@
module ApplicationHelper
- def paginate(rows, current_page, total_count, per_page = 10)
-
+ def paginate(rows, current_page, total_count, per_page = 10, params = {})
+
+ if per_page.is_a?(Hash)
+ params = per_page
+ per_page = 10
+ end
+
window = 2
total_pages = (total_count.to_f / per_page.to_f).ceil
last_page = total_pages - 1
@@ -11,7 +16,7 @@ def paginate(rows, current_page, total_count, per_page = 10)
pages = 1.upto(current_page - 1).to_a.map{|p| left.include?(p) || p == 1 ? p : '...'}.uniq + [current_page] + (current_page + 1).upto(last_page).to_a.map{|p| right.include?(p) || p == last_page ? p : '...'}.uniq
content_tag :div, :class => 'pagination' do
- raw pages.map{|page| page.eql?('...') ? content_tag(:span, page) : link_to(page, admin_path(:page => page), :class => page == current_page ? 'current' : nil)}.join(' ')
+ raw pages.map{|page| page.eql?('...') ? content_tag(:span, page) : link_to(page, admin_path(params.merge({:page => page})), :class => page == current_page ? 'current' : nil)}.join(' ')
end
end
end
View
10 app/views/admin/dashboard/index.html.erb
@@ -9,10 +9,10 @@
<div id="features_list" class="table">
<span class="title"><%= t('.your_features') %></span>
<div id="search">
- <%= form_tag '/', :method => :get do %>
- <%= label_tag 'search', t('.search_label') %>
- <%= text_field_tag 'search' %>
- <%= submit_tag t('.search') %>
+ <%= form_for :search, :url => admin_path, :html => {:method => :get} do |f|%>
+ <%= f.label 'q', t('.search_label') %>
+ <%= f.text_field 'q' %>
+ <%= f.submit t('.search') %>
<% end %>
<%= link_to content_tag(:span, t('.scroll_right')), '#', :id => 'scroll_right' %>
<%= link_to content_tag(:span, t('.scroll_left')), '#', :id => 'scroll_left' %>
@@ -37,7 +37,7 @@
</div>
<%= link_to t('.create_feature'), new_admin_feature_path, :id => 'create_feature', :class => 'main_action' %>
<%= link_to t('.edit_in_cartodb'), "#{@cartodb_host}/tables/#{@table_id}", :id => 'edit_in_cartodb', :class => 'alternate_action' %>
- <%= raw paginate @features, @page, @features_count %>
+ <%= raw paginate @features, @page, @features_count, params %>
</div>
<div id="pages_list" class="table">
View
4 spec/support/string_extension.rb
@@ -6,4 +6,8 @@ def self.random(size = 8)
(0..size).map{ @@chars[rand(@@chars.length)] }.join
end
+ def sanitize_sql!
+ self.gsub(/\\/, '\&\&').gsub(/'/, "''")
+ end
+
end

0 comments on commit 7ae38f8

Please sign in to comment.