Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Add basic Entity search using pg_search. Ongoing #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dcabo committed Aug 25, 2013
1 parent 3d0e760 commit 5eb0de1
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ gem 'cancan'
gem 'figaro'
gem 'stringex'
gem 'kaminari'
gem 'pg_search'
gem 'acts-as-taggable-on'
gem 'shortcodes'
gem 'font-awesome-rails'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ GEM
mini_portile (~> 0.5.0)
orm_adapter (0.4.0)
pg (0.16.0)
pg_search (0.7.0)
activerecord (>= 3.1)
activesupport (>= 3.1)
arel
polyglot (0.3.3)
pry (0.9.12.2)
coderay (~> 1.0.5)
Expand Down Expand Up @@ -272,6 +276,7 @@ DEPENDENCIES
mini_magick
newrelic_rpm
pg
pg_search
pry-rails
rails (= 4.0.0)
rails_12factor
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Set up a daily auto-backup by enabling the add-on:
* StringEx: friendly URLs
* RailsAdmin: the admin panel
* Kaminari: pagination
* pg_search: full text search (Postgres)
* Carrierwave + Fog: imageuploads
* CKEditor: rich-content editor
* ActsAsTaggableOn: tags
Expand Down
37 changes: 37 additions & 0 deletions app/assets/stylesheets/search.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Taken from http://bootsnipp.com/snipps/stylish-search-form */
#custom-search-form {
margin:0;
margin-top: 10px;
padding: 0;
}

#custom-search-form .search-query {
padding-right: 3px;
padding-right: 4px \9;
padding-left: 3px;
padding-left: 4px \9;
/* IE7-8 doesn't have border-radius, so don't indent the padding */
margin-bottom: 0;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}

#custom-search-form button {
border: 0;
background: none;
/** belows styles are working good */
padding: 2px 5px;
margin-top: 2px;
position: relative;
left: -28px;
/* IE7-8 doesn't have border-radius, so don't indent the padding */
margin-bottom: 0;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}

.search-query:focus + button {
z-index: 3;
}
7 changes: 7 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SearchController < ApplicationController
def search
@query = params[:q]

@results = PgSearch.multisearch(@query).page(params[:page]).per(15)
end
end
13 changes: 8 additions & 5 deletions app/models/entity.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
class Entity < ActiveRecord::Base
extend Enumerize
enumerize :priority, in: {:high => 1, :medium => 2, :low => 3}

mount_uploader :avatar, AvatarUploader

has_many :entity_photo_associations, dependent: :delete_all
has_many :related_photos, through: :entity_photo_associations, source: :photo

has_many :relations_as_source, foreign_key: :source_id, class_name: Relation, inverse_of: :source
has_many :relations_as_target, foreign_key: :target_id, class_name: Relation, inverse_of: :target

include PgSearch
multisearchable :against => [:name, :short_name, :description]

extend Enumerize
enumerize :priority, in: {:high => 1, :medium => 2, :low => 3}

mount_uploader :avatar, AvatarUploader

# Note: sync_url=true won't work here, because we are using a function (short_or_long_name),
# so acts_as_url (Stringex) can't detect when the attribute has changed. Bug or feature?
acts_as_url :short_or_long_name, url_attribute: :slug, only_when_blank: true
Expand Down
21 changes: 21 additions & 0 deletions app/views/search/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--start: Wrapper-->
<div id="wrapper" class="full">

<!--start: Row -->
<div class="row-fluid">

<div class="span8 separate">
<h3>Resultados para '<%= @query %>'</h3>

<ul>
<% @results.each do |result| %>
<li><%= result.searchable.name %></li>
<% end %>
</ul>

<%= paginate @results %>
</div>

</div>

</div>
12 changes: 12 additions & 0 deletions app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
<%= render 'devise/menu/registration_items' %>
<%= render 'devise/menu/login_items' %>
<li ><%= link_to 'Admin', rails_admin_path %></li>

<%= form_tag search_path, :id => 'custom-search-form',
:class => 'form-search form-horizontal pull-right',
:method => 'get' do %>
<div class="input-append span2">
<%= text_field_tag :q,
params[:q],
:class => 'search-query',
:placeholder => 'Busca...' %>
<button type="submit" class="btn"><i class="icon-search"></i></button>
</div>
<% end %>
</ul>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/initializers/pg_search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: Configure search. See https://github.com/casecommons/pg_search
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

get '/admin/import' => 'import#import'
post '/admin/commit' => 'import#commit'

mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'

resources :posts, only: [:index, :show]
Expand All @@ -20,6 +19,9 @@
end
end

# Global search
get '/search' => 'search#search', :as => 'search'

# We add this route just so ShowInApp works in Rails Admin
resources :entities, only: [:show]

Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20130825102612_create_pg_search_documents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreatePgSearchDocuments < ActiveRecord::Migration
def self.up
say_with_time("Creating table for pg_search multisearch") do
create_table :pg_search_documents do |t|
t.text :content
t.belongs_to :searchable, :polymorphic => true
t.timestamps
end
end
end

def self.down
say_with_time("Dropping table for pg_search multisearch") do
drop_table :pg_search_documents
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20130824205902) do
ActiveRecord::Schema.define(version: 20130825102612) do

create_table "entities", force: true do |t|
t.string "name", null: false
Expand Down Expand Up @@ -62,6 +62,14 @@
add_index "facts_relations", ["fact_id"], name: "index_facts_relations_on_fact_id", using: :btree
add_index "facts_relations", ["relation_id"], name: "index_facts_relations_on_relation_id", using: :btree

create_table "pg_search_documents", force: true do |t|
t.text "content"
t.integer "searchable_id"
t.string "searchable_type"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "photos", force: true do |t|
t.string "file"
t.string "copyright"
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/search_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe SearchController do

end

0 comments on commit 5eb0de1

Please sign in to comment.