Skip to content

Commit

Permalink
Add programmer and designer type
Browse files Browse the repository at this point in the history
  • Loading branch information
hrysd committed Mar 15, 2013
1 parent c1c2ecf commit bfadcc9
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 10 deletions.
1 change: 0 additions & 1 deletion app/assets/stylesheets/application.css.sass
@@ -1,3 +1,2 @@
@import bootstrap
@import font-awesome
@import base
11 changes: 9 additions & 2 deletions app/controllers/practices_controller.rb
Expand Up @@ -3,7 +3,14 @@ class PracticesController < ApplicationController
before_action :set_practice, only: %w(show edit update destroy)

def index
@practices = Practice.all
case current_user.major
when :programmer
@practices = Practice.for_programmer
when :designer
@practices = Practice.for_designer
else
@practices = Practice.all
end
end

def show
Expand Down Expand Up @@ -41,7 +48,7 @@ def destroy

private
def practice_params
params.require(:practice).permit(:title, :description)
params.require(:practice).permit(:title, :description, :aim)
end

def set_practice
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -49,7 +49,8 @@ def user_params
:last_name,
:email,
:password,
:password_cofirmation
:password_cofirmation,
:major
)
end

Expand Down
4 changes: 4 additions & 0 deletions app/decorators/practice_decorator.rb
Expand Up @@ -13,6 +13,10 @@ def learning_status(user_id)
end
end

def for
"for #{self.aim.to_s}"
end

private
def btn_group
{ 'unstarted' => 'btn', 'active' => 'btn btn-info', 'complete' => 'btn btn-success' }
Expand Down
2 changes: 1 addition & 1 deletion app/models/learning.rb
@@ -1,5 +1,5 @@
class Learning < ActiveRecord::Base
as_enum :status, active: 0, complete: 1
as_enum :status, [:active, :complete]
belongs_to :user
belongs_to :practice

Expand Down
4 changes: 4 additions & 0 deletions app/models/practice.rb
@@ -1,6 +1,10 @@
class Practice < ActiveRecord::Base
as_enum :aim, [:everyone, :programmer, :designer]
has_many :learnings

validates :title, presence: true
validates :description, presence: true

scope :for_programmer, ->{ where.not(aim_cd: Practice.designer) }
scope :for_designer, ->{ where.not(aim_cd: Practice.programmer) }
end
16 changes: 16 additions & 0 deletions app/models/user.rb
@@ -1,6 +1,22 @@
class User < ActiveRecord::Base
as_enum :major, [:programmer, :designer]
authenticates_with_sorcery!
has_many :learnings

has_many :completed_learnings,
-> { where(status_cd: 1) },
class_name: 'Learning'
has_many :completed_practices,
through: :completed_learnings,
source: :practice

has_many :active_learnings,
-> { where(status_cd: 0) },
class_name: 'Learning'
has_many :active_practices,
through: :active_learnings,
source: :practice

validates_length_of :password, minimum: 4, if: :password
validates_confirmation_of :password, if: :password
validates :login_name, presence: true, uniqueness: true
Expand Down
1 change: 1 addition & 0 deletions app/views/practices/_form.html.haml
Expand Up @@ -3,4 +3,5 @@
= error_messages_for :practice
= f.input :title, input_html: { class: 'input-block-level' }
= f.input :description, input_html: { class: 'input-block-level', rows: 20 }
= f.select :aim, Practice.aims.keys
= f.submit nil, class: 'btn btn-primary'
1 change: 1 addition & 0 deletions app/views/practices/show.html.haml
@@ -1,4 +1,5 @@
%h1= @practice.title
%p= @practice.for
- if @practice.description?
.practice= simple_format rendering(@practice.description)
= link_to t('edit'), edit_practice_path(@practice), class: 'btn'
Expand Down
1 change: 1 addition & 0 deletions app/views/users/_form.html.haml
Expand Up @@ -7,4 +7,5 @@
= f.input :email, hint: false, placeholder: t('helpers.label.user.email')
= f.input :password, hint: false, placeholder: t('helpers.label.user.password')
= f.input :password_confirmation, hint: false, placeholder: t('helpers.label.user.password_confirmation')
= f.select :major, User.majors.keys
= f.submit nil, class: 'btn btn-large'
1 change: 1 addition & 0 deletions app/views/users/show.html.haml
@@ -1,3 +1,4 @@
.icon= gravatar_tag @user, size: 100
%p= @user.login_name
%p= @user.full_name
%p= @user.major
@@ -0,0 +1,6 @@
class AddColumnsToUsersAndPractices < ActiveRecord::Migration
def change
add_column :practices, :aim_cd, :integer, null: false, default: 0
add_column :users, :major_cd, :integer
end
end
6 changes: 4 additions & 2 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20130313095813) do
ActiveRecord::Schema.define(version: 20130315065106) do

create_table "learnings", force: true do |t|
t.integer "user_id", null: false
Expand All @@ -22,10 +22,11 @@
end

create_table "practices", force: true do |t|
t.string "title", null: false
t.string "title", null: false
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "aim_cd", default: 0, null: false
end

create_table "users", force: true do |t|
Expand All @@ -39,6 +40,7 @@
t.datetime "remember_me_token_expires_at"
t.string "first_name"
t.string "last_name"
t.integer "major_cd"
end

add_index "users", ["remember_me_token"], name: "index_users_on_remember_me_token"
Expand Down
8 changes: 5 additions & 3 deletions db/seeds.rb
Expand Up @@ -11,16 +11,18 @@
last_name: 'Komagata',
email: 'komagata@gmail.com',
password: 'testtest',
password_confirmation: 'testtest'
password_confirmation: 'testtest',
major_cd: 0
)

User.create!(
login_name: 'machida',
first_name: 'Teppei',
last_name: 'machida',
last_name: 'Machida',
email: 'machidanohimitsu@gmail.com',
password: 'testtest',
password_confirmation: 'testtest'
password_confirmation: 'testtest',
major_cd: 1
)

import_fixture 'practices'

0 comments on commit bfadcc9

Please sign in to comment.