Skip to content

Commit

Permalink
Generating a Comments Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamysara committed Oct 2, 2024
1 parent 3c2d030 commit 01b43f2
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 5 deletions.
15 changes: 15 additions & 0 deletions app/controllers/blorgh/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Blorgh
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
flash[:notice] = "Comment has been created!"
redirect_to articles_path
end

private
def comment_params
params.require(:comment).permit(:text)
end
end
end
4 changes: 4 additions & 0 deletions app/helpers/blorgh/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Blorgh
module CommentsHelper
end
end
1 change: 1 addition & 0 deletions app/models/blorgh/article.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Blorgh
class Article < ApplicationRecord
has_many :comments
end
end
4 changes: 4 additions & 0 deletions app/models/blorgh/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Blorgh
class Comment < ApplicationRecord
end
end
5 changes: 5 additions & 0 deletions app/views/blorgh/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
<%= link_to "Edit this article", edit_article_path(@article) %> |
<%= link_to "Back to articles", articles_path %>

<%= render "blorgh/comments/form" %>

<h3>Comments</h3>
<%= render @article.comments %>

<%= button_to "Destroy this article", @article, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= comment_counter + 1 %>. <%= comment.text %>
8 changes: 8 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h3>New comment</h3>
<%= form_with model: [@article, @article.comments.build] do |form| %>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<%= form.submit %>
<% end %>
6 changes: 3 additions & 3 deletions blorgh.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ Gem::Specification.new do |spec|
spec.version = Blorgh::VERSION
spec.authors = [ "nadiamysara" ]
spec.email = [ "nadia.mysara1999@yahoo.com" ]
spec.homepage = "https://nadiam.blorgh.me/"
spec.homepage = "https://github.com/nadiamysara/blorgh"
spec.summary = "Summary of Blorgh."
spec.description = "Description of Blorgh."
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the "allowed_push_host"
# to allow pushing to a single host or delete this section to allow pushing to any host.
spec.metadata["allowed_push_host"] = #"TODO: Set to 'http://mygemserver.com'"
spec.metadata["allowed_push_host"] = "https://github.com/nadiamysara/blorgh" #"TODO: Set to 'http://mygemserver.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/nadiamysara/blorgh" # "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "https://github.com/nadiamysara/blorgh/README.md" # "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata["changelog_uri"] = "https://github.com/nadiamysara/blorgh" # "TODO: Put your gem's CHANGELOG.md URL here."

spec.files = Dir.chdir(File.expand_path(__dir__)) do
Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Blorgh::Engine.routes.draw do
root to: "articles#index"

resources :articles
resources :articles do
resources :comments
end
end
10 changes: 10 additions & 0 deletions db/migrate/20241002034937_create_blorgh_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateBlorghComments < ActiveRecord::Migration[7.2]
def change
create_table :blorgh_comments do |t|
t.integer :article_id
t.text :text

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions test/controllers/blorgh/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "test_helper"

module Blorgh
class CommentsControllerTest < ActionDispatch::IntegrationTest
include Engine.routes.url_helpers

# test "the truth" do
# assert true
# end
end
end
9 changes: 8 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_10_02_025747) do
ActiveRecord::Schema[7.2].define(version: 2024_10_02_034937) do
create_table "blorgh_articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "blorgh_comments", force: :cascade do |t|
t.integer "article_id"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
9 changes: 9 additions & 0 deletions test/fixtures/blorgh/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
article_id: 1
text: MyText

two:
article_id: 1
text: MyText
9 changes: 9 additions & 0 deletions test/models/blorgh/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "test_helper"

module Blorgh
class CommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
end

0 comments on commit 01b43f2

Please sign in to comment.