From 6e07f0e31630e56acd18d09b5487c64123726a50 Mon Sep 17 00:00:00 2001 From: Taekmin Kim Date: Wed, 14 Sep 2016 15:16:26 +0900 Subject: [PATCH 1/5] API: courses, posts, photos, comments, tags --- Gemfile | 2 + Gemfile.lock | 10 ++ app/assets/javascripts/posts.coffee | 3 + app/assets/stylesheets/posts.scss | 3 + app/controllers/application_controller.rb | 4 +- app/controllers/comments_controller.rb | 64 ++++++++++ app/controllers/courses_controller.rb | 86 ++++++++++++- app/controllers/photos_controller.rb | 114 +++++++++++++++--- app/controllers/posts_controller.rb | 82 +++++++++++++ app/controllers/users_controller.rb | 4 +- app/controllers/welcome_controller.rb | 14 ++- app/helpers/posts_helper.rb | 2 + app/models/admin.rb | 19 +++ app/models/comment.rb | 12 ++ app/models/course.rb | 41 +++++++ app/models/course_user.rb | 11 ++ app/models/photo.rb | 12 ++ app/models/post.rb | 13 ++ app/models/tagged_user.rb | 15 +++ app/models/user.rb | 38 +++++- app/views/welcome/index.html.erb | 2 +- config/environments/development.rb | 2 + config/environments/production.rb | 2 + .../simple_token_authentication.rb | 2 + config/key.yml.example | 1 + config/routes.rb | 34 ++++-- .../20160910140016_create_course_users.rb | 3 +- .../20160913120422_add_user_id_to_posts.rb | 6 + ...21942_add_authentication_token_to_users.rb | 6 + .../20160914052903_add_user_id_to_photos.rb | 6 + db/schema.rb | 18 ++- test/controllers/posts_controller_test.rb | 7 ++ test/fixtures/admins.yml | 19 +++ test/fixtures/comments.yml | 12 ++ test/fixtures/course_users.yml | 11 ++ test/fixtures/courses.yml | 12 ++ test/fixtures/photos.yml | 12 ++ test/fixtures/posts.yml | 12 ++ test/fixtures/tagged_users.yml | 15 +++ test/fixtures/users.yml | 22 ++++ test/models/admin_test.rb | 19 +++ test/models/comment_test.rb | 12 ++ test/models/course_test.rb | 12 ++ test/models/course_user_test.rb | 11 ++ test/models/photo_test.rb | 12 ++ test/models/post_test.rb | 12 ++ test/models/tagged_user_test.rb | 15 +++ test/models/user_test.rb | 22 ++++ 48 files changed, 823 insertions(+), 45 deletions(-) create mode 100644 app/assets/javascripts/posts.coffee create mode 100644 app/assets/stylesheets/posts.scss create mode 100644 app/controllers/posts_controller.rb create mode 100644 app/helpers/posts_helper.rb create mode 100644 config/initializers/simple_token_authentication.rb create mode 100644 db/migrate/20160913120422_add_user_id_to_posts.rb create mode 100644 db/migrate/20160913121942_add_authentication_token_to_users.rb create mode 100644 db/migrate/20160914052903_add_user_id_to_photos.rb create mode 100644 test/controllers/posts_controller_test.rb diff --git a/Gemfile b/Gemfile index 92c8da2..6b83616 100644 --- a/Gemfile +++ b/Gemfile @@ -59,3 +59,5 @@ gem 'rails_admin', '>= 1.0.0.rc' gem 'annotate' gem 'omniauth-facebook' gem 'httparty' +gem 'simple_token_authentication', '~> 1.0' +gem 'bitly' diff --git a/Gemfile.lock b/Gemfile.lock index 306c695..26a9144 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -49,6 +49,10 @@ GEM rake (>= 10.4, < 12.0) arel (7.1.1) bcrypt (3.1.11) + bitly (0.10.4) + httparty (>= 0.7.6) + multi_json (~> 1.3) + oauth2 (>= 0.5.0, < 2.0) builder (3.2.2) byebug (9.0.5) carrierwave (1.0.0.beta) @@ -192,6 +196,10 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + simple_token_authentication (1.14.0) + actionmailer (>= 3.2.6, < 6) + actionpack (>= 3.2.6, < 6) + devise (>= 3.2, < 6) spring (1.7.2) spring-watcher-listen (2.0.0) listen (>= 2.7, < 4.0) @@ -227,6 +235,7 @@ PLATFORMS DEPENDENCIES annotate + bitly byebug carrierwave (>= 1.0.0.beta, < 2.0) coffee-rails (~> 4.2) @@ -242,6 +251,7 @@ DEPENDENCIES rails_admin (>= 1.0.0.rc) remotipart! sass-rails (~> 5.0) + simple_token_authentication (~> 1.0) spring spring-watcher-listen (~> 2.0.0) sqlite3 diff --git a/app/assets/javascripts/posts.coffee b/app/assets/javascripts/posts.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/posts.coffee @@ -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://coffeescript.org/ diff --git a/app/assets/stylesheets/posts.scss b/app/assets/stylesheets/posts.scss new file mode 100644 index 0000000..1a7e153 --- /dev/null +++ b/app/assets/stylesheets/posts.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the posts controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b6f240a..fc2662d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ class ApplicationController < ActionController::Base - #protect_from_forgery with: :exception + #protect_from_forgery with: :exception + + acts_as_token_authentication_handler_for User end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7669955..78209ac 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,2 +1,66 @@ class CommentsController < ApplicationController + # TODO pagination + def index + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + comments = post.comments + + render :json => { + comments: comments + } + else + render :json => { + comments: [] + }, :status => 401 + end + end + + def create + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + comment = Comment.new(comment_params) + comment.user = current_user + comment.course = post.course + comment.save + + render :json => { + comment: comment + } + else + render :json => { + comment: {} + }, :status => 401 + end + end + + def update + comment = Comment.find(params[:id]) + + if comment.user_id == current_user.id + comment.update(comment_params) + + render :json => { + comment: comment + } + else + render :json => {}, :status => 401 + end + end + + def delete + comment = Comment.find(params[:id]) + + if comment.user_id == current_user.id + comment.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def comment_params + params.require(:comment).permit(:content) + end end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 4e26c99..97899d8 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1,6 +1,86 @@ class CoursesController < ApplicationController + # TODO pagintation def index - @course = Course.all - render :json => {course: @course} + courses = Course.all + my_courses = current_user.courses + + render :json => { + courses: courses, + my_courses: my_courses + } end -end \ No newline at end of file + + def create + course = Course.new(course_params) + course.manager = current_user + course.save + + course.generate_short_link + course.join(current_user) + + render :json => { + course: course + } + end + + def join + course = Course.find(params[:id]) + course.join(current_user) + + render :json => { + course: course + } + end + + def leave + course = Course.find(params[:id]) + course.leave(current_user) + + render :json => { + } + end + + def show + course = Course.find(params[:id]) + + if user.enrolled?(course.id) + render :json => { + course: course + } + else + render :json => { + course: {} + }, :status => 401 + end + end + + def update + course = Course.find(params[:id]) + + if course.manager_id == current_user.id + course.update(course_params) + + render :json => { + course: course + } + else + render :json => {}, :status => 401 + end + end + + def delete + course = Course.find(params[:id]) + + if course.manager_id == current_user.id + # TODO need to add condition + course.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def course_params + params.permit(:course).require(:name) + end +end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index acd37a4..e1f60ad 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -1,25 +1,111 @@ class PhotosController < ApplicationController - def index - render :json => {photos: []} + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + photos = post.photos + + render :json => { + photos: photos + } + else + render :json => { + photos: [] + }, :status => 401 + end end def create - course = Course.first - post = course.posts.first - - photo = Photo.new(photo_params) - photo.post = post - - if photo.save - render :json => {photo: photo} - else - logger.info photo.errors.to_s - render :json => {photo: nil} - end + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + photo = Photo.new(photo_params) + photo.user = current_user + photo.post = post + photo.save + + render :json => { + photo: photo + } + else + render :json => { + photo: {} + }, :status => 401 + end end + def update + photo = Photo.find(params[:id]) + + if photo.user_id == current_user.id + photo.update(photo_params) + + render :json => { + photo: photo + } + else + render :json => {}, :status => 401 + end + end + + def delete + photo = Photo.find(params[:id]) + + if photo.user_id == current_user.id + photo.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def add_tag + course = Course.find(params[:course_id]) + + # TODO remove duplicated tag on post_id, user_id + if current_user.enrolled?(course.id) + tag = TaggedUser.new(tag_params) + tag.user = current_user + tag.photo_id = params[:photo_id] + tag.save + + render :json => { + tag: tag + } + else + render :json => {}, :status => 401 + end + end + + def edit_tag + course = Course.find(params[:course_id]) + tag = TaggedUser.find(params[:id]) + + if tag.user_id == current_user.id || course.manager_id == current_user.id + tag.update(tag_params) + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def remove_tag + course = Course.find(params[:course_id]) + tag = TaggedUser.find(params[:id]) + + if tag.user_id == current_user.id || course.manager_id == current_user.id + tag.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + def photo_params params.require(:photo).permit(:attachment) end + + def tag_params + params.require(:tag).permit(:x, :y, :width, :height) + end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..e64e8a3 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,82 @@ +class PostsController < ApplicationController + # TODO pagination + def index + course = Course.find(params[:course_id]) + + if current_user.enrolled?(course.id) + posts = course.posts + + render :json => { + posts: posts + } + else + render :json => { + posts: [] + }, :status => 401 + end + end + + def create + course = Course.find(params[:course_id]) + + if current_user.enrolled?(course.id) + post = Post.new(post_params) + post.user = current_user + post.course = course + post.save + + render :json => { + post: post + } + else + render :json => { + post: {} + }, :status => 401 + end + end + + def show + course = Course.find(params[:course_id]) + + if current_user.enrolled?(course.id) + post = Post.find(params[:id]) + + render :json => { + post: post + } + else + render :json => { + post: {} + }, :status => 401 + end + end + + def update + post = Post.find(params[:id]) + + if post.user_id == current_user.id + post.update(post_params) + + render :json => { + post: post + } + else + render :json => {}, :status => 401 + end + end + + def delete + post = Post.find(params[:id]) + + if post.user_id == current_user.id + post.delete + render :json => {} + else + render :json => {}, :status => 401 + end + end + + def post_params + params.require(:post).permit(:content) + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4669f33..1fd8a1c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,6 @@ class UsersController < ApplicationController + acts_as_token_authentication_handler_for User, except: [:create] + def create token = params[:token] @@ -6,8 +8,6 @@ def create access_token: token }).parsed_response - logger.info facebook_data - # You need to implement the method below in your model (e.g. app/models/user.rb) @user = User.find_for_facebook_oauth(facebook_data) diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 47fd9b5..e714bc9 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,2 +1,14 @@ class WelcomeController < ApplicationController -end \ No newline at end of file + def index + @msg = "Hello DeepCheck!" + + respond_to do |format| + format.html + format.json { + render :json => { + msg: @msg + }, :status => 403 + } + end + end +end diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb new file mode 100644 index 0000000..a7b8cec --- /dev/null +++ b/app/helpers/posts_helper.rb @@ -0,0 +1,2 @@ +module PostsHelper +end diff --git a/app/models/admin.rb b/app/models/admin.rb index 4cae570..6d53dce 100644 --- a/app/models/admin.rb +++ b/app/models/admin.rb @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: admins +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# + class Admin < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable diff --git a/app/models/comment.rb b/app/models/comment.rb index 4a019df..095dd62 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: comments +# +# id :integer not null, primary key +# post_id :integer +# user_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# + class Comment < ApplicationRecord belongs_to :post belongs_to :user diff --git a/app/models/course.rb b/app/models/course.rb index c636e12..26c4f82 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,6 +1,47 @@ +# == Schema Information +# +# Table name: courses +# +# id :integer not null, primary key +# name :string +# short_link :string +# manager_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + class Course < ApplicationRecord has_many :posts has_many :course_users has_many :users, :through => :course_users belongs_to :manager, :class_name => "User", :foreign_key => :manager_id + + after_create :generate_short_link + + def join(user) + cu = CourseUser.where(:course_id => self.id, :user_id => user.id) + if cu.nil? + cu = Course.new + cu.user = user + cu.course = self + cu.save + end + cu + end + + def leave(user) + cu = CourseUser.where(:course_id => self.id, :user_id => user.id) + cu.delete if !cu.nil? + end + + private + def generate_short_link + link = ios_link_course_url(self) + bitly = Bitly.new("deepcheck", KEYS['bitly']) + + shorten = bitly.shorten(link) + + self.short_link = shorten.short_url + self.save + end end diff --git a/app/models/course_user.rb b/app/models/course_user.rb index 078e6e7..84bcbbb 100644 --- a/app/models/course_user.rb +++ b/app/models/course_user.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: course_users +# +# id :integer not null, primary key +# course_id :integer +# user_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + class CourseUser < ApplicationRecord belongs_to :course belongs_to :user diff --git a/app/models/photo.rb b/app/models/photo.rb index 30aa9b0..12d0035 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: photos +# +# id :integer not null, primary key +# post_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# attachment :string +# user_id :integer +# + class Photo < ApplicationRecord mount_uploader :attachment, AttachmentUploader diff --git a/app/models/post.rb b/app/models/post.rb index dc86b4d..cf154be 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,17 @@ +# == Schema Information +# +# Table name: posts +# +# id :integer not null, primary key +# course_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# user_id :integer +# + class Post < ApplicationRecord + belongs_to :user belongs_to :course has_many :photo has_many :comments diff --git a/app/models/tagged_user.rb b/app/models/tagged_user.rb index b052ea7..af347da 100644 --- a/app/models/tagged_user.rb +++ b/app/models/tagged_user.rb @@ -1,3 +1,18 @@ +# == Schema Information +# +# Table name: tagged_users +# +# id :integer not null, primary key +# photo_id :integer +# user_id :integer +# x :integer +# y :integer +# width :integer +# height :integer +# created_at :datetime not null +# updated_at :datetime not null +# + class TaggedUser < ApplicationRecord belongs_to :photo belongs_to :user diff --git a/app/models/user.rb b/app/models/user.rb index 1948111..b40d5e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,31 @@ +# == Schema Information +# +# Table name: users +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# provider :string +# uid :string +# authentication_token :string(30) +# + class User < ApplicationRecord + acts_as_token_authenticatable # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable - #devise :database_authenticatable, :registerable, - # :recoverable, :rememberable, :trackable, :validatable - - devise :omniauthable, :omniauth_providers => [:facebook] + devise :database_authenticatable,# :registerable, + :recoverable, :rememberable, :trackable, :validatable has_many :comments has_many :posts @@ -12,7 +33,12 @@ class User < ApplicationRecord has_many :photos, :through => :tagged_users has_many :course_users has_many :courses, :through => :course_users - has_many :managed_courses, :class_name => "Course", :inverse_of => :manager + has_many :managed_courses, :class_name => "Course", :foreign_key => :manager + + def enrolled?(course_id) + cu = CourseUser.where(:course_id => course_id, :user_id => self.id).first + !cu.nil? + end def self.find_for_facebook_oauth(data, provider = "facebook") uid = data['id'] @@ -21,7 +47,7 @@ def self.find_for_facebook_oauth(data, provider = "facebook") else user = User.new(:provider => "facebook", :uid => uid) user.email = "#{uid}@facebook.com" - #user.password = Digest::MD5.hexdigest(uid) + user.password = Digest::MD5.hexdigest(uid) user.save user end diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb index 99d696f..1a7767e 100644 --- a/app/views/welcome/index.html.erb +++ b/app/views/welcome/index.html.erb @@ -1 +1 @@ -

