From 3b904f95f2c4d18f3f351439e8c68a2b22e7f855 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sun, 9 Mar 2014 19:20:00 -0400 Subject: [PATCH] Adding api task_lists#show and spec, edit.haml updates --- .../api/v1/task_lists_controller.rb | 6 +-- app/models/task_list.rb | 2 +- app/views/task_lists/edit.haml | 5 +- config/routes.rb | 2 +- .../api/task_lists_controller_spec.rb | 52 +++++++++++++++++++ spec/factories/task_lists.rb | 7 +++ 6 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 spec/controllers/api/task_lists_controller_spec.rb create mode 100644 spec/factories/task_lists.rb diff --git a/app/controllers/api/v1/task_lists_controller.rb b/app/controllers/api/v1/task_lists_controller.rb index 9d4ff43..6c89a5e 100644 --- a/app/controllers/api/v1/task_lists_controller.rb +++ b/app/controllers/api/v1/task_lists_controller.rb @@ -8,13 +8,9 @@ class Api::V1::TaskListsController < Api::V1::BaseController def show end - def edit - #@task_list.update_attributes(params) - end - private def get_task_list - @task_list = TaskList.find(params[:id]) + @task_list = TaskList.find_by_id(params[:id]) if @task_list.nil? render json: { errors: [NOT_FOUND] }, status: 404 and return end diff --git a/app/models/task_list.rb b/app/models/task_list.rb index 49ace63..85ab5ed 100644 --- a/app/models/task_list.rb +++ b/app/models/task_list.rb @@ -4,7 +4,7 @@ class TaskList < ActiveRecord::Base attr_accessible :item_id, :item_type, :name belongs_to :item, :polymorphic => true - has_many :tasks + has_many :tasks, order: "id ASC" after_save :create_default_bike_tasks diff --git a/app/views/task_lists/edit.haml b/app/views/task_lists/edit.haml index fc7beed..76599af 100644 --- a/app/views/task_lists/edit.haml +++ b/app/views/task_lists/edit.haml @@ -6,5 +6,8 @@ .control-group - @task_list.tasks.each do |task| .controls - %input{class: "task_list_task", type: "checkbox", "data-id" => task.id} + %input{class: "task_list_task", type: "checkbox", "data-id" => task.id, checked: task.done} #{task.task} + .control-group + .controls + %input{id: "update_tasks_submit", value: "Save Changes", type: "button", class: "btn btn-lg btn-block btn-primary disabled", "data-url" => "#{api_update_task_path}"} diff --git a/config/routes.rb b/config/routes.rb index e62f66e..48f8990 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,7 +23,7 @@ Velocipede::Application.routes.draw do get 'bikes/:id' => "bikes#show", as: "api_bike" post 'bikes/create' => "bikes#create", as: "api_create_bike" - get 'task_lists/:id' => "task_lists#show", as: "api_task_list" + get 'task_lists/:id' => "task_lists#show", as: "api_task_list" end end diff --git a/spec/controllers/api/task_lists_controller_spec.rb b/spec/controllers/api/task_lists_controller_spec.rb new file mode 100644 index 0000000..60dfd33 --- /dev/null +++ b/spec/controllers/api/task_lists_controller_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper' + +describe Api::V1::TaskListsController do + render_views + + describe "#show" do + context "as a user" do + before(:each) do + @user = FactoryGirl.create(:user) + sign_in @user + end + + context "task list exists" do + let!(:task_list){ FactoryGirl.create(:task_list) } + + it "returns 200" do + get :show, id: task_list.id, format: :json + expect(@response.code.to_i).to eql 200 + end + + it "returns valid task list json" do + get :show, id: task_list.id, format: :json + json = JSON.parse(@response.body) + expect(json.to_s).to include(task_list.name) + end + + it "returns task list with tasks json" do + get :show, id: task_list.id, format: :json + json = JSON.parse(@response.body) + expect(task_list.tasks.count).to be > 0 + task_list.tasks.each do |task| + expect(json.to_s).to include(task.task) + end + end + end + + context "task list does not exist" do + it "returns 404" do + get :show, id: 999, format: :json + expect(@response.code.to_i).to eql 404 + end + + it "returns an error message" do + get :show, id: 999, format: :json + json = JSON.parse(@response.body) + expect(json["errors"].first). + to eql Api::V1::TaskListsController::NOT_FOUND + end + end + end + end +end diff --git a/spec/factories/task_lists.rb b/spec/factories/task_lists.rb new file mode 100644 index 0000000..5b70366 --- /dev/null +++ b/spec/factories/task_lists.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :task_list do + item_id { FactoryGirl.create(:bike).id } + item_type "Bike" + name { Faker::Lorem.words.join(" ")} + end +end