Skip to content

Commit

Permalink
Getting list of todo items
Browse files Browse the repository at this point in the history
  • Loading branch information
h_dung committed Jul 7, 2014
1 parent 5417d61 commit 27f09b6
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 32 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ group :test do
gem 'capybara'
gem 'rspec-rails'
gem 'poltergeist'
gem 'database_cleaner'
end
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.7.0)
database_cleaner (1.3.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.1)
Expand Down Expand Up @@ -140,6 +141,7 @@ PLATFORMS
DEPENDENCIES
capybara
coffee-rails (~> 4.0.0)
database_cleaner
jbuilder (~> 1.2)
jquery-rails
poltergeist
Expand Down
54 changes: 34 additions & 20 deletions app/assets/javascripts/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@

var sampleApp = angular.module('sampleApp', ['ngRoute']);

sampleApp.config(['$routeProvider',
function($routeProvider){
$routeProvider.
when('/AddNewOrder', {
templateUrl: '/assets/add_order.html',
controller: 'AddOrderController'
}).when('/ShowOrders', {
templateUrl: '/assets/show_orders.html',
controller: 'ShowOrdersController'
}).otherwise({
redirectTo: '/AddNewOrder'
});
}]);
sampleApp.controller('AddOrderController', function($scope){
$scope.message = 'This is Add new order screen';
var model = {
user: "Adam",
};
var sampleApp = angular.module('sampleApp', []);
sampleApp.run(function ($http) {
$http.get("items").success(function (data) {
model.items = data;
});
});
sampleApp.controller('ShowOrdersController', function($scope){
$scope.message = 'This is Show Orders screen';
sampleApp.filter("checkedItems", function () {
return function (items, showComplete) {
var resultArr = [];
angular.forEach(items, function (item) {
if (item.done == false || showComplete == true) {
resultArr.push(item);
}
});
return resultArr;
}
});
sampleApp.controller("ToDoCtrl", function ($scope) {
$scope.todo = model;
$scope.incompleteCount = function () {
var count = 0;
angular.forEach($scope.todo.items, function (item) {
if (!item.done) { count++ }
});
return count;
}
$scope.warningLevel = function () {
return $scope.incompleteCount() < 3 ? "label-success" : "label-warning";
}
$scope.addNewItem = function (actionText) {
$scope.todo.items.push({ action: actionText, done: false });
}
});
3 changes: 3 additions & 0 deletions app/assets/javascripts/items.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/items.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Items controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h2>Add New Order</h2>

{{ message }}
{{ message }}
File renamed without changes.
6 changes: 6 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ItemsController < ApplicationController
def index
@items = Item.all
render json: @items
end
end
2 changes: 2 additions & 0 deletions app/helpers/items_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ItemsHelper
end
2 changes: 2 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Item < ActiveRecord::Base
end
46 changes: 35 additions & 11 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
<div class="container">
<div class="row">
<div class="col-md-3">
<ul class="nav">
<li><a href="#AddNewOrder"> Add New Order </a></li>
<li><a href="#ShowOrders"> Show Orders </a></li>
</ul>
</div>
<div class="col-md-9">
<div ng-view></div>
</div>
<div class="container">
<div ng-controller='ToDoCtrl'>
<div class="page-header">
<h1>
{{todo.user}}'s To Do List
<span class="label label-default" ng-class="warningLevel()" ng-hide="incompleteCount() == 0">
{{incompleteCount()}}
</span>
</h1>
</div>
<div class="panel">
<div class="input-group">
<input class="form-control" ng-model="actionText" />
<span class="input-group-btn">
<button class="btn btn-default" ng-click="addNewItem(actionText)">Add</button>
</span>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Done</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in todo.items | checkedItems:showComplete | orderBy:'action'">
<td>{{item.action}}</td>
<td><input type="checkbox" ng-model="item.done" /></td>
</tr>
</tbody>
</table>
<div class="checkbox-inline">
<label><input type="checkbox" ng_model="showComplete"> Show Complete</label>
</div>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Items#index</h1>
<p>Find me in app/views/items/index.html.erb</p>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Testangular::Application.routes.draw do
resources :items
root "home#index"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20140707042945_create_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :action
t.boolean :done

t.timestamps
end
end
end
23 changes: 23 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140707042945) do

create_table "items", force: true do |t|
t.string "action"
t.boolean "done"
t.datetime "created_at"
t.datetime "updated_at"
end

end
9 changes: 9 additions & 0 deletions spec/features/homepage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
click_link 'Show Orders'
expect(page).to have_content 'Show Orders'
end

scenario 'viewing products', :js => true do
c = Category.create(name: 'Category1')
Product.create(name: 'Product1', category: c)
visit '/'
click_link 'Products'
expect(page).to have_content 'Product1'
expect(page).to have_content 'Category1'
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'rspec/autorun'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
require 'database_cleaner'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Expand Down Expand Up @@ -40,4 +41,17 @@
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.use_transactional_fixtures = false

config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end
end
9 changes: 9 additions & 0 deletions test/controllers/items_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class ItemsControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end

end
9 changes: 9 additions & 0 deletions test/fixtures/items.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
action: MyString
done: false

two:
action: MyString
done: false
4 changes: 4 additions & 0 deletions test/helpers/items_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class ItemsHelperTest < ActionView::TestCase
end
7 changes: 7 additions & 0 deletions test/models/item_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ItemTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 27f09b6

Please sign in to comment.