Permalink
Browse files

Display your 10 most recent searches.

  • Loading branch information...
gma committed May 15, 2011
1 parent c90b37b commit 517dfe46db20fc2423acd62afc07410672928977
@@ -0,0 +1,5 @@
+class SearchesController < ApplicationController
+ def index
+ @searches = current_user.recent_searches
+ end
+end
@@ -3,6 +3,7 @@ class WorksController < ApplicationController
def show
@work = Work.find(params[:id])
+ @current_user.searches << Search.new(:work_id => @work.id)
@workdetails = @work.workdetails.order("movement_id")
@workdetail = @work.workdetails.order.first
@@ -0,0 +1,6 @@
+module SearchesHelper
+ def link_to_work(work)
+ composer = work.composer.composer_name
+ link_to "#{composer}: #{work.work_name}", work_path(work)
+ end
+end
View
@@ -0,0 +1,4 @@
+class Search < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :work
+end
View
@@ -52,6 +52,8 @@ class User < ActiveRecord::Base
:confirmation => true,
:length => { :within => 6..40 }
+ has_many :searches
+
before_save :encrypt_password
# Return true if the user's password matches the submitted password.
@@ -71,6 +73,11 @@ def self.authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
(user && user.salt == cookie_salt) ? user : nil
end
+
+ def recent_searches
+ searches.group('work_id').
+ includes(:work => :composer).limit(10).order('created_at DESC')
+ end
private
@@ -44,7 +44,7 @@
<br /><br />
<p class="MainNavigation">
<%= link_to I18n.t('layout.menu.instrument_translations'), instrument_translations_path %>
- <%= link_to I18n.t('layout.menu.searches'), '#' %>
+ <%= link_to I18n.t('layout.menu.searches'), searches_path %>
<%= link_to I18n.t('layout.menu.gig'), '#' %>
<%= link_to I18n.t('layout.menu.schedule'), '#' %>
<%= link_to I18n.t('layout.menu.planner'), '#' %>
@@ -0,0 +1,9 @@
+<h1>My recent searches</h1>
+<ul class="works">
+<% @searches.each do |search| %>
+ <li>
+ <%= link_to_work(search.work) %>
+ (<%= search.created_at.to_date.to_s(:short) %>)
+ </li>
+<% end %>
+<ul>
@@ -1,10 +1,8 @@
-
-
LANGUAGES = {
'English' => 'en',
"Francais" => 'fr',
"Deutsch" => 'de',
"Espanol" => 'es',
"Italiano" => 'it'
-}
+}
View
@@ -45,6 +45,8 @@
resources :works
+ resources :searches
+
resources :workdetails
# The priority is based upon order of creation:
# first created -> highest priority.
View
Binary file not shown.
@@ -0,0 +1,14 @@
+class CreateSearches < ActiveRecord::Migration
+ def self.up
+ create_table :searches do |t|
+ t.integer :user_id
+ t.integer :work_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :searches
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110514174930) do
+ActiveRecord::Schema.define(:version => 20110515171353) do
create_table "anvils", :force => true do |t|
t.integer "work_id"
@@ -193,6 +193,13 @@
t.datetime "updated_at"
end
+ create_table "searches", :force => true do |t|
+ t.integer "user_id"
+ t.integer "work_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
create_table "sleighbells", :force => true do |t|
t.integer "work_id"
t.string "movement_name"
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe SearchesController do
+
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the SearchesHelper. For example:
+#
+# describe SearchesHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe SearchesHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Search do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit 517dfe4

Please sign in to comment.