diff --git a/app/assets/stylesheets/posts.scss b/app/assets/stylesheets/posts.scss deleted file mode 100644 index 1a7e1539..00000000 --- a/app/assets/stylesheets/posts.scss +++ /dev/null @@ -1,3 +0,0 @@ -// 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/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 00000000..80a6b322 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,11 @@ +class CommentsController < ApplicationController + def index + comments = Comment + .where(post_id: params[:post_id]) + .left_outer_joins(:user) + .select('comments.body, comments.updated_at, users.full_name') + .order(updated_at: :desc) + + render json: comments + end +end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 00000000..0ec9ca5f --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/models/post.rb b/app/models/post.rb index d1568c34..26540ab6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -2,6 +2,7 @@ class Post < ApplicationRecord belongs_to :board belongs_to :user belongs_to :post_status, optional: true + has_many :comments validates :title, presence: true, length: { in: 4..64 } diff --git a/app/models/user.rb b/app/models/user.rb index 71e90fa7..c54ae449 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,8 @@ class User < ApplicationRecord :recoverable, :rememberable, :validatable, :confirmable + has_many :comments + enum role: [:user, :moderator, :admin] after_initialize :set_default_role, if: :new_record? diff --git a/config/routes.rb b/config/routes.rb index d32ea5c8..a4cc7733 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,8 @@ Rails.application.routes.draw do devise_for :users resources :boards, only: [:show] - resources :posts, only: [:index, :create, :show, :update] + resources :posts, only: [:index, :create, :show, :update] do + resources :comments, only: [:index] + end resources :post_statuses, only: [:index] end diff --git a/db/seeds.rb b/db/seeds.rb index 2739f391..fc74be09 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -13,20 +13,20 @@ completed_post_status = PostStatus.create(name: 'Completed', color: '#6ac47c', o rejected_post_status = PostStatus.create(name: 'Rejected', color: '#ff2600', order: 4, show_in_roadmap: false) # Create some posts -Post.create( +post1 = Post.create( title: 'This is how users give you feedback', description: 'They can also provide an extendend description like this... bla bla...', board_id: feature_board.id, user_id: admin.id ) -Post.create( +post2 = Post.create( title: 'You can assign a status to each post', description: 'This one, for example, is marked as "Planned"', board_id: feature_board.id, user_id: admin.id, post_status_id: planned_post_status.id ) -Post.create( +post3 = Post.create( title: 'There are multiple boards', description: 'For now you have Feature Requests and Bug Reports, but you can add or remove as many as you want!', board_id: bug_board.id, @@ -34,6 +34,9 @@ Post.create( post_status_id: in_progress_post_status.id ) +# Create some comments +post1.comments.create(body: 'Users can comment to express their opinions!', user_id: admin.id) + # Let the user know how to log in with admin account puts 'A default admin account has been created. Credentials:' puts "-> email: #{admin.email}"