Hello DeepCheck!

\ No newline at end of file +

<%= @msg %>

diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f71970..6e76336 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -51,4 +51,6 @@ # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker + + Rails.application.routes.default_url_options[:host] = 'localhost:3000' end diff --git a/config/environments/production.rb b/config/environments/production.rb index ee5d650..2224404 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -83,4 +83,6 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + Rails.application.routes.default_url_options[:host] = 'deepcheck.herokuapp.com' end diff --git a/config/initializers/simple_token_authentication.rb b/config/initializers/simple_token_authentication.rb new file mode 100644 index 0000000..69e51cd --- /dev/null +++ b/config/initializers/simple_token_authentication.rb @@ -0,0 +1,2 @@ +SimpleTokenAuthentication.configure do |config| +end diff --git a/config/key.yml.example b/config/key.yml.example index 4ccd448..ac86cfd 100644 --- a/config/key.yml.example +++ b/config/key.yml.example @@ -1,6 +1,7 @@ defaults: &defaults facebook_app_id: 'a1b2c3' facebook_app_secret: 'a1b2c3' + bitly: 'a1b2c3' development: <<: *defaults diff --git a/config/routes.rb b/config/routes.rb index f1fca74..146a59a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,13 +1,29 @@ Rails.application.routes.draw do - devise_for :admins - mount RailsAdmin::Engine => '/admin', as: 'rails_admin' - #devise_for :users + devise_for :users + devise_scope :user do + authenticated :user do + resources :courses, :only => [:index, :create, :show, :update, :delete] do + post "/join" => "courses#join", :on => :member + delete "/leave" => "courses#leave", :on => :member - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - root "welcome#index" + resources :posts, :only => [:index, :create, :show, :update, :delete] do + resources :photos, :only => [:index, :create, :update, :delete] do + post '/tag' => "photos#add_tag", :on => :member + put '/tag/:tag_id' => "photos#edit_tag", :on => :member + delete '/tag/:tag_id' => "photos#remove_tag", :on => :member + end + resources :comments, :only => [:index, :create, :update, :delete] + end + end + resources :users + end - resources :photos, :only => [:index, :create] - resources :courses, :only => [:index] - resources :users - resources :comments + unauthenticated :user do + root "welcome#index" + resources :users, :only => [:create] + end + end + + devise_for :admins + mount RailsAdmin::Engine => '/admin', as: 'rails_admin' end diff --git a/db/migrate/20160910140016_create_course_users.rb b/db/migrate/20160910140016_create_course_users.rb index 8f29d8a..89d01ad 100644 --- a/db/migrate/20160910140016_create_course_users.rb +++ b/db/migrate/20160910140016_create_course_users.rb @@ -7,7 +7,6 @@ def change t.timestamps end - add_index :course_users, :course_id - add_index :course_users, :user_id + add_index :course_users, [:course_id, :user_id], :unique => true end end diff --git a/db/migrate/20160913120422_add_user_id_to_posts.rb b/db/migrate/20160913120422_add_user_id_to_posts.rb new file mode 100644 index 0000000..346426c --- /dev/null +++ b/db/migrate/20160913120422_add_user_id_to_posts.rb @@ -0,0 +1,6 @@ +class AddUserIdToPosts < ActiveRecord::Migration[5.0] + def change + add_column :posts, :user_id, :integer + add_index :posts, :user_id + end +end diff --git a/db/migrate/20160913121942_add_authentication_token_to_users.rb b/db/migrate/20160913121942_add_authentication_token_to_users.rb new file mode 100644 index 0000000..0a1b45c --- /dev/null +++ b/db/migrate/20160913121942_add_authentication_token_to_users.rb @@ -0,0 +1,6 @@ +class AddAuthenticationTokenToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :authentication_token, :string, limit: 30 + add_index :users, :authentication_token, unique: true + end +end diff --git a/db/migrate/20160914052903_add_user_id_to_photos.rb b/db/migrate/20160914052903_add_user_id_to_photos.rb new file mode 100644 index 0000000..6392495 --- /dev/null +++ b/db/migrate/20160914052903_add_user_id_to_photos.rb @@ -0,0 +1,6 @@ +class AddUserIdToPhotos < ActiveRecord::Migration[5.0] + def change + add_column :photos, :user_id, :integer + add_index :photos, :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index fff3d25..5437843 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160910140016) do +ActiveRecord::Schema.define(version: 20160914052903) do create_table "admins", force: :cascade do |t| t.string "email", default: "", null: false @@ -62,6 +62,8 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "attachment" + t.integer "user_id" + t.index ["user_id"], name: "index_photos_on_user_id" end create_table "posts", force: :cascade do |t| @@ -69,7 +71,9 @@ t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "user_id" t.index ["course_id"], name: "index_posts_on_course_id" + t.index ["user_id"], name: "index_posts_on_user_id" end create_table "tagged_users", force: :cascade do |t| @@ -86,20 +90,22 @@ end create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false - t.string "encrypted_password", default: "", null: false + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "provider" t.string "uid" + t.string "authentication_token", limit: 30 + t.index ["authentication_token"], name: "index_users_on_authentication_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb new file mode 100644 index 0000000..12c4a7f --- /dev/null +++ b/test/controllers/posts_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PostsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/admins.yml b/test/fixtures/admins.yml index 80aed36..4c4dff9 100644 --- a/test/fixtures/admins.yml +++ b/test/fixtures/admins.yml @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: admins +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # This model initially had no columns defined. If you add columns to the diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index 6c4a6ea..48f0f9d 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: comments +# +# id :integer not null, primary key +# post_id :integer +# user_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/course_users.yml b/test/fixtures/course_users.yml index dd9b8da..2867874 100644 --- a/test/fixtures/course_users.yml +++ b/test/fixtures/course_users.yml @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: course_users +# +# id :integer not null, primary key +# course_id :integer +# user_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/courses.yml b/test/fixtures/courses.yml index 80aed36..3007fc8 100644 --- a/test/fixtures/courses.yml +++ b/test/fixtures/courses.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: courses +# +# id :integer not null, primary key +# name :string +# short_link :string +# manager_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # This model initially had no columns defined. If you add columns to the diff --git a/test/fixtures/photos.yml b/test/fixtures/photos.yml index b703e75..845ad3f 100644 --- a/test/fixtures/photos.yml +++ b/test/fixtures/photos.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: photos +# +# id :integer not null, primary key +# post_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# attachment :string +# user_id :integer +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml index b96e1bf..dfbe198 100644 --- a/test/fixtures/posts.yml +++ b/test/fixtures/posts.yml @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: posts +# +# id :integer not null, primary key +# course_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# user_id :integer +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/tagged_users.yml b/test/fixtures/tagged_users.yml index ab5377b..06bd246 100644 --- a/test/fixtures/tagged_users.yml +++ b/test/fixtures/tagged_users.yml @@ -1,3 +1,18 @@ +# == Schema Information +# +# Table name: tagged_users +# +# id :integer not null, primary key +# photo_id :integer +# user_id :integer +# x :integer +# y :integer +# width :integer +# height :integer +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 80aed36..650f27e 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,3 +1,25 @@ +# == Schema Information +# +# Table name: users +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# provider :string +# uid :string +# authentication_token :string(30) +# + # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # This model initially had no columns defined. If you add columns to the diff --git a/test/models/admin_test.rb b/test/models/admin_test.rb index ab20b8c..6fcb76f 100644 --- a/test/models/admin_test.rb +++ b/test/models/admin_test.rb @@ -1,3 +1,22 @@ +# == Schema Information +# +# Table name: admins +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class AdminTest < ActiveSupport::TestCase diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb index b6d6131..d4e34cd 100644 --- a/test/models/comment_test.rb +++ b/test/models/comment_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: comments +# +# id :integer not null, primary key +# post_id :integer +# user_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class CommentTest < ActiveSupport::TestCase diff --git a/test/models/course_test.rb b/test/models/course_test.rb index 4afb5cd..fe00724 100644 --- a/test/models/course_test.rb +++ b/test/models/course_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: courses +# +# id :integer not null, primary key +# name :string +# short_link :string +# manager_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class CourseTest < ActiveSupport::TestCase diff --git a/test/models/course_user_test.rb b/test/models/course_user_test.rb index 09b0ec4..c2041a7 100644 --- a/test/models/course_user_test.rb +++ b/test/models/course_user_test.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: course_users +# +# id :integer not null, primary key +# course_id :integer +# user_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class CourseUserTest < ActiveSupport::TestCase diff --git a/test/models/photo_test.rb b/test/models/photo_test.rb index e2ec03a..c8df96d 100644 --- a/test/models/photo_test.rb +++ b/test/models/photo_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: photos +# +# id :integer not null, primary key +# post_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# attachment :string +# user_id :integer +# + require 'test_helper' class PhotoTest < ActiveSupport::TestCase diff --git a/test/models/post_test.rb b/test/models/post_test.rb index 6d9d463..e111e36 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: posts +# +# id :integer not null, primary key +# course_id :integer +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# user_id :integer +# + require 'test_helper' class PostTest < ActiveSupport::TestCase diff --git a/test/models/tagged_user_test.rb b/test/models/tagged_user_test.rb index 22b7454..21b60a1 100644 --- a/test/models/tagged_user_test.rb +++ b/test/models/tagged_user_test.rb @@ -1,3 +1,18 @@ +# == Schema Information +# +# Table name: tagged_users +# +# id :integer not null, primary key +# photo_id :integer +# user_id :integer +# x :integer +# y :integer +# width :integer +# height :integer +# created_at :datetime not null +# updated_at :datetime not null +# + require 'test_helper' class TaggedUserTest < ActiveSupport::TestCase diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 82f61e0..dcfaa0c 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,3 +1,25 @@ +# == Schema Information +# +# Table name: users +# +# id :integer not null, primary key +# email :string default(""), not null +# encrypted_password :string default(""), not null +# reset_password_token :string +# reset_password_sent_at :datetime +# remember_created_at :datetime +# sign_in_count :integer default(0), not null +# current_sign_in_at :datetime +# last_sign_in_at :datetime +# current_sign_in_ip :string +# last_sign_in_ip :string +# created_at :datetime not null +# updated_at :datetime not null +# provider :string +# uid :string +# authentication_token :string(30) +# + require 'test_helper' class UserTest < ActiveSupport::TestCase From 2edca66a8a3c17cab08d23ea0669726a0a144d56 Mon Sep 17 00:00:00 2001 From: Taekmin Kim Date: Mon, 19 Sep 2016 23:02:35 +0900 Subject: [PATCH 2/5] bug fixed --- app/controllers/courses_controller.rb | 2 +- app/models/course.rb | 6 +++--- config/initializers/filter_parameter_logging.rb | 2 +- config/routes.rb | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 97899d8..848f027 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -43,7 +43,7 @@ def leave def show course = Course.find(params[:id]) - if user.enrolled?(course.id) + if current_user.enrolled?(course.id) render :json => { course: course } diff --git a/app/models/course.rb b/app/models/course.rb index 26c4f82..bcb1b65 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -19,9 +19,9 @@ class Course < ApplicationRecord after_create :generate_short_link def join(user) - cu = CourseUser.where(:course_id => self.id, :user_id => user.id) + cu = CourseUser.where(:course_id => self.id, :user_id => user.id).first if cu.nil? - cu = Course.new + cu = CourseUser.new cu.user = user cu.course = self cu.save @@ -30,7 +30,7 @@ def join(user) end def leave(user) - cu = CourseUser.where(:course_id => self.id, :user_id => user.id) + cu = CourseUser.where(:course_id => self.id, :user_id => user.id).first cu.delete if !cu.nil? end diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb index 4a994e1..6733967 100644 --- a/config/initializers/filter_parameter_logging.rb +++ b/config/initializers/filter_parameter_logging.rb @@ -1,4 +1,4 @@ # Be sure to restart your server when you modify this file. # Configure sensitive parameters which will be filtered from the log file. -Rails.application.config.filter_parameters += [:password] +Rails.application.config.filter_parameters += [:password, :token] diff --git a/config/routes.rb b/config/routes.rb index 146a59a..4695d23 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do devise_for :users devise_scope :user do - authenticated :user do + #authenticated :user do resources :courses, :only => [:index, :create, :show, :update, :delete] do post "/join" => "courses#join", :on => :member delete "/leave" => "courses#leave", :on => :member @@ -16,7 +16,7 @@ end end resources :users - end + #end unauthenticated :user do root "welcome#index" From ada6480c447c68c5700f14e546ed81816e12d442 Mon Sep 17 00:00:00 2001 From: Taekmin Kim Date: Wed, 21 Sep 2016 21:57:41 +0900 Subject: [PATCH 3/5] =?UTF-8?q?mobile=EC=97=90=20webview=EB=9E=91=20?= =?UTF-8?q?=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/welcome_controller.rb | 5 ++++ config/routes.rb | 35 +++++++++++++-------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index e714bc9..a917f1e 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,5 +1,10 @@ class WelcomeController < ApplicationController def index + if user_signed_in? + redirect_to courses_path + return + end + @msg = "Hello DeepCheck!" respond_to do |format| diff --git a/config/routes.rb b/config/routes.rb index 4695d23..eb09728 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,27 +1,26 @@ Rails.application.routes.draw do devise_for :users - devise_scope :user do - #authenticated :user do - resources :courses, :only => [:index, :create, :show, :update, :delete] do - post "/join" => "courses#join", :on => :member - delete "/leave" => "courses#leave", :on => :member + authenticated :user do + root "welcome#index" + resources :courses, :only => [:index, :create, :show, :update, :delete] do + post "/join" => "courses#join", :on => :member + delete "/leave" => "courses#leave", :on => :member - resources :posts, :only => [:index, :create, :show, :update, :delete] do - resources :photos, :only => [:index, :create, :update, :delete] do - post '/tag' => "photos#add_tag", :on => :member - put '/tag/:tag_id' => "photos#edit_tag", :on => :member - delete '/tag/:tag_id' => "photos#remove_tag", :on => :member - end - resources :comments, :only => [:index, :create, :update, :delete] + resources :posts, :only => [:index, :create, :show, :update, :delete] do + resources :photos, :only => [:index, :create, :update, :delete] do + post '/tag' => "photos#add_tag", :on => :member + put '/tag/:tag_id' => "photos#edit_tag", :on => :member + delete '/tag/:tag_id' => "photos#remove_tag", :on => :member end + resources :comments, :only => [:index, :create, :update, :delete] end - resources :users - #end - - unauthenticated :user do - root "welcome#index" - resources :users, :only => [:create] end + resources :users + end + + unauthenticated :user do + root "welcome#index" + resources :users, :only => [:create] end devise_for :admins From c1efbdc21a94983a8eac94078b8a12f6685b7aed Mon Sep 17 00:00:00 2001 From: Taekmin Kim Date: Thu, 22 Sep 2016 13:17:13 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=EA=B0=95=EC=9D=98=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=B0=8F=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + app/assets/stylesheets/application.css | 14 +++++ app/assets/stylesheets/courses.scss | 6 +-- app/controllers/courses_controller.rb | 17 +++++- app/controllers/welcome_controller.rb | 2 + app/models/course.rb | 11 +++- app/views/courses/_form.html.erb | 13 +++++ app/views/courses/edit.html.erb | 3 ++ app/views/courses/index.html.erb | 29 +++++++++++ app/views/courses/join.html.erb | 3 ++ app/views/courses/new.html.erb | 3 ++ app/views/courses/show.html.erb | 1 + app/views/layouts/application.html.erb | 71 ++++++++++++++++++++------ config/routes.rb | 3 +- 14 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 app/views/courses/_form.html.erb create mode 100644 app/views/courses/edit.html.erb create mode 100644 app/views/courses/index.html.erb create mode 100644 app/views/courses/join.html.erb create mode 100644 app/views/courses/new.html.erb diff --git a/.gitignore b/.gitignore index bb6354a..115118e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ *.swp /config/key.yml .rvmrc +.DS_Store diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 0ebd7fe..f984b83 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,17 @@ *= require_tree . *= require_self */ + +body { + min-height: 2000px; + padding-top: 70px; +} + +.row.courses { + text-align: center; +} + +.nopadding { + padding: 0 !important; + margin: 0 !important; +} diff --git a/app/assets/stylesheets/courses.scss b/app/assets/stylesheets/courses.scss index a54ff0f..5c22a36 100644 --- a/app/assets/stylesheets/courses.scss +++ b/app/assets/stylesheets/courses.scss @@ -4,8 +4,8 @@ #course-title { min-height: 50px; - position: fixed; - top: 0; + //position: fixed; + //top: 0; background-color: #f8f8f8; border-color: #e7e7e7; border: 1px solid transparent; @@ -88,4 +88,4 @@ a#btn-new-post { position: fixed; bottom: 0; border-radius: 0; -} \ No newline at end of file +} diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 88aaa31..39819d6 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -5,6 +5,14 @@ def index @my_courses = current_user.courses end + def new + @course = Course.new + end + + def edit + @course = Course.find(params[:id]) + end + def create course = Course.new(course_params) course.manager = current_user @@ -13,7 +21,12 @@ def create course.generate_short_link course.join(current_user) - redirect_to courses_path + redirect_to course_path(course) + end + + def join + @course = Course.find(params[:id]) + render :layout => false end def leave @@ -34,7 +47,7 @@ def update course.update(course_params) end - redirect_to courses_path(course) + redirect_to course_path(course) end def delete diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 8679f06..e4f4bed 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,4 +1,6 @@ class WelcomeController < ApplicationController + acts_as_token_authentication_handler_for User, except: [:index] + def index @msg = "Hello DeepCheck!" diff --git a/app/models/course.rb b/app/models/course.rb index bcb1b65..1f64ce0 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -11,6 +11,7 @@ # class Course < ApplicationRecord + include Rails.application.routes.url_helpers has_many :posts has_many :course_users has_many :users, :through => :course_users @@ -34,9 +35,15 @@ def leave(user) cu.delete if !cu.nil? end - private + def ios_link_course_url + url_for(controller: 'courses', + action: 'join', + id: self.id, + only_path: false) + end + def generate_short_link - link = ios_link_course_url(self) + link = self.ios_link_course_url bitly = Bitly.new("deepcheck", KEYS['bitly']) shorten = bitly.shorten(link) diff --git a/app/views/courses/_form.html.erb b/app/views/courses/_form.html.erb new file mode 100644 index 0000000..742ad39 --- /dev/null +++ b/app/views/courses/_form.html.erb @@ -0,0 +1,13 @@ +<%= form_for(@course, :html => {:class => "form-horizontal"}) do |f| %> +
+ <%= f.label :name, :class => "col-sm-2 control-label" %> +
+ <%= f.text_field :name, :class => "form-control", :placeholder => "Email" %> +
+
+
+
+ <%= f.submit :class => "btn btn-default" %> +
+
+<% end %> diff --git a/app/views/courses/edit.html.erb b/app/views/courses/edit.html.erb new file mode 100644 index 0000000..6ddee45 --- /dev/null +++ b/app/views/courses/edit.html.erb @@ -0,0 +1,3 @@ +

