Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add tasks, completed tasks MVCs; random task view; task saves if done

  • Loading branch information...
commit 05888658ef7a866b436e8550935450517a097e4d 1 parent 9388218
@joemerante joemerante authored
Showing with 629 additions and 1 deletion.
  1. +3 −0  app/assets/javascripts/completed_tasks.js.coffee
  2. +3 −0  app/assets/javascripts/tasks.js.coffee
  3. +3 −0  app/assets/stylesheets/completed_tasks.css.scss
  4. +56 −0 app/assets/stylesheets/scaffolds.css.scss
  5. +3 −0  app/assets/stylesheets/tasks.css.scss
  6. +97 −0 app/controllers/completed_tasks_controller.rb
  7. +85 −0 app/controllers/tasks_controller.rb
  8. +2 −0  app/helpers/completed_tasks_helper.rb
  9. +2 −0  app/helpers/tasks_helper.rb
  10. +3 −0  app/models/completed_task.rb
  11. +9 −0 app/models/task.rb
  12. +1 −0  app/models/user.rb
  13. +33 −0 app/views/completed_tasks/_form.html.erb
  14. +6 −0 app/views/completed_tasks/edit.html.erb
  15. +29 −0 app/views/completed_tasks/index.html.erb
  16. +5 −0 app/views/completed_tasks/new.html.erb
  17. +25 −0 app/views/completed_tasks/show.html.erb
  18. +1 −0  app/views/devise/_links.erb
  19. +25 −0 app/views/tasks/_form.html.erb
  20. +6 −0 app/views/tasks/edit.html.erb
  21. +27 −0 app/views/tasks/index.html.erb
  22. +5 −0 app/views/tasks/new.html.erb
  23. +15 −0 app/views/tasks/show.html.erb
  24. +7 −0 config/routes.rb
  25. +10 −0 db/migrate/20120521214235_create_tasks.rb
  26. +10 −0 db/migrate/20120521214603_create_completed_tasks.rb
  27. +16 −1 db/schema.rb
  28. +13 −0 test/fixtures/completed_tasks.yml
  29. +9 −0 test/fixtures/tasks.yml
  30. +49 −0 test/functional/completed_tasks_controller_test.rb
  31. +49 −0 test/functional/tasks_controller_test.rb
  32. +7 −0 test/unit/completed_task_test.rb
  33. +4 −0 test/unit/helpers/completed_tasks_helper_test.rb
  34. +4 −0 test/unit/helpers/tasks_helper_test.rb
  35. +7 −0 test/unit/task_test.rb
