Skip to content

Instantly share code, notes, and snippets.

@frankie-loves-jesus
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankie-loves-jesus/f34fee3e2c739127aea8 to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/f34fee3e2c739127aea8 to your computer and use it in GitHub Desktop.
Forem with PgSearch + Google-like stats

Forem with PgSearch

Live app:

Run /etc/init.d/postgresql start before hitting the Run button.

app/decorators/models/forem/topic_decorator.rb

Forem::Topic.class_eval do
  include PgSearch

  pg_search_scope :search, against: [:subject], using: { tsearch: { dictionary: "english" } }, associated_against: { posts: [:text] }
end

app/decorators/controllers/forem/forums_controller_decorator.rb

Forem::ForumsController.class_eval do
  def search
    @search = params[:keywords]
    @topics = Forem::Topic.search(@search)
  end
end

app/views/layouts/application.html.erb

<div class="search">
  <%= form_tag main_app.search_path, method: :get do %>
    <label for="search">Search</label>
    <%= search_field_tag :keywords, params[:keywords], id: "search" %>
  <% end %>
</div>

app/views/forem/forums/search.html.erb

<h1>Search results</h1>
<%= render @topics %>

config/routes.rb

get '/search', to: 'forem/forums#search', as: 'search'
  
mount Forem::Engine, :at => '/'
root :to => "forums#index"

config/database.yml

development:
  adapter: postgresql
  username: admin
  password: admin1234
  database: forem
  template: template0 # Required for Runnable

Console (Ubuntu):

apt-get install postgresql
/etc/init.d/postgresql start
su postgres -c psql

PostgreSQL console:

CREATE ROLE admin SUPERUSER LOGIN PASSWORD 'admin1234';
\q

When searching for Mangos:

Started GET "/search?utf8=%E2%9C%93&keywords=Mangos" at 2014-10-17 19:33:15 +0000
Processing by Forem::ForumsController#show as HTML
Parameters: {"utf8"=>"?", "keywords"=>"Mangos", "id"=>"search"}
�[1m�[35mForem::Forum Load (0.5ms)�[0m SELECT "forem_forums".* FROM "forem_forums" WHERE "forem_forums"."slug" = 'search' ORDER BY "forem_forums"."name" ASC LIMIT 1
Completed 404 Not Found in 7ms
 
ActiveRecord::RecordNotFound (ActiveRecord::RecordNotFound):
friendly_id (5.0.4) lib/friendly_id/finder_methods.rb:23:in `find'
activerecord (4.1.2) lib/active_record/querying.rb:3:in `find'
cancancan (1.9.1) lib/cancan/model_adapters/abstract_adapter.rb:20:in `find'
cancancan (1.9.1) lib/cancan/controller_resource.rb:116:in `find_resource'
cancancan (1.9.1) lib/cancan/controller_resource.rb:68:in `load_resource_instance'
cancancan (1.9.1) lib/cancan/controller_resource.rb:32:in `load_resource'
cancancan (1.9.1) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancancan (1.9.1) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (4.1.2) lib/active_support/callbacks.rb:440:in `instance_exec'
activesupport (4.1.2) lib/active_support/callbacks.rb:440:in `block in make_lambda'
activesupport (4.1.2) lib/active_support/callbacks.rb:143:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:143:in `block in halting_and_conditional'
activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:149:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.2) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.2) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.2) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.2) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.2) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.2) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.2) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.2) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:678:in `call'
railties (4.1.2) lib/rails/engine.rb:514:in `call'
railties (4.1.2) lib/rails/railtie.rb:194:in `public_send'
railties (4.1.2) lib/rails/railtie.rb:194:in `method_missing'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.2) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.2) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.2) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.2) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.2) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.2) lib/rails/engine.rb:514:in `call'
railties (4.1.2) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

-- which was added via rake db:seed:

db/seeds.rb

Forem::Category.create!(:name => 'General')

user = User.create(
  :email => "admin@example.com",
  :password => "admin1234"
)
user.forem_admin = true
user.save!
user.update_attribute(:forem_state, 'approved')

unless user.nil?
  forum = Forem::Forum.create(:category_id => Forem::Category.first.id, :name => "Default", :description => "Default forem created by install")

  topic1 = forum.topics.build({ :subject => "Apples", :posts_attributes => [:text => "Eat apples"] })
  topic1.user = user
  topic1.save!

  topic2 = forum.topics.build({ :subject => "Bananas", :posts_attributes => [:text => "Eat bananas"] })
  topic2.user = user
  topic2.save!

  topic3 = forum.topics.build({ :subject => "Oranges", :posts_attributes => [:text => "Eat oranges"] })
  topic3.user = user
  topic3.save!

  topic4 = forum.topics.build({ :subject => "Mangos", :posts_attributes => [:text => "Eat mangos"] })
  topic4.user = user
  topic4.save!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment