Permalink
Browse files

Create methods for searching fwd and back columns

Moves code for forward and backwards column search from StoryView
to Project model.
  • Loading branch information...
1 parent 26d45fd commit 35118a86bfd603304091b00f2558199c171fe76a @malclocke malclocke committed Oct 4, 2011
@@ -26,6 +26,31 @@ var Project = Backbone.Model.extend({
return '/projects/' + this.id;
},
+ // The ids of the columns, in the order that they appear by story weight
+ columnIds: ['#done', '#in_progress', '#backlog', '#chilly_bin'],
+
+ // Return an array of the columns that appear after column, or an empty
+ // array if the column is the last
+ columnsAfter: function(column) {
+ var index = _.indexOf(this.columnIds, column);
+ if (index === -1) {
+ // column was not found in the array
+ throw column.toString() + ' is not a valid column';
+ }
+ return this.columnIds.slice(index + 1);
+ },
+
+ // Return an array of the columns that appear before column, or an empty
+ // array if the column is the first
+ columnsBefore: function(column) {
+ var index = _.indexOf(this.columnIds, column);
+ if (index === -1) {
+ // column was not found in the array
+ throw column.toString() + ' is not a valid column';
+ }
+ return this.columnIds.slice(0, index);
+ },
+
// This method is triggered when the last_changeset_id attribute is changed,
// which indicates there are changed or new stories on the server which need
// to be loaded.
@@ -72,14 +72,8 @@ var StoryView = FormView.extend({
// are the only columns that can receive drops from other columns.
if (typeof previous_story_id == 'undefined' && typeof next_story_id == 'undefined') {
- var beforeSearchColumns = [], afterSearchColumns = [];
-
- if (column == 'chilly_bin') {
- beforeSearchColumns = ['#done', '#current', '#backlog'];
- } else if (column == 'backlog') {
- beforeSearchColumns = ['#done', '#current'];
- afterSearchColumns = ['#chilly_bin'];
- }
+ var beforeSearchColumns = this.model.collection.project.columnsBefore('#' + column);
+ var afterSearchColumns = this.model.collection.project.columnsAfter('#' + column);
var previousStory = _.last(this.model.collection.columns(beforeSearchColumns));
var nextStory = _.first(this.model.collection.columns(afterSearchColumns));
@@ -339,4 +339,46 @@ describe('Project model', function() {
});
+ describe("columns", function() {
+
+ it("should define the columns", function() {
+ expect(this.project.columnIds).toEqual([
+ '#done', '#in_progress', '#backlog', '#chilly_bin'
+ ]);
+ });
+
+ it("should return the columns after a given column", function() {
+ expect(this.project.columnsAfter('#done')).toEqual([
+ '#in_progress', '#backlog', '#chilly_bin'
+ ]);
+ expect(this.project.columnsAfter('#in_progress')).toEqual([
+ '#backlog', '#chilly_bin'
+ ]);
+ expect(this.project.columnsAfter('#backlog')).toEqual(['#chilly_bin']);
+ expect(this.project.columnsAfter('#chilly_bin')).toEqual([]);
+
+ var project = this.project;
+ expect(function() {project.columnsAfter('#foobar');}).toThrow(
+ "#foobar is not a valid column"
+ );
+ });
+
+ it("should return the columns before a given column", function() {
+ expect(this.project.columnsBefore('#done')).toEqual([]);
+ expect(this.project.columnsBefore('#in_progress')).toEqual(['#done']);
+ expect(this.project.columnsBefore('#backlog')).toEqual([
+ '#done', '#in_progress'
+ ]);
+ expect(this.project.columnsBefore('#chilly_bin')).toEqual([
+ '#done', '#in_progress', '#backlog'
+ ]);
+
+ var project = this.project;
+ expect(function() {project.columnsBefore('#foobar');}).toThrow(
+ "#foobar is not a valid column"
+ );
+ });
+
+ });
+
});
@@ -211,6 +211,8 @@ describe('StoryView', function() {
beforeEach(function() {
this.story.collection.length = 1;
this.story.collection.columns = function() {return [];};
+ this.story.collection.project.columnsBefore = sinon.stub();
+ this.story.collection.project.columnsAfter = sinon.stub();
});
it("sets state to unstarted if dropped on the backlog column", function() {

0 comments on commit 35118a8

Please sign in to comment.