Edit Course

+ +<%= render 'form' %> diff --git a/app/views/courses/index.html.erb b/app/views/courses/index.html.erb new file mode 100644 index 0000000..22b6dbe --- /dev/null +++ b/app/views/courses/index.html.erb @@ -0,0 +1,29 @@ +

My Courses <%= link_to "Create", new_course_path %>

+ +
+ <% @my_courses.each do |course| %> +
+ Generic placeholder image +

<%= course.name %>

+

+ <%= link_to "View details »", course_path(course), :class => "btn btn-default" %> +

+
+ <% end %> +
+ +
+ +

All Courses

+ +
+ <% @courses.each do |course| %> +
+ Generic placeholder image +

<%= course.name %>

+

+ <%= link_to "View details »", course_path(course), :class => "btn btn-default" %> +

+
+ <% end %> +
diff --git a/app/views/courses/join.html.erb b/app/views/courses/join.html.erb new file mode 100644 index 0000000..61ebd62 --- /dev/null +++ b/app/views/courses/join.html.erb @@ -0,0 +1,3 @@ + diff --git a/app/views/courses/new.html.erb b/app/views/courses/new.html.erb new file mode 100644 index 0000000..5211bfb --- /dev/null +++ b/app/views/courses/new.html.erb @@ -0,0 +1,3 @@ +

