mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-04-14 19:47:03 +00:00
Adding rewards scaffold
This commit is contained in:
parent
3abcf468e9
commit
50f713e7e9
17 changed files with 280 additions and 0 deletions
3
app/assets/javascripts/rewards.js.coffee
Normal file
3
app/assets/javascripts/rewards.js.coffee
Normal 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/
|
||||
3
app/assets/stylesheets/rewards.css.scss
Normal file
3
app/assets/stylesheets/rewards.css.scss
Normal 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/
|
||||
83
app/controllers/rewards_controller.rb
Normal file
83
app/controllers/rewards_controller.rb
Normal 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
|
||||
2
app/helpers/rewards_helper.rb
Normal file
2
app/helpers/rewards_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
module RewardsHelper
|
||||
end
|
||||
3
app/models/reward.rb
Normal file
3
app/models/reward.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Reward < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
|
|||
33
app/views/rewards/_form.html.erb
Normal file
33
app/views/rewards/_form.html.erb
Normal 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 %>
|
||||
6
app/views/rewards/edit.html.erb
Normal file
6
app/views/rewards/edit.html.erb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<h1>Editing reward</h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Show', @reward %> |
|
||||
<%= link_to 'Back', rewards_path %>
|
||||
29
app/views/rewards/index.html.erb
Normal file
29
app/views/rewards/index.html.erb
Normal 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 %>
|
||||
5
app/views/rewards/new.html.erb
Normal file
5
app/views/rewards/new.html.erb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<h1>New reward</h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
<%= link_to 'Back', rewards_path %>
|
||||
25
app/views/rewards/show.html.erb
Normal file
25
app/views/rewards/show.html.erb
Normal 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 %>
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
HabitTracker::Application.routes.draw do
|
||||
resources :rewards
|
||||
|
||||
resources :habits do
|
||||
post :sort, on: :collection
|
||||
get :completed, on: :collection
|
||||
|
|
|
|||
12
db/migrate/20120204214727_create_rewards.rb
Normal file
12
db/migrate/20120204214727_create_rewards.rb
Normal 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
13
test/fixtures/rewards.yml
vendored
Normal 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
|
||||
49
test/functional/rewards_controller_test.rb
Normal file
49
test/functional/rewards_controller_test.rb
Normal 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
|
||||
4
test/unit/helpers/rewards_helper_test.rb
Normal file
4
test/unit/helpers/rewards_helper_test.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class RewardsHelperTest < ActionView::TestCase
|
||||
end
|
||||
7
test/unit/reward_test.rb
Normal file
7
test/unit/reward_test.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class RewardTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
Loading…
Reference in a new issue