diff --git a/app/controllers/mailing_lists_controller.rb b/app/controllers/mailing_lists_controller.rb index c52826c..eabcf13 100644 --- a/app/controllers/mailing_lists_controller.rb +++ b/app/controllers/mailing_lists_controller.rb @@ -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 diff --git a/app/models/mailing_list.rb b/app/models/mailing_list.rb index 3d796af..d00ab6e 100644 --- a/app/models/mailing_list.rb +++ b/app/models/mailing_list.rb @@ -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 diff --git a/public/javascripts/app/controllers/main.js b/public/javascripts/app/controllers/main.js index 0c293bd..18eec75 100644 --- a/public/javascripts/app/controllers/main.js +++ b/public/javascripts/app/controllers/main.js @@ -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(); } diff --git a/public/javascripts/app/models/mailing_list.js b/public/javascripts/app/models/mailing_list.js index 8335eb8..37252b2 100644 --- a/public/javascripts/app/models/mailing_list.js +++ b/public/javascripts/app/models/mailing_list.js @@ -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'; \ No newline at end of file +// MailingLists.model = MailingList; +// MailingLists.url = '/mailing_lists.json'; \ No newline at end of file diff --git a/public/javascripts/app/templates/index.ejs b/public/javascripts/app/templates/index.ejs index 1c4b68c..7cfa2de 100644 --- a/public/javascripts/app/templates/index.ejs +++ b/public/javascripts/app/templates/index.ejs @@ -1,5 +1,9 @@

Create New

+
+ +
+ \ No newline at end of file diff --git a/public/javascripts/app/views/index.js b/public/javascripts/app/views/index.js index ab4ff84..f901945 100644 --- a/public/javascripts/app/views/index.js +++ b/public/javascripts/app/views/index.js @@ -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); @@ -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 = "

Create New

"; - // } else { - // out = "

No documents! Create one

"; - // } $(this.el).html(this.template.render(this)); $('#'+this.id).html(this.el); }, @@ -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; } }); diff --git a/test/functional/mailing_lists_controller_test.rb b/test/functional/mailing_lists_controller_test.rb index c0c9510..9659731 100644 --- a/test/functional/mailing_lists_controller_test.rb +++ b/test/functional/mailing_lists_controller_test.rb @@ -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