New Course

+ +<%= render 'form' %> diff --git a/app/views/courses/show.html.erb b/app/views/courses/show.html.erb index 697c697..4ffdf6a 100644 --- a/app/views/courses/show.html.erb +++ b/app/views/courses/show.html.erb @@ -2,6 +2,7 @@

<%= @course.name %> + <%= link_to "Edit", edit_course_path(@course) %>

diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a5cf02f..0d77dd1 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,23 +1,64 @@ - - DeepcheckBackend - <%= csrf_meta_tags %> + + DeepcheckBackend + + <%= csrf_meta_tags %> - <%= stylesheet_link_tag 'application', media: 'all' %> - <%= javascript_include_tag 'application' %> + <%= javascript_include_tag 'application' %> - - + + - - - + + + - - + <%= stylesheet_link_tag 'application', media: 'all' %> + - - <%= yield %> - + + + + +
+ <%= yield %> +
+ diff --git a/config/routes.rb b/config/routes.rb index 232d646..ed87100 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,8 @@ # for webview root "welcome#index" devise_for :users - resources :courses, :only => [:index, :create, :show, :update, :delete] do + resources :courses do + get "/join" => "courses#join", :on => :member resources :posts, :only => [:index, :create, :show, :update, :delete] do resources :photos, :only => [:index, :create, :update, :delete] resources :comments, :only => [:index, :create, :update, :delete] From 4bb3a2941b9dc05483505b873f819b389a98e104 Mon Sep 17 00:00:00 2001 From: Taekmin Kim Date: Sat, 24 Sep 2016 16:20:15 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=ED=83=9C=EA=B7=B8=20=EB=8B=AC=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/stylesheets/courses.scss | 4 ++++ app/models/photo.rb | 1 + app/models/post.rb | 8 ++++++++ app/views/courses/show.html.erb | 11 ++++++++++- db/migrate/20160922042833_add_name_to_users.rb | 5 +++++ db/schema.rb | 3 ++- public/.DS_Store | Bin 6148 -> 0 bytes 7 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20160922042833_add_name_to_users.rb delete mode 100644 public/.DS_Store diff --git a/app/assets/stylesheets/courses.scss b/app/assets/stylesheets/courses.scss index 5c22a36..f45e0d5 100644 --- a/app/assets/stylesheets/courses.scss +++ b/app/assets/stylesheets/courses.scss @@ -89,3 +89,7 @@ a#btn-new-post { bottom: 0; border-radius: 0; } + +.users { + text-align: left; +} diff --git a/app/models/photo.rb b/app/models/photo.rb index 74106e9..59fc43d 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -15,5 +15,6 @@ class Photo < ApplicationRecord belongs_to :post has_many :tagged_users + belongs_to :user has_many :users, :through => :tagged_users end diff --git a/app/models/post.rb b/app/models/post.rb index e20c351..87583fc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -20,4 +20,12 @@ class Post < ApplicationRecord def self.latest self.order(:created_at => :desc) end + + def tagged_users + users = [] + self.photos.each do |p| + users += p.users + end + users.uniq + end end diff --git a/app/views/courses/show.html.erb b/app/views/courses/show.html.erb index 4ffdf6a..a626629 100644 --- a/app/views/courses/show.html.erb +++ b/app/views/courses/show.html.erb @@ -7,7 +7,7 @@ -
+
create
@@ -22,6 +22,15 @@

