From 04a814b71b2439d3bf7c3b0b63f89566a3c9ce40 Mon Sep 17 00:00:00 2001 From: Neetpone <132411956+Neetpone@users.noreply.github.com> Date: Sat, 6 Apr 2024 07:00:46 -0400 Subject: [PATCH] feat: quick blogs/news feature --- .rubocop.yml | 1 + Gemfile | 16 ++++++++++----- Gemfile.lock | 8 ++++++++ app/controllers/blogs_controller.rb | 6 ++++++ app/controllers/search_controller.rb | 2 +- app/helpers/blogs_helper.rb | 3 +++ app/models/blog.rb | 15 ++++++++++++++ app/views/blogs/index.html.slim | 15 ++++++++++++++ config/application.rb | 24 ++++++++++++----------- config/routes.rb | 4 ++++ db/migrate/20240406101101_create_blogs.rb | 9 +++++++++ db/schema.rb | 11 +++++++++-- test/controllers/blogs_controller_test.rb | 8 ++++++++ test/fixtures/blogs.yml | 20 +++++++++++++++++++ test/models/blog_test.rb | 18 +++++++++++++++++ 15 files changed, 141 insertions(+), 19 deletions(-) create mode 100644 app/controllers/blogs_controller.rb create mode 100644 app/helpers/blogs_helper.rb create mode 100644 app/models/blog.rb create mode 100644 app/views/blogs/index.html.slim create mode 100644 db/migrate/20240406101101_create_blogs.rb create mode 100644 test/controllers/blogs_controller_test.rb create mode 100644 test/fixtures/blogs.yml create mode 100644 test/models/blog_test.rb diff --git a/.rubocop.yml b/.rubocop.yml index 4fda21a..1cbf9c9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,4 @@ +require: rubocop-rails inherit_from: .rubocop_todo.yml AllCops: SuggestExtensions: false diff --git a/Gemfile b/Gemfile index 569811e..c9358ec 100644 --- a/Gemfile +++ b/Gemfile @@ -4,20 +4,25 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.2.2' -gem 'kaminari' -gem 'pg', '~> 1.1' -gem 'puma', '~> 5.0' gem 'rails', '~> 7.0.8', '>= 7.0.8.1' -gem 'redcarpet' -gem 'slim-rails' gem 'sprockets-rails' +# Database stuff +gem 'pg', '~> 1.1' gem 'redis' +# Search gem 'elasticsearch-model' gem 'fancy_searchable', github: 'Twibooru/fancy_searchable', ref: '40687c9' gem 'model-msearch' +# Views +gem 'kaminari' +gem 'redcarpet' +gem 'slim-rails' + +# Programs +gem 'puma', '~> 5.0' gem 'sidekiq' # Windows does not include zoneinfo files, so bundle the tzinfo-data gem @@ -35,6 +40,7 @@ group :development do gem 'annotate' gem 'bullet' gem 'rubocop', require: false + gem 'rubocop-rails', require: false gem 'web-console' end diff --git a/Gemfile.lock b/Gemfile.lock index 1089632..d1c5b78 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -82,6 +82,7 @@ GEM rake (>= 10.4, < 14.0) ast (2.4.2) base64 (0.2.0) + bcrypt (3.1.20) bindex (0.8.1) builder (3.2.4) bullet (7.1.6) @@ -241,6 +242,11 @@ GEM unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.31.2) parser (>= 3.3.0.4) + rubocop-rails (2.24.1) + activesupport (>= 4.2.0) + rack (>= 1.1) + rubocop (>= 1.33.0, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) ruby-progressbar (1.13.0) rubyzip (2.3.2) selenium-webdriver (4.19.0) @@ -295,6 +301,7 @@ PLATFORMS DEPENDENCIES annotate + bcrypt bullet capybara debug @@ -308,6 +315,7 @@ DEPENDENCIES redcarpet redis rubocop + rubocop-rails selenium-webdriver sidekiq slim-rails diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb new file mode 100644 index 0000000..f544658 --- /dev/null +++ b/app/controllers/blogs_controller.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +class BlogsController < ApplicationController + def index + @blogs = Blog.order(created_at: :desc) + end +end diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 4028fa5..d86de57 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -38,7 +38,7 @@ class SearchController < ApplicationController tag_must_nots.map { |t| { term: { tags: t } } } ) if tag_must_nots.any? - s.add_query(bool: boolses ) if boolses.any? + s.add_query(bool: boolses) if boolses.any? # ratings -> match stories with any of them s.add_filter(bool: { diff --git a/app/helpers/blogs_helper.rb b/app/helpers/blogs_helper.rb new file mode 100644 index 0000000..4f7459b --- /dev/null +++ b/app/helpers/blogs_helper.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true +module BlogsHelper +end diff --git a/app/models/blog.rb b/app/models/blog.rb new file mode 100644 index 0000000..5b92f21 --- /dev/null +++ b/app/models/blog.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: blogs +# +# id :bigint not null, primary key +# body :text not null +# title :text not null +# created_at :datetime not null +# updated_at :datetime not null +# +class Blog < ApplicationRecord + validates :title, presence: true, length: { maximum: 128 } + validates :body, presence: true +end diff --git a/app/views/blogs/index.html.slim b/app/views/blogs/index.html.slim new file mode 100644 index 0000000..c5937b4 --- /dev/null +++ b/app/views/blogs/index.html.slim @@ -0,0 +1,15 @@ +.news + #wrap + = render partial: 'layouts/banner' + nav.home + = link_to 'Home', root_path + = link_to 'News', '/news', class: 'current' + = link_to 'About', '/about' + section.news-list + - @blogs.each do |blog| + article + header + h1= blog.title + section + == blog.body + footer Posted by Floorb on #{blog.created_at}. \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 1e70dc7..c898f81 100644 --- a/config/application.rb +++ b/config/application.rb @@ -26,15 +26,17 @@ Bundler.require(*Rails.groups) # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") -class Foalfetch::Application < Rails::Application - # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 7.0 +module Foalfetch + class Application < Rails::Application + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 7.0 - # Configuration for the application, engines, and railties goes here. - # - # These settings can be overridden in specific environments using the files - # in config/environments, which are processed later. - # - # config.time_zone = "Central Time (US & Canada)" - # config.eager_load_paths << Rails.root.join("extras") -end + # Configuration for the application, engines, and railties goes here. + # + # These settings can be overridden in specific environments using the files + # in config/environments, which are processed later. + # + # config.time_zone = "Central Time (US & Canada)" + # config.eager_load_paths << Rails.root.join("extras") + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 5ea098e..087acbc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,8 +7,12 @@ Rails.application.routes.draw do get '/images' => 'images#show' resources :authors, only: [:show] + # using singular-named routes to match FiMFetch/FiMFiction. resources :stories, only: [:show], path: :story do resources :chapters, only: [:show], path: :chapter end + + # Different route name again to match FiMFetch + resources :blogs, only: [:index], path: :news end diff --git a/db/migrate/20240406101101_create_blogs.rb b/db/migrate/20240406101101_create_blogs.rb new file mode 100644 index 0000000..0edde8e --- /dev/null +++ b/db/migrate/20240406101101_create_blogs.rb @@ -0,0 +1,9 @@ +class CreateBlogs < ActiveRecord::Migration[7.0] + def change + create_table :blogs do |t| + t.timestamps null: false + t.text :title, null: false + t.text :body, null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8dbebf4..9195b08 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[7.0].define(version: 2024_04_02_172140) do +ActiveRecord::Schema[7.0].define(version: 2024_04_06_101101) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,13 +28,20 @@ ActiveRecord::Schema[7.0].define(version: 2024_04_02_172140) do t.datetime "date_joined", null: false end + create_table "blogs", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "title", null: false + t.text "body", null: false + end + create_table "chapters", force: :cascade do |t| t.bigint "story_id", null: false t.integer "number", default: 1, null: false t.datetime "date_published", null: false t.datetime "date_modified" t.integer "num_views", default: 0, null: false - t.integer "num_words", null: false + t.integer "num_words" t.text "title", null: false t.text "body" t.index ["story_id"], name: "index_chapters_on_story_id" diff --git a/test/controllers/blogs_controller_test.rb b/test/controllers/blogs_controller_test.rb new file mode 100644 index 0000000..8095481 --- /dev/null +++ b/test/controllers/blogs_controller_test.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true +require "test_helper" + +class BlogsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/blogs.yml b/test/fixtures/blogs.yml new file mode 100644 index 0000000..113a855 --- /dev/null +++ b/test/fixtures/blogs.yml @@ -0,0 +1,20 @@ +# == Schema Information +# +# Table name: blogs +# +# id :bigint not null, primary key +# body :text not null +# title :text not null +# created_at :datetime not null +# updated_at :datetime not null +# + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/blog_test.rb b/test/models/blog_test.rb new file mode 100644 index 0000000..a6f5903 --- /dev/null +++ b/test/models/blog_test.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: blogs +# +# id :bigint not null, primary key +# body :text not null +# title :text not null +# created_at :datetime not null +# updated_at :datetime not null +# +require "test_helper" + +class BlogTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end