Skip to content

Commit 55dee63

Browse files
committed
Added part 5 of the blog tutorial.
1 parent 95169b8 commit 55dee63

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+48733
-0
lines changed

part-5/.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore the default SQLite database.
11+
/db/*.sqlite3
12+
/db/*.sqlite3-journal
13+
14+
# Ignore all logfiles and tempfiles.
15+
/log/*.log
16+
/tmp
17+
18+
# Ignore other unneeded files
19+
*.DS_store
20+
*.sublime-project
21+
*.sublime-workspace

part-5/Gemfile

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
source 'https://rubygems.org'
2+
3+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
4+
gem 'rails', '4.0.0'
5+
6+
gem 'pg'
7+
8+
# Gems used only for assets and not required
9+
# in production environments by default.
10+
group :assets do
11+
gem 'sass-rails', '~> 4.0.0.rc2'
12+
gem 'coffee-rails', '~> 4.0.0.rc2'
13+
gem 'bootstrap-sass', '~> 2.3.1.0'
14+
15+
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
16+
# gem 'therubyracer', platforms: :ruby
17+
18+
gem 'uglifier', '>= 1.0.3'
19+
end
20+
21+
gem 'jquery-rails'
22+
23+
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
24+
gem 'turbolinks'
25+
26+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
27+
gem 'jbuilder', '~> 1.0.1'
28+
29+
# To use ActiveModel has_secure_password
30+
# gem 'bcrypt-ruby', '~> 3.0.0'
31+
32+
# Use unicorn as the app server
33+
# gem 'unicorn'
34+
35+
# Deploy with Capistrano
36+
# gem 'capistrano', group: :development
37+
38+
# To use debugger
39+
# gem 'debugger'

part-5/Gemfile.lock

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
actionmailer (4.0.0)
5+
actionpack (= 4.0.0)
6+
mail (~> 2.5.3)
7+
actionpack (4.0.0)
8+
activesupport (= 4.0.0)
9+
builder (~> 3.1.0)
10+
erubis (~> 2.7.0)
11+
rack (~> 1.5.2)
12+
rack-test (~> 0.6.2)
13+
activemodel (4.0.0)
14+
activesupport (= 4.0.0)
15+
builder (~> 3.1.0)
16+
activerecord (4.0.0)
17+
activemodel (= 4.0.0)
18+
activerecord-deprecated_finders (~> 1.0.2)
19+
activesupport (= 4.0.0)
20+
arel (~> 4.0.0)
21+
activerecord-deprecated_finders (1.0.3)
22+
activesupport (4.0.0)
23+
i18n (~> 0.6, >= 0.6.4)
24+
minitest (~> 4.2)
25+
multi_json (~> 1.3)
26+
thread_safe (~> 0.1)
27+
tzinfo (~> 0.3.37)
28+
arel (4.0.1)
29+
atomic (1.1.14)
30+
bootstrap-sass (2.3.1.3)
31+
sass (~> 3.2)
32+
builder (3.1.4)
33+
coffee-rails (4.0.1)
34+
coffee-script (>= 2.2.0)
35+
railties (>= 4.0.0, < 5.0)
36+
coffee-script (2.2.0)
37+
coffee-script-source
38+
execjs
39+
coffee-script-source (1.6.3)
40+
erubis (2.7.0)
41+
execjs (2.0.2)
42+
hike (1.2.3)
43+
i18n (0.6.5)
44+
jbuilder (1.0.2)
45+
activesupport (>= 3.0.0)
46+
jquery-rails (3.0.4)
47+
railties (>= 3.0, < 5.0)
48+
thor (>= 0.14, < 2.0)
49+
json (1.8.1)
50+
mail (2.5.4)
51+
mime-types (~> 1.16)
52+
treetop (~> 1.4.8)
53+
mime-types (1.25)
54+
minitest (4.7.5)
55+
multi_json (1.8.2)
56+
pg (0.17.0)
57+
polyglot (0.3.3)
58+
rack (1.5.2)
59+
rack-test (0.6.2)
60+
rack (>= 1.0)
61+
rails (4.0.0)
62+
actionmailer (= 4.0.0)
63+
actionpack (= 4.0.0)
64+
activerecord (= 4.0.0)
65+
activesupport (= 4.0.0)
66+
bundler (>= 1.3.0, < 2.0)
67+
railties (= 4.0.0)
68+
sprockets-rails (~> 2.0.0)
69+
railties (4.0.0)
70+
actionpack (= 4.0.0)
71+
activesupport (= 4.0.0)
72+
rake (>= 0.8.7)
73+
thor (>= 0.18.1, < 2.0)
74+
rake (10.1.0)
75+
sass (3.2.12)
76+
sass-rails (4.0.1)
77+
railties (>= 4.0.0, < 5.0)
78+
sass (>= 3.1.10)
79+
sprockets-rails (~> 2.0.0)
80+
sprockets (2.10.0)
81+
hike (~> 1.2)
82+
multi_json (~> 1.0)
83+
rack (~> 1.0)
84+
tilt (~> 1.1, != 1.3.0)
85+
sprockets-rails (2.0.1)
86+
actionpack (>= 3.0)
87+
activesupport (>= 3.0)
88+
sprockets (~> 2.8)
89+
thor (0.18.1)
90+
thread_safe (0.1.3)
91+
atomic
92+
tilt (1.4.1)
93+
treetop (1.4.15)
94+
polyglot
95+
polyglot (>= 0.3.1)
96+
turbolinks (1.3.0)
97+
coffee-rails
98+
tzinfo (0.3.38)
99+
uglifier (2.3.0)
100+
execjs (>= 0.3.0)
101+
json (>= 1.8.0)
102+
103+
PLATFORMS
104+
ruby
105+
106+
DEPENDENCIES
107+
bootstrap-sass (~> 2.3.1.0)
108+
coffee-rails (~> 4.0.0.rc2)
109+
jbuilder (~> 1.0.1)
110+
jquery-rails
111+
pg
112+
rails (= 4.0.0)
113+
sass-rails (~> 4.0.0.rc2)
114+
turbolinks
115+
uglifier (>= 1.0.3)

part-5/README.rdoc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
== README
2+
3+
This README would normally document whatever steps are necessary to get the
4+
application up and running.
5+
6+
Things you may want to cover:
7+
8+
* Ruby version
9+
10+
* System dependencies
11+
12+
* Configuration
13+
14+
* Database creation
15+
16+
* Database initialization
17+
18+
* How to run the test suite
19+
20+
* Services (job queues, cache servers, search engines, etc.)
21+
22+
* Deployment instructions
23+
24+
* ...
25+
26+
27+
Please feel free to use a different markup language if you do not plan to run
28+
<tt>rake doc:app</tt>.

part-5/Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require File.expand_path('../config/application', __FILE__)
5+
6+
Blog::Application.load_tasks
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@CreatePostCtrl = ($scope, $location, postData) ->
2+
3+
$scope.data = postData.data
4+
postData.loadPosts(null)
5+
6+
$scope.formData =
7+
newPostTitle: ''
8+
newPostContents: ''
9+
10+
$scope.navNewPost = ->
11+
$location.url('/post/new')
12+
13+
$scope.navHome = ->
14+
$location.url('/')
15+
16+
$scope.createPost = ->
17+
postData.createPost($scope.formData)
18+
19+
$scope.clearPost = ->
20+
$scope.formData.newPostTitle = ''
21+
$scope.formData.newPostContents = ''
22+
23+
24+
@CreatePostCtrl.$inject = ['$scope', '$location', 'postData']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@IndexCtrl = ($scope, $location, $http, postData) ->
2+
3+
$scope.data = postData.data
4+
5+
postData.loadPosts(null)
6+
7+
$scope.viewPost = (postId) ->
8+
$location.url('/post/'+postId)
9+
10+
$scope.navNewPost = ->
11+
$location.url('/post/new')
12+
13+
@IndexCtrl.$inject = ['$scope', '$location', '$http', 'postData']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@PostCtrl = ($scope, $routeParams, $q, postData) ->
2+
3+
$scope.data =
4+
postData: postData.data
5+
currentPost:
6+
title: 'Loading...'
7+
contents: ''
8+
9+
$scope.data.postId = $routeParams.postId
10+
11+
$scope.navNewPost = ->
12+
$location.url('/post/new')
13+
14+
$scope.navHome = ->
15+
$location.url('/')
16+
17+
# This will be run once the loadPosts successfully completes (or immediately
18+
# if data is already loaded)
19+
$scope.prepPostData = ->
20+
post = _.findWhere(postData.data.posts, { id: parseInt($scope.data.postId) })
21+
$scope.data.currentPost.title = post.title
22+
$scope.data.currentPost.contents = post.contents
23+
24+
# Create promise to be resolved after posts load
25+
@deferred = $q.defer()
26+
@deferred.promise.then($scope.prepPostData)
27+
28+
# Provide deferred promise chain to the loadPosts function
29+
postData.loadPosts(@deferred)
30+
31+
32+
@PostCtrl.$inject = ['$scope', '$routeParams', '$q', 'postData']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
angular.module('Blog').factory('postData', ['$http', ($http) ->
2+
3+
postData =
4+
data:
5+
posts: [{title: 'Loading', contents: ''}]
6+
isLoaded: false
7+
8+
postData.loadPosts = (deferred) ->
9+
if !postData.isLoaded
10+
$http.get('./posts.json').success( (data) ->
11+
postData.data.posts = data
12+
postData.isLoaded = true
13+
console.log('Successfully loaded posts.')
14+
if deferred
15+
deferred.resolve()
16+
).error( ->
17+
console.error('Failed to load posts.')
18+
if deferred
19+
deferred.resolve()
20+
)
21+
else
22+
if deferred
23+
deferred.resolve()
24+
25+
26+
postData.createPost = (newPost) ->
27+
# Client-side data validation
28+
if newPost.newPostTitle == '' or newPost.newPostContents == ''
29+
alert('Neither the Title nor the Body are allowed to be left blank.')
30+
return false
31+
32+
# Create data object to POST
33+
data =
34+
new_post:
35+
title: newPost.newPostTitle
36+
contents: newPost.newPostContents
37+
38+
# Do POST request to /posts.json
39+
$http.post('./posts.json', data).success( (data) ->
40+
41+
# Add new post to array of posts
42+
postData.data.posts.push(data)
43+
console.log('Successfully created post.')
44+
45+
).error( ->
46+
console.error('Failed to create new post.')
47+
)
48+
49+
return true
50+
51+
return postData
52+
53+
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This is a manifest file that'll be compiled into application.js, which will include all the files
2+
// listed below.
3+
//
4+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6+
//
7+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8+
// compiled file.
9+
//
10+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11+
// GO AFTER THE REQUIRES BELOW.
12+
//
13+
//= require jquery
14+
//= require jquery_ujs
15+
//= require underscore
16+
//= require bootstrap
17+
//= require angular
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
4+
5+
#= require_self
6+
#= require_tree ./Controllers/main
7+
#= require_tree ./Directives/main
8+
#= require_tree ./Filters/main
9+
#= require_tree ./Services/main
10+
11+
# Creates new Angular module called 'Blog'
12+
Blog = angular.module('Blog', [])
13+
14+
# Sets up routing
15+
Blog.config(['$routeProvider', ($routeProvider) ->
16+
# Route for '/post/'
17+
$routeProvider
18+
.when('/post/new', { templateUrl: '../assets/mainCreatePost.html', controller: 'CreatePostCtrl' } )
19+
.when('/post/:postId', { templateUrl: '../assets/mainPost.html', controller: 'PostCtrl' } )
20+
#.when('/post/:postId/edit', { templateUrl: '../assets/mainEditPost.html', controller: 'EditPostCtrl' } )
21+
22+
# Default
23+
$routeProvider.otherwise({ templateUrl: '../assets/mainIndex.html', controller: 'IndexCtrl' } )
24+
25+
])
26+
27+
Blog.config(["$httpProvider", (provider) ->
28+
provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
29+
])

0 commit comments

Comments
 (0)