<%= post.created_at.strftime("%Y-%m-%d") %>

+ +
+ <% post.tagged_users.each do |u| %> + <%= u.name %> + <% end %> + <% (@course.users - post.tagged_users).each do |u| %> + <%= u.name %> + <% end %> +
<% end %>
diff --git a/db/migrate/20160922042833_add_name_to_users.rb b/db/migrate/20160922042833_add_name_to_users.rb new file mode 100644 index 0000000..f3e327b --- /dev/null +++ b/db/migrate/20160922042833_add_name_to_users.rb @@ -0,0 +1,5 @@ +class AddNameToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 5437843..4d5dee9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160914052903) do +ActiveRecord::Schema.define(version: 20160922042833) do create_table "admins", force: :cascade do |t| t.string "email", default: "", null: false @@ -105,6 +105,7 @@ t.string "provider" t.string "uid" t.string "authentication_token", limit: 30 + t.string "name" t.index ["authentication_token"], name: "index_users_on_authentication_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true diff --git a/public/.DS_Store b/public/.DS_Store deleted file mode 100644 index d249b91a42c5ed595abae49475bb0fc798886abb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKOKL(v5Uh?uglt@9IakOH2GJ9Efe1o2q9MVo-zw+wXjXp^<(W+&n@|niHPh2I zk5`M=uL0QNxP1ZU0A_SYe0vz1pSzFjp)y9K^NtO+`21Rb?01Jz_5Fl%A92Jxb~s$~ z_iuUYeVVdTKnh3!DIf);z%Lc>-bMeX zj#+}(JVES*Ln1RYODZv`RwITbo%vREy>LiOI;@5dt0!AcC>BrW{VmF2JyB5#NP$}g zCb^w^|6kF6nE!7{+DQQ^@UIlG>2keX@Rh2!j$Y1tZKJ=^z2-!B<2on|(T<7Hj=Ax6 e{2WDD*L=^)ukQ$fUquD{uzbIu)Y;