diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 0000000..0c6fb8c --- /dev/null +++ b/.byebug_history @@ -0,0 +1,9 @@ +continue +j`qqqldfjkdfj +continue +@comment.errors.full_messages_for(:body).join(", ") +@comment.error.full_messages_for(:body).join +@comment.error.full_messages_for(body) +continue +contunue +e diff --git a/.ruby-version b/.ruby-version index 85588be..2a49746 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-3.0.0 +ruby-3.1.1 diff --git a/Gemfile b/Gemfile index aeead36..4107249 100644 --- a/Gemfile +++ b/Gemfile @@ -1,31 +1,31 @@ -source "https://rubygems.org" +source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby "3.0.0" +ruby '3.1.1' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" -gem "rails", "~> 7.0.2", ">= 7.0.2.3" +gem 'rails', '~> 7.0.2', '>= 7.0.2.3' # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] -gem "sprockets-rails" +gem 'sprockets-rails' # Use postgresql as the database for Active Record -gem "pg", "~> 1.1" +gem 'pg', '~> 1.1' # Use the Puma web server [https://github.com/puma/puma] -gem "puma", "~> 5.0" +gem 'puma', '~> 5.0' # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] -gem "importmap-rails" +gem 'importmap-rails' # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] -gem "turbo-rails" +gem 'turbo-rails' # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] -gem "stimulus-rails" +gem 'stimulus-rails' # Build JSON APIs with ease [https://github.com/rails/jbuilder] -gem "jbuilder" +gem 'jbuilder' # Use Redis adapter to run Action Cable in production # gem "redis", "~> 4.0" @@ -37,30 +37,36 @@ gem "jbuilder" # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] +gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] # Reduces boot times through caching; required in config/boot.rb -gem "bootsnap", require: false +gem 'bootsnap', require: false # Use Sass to process CSS -# gem "sassc-rails" +gem 'sassc-rails' # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" +gem 'bootstrap', '~> 5.1.3' + group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem - gem "debug", platforms: %i[ mri mingw x64_mingw ] + gem 'debug', platforms: %i[mri mingw x64_mingw] + + gem 'cowsay' + gem 'faker' end group :development do # Use console on exceptions pages [https://github.com/rails/web-console] - gem "web-console" + gem 'web-console' # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] # gem "rack-mini-profiler" # Speed up commands on slow machines / big apps [https://github.com/rails/spring] # gem "spring" -end + gem 'byebug' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index bf58548..386d7bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,17 +66,29 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) + autoprefixer-rails (10.4.2.0) + execjs (~> 2) bindex (0.8.1) bootsnap (1.11.1) msgpack (~> 1.2) + bootstrap (5.1.3) + autoprefixer-rails (>= 9.1.0) + popper_js (>= 2.9.3, < 3) + sassc-rails (>= 2.0.0) builder (3.2.4) - concurrent-ruby (1.1.9) + byebug (11.1.3) + concurrent-ruby (1.1.10) + cowsay (0.3.0) crass (1.0.6) debug (1.4.0) irb (>= 1.3.6) reline (>= 0.2.7) digest (3.1.0) erubi (1.10.0) + execjs (2.8.1) + faker (2.20.0) + i18n (>= 1.8.11, < 2) + ffi (1.15.5) globalid (1.0.0) activesupport (>= 5.0) i18n (1.10.0) @@ -91,7 +103,7 @@ GEM jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) - loofah (2.14.0) + loofah (2.15.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -120,6 +132,7 @@ GEM nokogiri (1.13.3-x86_64-linux) racc (~> 1.4) pg (1.3.4) + popper_js (2.9.3) puma (5.6.2) nio4r (~> 2.0) racc (1.6.0) @@ -155,6 +168,14 @@ GEM rake (13.0.6) reline (0.3.1) io-console (~> 0.5) + sassc (2.4.0) + ffi (~> 1.9) + sassc-rails (2.1.2) + railties (>= 4.0.0) + sassc (>= 2.0) + sprockets (> 3.0) + sprockets-rails + tilt sprockets (4.0.3) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -166,6 +187,7 @@ GEM railties (>= 6.0.0) strscan (3.0.1) thor (1.2.1) + tilt (2.0.10) timeout (0.2.0) turbo-rails (1.0.1) actionpack (>= 6.0.0) @@ -187,12 +209,17 @@ PLATFORMS DEPENDENCIES bootsnap + bootstrap (~> 5.1.3) + byebug + cowsay debug + faker importmap-rails jbuilder pg (~> 1.1) puma (~> 5.0) rails (~> 7.0.2, >= 7.0.2.3) + sassc-rails sprockets-rails stimulus-rails turbo-rails @@ -200,7 +227,7 @@ DEPENDENCIES web-console RUBY VERSION - ruby 3.0.0p0 + ruby 3.1.1p18 BUNDLED WITH 2.3.8 diff --git a/README.md b/README.md deleted file mode 100644 index 7db80e4..0000000 --- a/README.md +++ /dev/null @@ -1,24 +0,0 @@ -# README - -This README would normally document whatever steps are necessary to get the -application up and running. - -Things you may want to cover: - -* Ruby version - -* System dependencies - -* Configuration - -* Database creation - -* Database initialization - -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... diff --git a/app/assets/images/favicon.ico b/app/assets/images/favicon.ico new file mode 100644 index 0000000..b63d10b Binary files /dev/null and b/app/assets/images/favicon.ico differ diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 288b9ab..f68bba7 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -10,6 +10,75 @@ * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * + *= require reset + *= require post + *= require comment *= require_tree . *= require_self */ + + a { + text-decoration: none; + color: blue; +} + +header { + padding: 15px; + background-color: lightgray; +} + +nav { + display: flex; + justify-content: space-between; + align-items: baseline; +} + +nav > .brand > a { + font-size: x-large; + color: blue; +} + +nav > ul { + display: flex; + list-style: none; +} + +nav > ul > li > a { + margin-left: 20px; + color: blue; +} + +input[type="submit"], +button { + color: blue; + border-color: blue; + border-radius: 5px; + background-color: white; + font: arial; + font-size: 15px; + height: 35px; +} + +@media only screen and (max-width: 960px) { + .posts { + display: flex; + flex-wrap: wrap; + } + + article { + flex-grow: 1; + width: 50%; + } +} + +@media only screen and (max-width: 600px) { + .posts { + display: flex; + flex-wrap: wrap; + } + + article { + flex-grow: 1; + width: 100%; + } +} diff --git a/app/assets/stylesheets/comment.css b/app/assets/stylesheets/comment.css new file mode 100644 index 0000000..5dc5cfc --- /dev/null +++ b/app/assets/stylesheets/comment.css @@ -0,0 +1,48 @@ +.comment-form { + padding: 15px; + display: flex; + flex-direction: column; +} + +.comment-form > .field_with_errors { + width: 100%; +} + +.comment-form > label, +.comment-form > .field_with_errors > label { + font-size: large; + font-weight: bold; +} + +.comment-form > textarea, +.comment-form > .field_with_errors > textarea { + margin: 10px 0; + width: 100%; +} + +.comment-form > p { + margin-bottom: 10px; + color: red; +} + +.comment-form > input { + padding: 5px; + width: 100px; +} + +.comments { + padding: 15px; +} + +.comment { + margin-bottom: 10px; +} + +.comment > .body { + word-break: break-all; +} + +.comment > .date { + margin-top: 5px; + font-size: x-small; +} \ No newline at end of file diff --git a/app/assets/stylesheets/post.css b/app/assets/stylesheets/post.css new file mode 100644 index 0000000..41b7dd4 --- /dev/null +++ b/app/assets/stylesheets/post.css @@ -0,0 +1,106 @@ +/* Index view */ +.posts { + display: flex; + flex-wrap: wrap; +} + +article { + flex-grow: 1; + width: 33%; +} + +.posts > .post > .details { + margin: 10px; + padding: 15px; + display: flex; + flex-direction: column; + border: 1px solid lightgrey; +} + +.posts > .post > .details > a { + padding-bottom: 5px; + font-size: x-large; + font-weight: bold; + width: auto; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.posts > .post > .details > p { + width: auto; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +/* Show view */ +main.post { + padding: 15px; + display: flex; + flex-direction: column; +} + +main.post > .buttons { + display: flex; +} + +main.post > .buttons > form > button { + margin-right: 5px; + margin-top: 10px; + padding: 5px; +} + +main.post > .details > .title { + font-size: x-large; + font-weight: bold; + word-break: break-all; +} + +main.post > .details > .body { + margin-top: 10px; + font-size: medium; + word-break: break-all; +} + +main.post > .details > .date { + margin-top: 15px; + font-size: x-small; +} + +/* New/Edit form views */ +.post-form { + padding: 15px; + margin: 10px 0; +} + +.post-form > .input-group { + display: flex; + flex-direction: column; +} + +.post-form > .input-group > .field_with_errors { + display: flex; + width: 100%; +} + +.post-form > .input-group > label, +.post-form > .input-group > .field_with_errors > label { + margin: 10px 0; +} + +.post-form > .input-group > .field_with_errors > input[type="text"] { + margin: 10px 0; + width: 100%; +} + +.post-form > .input-group > textarea, +.post-form > .input-group > .field_with_errors > textarea { + margin-bottom: 10px; + width: 100%; +} + +.post-form > .input-group > p { + margin-bottom: 10px; + color: red; +} \ No newline at end of file diff --git a/app/assets/stylesheets/reset.css b/app/assets/stylesheets/reset.css new file mode 100644 index 0000000..8c6087e --- /dev/null +++ b/app/assets/stylesheets/reset.css @@ -0,0 +1,54 @@ +/* http://meyerweb.com/eric/tools/css/reset/ + v2.0 | 20110126 + License: none (public domain) +*/ + +html, body, div, span, applet, object, iframe, +h1, h2, h3, h4, h5, h6, p, blockquote, pre, +a, abbr, acronym, address, big, cite, code, +del, dfn, em, img, ins, kbd, q, s, samp, +small, strike, strong, sub, sup, tt, var, +b, u, i, center, +dl, dt, dd, ol, ul, li, +fieldset, form, label, legend, +table, caption, tbody, tfoot, thead, tr, th, td, +article, aside, canvas, details, embed, +figure, figcaption, footer, header, hgroup, +menu, nav, output, ruby, section, summary, +time, mark, audio, video { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + vertical-align: baseline; +} +/* HTML5 display-role reset for older browsers */ +article, aside, details, figcaption, figure, +footer, header, hgroup, menu, nav, section { + display: block; +} +body { + line-height: 1; +} +ol, ul { + list-style: none; +} +blockquote, q { + quotes: none; +} +blockquote:before, blockquote:after, +q:before, q:after { + content: ''; + content: none; +} +table { + border-collapse: collapse; + border-spacing: 0; +} + +*, +*::before, +*::after { + box-sizing: border-box; +} diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..8a10f99 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,25 @@ +class CommentsController < ApplicationController + def create + @post = Post.find params[:post_id] + @comment = Comment.new params.require(:comment).permit(:body) + @comment.post = @post + if @comment.save + redirect_to post_path(@post) + else + @comments = @post.comments.order(created_at: :desc) + render 'posts/show', status: 303 + end + rescue => e + redirect_to root_path, alert: e.message + end + + def destroy + @comment = Comment.find params[:id] + @comment.destroy + @post = Post.find params[:post_id] + redirect_to post_path(@post), status: 303 + rescue => e + redirect_to root_path, alert: e.message + end + end + \ No newline at end of file diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..02268a6 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,55 @@ +# postsController +class PostsController < ApplicationController + def index + @posts = Post.order(created_at: :desc) + end + + def show + @post = Post.find params[:id] + @comment = Comment.new + @comments = @post.comments.order(created_at: :desc) + rescue StandardError => e + redirect_to root_path, alert: e.message + end + + def destroy + @post = Post.find params[:id] + @post.destroy + redirect_to posts_path, { notice: 'Post deleted successfully', status: 303 } + rescue StandardError => e + redirect_to root_path, alert: e.message + end + + def new + @post = Post.new + end + + def create + @post = Post.new params.require(:post).permit(:title, :body) + if @post.save + redirect_to post_path(@post) #{ status: 303, notice: 'Post created successfully' } + else + render :new, status: 303 + end + end + + def edit + @post = Post.find params[:id] + rescue StandardError => e + redirect_to root_path, alert: e.message + end + + def update + @post = Post.find params[:id] + puts @post + if @post.update params.require(:post).permit(:title, :body) + puts @post + redirect_to post_path(@post), { status: 303, notice: 'Post updated successfully' } + else + render :edit, status: 303 + end + rescue StandardError => e + redirect_to posts_path, alert: e.message + end + end + \ No newline at end of file diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +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/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..cad758d --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,5 @@ +class Comment < ApplicationRecord + belongs_to :post + + validates :body, presence: true, length: {maximum:255} +end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..63cb440 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,6 @@ +class Post < ApplicationRecord + has_many :comments, dependent: :destroy + + validates :title, presence: true, uniqueness: true + validates :body, presence: true, length: {minimum:50} +end \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 344c627..32b993e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,16 +1,32 @@
-