Skip to content

Commit

Permalink
Server side Search
Browse files Browse the repository at this point in the history
  • Loading branch information
sl80 committed Jun 10, 2011
1 parent dded753 commit 7a54641
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 20 deletions.
5 changes: 3 additions & 2 deletions app/controllers/mailing_lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ class MailingListsController < ApplicationController
#respond_to :json

def index
@lists = MailingList.full.all
@lists = MailingList.full
@lists = @lists.search(params['search']) if params['search']

respond_to do |format|
format.json { render :json => @lists.to_json(:include => [:host, :list_type]) }
format.json { render :json => @lists.all.to_json(:include => [:host, :list_type]) }
end


Expand Down
4 changes: 4 additions & 0 deletions app/models/mailing_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class MailingList < ActiveRecord::Base

class << self

def search(term)
where('name LIKE ?', "%#{term}%")
end

def full
includes [:host, :list_type]
end
Expand Down
3 changes: 1 addition & 2 deletions public/javascripts/app/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ App.Controllers.Main = Backbone.Controller.extend({
},

index: function() {
console.log(MailingLists.fetch);
MailingLists.fetch({
mailingLists.fetch({
success:function(){
new App.Views.Index();
}
Expand Down
17 changes: 14 additions & 3 deletions public/javascripts/app/models/mailing_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ var MailingList = Backbone.Model.extend({
// url: '/mailing_lists.json'
// });

var MailingLists = new Backbone.Collection;
var MailingLists = Backbone.Collection.extend({
model: MailingList,
url: '/mailing_lists.json',
search: function(string){
var url = this.url;
this.url = this.url + '?search=' + string;
this.fetch();
this.url = url;
}
});

var mailingLists = new MailingLists;

MailingLists.model = MailingList;
MailingLists.url = '/mailing_lists.json';
// MailingLists.model = MailingList;
// MailingLists.url = '/mailing_lists.json';
4 changes: 4 additions & 0 deletions public/javascripts/app/templates/index.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<h3>
<a href='#new'>Create New</a>
</h3>
<form>
<input type="text" class="searchfield"/>
</form>
<div class="search">click</div>
<ul class="mailing-lists">
</ul>
26 changes: 15 additions & 11 deletions public/javascripts/app/views/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
App.Views.Index = Backbone.View.extend({
initialize: function() {
this.id = 'main';
this.collection = MailingLists;
this.collection = mailingLists;
this.template = new EJS({url: 'javascripts/app/templates/index.ejs'})

this.collection.bind('add', this.addOne);
Expand All @@ -12,17 +12,11 @@ App.Views.Index = Backbone.View.extend({
this.addAll();
},

events: {
"click .search" : "search"
},

render: function() {
console.log('rendering');
// if(this.collection.length > 0) {
// var out = "<h3><a href='#new'>Create New</a></h3><ul>";
// this.collection.each(function(item) {
// out += "<li><a href='#documents/" + item.id + "'>" + item.escape('name') + "</a></li>";
// });
// out += "</ul>";
// } else {
// out = "<h3>No documents! <a href='#new'>Create one</a></h3>";
// }
$(this.el).html(this.template.render(this));
$('#'+this.id).html(this.el);
},
Expand All @@ -35,6 +29,16 @@ App.Views.Index = Backbone.View.extend({
addAll: function() {
var self = this;
this.collection.each(function(list){ self.addOne(list); });
},
resetAll: function(){
this.$(".mailing-lists").html('');
this.addAll();
},

search: function(){
this.collection.search(this.$('.searchfield')[0].value);
this.resetAll();
return false;
}

});
24 changes: 22 additions & 2 deletions test/functional/mailing_lists_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,27 @@

class MailingListsControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
test "should get all lists" do

get :index, {:format => 'json'}
assert_response :success

lists = JSON.parse(@response.body)

assert_equal 3, lists.count
assert_equal "AG innere Sicherheit", lists[0]['name']
end

test "should do search by name" do

get :index, {:format => 'json', :search => 'AG'}
assert_response :success

lists = JSON.parse(@response.body)

assert_equal 1, lists.count
assert_equal "AG innere Sicherheit", lists[0]['name']
end


end

0 comments on commit 7a54641

Please sign in to comment.