Skip to content

Commit

Permalink
Rename enum column
Browse files Browse the repository at this point in the history
  • Loading branch information
hrysd committed Mar 16, 2013
1 parent 42f0c96 commit ee63102
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 26 deletions.
21 changes: 12 additions & 9 deletions app/controllers/practices_controller.rb
Expand Up @@ -3,14 +3,17 @@ class PracticesController < ApplicationController
before_action :set_practice, only: %w(show edit update destroy)

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

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

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

def set_practice
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -50,7 +50,7 @@ def user_params
:email,
:password,
:password_cofirmation,
:major
:job
)
end

Expand Down
8 changes: 6 additions & 2 deletions app/decorators/practice_decorator.rb
Expand Up @@ -14,12 +14,16 @@ def learning_status(user_id)
end

def for
t('for', aim: t(self.aim))
t('for', target: t(self.target))
end

private
def btn_group
{ 'unstarted' => 'btn', 'active' => 'btn btn-info', 'complete' => 'btn btn-success' }
{
'unstarted' => 'btn',
'active' => 'btn btn-info',
'complete' => 'btn btn-success'
}.freeze
end

def generate_button(status, btn_name, btn_class)
Expand Down
7 changes: 4 additions & 3 deletions app/models/practice.rb
@@ -1,10 +1,11 @@
class Practice < ActiveRecord::Base
include RankedModel
ranks :row_order
as_enum :aim, [:everyone, :programmer, :designer]
as_enum :target, [: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) }

scope :for_programmer, ->{ where.not(target_cd: Practice.designer) }
scope :for_designer, ->{ where.not(target_cd: Practice.programmer) }
end
2 changes: 1 addition & 1 deletion app/models/user.rb
@@ -1,5 +1,5 @@
class User < ActiveRecord::Base
as_enum :major, [:programmer, :designer]
as_enum :job, [:programmer, :designer]
authenticates_with_sorcery!
has_many :learnings

Expand Down
2 changes: 1 addition & 1 deletion app/views/practices/_form.html.haml
Expand Up @@ -3,5 +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.input :aim, collection: Practice.aims_for_select
= f.input :target, collection: Practice.targets_for_select
= f.submit nil, class: 'btn btn-primary'
2 changes: 1 addition & 1 deletion app/views/users/_form.html.haml
Expand Up @@ -7,5 +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.input :major, collection: User.majors_for_select
= f.input :job, collection: User.jobs_for_select
= f.submit nil, class: 'btn btn-large'
2 changes: 1 addition & 1 deletion app/views/users/show.html.haml
@@ -1,4 +1,4 @@
.icon= gravatar_tag @user, size: 100
%p= @user.login_name
%p= @user.full_name
%p= t(@user.major)
%p= t(@user.job)
2 changes: 1 addition & 1 deletion config/locales/ja.yml
Expand Up @@ -337,4 +337,4 @@ ja:
user_was_successfully_updated: "ユーザーを更新しました。"
practice_was_successfully_updated: "ユーザーを更新しました。"
complete: '完了'
for: '%{aim}向け'
for: '%{target} 課題'
@@ -1,6 +1,6 @@
class AddColumnsToUsersAndPractices < ActiveRecord::Migration
def change
add_column :practices, :aim_cd, :integer, null: false, default: 0
add_column :users, :major_cd, :integer
add_column :practices, :target_cd, :integer, null: false, default: 0
add_column :users, :job_cd, :integer
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Expand Up @@ -26,7 +26,7 @@
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "aim_cd", default: 0, null: false
t.integer "target_cd", default: 0, null: false
t.integer "row_order"
end

Expand All @@ -41,7 +41,7 @@
t.datetime "remember_me_token_expires_at"
t.string "first_name"
t.string "last_name"
t.integer "major_cd"
t.integer "job_cd"
end

add_index "users", ["remember_me_token"], name: "index_users_on_remember_me_token"
Expand Down
4 changes: 2 additions & 2 deletions db/seeds.rb
Expand Up @@ -12,7 +12,7 @@
email: 'komagata@gmail.com',
password: 'testtest',
password_confirmation: 'testtest',
major_cd: 0
job_cd: 0
)

User.create!(
Expand All @@ -22,7 +22,7 @@
email: 'machidanohimitsu@gmail.com',
password: 'testtest',
password_confirmation: 'testtest',
major_cd: 1
job_cd: 1
)

import_fixture 'practices'

0 comments on commit ee63102

Please sign in to comment.