Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

# DashboardController
class DashboardController < ApplicationController

def index
@goals = Goal.includes(:goal_progresses).order("goal_progresses.updated_at desc")
end

end
76 changes: 76 additions & 0 deletions app/controllers/goal_progresses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

# GoalProgressesController
class GoalProgressesController < ApplicationController
before_action :set_goal_progress, only: %i[show edit update destroy]

# GET /goal_progresses
# GET /goal_progresses.json
def index
@goal_progresses = GoalProgress.all
end

# GET /goal_progresses/1
# GET /goal_progresses/1.json
def show; end

# GET /goal_progresses/new
def new
@goal_progress = GoalProgress.new
end

# GET /goal_progresses/1/edit
def edit; end

# POST /goal_progresses
# POST /goal_progresses.json
def create
@goal_progress = GoalProgress.new(goal_progress_params)

respond_to do |format|
if @goal_progress.save
format.html { redirect_to @goal_progress, notice: 'Progress was successfully created.' }
format.json { render :show, status: :created, location: @goal_progress }
else
format.html { render :new }
format.json { render json: @goal_progress.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /goal_progresses/1
# PATCH/PUT /goal_progresses/1.json
def update
respond_to do |format|
if @goal_progress.update(goal_progress_params)
format.html { redirect_to @goal_progress, notice: 'Progress was successfully updated.' }
format.json { render :show, status: :ok, location: @goal_progress }
else
format.html { render :edit }
format.json { render json: @goal_progress.errors, status: :unprocessable_entity }
end
end
end

# DELETE /goal_progresses/1
# DELETE /goal_progresses/1.json
def destroy
@goal_progress.destroy
respond_to do |format|
format.html { redirect_to goal_progresses_url, notice: 'Progress was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_goal_progress
@goal_progress = GoalProgress.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def goal_progress_params
params.require(:goal_progress).permit(:goal_id, :achieve, :achieve_at)
end
end
2 changes: 1 addition & 1 deletion app/controllers/goals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ def set_goal

# Never trust parameters from the scary internet, only allow the white list through.
def goal_params
params.require(:goal).permit(:name)
params.require(:goal).permit(:name, :benchmark)
end
end
2 changes: 2 additions & 0 deletions app/models/goal.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

class Goal < ApplicationRecord
validates :name, :benchmark, presence: true
has_many :goal_progresses
end
6 changes: 6 additions & 0 deletions app/models/goal_progress.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class GoalProgress < ApplicationRecord
validates :achieve, :achieve_at, presence: true
belongs_to :goal
end
29 changes: 29 additions & 0 deletions app/views/dashboard/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
h1.title Your personal dashboard

table.table
thead
tr
th Goal
th Monday
th Tuesday
th Wednesday
th Thursday
th Friday

tbody
- @goals.each do |goal|
tr
td = goal.name
- progresses = goal.goal_progresses.where("goal_progresses.created_at >= ?", 1.week.ago)
td = progresses.select { |p| p.created_at.monday? }.last.try(:achieve) || "-"
td = progresses.select { |p| p.created_at.tuesday? }.last.try(:achieve) || "-"
td = progresses.select { |p| p.created_at.wednesday? }.last.try(:achieve) || "-"
td = progresses.select { |p| p.created_at.thursday? }.last.try(:achieve) || "-"
td = progresses.select { |p| p.created_at.friday? }.last.try(:achieve) || "-"
br

ul
li
= link_to 'New Goal', new_goal_path
li
= link_to 'New Progress', new_goal_progress_path
25 changes: 25 additions & 0 deletions app/views/goal_progresses/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
= form_for [@goal_progress] do |f|
= error_messages_for f.object

.field
= f.label :goal, class: 'label'
.control= f.collection_select(:goal_id, Goal.all, :id, :name)

.field
= f.label :achieve_at, class: 'label'
.control= f.date_field :achieve_at, placeholder: f.object.class.human_attribute_name(:achieve_at), class: 'input', required: true

.field
= f.label :achieve, class: 'label'
.control= f.number_field :achieve, placeholder: f.object.class.human_attribute_name(:achieve), class: 'input', required: true

.field.is-grouped
p.control
= submit_tag t('.save'), class: 'button is-primary', data: {disable_with: "Saving..."}
= link_to "Cancel", [:goal_progresses], class: 'button is-text'

p.control.has-text-right
- if f.object.persisted? # and policy(f.object).destroy?
= link_to [f.object], method: :delete, class: 'button is-danger is-inverted', data: {confirm: t('.destroy.title')} do
i.fa.fa-trash
= t('.destroy.button')
7 changes: 7 additions & 0 deletions app/views/goal_progresses/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h1 Editing progress

== render 'form'

=> link_to 'Show', @goal_progress
'|
=< link_to 'Back', goal_progresses_path
25 changes: 25 additions & 0 deletions app/views/goal_progresses/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
h1.title Listing progresses

table.table
thead
tr
th Goal
th Achieve At
th Achieve
th
th
th

tbody
- @goal_progresses.each do |progress|
tr
td = progress.goal.name
td = progress.achieve_at
td = progress.achieve
td = link_to 'Show', progress
td = link_to 'Edit', edit_goal_progress_path(progress)
td = link_to 'Destroy', progress, data: { confirm: 'Are you sure?' }, method: :delete

br

= link_to 'New Progress', new_goal_progress_path
5 changes: 5 additions & 0 deletions app/views/goal_progresses/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
h1 New progress

== render 'form'

= link_to 'Back', goal_progresses_path
16 changes: 16 additions & 0 deletions app/views/goal_progresses/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
p#notice = notice

p
strong Goal:
= @goal_progress.goal.name
p
strong achieve_at:
= @goal_progress.achieve_at

p
strong achieve_at:
= @goal_progress.achieve

=> link_to 'Edit', edit_goal_progress_path(@goal_progress)
'|
=< link_to 'Back', goal_progresses_path
7 changes: 5 additions & 2 deletions app/views/goals/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

.field
= f.label :name, class: 'label'
.control= f.text_field :name, placeholder: f.object.class.human_attribute_name(:name), class: 'input'
.control= f.text_field :name, placeholder: f.object.class.human_attribute_name(:name), class: 'input', required: true

.field
= f.label :benchmark, class: 'label'
.control= f.number_field :benchmark, placeholder: f.object.class.human_attribute_name(:benchmark), class: 'input', required: true

.field.is-grouped
p.control
Expand All @@ -16,4 +20,3 @@
= link_to [f.object], method: :delete, class: 'button is-danger is-inverted', data: {confirm: t('.destroy.title')} do
i.fa.fa-trash
= t('.destroy.button')

2 changes: 1 addition & 1 deletion app/views/goals/_goal.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

json.extract! goal, :id, :name, :created_at, :updated_at
json.extract! goal, :id, :name, :benchmark, :created_at, :updated_at
json.url goal_url(goal, format: :json)
2 changes: 2 additions & 0 deletions app/views/goals/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ table.table
thead
tr
th Name
th Benchmark
th
th
th
Expand All @@ -12,6 +13,7 @@ table.table
- @goals.each do |goal|
tr
td = goal.name
td = goal.benchmark
td = link_to 'Show', goal
td = link_to 'Edit', edit_goal_path(goal)
td = link_to 'Destroy', goal, data: { confirm: 'Are you sure?' }, method: :delete
Expand Down
3 changes: 3 additions & 0 deletions app/views/goals/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ p#notice = notice
p
strong Name:
= @goal.name
p
strong Benchmark:
= @goal.benchmark

=> link_to 'Edit', edit_goal_path(@goal)
'|
Expand Down
5 changes: 2 additions & 3 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ html
.navbar-menu#navMenu
.navbar-end
= link_to "My Goals", goals_path, class: ['navbar-item', 'is-tab', active_if(controller?('goals'))]
.navbar-end
= link_to "My Progress", goal_progresses_path, class: ['navbar-item', 'is-tab', active_if(controller?('goal_progresses'))]

- flash.select{|k,v| %w(notice alert).include?(k)}.each do |type, message|
section.section
Expand All @@ -45,6 +47,3 @@ html
' Booster Stage
' &copy;
= Date.today.year



3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ en:
goals:
form:
save: Save Goal
goal_progresses:
form:
save: Save Progress
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

Rails.application.routes.draw do
resources :goals
root to: 'goals#index'
resources :goal_progresses
root to: 'dashboard#index'
end
5 changes: 5 additions & 0 deletions db/migrate/20190228173758_add_benchmark_to_goals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBenchmarkToGoals < ActiveRecord::Migration[5.2]
def change
add_column :goals, :benchmark, :integer, required: true
end
end
11 changes: 11 additions & 0 deletions db/migrate/20190228180507_create_goal_progress.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateGoalProgress < ActiveRecord::Migration[5.2]
def change
create_table :goal_progresses do |t|
t.references :goal, index: true
t.integer :achieve, null: false
t.date :achieve_at, null: false

t.timestamps
end
end
end
24 changes: 17 additions & 7 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand All @@ -12,10 +10,22 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20_180_815_015_756) do
create_table 'goals', force: :cascade do |t|
t.string 'name'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
ActiveRecord::Schema.define(version: 2019_02_28_180507) do

create_table "goal_progresses", force: :cascade do |t|
t.integer "goal_id"
t.integer "achieve", null: false
t.date "achieve_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["goal_id"], name: "index_goal_progresses_on_goal_id"
end

create_table "goals", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "benchmark"
end

end
Loading