View
3  app/assets/javascripts/completed_tasks.js.coffee
@@ -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://jashkenas.github.com/coffee-script/
View
3  app/assets/javascripts/tasks.js.coffee
@@ -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://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/completed_tasks.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Completed_Tasks controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
56 app/assets/stylesheets/scaffolds.css.scss
@@ -0,0 +1,56 @@
+body {
+ background-color: #fff;
+ color: #333;
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px; }
+
+p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px; }
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px; }
+
+a {
+ color: #000;
+ &:visited {
+ color: #666; }
+ &:hover {
+ color: #fff;
+ background-color: #000; } }
+
+div {
+ &.field, &.actions {
+ margin-bottom: 10px; } }
+
+#notice {
+ color: green; }
+
+.field_with_errors {
+ padding: 2px;
+ background-color: red;
+ display: table; }
+
+#error_explanation {
+ width: 450px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 0;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+ h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ margin-bottom: 0px;
+ background-color: #c00;
+ color: #fff; }
+ ul li {
+ font-size: 12px;
+ list-style: square; } }
View
3  app/assets/stylesheets/tasks.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Tasks controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
97 app/controllers/completed_tasks_controller.rb
@@ -0,0 +1,97 @@
+class CompletedTasksController < ApplicationController
+ # GET /completed_tasks
+ # GET /completed_tasks.json
+ def index
+ @completed_tasks = CompletedTask.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @completed_tasks }
+ end
+ end
+
+ # GET /completed_tasks/1
+ # GET /completed_tasks/1.json
+ def show
+ @completed_task = CompletedTask.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @completed_task }
+ end
+ end
+
+ def completed
+ @user = current_user
+ task_id = session[:task_id]
+ CompletedTask.create!(:user_id => current_user.id, :task_id => task_id, :completed => TRUE)
+ redirect_to tasks_path
+ end
+
+ # GET /completed_tasks/new
+ # GET /completed_tasks/new.json
+ def new
+ # if user_signed_in?
+ # @user = current_user
+ # @findrandom = Task.find(params[:id])
+ # @completed_task = CompletedTask.create!(:user_id => @user.user_id, :task_id => @findrandom, :completed => yes)
+ # #will the created_at and updated_at get populated automatically in the new completed task?
+ # else
+ # redirect_to(new_user_registration_path)
+ # end
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @completed_task }
+ end
+ end
+
+ # GET /completed_tasks/1/edit
+ def edit
+ @completed_task = CompletedTask.find(params[:id])
+ end
+
+ # POST /completed_tasks
+ # POST /completed_tasks.json
+ def create
+ @completed_task = CompletedTask.new(params[:completed_task])
+
+ respond_to do |format|
+ if @completed_task.save
+ format.html { redirect_to @completed_task, notice: 'Completed task was successfully created.' }
+ format.json { render json: @completed_task, status: :created, location: @completed_task }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @completed_task.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /completed_tasks/1
+ # PUT /completed_tasks/1.json
+ def update
+ @completed_task = CompletedTask.find(params[:id])
+
+ respond_to do |format|
+ if @completed_task.update_attributes(params[:completed_task])
+ format.html { redirect_to @completed_task, notice: 'Completed task was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @completed_task.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /completed_tasks/1
+ # DELETE /completed_tasks/1.json
+ def destroy
+ @completed_task = CompletedTask.find(params[:id])
+ @completed_task.destroy
+
+ respond_to do |format|
+ format.html { redirect_to completed_tasks_url }
+ format.json { head :no_content }
+ end
+ end
+end
View
85 app/controllers/tasks_controller.rb
@@ -0,0 +1,85 @@
+class TasksController < ApplicationController
+ # GET /tasks
+ # GET /tasks.json
+
+ def index
+ # @tasks = Task.all
+ @random = Task.random
+ session[:task_id] = @random.id
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @tasks }
+ end
+ end
+
+ # GET /tasks/1
+ # GET /tasks/1.json
+ def show
+ @task = Task.find(params[:id])
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @task }
+ end
+ end
+
+ # GET /tasks/new
+ # GET /tasks/new.json
+ def new
+ @task = Task.new
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @task }
+ end
+ end
+
+ # GET /tasks/1/edit
+ def edit
+ @task = Task.find(params[:id])
+ end
+
+ # POST /tasks
+ # POST /tasks.json
+ def create
+ @task = Task.new(params[:task])
+
+ respond_to do |format|
+ if @task.save
+ format.html { redirect_to @task, notice: 'Task was successfully created.' }
+ format.json { render json: @task, status: :created, location: @task }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @task.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /tasks/1
+ # PUT /tasks/1.json
+ def update
+ @task = Task.find(params[:id])
+
+ respond_to do |format|
+ if @task.update_attributes(params[:task])
+ format.html { redirect_to @task, notice: 'Task was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @task.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /tasks/1
+ # DELETE /tasks/1.json
+ def destroy
+ @task = Task.find(params[:id])
+ @task.destroy
+
+ respond_to do |format|
+ format.html { redirect_to tasks_url }
+ format.json { head :no_content }
+ end
+ end
+end
+
+
View
2  app/helpers/completed_tasks_helper.rb
@@ -0,0 +1,2 @@
+module CompletedTasksHelper
+end
View
2  app/helpers/tasks_helper.rb
@@ -0,0 +1,2 @@
+module TasksHelper
+end
View
3  app/models/completed_task.rb
@@ -0,0 +1,3 @@
+class CompletedTask < ActiveRecord::Base
+ attr_accessible :completed, :remind, :task_id, :user_id
+end
View
9 app/models/task.rb
@@ -0,0 +1,9 @@
+class Task < ActiveRecord::Base
+ attr_accessible :difficulty, :task
+ has_many :users
+
+ def self.random
+ ids = connection.select_all("SELECT id from tasks")
+ find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
+ end
+end
View
1  app/models/user.rb
@@ -1,6 +1,7 @@
class User < ActiveRecord::Base
# to have many ways to authenticate into a single user account, add this line and a related controller and model
has_many :authentications
+ has_many :tasks
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable
View
33 app/views/completed_tasks/_form.html.erb
@@ -0,0 +1,33 @@
+<%= form_for(@completed_task) do |f| %>
+ <% if @completed_task.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@completed_task.errors.count, "error") %> prohibited this completed_task from being saved:</h2>
+
+ <ul>
+ <% @completed_task.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :user_id %><br />
+ <%= f.text_field :user_id %>
+ </div>
+ <div class="field">
+ <%= f.label :task_id %><br />
+ <%= f.text_field :task_id %>
+ </div>
+ <div class="field">
+ <%= f.label :completed %><br />
+ <%= f.text_field :completed %>
+ </div>
+ <div class="field">
+ <%= f.label :remind %><br />
+ <%= f.date_select :remind %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
View
6 app/views/completed_tasks/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing completed_task</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @completed_task %> |
+<%= link_to 'Back', completed_tasks_path %>
View
29 app/views/completed_tasks/index.html.erb
@@ -0,0 +1,29 @@
+<h1>Listing completed_tasks</h1>
+
+<table>
+ <tr>
+ <th>User</th>
+ <th>Task</th>
+ <th>Completed</th>
+ <th>Remind</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @completed_tasks.each do |completed_task| %>
+ <tr>
+ <td><%= completed_task.user_id %></td>
+ <td><%= completed_task.task_id %></td>
+ <td><%= completed_task.completed %></td>
+ <td><%= completed_task.remind %></td>
+ <td><%= link_to 'Show', completed_task %></td>
+ <td><%= link_to 'Edit', edit_completed_task_path(completed_task) %></td>
+ <td><%= link_to 'Destroy', completed_task, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Completed task', new_completed_task_path %>
View
5 app/views/completed_tasks/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New completed_task</h1>
+
+<%=# render 'form' %>
+
+<%= link_to 'Back', completed_tasks_path %>
View
25 app/views/completed_tasks/show.html.erb
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>User:</b>
+ <%= @completed_task.user_id %>
+</p>
+
+<p>
+ <b>Task:</b>
+ <%= @completed_task.task_id %>
+</p>
+
+<p>
+ <b>Completed:</b>
+ <%= @completed_task.completed %>
+</p>
+
+<p>
+ <b>Remind:</b>
+ <%= @completed_task.remind %>
+</p>
+
+
+<%= link_to 'Edit', edit_completed_task_path(@completed_task) %> |
+<%= link_to 'Back', completed_tasks_path %>
View
1  app/views/devise/_links.erb
@@ -20,6 +20,7 @@
<%- if devise_mapping.omniauthable? %>
+ <%# display Sign in with Google instead of google_oauth2 in sign up link %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%- if (provider.to_s == "google_oauth2" ) %>
<%= provider_icon "google_oauth2" %>
View
25 app/views/tasks/_form.html.erb
@@ -0,0 +1,25 @@
+<%= form_for(@task) do |f| %>
+ <% if @task.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
+
+ <ul>
+ <% @task.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :task %><br />
+ <%= f.text_area :task %>
+ </div>
+ <div class="field">
+ <%= f.label :difficulty %><br />
+ <%= f.text_field :difficulty %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
View
6 app/views/tasks/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing task</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @task %> |
+<%= link_to 'Back', tasks_path %>
View
27 app/views/tasks/index.html.erb
@@ -0,0 +1,27 @@
+<h1>Listing tasks</h1>
+
+<table>
+ <tr>
+ <th>Task</th>
+ <th>Difficulty</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+ <tr>
+ <td><%= @random.task %></td>
+ <td><%= @random.difficulty %></td>
+ </tr>
+</table>
+
+<br />
+
+<%= link_to 'Skip for now', tasks_path %>
+
+<br />
+
+<%= link_to 'Already Done', :controller => 'completed_tasks', :action => 'completed' %>
+
+<br />
+<% link_to 'New Task', new_task_path %>
+
View
5 app/views/tasks/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New task</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', tasks_path %>
View
15 app/views/tasks/show.html.erb
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Task:</b>
+ <%= @task.task %>
+</p>
+
+<p>
+ <b>Difficulty:</b>
+ <%= @task.difficulty %>
+</p>
+
+
+<%= link_to 'Edit', edit_task_path(@task) %> |
+<%= link_to 'Back', tasks_path %>
View
7 config/routes.rb
@@ -1,7 +1,14 @@
Preparedly::Application.routes.draw do
+ resources :completed_tasks
+ resources :tasks
+
match "resources" => "resources#index"
+ match 'completed' => 'completed_tasks#completed'
+
+ #match "completed" => :controller => { :completed_tasks => "completed" }
+
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
View
10 db/migrate/20120521214235_create_tasks.rb
@@ -0,0 +1,10 @@
+class CreateTasks < ActiveRecord::Migration
+ def change
+ create_table :tasks do |t|
+ t.text :task
+ t.string :difficulty
+
+ t.timestamps
+ end
+ end
+end
View
10 db/migrate/20120521214603_create_completed_tasks.rb
@@ -0,0 +1,10 @@
+class CreateCompletedTasks < ActiveRecord::Migration
+ def change
+ create_table :completed_tasks do |t|
+ t.references :user
+ t.references :task
+ t.boolean :completed
+ t.timestamps
+ end
+ end
+end
View
17 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120509204947) do
+ActiveRecord::Schema.define(:version => 20120521214603) do
create_table "addresses", :force => true do |t|
t.string "address"
@@ -31,6 +31,14 @@
t.datetime "updated_at", :null => false
end
+ create_table "completed_tasks", :force => true do |t|
+ t.integer "user_id"
+ t.integer "task_id"
+ t.boolean "completed"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "counties", :force => true do |t|
t.string "name"
t.spatial "geometry", :limit => {:srid=>4326, :type=>"geometry", :geographic=>true}
@@ -77,6 +85,13 @@
add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
+ create_table "tasks", :force => true do |t|
+ t.text "task"
+ t.string "difficulty"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
View
13 test/fixtures/completed_tasks.yml
@@ -0,0 +1,13 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ user_id:
+ task_id:
+ completed:
+ remind: 2012-05-21
+
+two:
+ user_id:
+ task_id:
+ completed:
+ remind: 2012-05-21
View
9 test/fixtures/tasks.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ task: MyText
+ difficulty: MyString
+
+two:
+ task: MyText
+ difficulty: MyString
View
49 test/functional/completed_tasks_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class CompletedTasksControllerTest < ActionController::TestCase
+ setup do
+ @completed_task = completed_tasks(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:completed_tasks)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create completed_task" do
+ assert_difference('CompletedTask.count') do
+ post :create, completed_task: { completed: @completed_task.completed, remind: @completed_task.remind, task_id: @completed_task.task_id, user_id: @completed_task.user_id }
+ end
+
+ assert_redirected_to completed_task_path(assigns(:completed_task))
+ end
+
+ test "should show completed_task" do
+ get :show, id: @completed_task
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @completed_task
+ assert_response :success
+ end
+
+ test "should update completed_task" do
+ put :update, id: @completed_task, completed_task: { completed: @completed_task.completed, remind: @completed_task.remind, task_id: @completed_task.task_id, user_id: @completed_task.user_id }
+ assert_redirected_to completed_task_path(assigns(:completed_task))
+ end
+
+ test "should destroy completed_task" do
+ assert_difference('CompletedTask.count', -1) do
+ delete :destroy, id: @completed_task
+ end
+
+ assert_redirected_to completed_tasks_path
+ end
+end
View
49 test/functional/tasks_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class TasksControllerTest < ActionController::TestCase
+ setup do
+ @task = tasks(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:tasks)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create task" do
+ assert_difference('Task.count') do
+ post :create, task: { difficulty: @task.difficulty, task: @task.task }
+ end
+
+ assert_redirected_to task_path(assigns(:task))
+ end
+
+ test "should show task" do
+ get :show, id: @task
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @task
+ assert_response :success
+ end
+
+ test "should update task" do
+ put :update, id: @task, task: { difficulty: @task.difficulty, task: @task.task }
+ assert_redirected_to task_path(assigns(:task))
+ end
+
+ test "should destroy task" do
+ assert_difference('Task.count', -1) do
+ delete :destroy, id: @task
+ end
+
+ assert_redirected_to tasks_path
+ end
+end
View
7 test/unit/completed_task_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CompletedTaskTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/completed_tasks_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CompletedTasksHelperTest < ActionView::TestCase
+end
View
4 test/unit/helpers/tasks_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class TasksHelperTest < ActionView::TestCase
+end
View
7 test/unit/task_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TaskTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 0588865

Please sign in to comment.
Something went wrong with that request. Please try again.