From 50f713e7e96ac74a5a08b72e6b44b5060f3e41ca Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sat, 4 Feb 2012 16:49:11 -0500 Subject: [PATCH] Adding rewards scaffold --- app/assets/javascripts/rewards.js.coffee | 3 + app/assets/stylesheets/rewards.css.scss | 3 + app/controllers/rewards_controller.rb | 83 +++++++++++++++++++++ app/helpers/rewards_helper.rb | 2 + app/models/reward.rb | 3 + app/models/user.rb | 1 + app/views/rewards/_form.html.erb | 33 ++++++++ app/views/rewards/edit.html.erb | 6 ++ app/views/rewards/index.html.erb | 29 +++++++ app/views/rewards/new.html.erb | 5 ++ app/views/rewards/show.html.erb | 25 +++++++ config/routes.rb | 2 + db/migrate/20120204214727_create_rewards.rb | 12 +++ test/fixtures/rewards.yml | 13 ++++ test/functional/rewards_controller_test.rb | 49 ++++++++++++ test/unit/helpers/rewards_helper_test.rb | 4 + test/unit/reward_test.rb | 7 ++ 17 files changed, 280 insertions(+) create mode 100644 app/assets/javascripts/rewards.js.coffee create mode 100644 app/assets/stylesheets/rewards.css.scss create mode 100644 app/controllers/rewards_controller.rb create mode 100644 app/helpers/rewards_helper.rb create mode 100644 app/models/reward.rb create mode 100644 app/views/rewards/_form.html.erb create mode 100644 app/views/rewards/edit.html.erb create mode 100644 app/views/rewards/index.html.erb create mode 100644 app/views/rewards/new.html.erb create mode 100644 app/views/rewards/show.html.erb create mode 100644 db/migrate/20120204214727_create_rewards.rb create mode 100644 test/fixtures/rewards.yml create mode 100644 test/functional/rewards_controller_test.rb create mode 100644 test/unit/helpers/rewards_helper_test.rb create mode 100644 test/unit/reward_test.rb diff --git a/app/assets/javascripts/rewards.js.coffee b/app/assets/javascripts/rewards.js.coffee new file mode 100644 index 0000000000..761567942f --- /dev/null +++ b/app/assets/javascripts/rewards.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/ diff --git a/app/assets/stylesheets/rewards.css.scss b/app/assets/stylesheets/rewards.css.scss new file mode 100644 index 0000000000..fb8a5be462 --- /dev/null +++ b/app/assets/stylesheets/rewards.css.scss @@ -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/ diff --git a/app/controllers/rewards_controller.rb b/app/controllers/rewards_controller.rb new file mode 100644 index 0000000000..1a30f9064a --- /dev/null +++ b/app/controllers/rewards_controller.rb @@ -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 diff --git a/app/helpers/rewards_helper.rb b/app/helpers/rewards_helper.rb new file mode 100644 index 0000000000..5d114420ee --- /dev/null +++ b/app/helpers/rewards_helper.rb @@ -0,0 +1,2 @@ +module RewardsHelper +end diff --git a/app/models/reward.rb b/app/models/reward.rb new file mode 100644 index 0000000000..2867bbd747 --- /dev/null +++ b/app/models/reward.rb @@ -0,0 +1,3 @@ +class Reward < ActiveRecord::Base + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 7d096199e5..428674acd9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/rewards/_form.html.erb b/app/views/rewards/_form.html.erb new file mode 100644 index 0000000000..ff60f9283a --- /dev/null +++ b/app/views/rewards/_form.html.erb @@ -0,0 +1,33 @@ +<%= form_for(@reward) do |f| %> + <% if @reward.errors.any? %> +
+

<%= pluralize(@reward.errors.count, "error") %> prohibited this reward from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :value %>
+ <%= f.number_field :value %> +
+
+ <%= f.label :position %>
+ <%= f.number_field :position %> +
+
+ <%= f.label :user_id %>
+ <%= f.number_field :user_id %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/rewards/edit.html.erb b/app/views/rewards/edit.html.erb new file mode 100644 index 0000000000..93e2b4d8bc --- /dev/null +++ b/app/views/rewards/edit.html.erb @@ -0,0 +1,6 @@ +

Editing reward

+ +<%= render 'form' %> + +<%= link_to 'Show', @reward %> | +<%= link_to 'Back', rewards_path %> diff --git a/app/views/rewards/index.html.erb b/app/views/rewards/index.html.erb new file mode 100644 index 0000000000..0128a2f284 --- /dev/null +++ b/app/views/rewards/index.html.erb @@ -0,0 +1,29 @@ +

Listing rewards

+ + + + + + + + + + + + +<% @rewards.each do |reward| %> + + + + + + + + + +<% end %> +
NameValuePositionUser
<%= reward.name %><%= reward.value %><%= reward.position %><%= reward.user_id %><%= link_to 'Show', reward %><%= link_to 'Edit', edit_reward_path(reward) %><%= link_to 'Destroy', reward, confirm: 'Are you sure?', method: :delete %>
+ +
+ +<%= link_to 'New Reward', new_reward_path %> diff --git a/app/views/rewards/new.html.erb b/app/views/rewards/new.html.erb new file mode 100644 index 0000000000..91b7a7a41b --- /dev/null +++ b/app/views/rewards/new.html.erb @@ -0,0 +1,5 @@ +

New reward

+ +<%= render 'form' %> + +<%= link_to 'Back', rewards_path %> diff --git a/app/views/rewards/show.html.erb b/app/views/rewards/show.html.erb new file mode 100644 index 0000000000..d902a11661 --- /dev/null +++ b/app/views/rewards/show.html.erb @@ -0,0 +1,25 @@ +

<%= notice %>

+ +

+ Name: + <%= @reward.name %> +

+ +

+ Value: + <%= @reward.value %> +

+ +

+ Position: + <%= @reward.position %> +

+ +

+ User: + <%= @reward.user_id %> +

+ + +<%= link_to 'Edit', edit_reward_path(@reward) %> | +<%= link_to 'Back', rewards_path %> diff --git a/config/routes.rb b/config/routes.rb index 614ba2b67d..50ad64bb24 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ HabitTracker::Application.routes.draw do + resources :rewards + resources :habits do post :sort, on: :collection get :completed, on: :collection diff --git a/db/migrate/20120204214727_create_rewards.rb b/db/migrate/20120204214727_create_rewards.rb new file mode 100644 index 0000000000..347c8d21b6 --- /dev/null +++ b/db/migrate/20120204214727_create_rewards.rb @@ -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 diff --git a/test/fixtures/rewards.yml b/test/fixtures/rewards.yml new file mode 100644 index 0000000000..4903f921fc --- /dev/null +++ b/test/fixtures/rewards.yml @@ -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 diff --git a/test/functional/rewards_controller_test.rb b/test/functional/rewards_controller_test.rb new file mode 100644 index 0000000000..bb4f9bb79f --- /dev/null +++ b/test/functional/rewards_controller_test.rb @@ -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 diff --git a/test/unit/helpers/rewards_helper_test.rb b/test/unit/helpers/rewards_helper_test.rb new file mode 100644 index 0000000000..d160aaed8c --- /dev/null +++ b/test/unit/helpers/rewards_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class RewardsHelperTest < ActionView::TestCase +end diff --git a/test/unit/reward_test.rb b/test/unit/reward_test.rb new file mode 100644 index 0000000000..5e49f57268 --- /dev/null +++ b/test/unit/reward_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class RewardTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end