Adding rewards scaffold

This commit is contained in:
Tyler Renelle 2012-02-04 16:49:11 -05:00
parent 3abcf468e9
commit 50f713e7e9
17 changed files with 280 additions and 0 deletions

View file

@ -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 file

@ -0,0 +1,3 @@
// Place all the styles related to the rewards controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -0,0 +1,83 @@
class RewardsController < ApplicationController
# GET /rewards
# GET /rewards.json
def index
@rewards = Reward.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @rewards }
end
end
# GET /rewards/1
# GET /rewards/1.json
def show
@reward = Reward.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @reward }
end
end
# GET /rewards/new
# GET /rewards/new.json
def new
@reward = Reward.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @reward }
end
end
# GET /rewards/1/edit
def edit
@reward = Reward.find(params[:id])
end
# POST /rewards
# POST /rewards.json
def create
@reward = Reward.new(params[:reward])
respond_to do |format|
if @reward.save
format.html { redirect_to @reward, notice: 'Reward was successfully created.' }
format.json { render json: @reward, status: :created, location: @reward }
else
format.html { render action: "new" }
format.json { render json: @reward.errors, status: :unprocessable_entity }
end
end
end
# PUT /rewards/1
# PUT /rewards/1.json
def update
@reward = Reward.find(params[:id])
respond_to do |format|
if @reward.update_attributes(params[:reward])
format.html { redirect_to @reward, notice: 'Reward was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @reward.errors, status: :unprocessable_entity }
end
end
end
# DELETE /rewards/1
# DELETE /rewards/1.json
def destroy
@reward = Reward.find(params[:id])
@reward.destroy
respond_to do |format|
format.html { redirect_to rewards_url }
format.json { head :no_content }
end
end
end

View file

@ -0,0 +1,2 @@
module RewardsHelper
end

3
app/models/reward.rb Normal file
View file

@ -0,0 +1,3 @@
class Reward < ActiveRecord::Base
belongs_to :user
end

View file

@ -8,4 +8,5 @@ class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :habits, :dependent => :destroy
has_many :rewards, :dependent => :destroy
end

View file

@ -0,0 +1,33 @@
<%= form_for(@reward) do |f| %>
<% if @reward.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@reward.errors.count, "error") %> prohibited this reward from being saved:</h2>
<ul>
<% @reward.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :value %><br />
<%= f.number_field :value %>
</div>
<div class="field">
<%= f.label :position %><br />
<%= f.number_field :position %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View file

@ -0,0 +1,6 @@
<h1>Editing reward</h1>
<%= render 'form' %>
<%= link_to 'Show', @reward %> |
<%= link_to 'Back', rewards_path %>

View file

@ -0,0 +1,29 @@
<h1>Listing rewards</h1>
<table>
<tr>
<th>Name</th>
<th>Value</th>
<th>Position</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @rewards.each do |reward| %>
<tr>
<td><%= reward.name %></td>
<td><%= reward.value %></td>
<td><%= reward.position %></td>
<td><%= reward.user_id %></td>
<td><%= link_to 'Show', reward %></td>
<td><%= link_to 'Edit', edit_reward_path(reward) %></td>
<td><%= link_to 'Destroy', reward, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Reward', new_reward_path %>

View file

@ -0,0 +1,5 @@
<h1>New reward</h1>
<%= render 'form' %>
<%= link_to 'Back', rewards_path %>

View file

@ -0,0 +1,25 @@
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @reward.name %>
</p>
<p>
<b>Value:</b>
<%= @reward.value %>
</p>
<p>
<b>Position:</b>
<%= @reward.position %>
</p>
<p>
<b>User:</b>
<%= @reward.user_id %>
</p>
<%= link_to 'Edit', edit_reward_path(@reward) %> |
<%= link_to 'Back', rewards_path %>

View file

@ -1,4 +1,6 @@
HabitTracker::Application.routes.draw do
resources :rewards
resources :habits do
post :sort, on: :collection
get :completed, on: :collection

View file

@ -0,0 +1,12 @@
class CreateRewards < ActiveRecord::Migration
def change
create_table :rewards do |t|
t.string :name
t.integer :value, :default=>1
t.integer :position, :default=>0
t.references :user
t.timestamps
end
end
end

13
test/fixtures/rewards.yml vendored Normal file
View file

@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
name: MyString
value: 1
position: 1
user_id: 1
two:
name: MyString
value: 1
position: 1
user_id: 1

View file

@ -0,0 +1,49 @@
require 'test_helper'
class RewardsControllerTest < ActionController::TestCase
setup do
@reward = rewards(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:rewards)
end
test "should get new" do
get :new
assert_response :success
end
test "should create reward" do
assert_difference('Reward.count') do
post :create, reward: @reward.attributes
end
assert_redirected_to reward_path(assigns(:reward))
end
test "should show reward" do
get :show, id: @reward
assert_response :success
end
test "should get edit" do
get :edit, id: @reward
assert_response :success
end
test "should update reward" do
put :update, id: @reward, reward: @reward.attributes
assert_redirected_to reward_path(assigns(:reward))
end
test "should destroy reward" do
assert_difference('Reward.count', -1) do
delete :destroy, id: @reward
end
assert_redirected_to rewards_path
end
end

View file

@ -0,0 +1,4 @@
require 'test_helper'
class RewardsHelperTest < ActionView::TestCase
end

7
test/unit/reward_test.rb Normal file
View file

@ -0,0 +1,7 @@
require 'test_helper'
class RewardTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end