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/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/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..f45e0d5 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,8 @@ a#btn-new-post { position: fixed; bottom: 0; border-radius: 0; -} \ No newline at end of file +} + +.users { + text-align: left; +} diff --git a/app/assets/stylesheets/posts.scss b/app/assets/stylesheets/posts.scss index 4507eee..32c7698 100644 --- a/app/assets/stylesheets/posts.scss +++ b/app/assets/stylesheets/posts.scss @@ -1,7 +1,6 @@ // 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/ - #post-title { min-height: 50px; position: fixed; @@ -31,4 +30,4 @@ max-width: 100%; height: auto; } -} \ No newline at end of file +} diff --git a/app/controllers/api/comments_controller.rb b/app/controllers/api/comments_controller.rb new file mode 100644 index 0000000..c71e91e --- /dev/null +++ b/app/controllers/api/comments_controller.rb @@ -0,0 +1,66 @@ +class Api::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/api/courses_controller.rb b/app/controllers/api/courses_controller.rb new file mode 100644 index 0000000..a250afc --- /dev/null +++ b/app/controllers/api/courses_controller.rb @@ -0,0 +1,94 @@ +class Api::CoursesController < ApplicationController + # TODO pagintation + def index + courses = Course.all + my_courses = current_user.courses + + render :json => { + courses: courses, + my_courses: my_courses + } + end + + 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 + respond_to do |format| + format.html { + @course = Course.find(params[:id]) + } + format.json { + course = Course.find(params[:id]) + + if current_user.enrolled?(course.id) + render :json => { + course: course + } + else + render :json => { + course: {} + }, :status => 401 + end + } + 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.require(:course).permit(:name) + params.require(:course).permit(:name, :attachment, :date) + end +end diff --git a/app/controllers/api/photos_controller.rb b/app/controllers/api/photos_controller.rb new file mode 100644 index 0000000..c44c7c8 --- /dev/null +++ b/app/controllers/api/photos_controller.rb @@ -0,0 +1,127 @@ +class Api::PhotosController < ApplicationController + def index + 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 + 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 show + photo = Photo.find(params[:id]) + + if current_user.enrolled?(params[:course_id]) + render :json => { + photo: photo.attachment, + date: photo.created_at + } + else + render :json => { + photo: {}, + date: nil + }, :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, :post_id) + end + + def tag_params + params.require(:tag).permit(:x, :y, :width, :height) + end +end diff --git a/app/controllers/api/posts_controller.rb b/app/controllers/api/posts_controller.rb new file mode 100644 index 0000000..e2abe84 --- /dev/null +++ b/app/controllers/api/posts_controller.rb @@ -0,0 +1,105 @@ +class Api::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 + + respond_to do |format| + format.html { + photo = Photo.new(photo_params) + photo.user = current_user + photo.save + + redirect_to(:back) + } + format.json { + render :json => { + post: post + } + } + end + 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]) + + respond_to do |format| + format.html { + @course_name = course.name + @post = course.posts.find(params[:id]) + } + format.json { + render :json => { + post: post + } + } + end + 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 + + def photo_params + params.require(:post).permit(:attachment, :post_id) + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/api/users_controller.rb similarity index 77% rename from app/controllers/users_controller.rb rename to app/controllers/api/users_controller.rb index 4669f33..5f43f6b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/api/users_controller.rb @@ -1,4 +1,6 @@ -class UsersController < ApplicationController +class Api::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/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 485a184..39819d6 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1,22 +1,67 @@ class CoursesController < ApplicationController + # TODO pagintation def index - @course = Course.all - render :json => {courses: @course} + @courses = Course.all + @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 + course.save + + course.generate_short_link + course.join(current_user) + + redirect_to course_path(course) + end + + def join + @course = Course.find(params[:id]) + render :layout => false + end + + def leave + course = Course.find(params[:id]) + course.leave(current_user) + + redirect_to courses_path + end + def show - @course = Course.find(params[:id]) - render :layout => true + @course = Course.find(params[:id]) end def update - @course = Course.find(params[:id]) - @posts = @course.posts - @photos = @posts.first - redirect_to(:back) - end + course = Course.find(params[:id]) - def course_params - params.require(:course).permit(:name, :attachment, :date, :short_link, :manager_id) + if course.manager_id == current_user.id + course.update(course_params) + end + + redirect_to course_path(course) end -end \ No newline at end of file + + def delete + course = Course.find(params[:id]) + + if course.manager_id == current_user.id + # TODO need to add condition + course.delete + end + + redirect_to courses_path + end + + def course_params + params.require(:course).permit(:name, :attachment, :date) + end +end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 4c906d9..b81ac36 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -1,25 +1,126 @@ class PhotosController < ApplicationController - def index - @course = Course.find(params[:course_id]) - post = @course.posts.find(params[:post_id]) - photos = post.photos - render :json => {photos: photos} + post = Post.find(params[:post_id]) + + if current_user.enrolled?(post.course_id) + @photos = post.photos + end end + + # TODO + def create + post = Post.find(params[:post_id]) - def show - @course = Course.find(params[:course_id]) - post = @course.posts.find(params[:post_id]) - photo = post.photos.find(params[: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.attachment, date: photo.created_at} + render :json => { + photo: photo + } + else + render :json => { + photo: {} + }, :status => 401 + end end - def create - render :json => {photo: nil} + # TODO + def show + photo = Photo.find(params[:id]) + + if current_user.enrolled?(params[:course_id]) + render :json => { + photo: photo.attachment, + date: photo.created_at + } + else + render :json => { + photo: {}, + date: nil + }, :status => 401 + end end + # TODO + 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 + + # TODO + 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 + + # TODO + 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 + + # TODO + 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 + + # TODO + 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, :post_id) 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 index a7c1c84..2112ee0 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,39 +1,80 @@ class PostsController < ApplicationController - def index - @course = Course.find(params[:course_id]) - posts = @course.posts - render :json => {posts: posts} - end + # TODO pagination + # TODO + def index + course = Course.find(params[:course_id]) - def show - course = Course.find(params[:course_id]) - @course_name = course.name - @post = course.posts.find(params[:id]) - render :layout => true - end - - def create - @course = Course.find(params[:course_id]) - post = @course.posts.create(post_params) - photo = post.photos.create(photo_params) - - if post.save - redirect_to(:back) - else - logger.info photo.errors.to_s - render :json => {post: nil} - end - end + if current_user.enrolled?(course.id) + posts = course.posts - def photo_params - params.require(:post).permit(:attachment, :post_id) - end + render :json => { + posts: posts + } + else + render :json => { + posts: [] + }, :status => 401 + end + end - def post_params - params.require(:post).permit(:course_id) - 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 + + photo = Photo.new(photo_params) + photo.user = current_user + photo.save + end + + redirect_to(:back) + end + + def show + course = Course.find(params[:course_id]) - def course_params - params.require(:post).permit(:id) + if current_user.enrolled?(course.id) + @course_name = course.name + @post = course.posts.find(params[:id]) + end + end + + # TODO + 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 + + # TODO + 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 + + def photo_params + params.require(:post).permit(:attachment, :post_id) end end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 47fd9b5..e4f4bed 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,2 +1,9 @@ class WelcomeController < ApplicationController -end \ No newline at end of file + acts_as_token_authentication_handler_for User, except: [:index] + + def index + @msg = "Hello DeepCheck!" + + redirect_to courses_path if user_signed_in? + end +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 3a13a8d..1f64ce0 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,6 +1,54 @@ +# == 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 + include Rails.application.routes.url_helpers has_many :posts has_many :course_users has_many :users, :through => :course_users - #belongs_to :manager, :class_name => "User", :foreign_key => :manager_id + 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).first + if cu.nil? + cu = CourseUser.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).first + cu.delete if !cu.nil? + end + + def ios_link_course_url + url_for(controller: 'courses', + action: 'join', + id: self.id, + only_path: false) + end + + def generate_short_link + link = self.ios_link_course_url + 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 3550c33..59fc43d 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -1,7 +1,20 @@ +# == 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 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 34582cb..87583fc 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 :photos @@ -7,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/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/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| %> +
+ <%= link_to "View details »", course_path(course), :class => "btn btn-default" %> +
++ <%= link_to "View details »", course_path(course), :class => "btn btn-default" %> +
+<%= post.created_at.strftime("%Y-%m-%d") %>