Skip to content

Commit

Permalink
루머를 추가합니다, 사용자는 이름을 입력할 수 있습니다
Browse files Browse the repository at this point in the history
  • Loading branch information
rest515 committed Apr 17, 2020
1 parent cc269c1 commit ea14aa5
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
around_action :switch_locale

def switch_locale(&action)
I18n.with_locale(http_accept_language.compatible_language_from(I18n.available_locales), &action)
end

protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
end
55 changes: 55 additions & 0 deletions app/controllers/rumors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class RumorsController < ApplicationController
before_action :set_rumor, only: [:show, :edit, :update, :destroy]

def index
@rumors = Rumor.all
@rumors = @rumors.search_for(params[:q]) if params[:q].present?
@rumors = @rumors.page(params[:page])
end

def new
@rumor = Rumor.new
end

def create
@rumor = Rumor.new(rumor_params)
@rumor.user = current_user

if @rumor.save
redirect_to @rumor, notice: '저장하였습니다.'
else
render :new
end
end

def show
end

def edit
end

def update
if @rumor.update(rumor_params)
redirect_to @rumor, notice: '수정하였습니다.'
else
render :edit
end
end

def destroy
@rumor.destroy
redirect_to rumors_url
end

private

def set_rumor
@rumor = Rumor.find(params[:id])
end

def rumor_params
params.require(:rumor).permit(
:title, :body,
)
end
end
6 changes: 6 additions & 0 deletions app/models/rumor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Rumor < ApplicationRecord
belongs_to :user

validates :title, presence: true
validates :body, presence: true
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class User < ApplicationRecord
:recoverable, :rememberable, :validatable, :trackable

enum role: { admin: "admin", general: "general" }

validates :name, presence: true, uniqueness: true
end
11 changes: 9 additions & 2 deletions app/views/application/_header.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@
%li.nav-item.mt-4
%small.text-light
%i.far.fa-database
원본 데이터
%li.nav-item= link_to t("menu.data_sets"), data_sets_path, class: "nav-link"
%li.nav-item= link_to t("menu.fact_checks"), '#', onclick: "alert('준비 중입니다');", class: "nav-link"
- if user_signed_in?
%li.nav-item= link_to t("link.new_data_set"), new_data_set_path, class: "nav-link"
%li.nav-item.mt-4
%small.text-light
%i.far.fa-wind
팩트체크
%li.nav-item= link_to t("menu.rumors"), rumors_path, class: "nav-link"
%li.nav-item.mt-4
%small.text-light
%i.far.fa-user
- if user_signed_in?
%li.nav-item
.nav-link= current_user.email
.nav-link
#{current_user.name} (#{current_user.email})
%li.nav-item= link_to t("link.sign_out"), destroy_user_session_path, method: :delete, class: "nav-link"
- else
%li.nav-item= link_to t("link.sign_in"), new_user_session_path, class: "nav-link"
Expand Down
3 changes: 1 addition & 2 deletions app/views/data_sets/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
%h3.pb-2
%h4.font-weight-bold.pb-2
= t('main.title')
- if params[:q].present?
= "'#{params[:q]}'"


.row
- @data_sets.each do |data_set|
.col-12
Expand Down
5 changes: 5 additions & 0 deletions app/views/devise/registrations/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
autofocus: true, |
input_html: { autocomplete: "email" } |
-#
= f.input :name, |
required: true, |
autofocus: true, |
input_html: { autocomplete: "name" } |
-#
= f.input :password, |
required: true, |
hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length), |
Expand Down
5 changes: 5 additions & 0 deletions app/views/rumors/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= simple_form_for @rumor do |f|
= f.input :title
= f.input :body, as: :summernote

= f.submit data: { disable_with: "Please wait..." }, class: 'btn btn-primary'
5 changes: 5 additions & 0 deletions app/views/rumors/_link_fields.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.nested-fields.border.bg-white.p-4.my-4
= f.input :title
= f.input :url
= f.input :body
= link_to_remove_association "항목 삭제", f
6 changes: 6 additions & 0 deletions app/views/rumors/_rumor.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mb-4.card
.card-body
%h5.card-title= link_to rumor.title, rumor, class: "text-dark"

%p.card-text
%small= strip_tags(rumor.body).truncate(150)
3 changes: 3 additions & 0 deletions app/views/rumors/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%h3.pb-2 데이터 보완하기

= render 'form'
16 changes: 16 additions & 0 deletions app/views/rumors/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.d-flex.justify-content-between.pb-2
%h4.font-weight-bold
= t('menu.rumors')
- if params[:q].present?
= "'#{params[:q]}'"
- if user_signed_in?
= link_to t('link.new_rumor'), new_rumor_path


.row
- @rumors.each do |rumor|
.col-12
= render rumor

.d-flex.justify-content-center
= paginate @rumors
3 changes: 3 additions & 0 deletions app/views/rumors/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%h3.pb-2 루머 추가

= render 'form'
31 changes: 31 additions & 0 deletions app/views/rumors/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.mb-2
= link_to t('menu.rumors'), rumors_path, class: "text-dark"

%h3.pb-2= @rumor.title

.py-2.text-muted
%span.mr-2
%i.far.fa-user
= @rumor.user.name
%span.mr-2
%i.far.fa-calendar
= l @rumor.created_at.to_date

.my-4.p-3.border.bg-white
= raw @rumor.body

.my-4
.d-flex.justify-content-between
%div
- if user_signed_in?
= link_to t('link.edit'), edit_rumor_path(@rumor), class: "btn btn-outline-primary"
%div
- if user_signed_in? && (@rumor.user == current_user || current_user.admin?)
= link_to t('link.destroy'), @rumor, method: :delete, data: { confirm: "정말 삭제하시겠습니까?" }, class: "btn btn-outline-danger"

%hr

.my-4
%h5.text-muted= DataSet.human_attribute_name("comments")
.my-4.p-4.border.bg-white
= link_to "루머에 관한 의견이 있으세요?", "https://democracy-activists.parti.xyz/p/data_activists", target: "_blank", class: "text-dark"
11 changes: 9 additions & 2 deletions config/locales/ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ko:
attributes:
user:
email: 이메일
name: 닉네임
password: 비밀번호
password_confirmation: 비밀번호 확인
data_set:
Expand All @@ -21,21 +22,27 @@ ko:
tags: 태그
links: 관련정보
comments: 댓글
rumor:
title: 제목
body: 내용
comments: 댓글
link:
title: 제목
body: 요약
url: URL

site_name: 데이터퍼블릭.kr
menu:
data_sets: 데이터 찾기
data_sets: 데이터
rumors: 루머
fact_checks: 팩트체크
tags: 태그
search: 검색
main:
title: 데이터 찾기
title: 데이터
link:
new_data_set: 데이터 추가
new_rumor: 루머 추가
edit: 수정
destroy: 삭제
sign_in: 로그인
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
devise_for :users

resources :data_sets
resources :rumors
resources :tags

root "data_sets#index"
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20200417040752_create_rumors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateRumors < ActiveRecord::Migration[5.2]
def change
create_table :rumors do |t|
t.string :title
t.text :body
t.belongs_to :user, foreign_key: true

t.timestamps
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20200417042529_get_username_from_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class GetUsernameFromEmail < ActiveRecord::Migration[5.2]
def up
User.all.each do |user|
user.update(name: user.email.split('@').first)
end
end

def down
User.all.each do |user|
user.update(name: nil)
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_02_27_101505) do
ActiveRecord::Schema.define(version: 2020_04_17_042529) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -60,6 +60,15 @@
t.index ["data_set_id"], name: "index_relateds_on_data_set_id"
end

create_table "rumors", force: :cascade do |t|
t.string "title"
t.text "body"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_rumors_on_user_id"
end

create_table "taggings", id: :serial, force: :cascade do |t|
t.integer "tag_id"
t.string "taggable_type"
Expand Down Expand Up @@ -120,5 +129,6 @@
end

add_foreign_key "data_sets", "users"
add_foreign_key "rumors", "users"
add_foreign_key "taggings", "tags"
end
9 changes: 9 additions & 0 deletions test/fixtures/rumors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
body: MyText

two:
title: MyString
body: MyText
7 changes: 7 additions & 0 deletions test/models/rumor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class RumorTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit ea14aa5

Please sign in to comment.