diff --git a/3.0 b/3.0 new file mode 100644 index 00000000..e69de29b diff --git a/Gemfile.lock b/Gemfile.lock index ac898c77..b6e7077d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -128,7 +128,6 @@ GEM marcel (1.0.2) matrix (0.4.2) mini_mime (1.1.5) - mini_portile2 (2.8.5) minitest (5.21.2) msgpack (1.7.2) mutex_m (0.2.0) @@ -142,16 +141,7 @@ GEM net-smtp (0.4.0.1) net-protocol nio4r (2.7.0) - nokogiri (1.16.0) - mini_portile2 (~> 2.8.2) - racc (~> 1.4) - nokogiri (1.16.0-aarch64-linux) - racc (~> 1.4) - nokogiri (1.16.0-arm64-darwin) - racc (~> 1.4) - nokogiri (1.16.0-x86_64-darwin) - racc (~> 1.4) - nokogiri (1.16.0-x86_64-linux) + nokogiri (1.16.0-x64-mingw-ucrt) racc (~> 1.4) psych (5.1.2) stringio @@ -216,12 +206,7 @@ GEM actionpack (>= 5.2) activesupport (>= 5.2) sprockets (>= 3.0.0) - sqlite3 (1.7.1) - mini_portile2 (~> 2.8.0) - sqlite3 (1.7.1-aarch64-linux) - sqlite3 (1.7.1-arm64-darwin) - sqlite3 (1.7.1-x86_64-darwin) - sqlite3 (1.7.1-x86_64-linux) + sqlite3 (1.7.1-x64-mingw-ucrt) stimulus-rails (1.3.3) railties (>= 6.0.0) stringio (3.1.0) @@ -233,6 +218,8 @@ GEM railties (>= 6.0.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + tzinfo-data (1.2023.4) + tzinfo (>= 1.0.0) web-console (4.2.1) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -252,11 +239,7 @@ GEM zeitwerk (2.6.12) PLATFORMS - aarch64-linux - arm64-darwin - ruby - x86_64-darwin - x86_64-linux + x64-mingw-ucrt DEPENDENCIES bootsnap diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index ddd546a0..819489a4 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -1,4 +1,7 @@ //= link_tree ../images //= link_directory ../stylesheets .css -//= link_tree ../../javascript .js -//= link_tree ../../../vendor/javascript .js +//= link application.js +//= link rails-ujs.js +//= link controllers/application.js +//= link controllers/hello_controller.js + //= link controllers/index.js diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 00000000..acccc328 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,58 @@ +class ArticlesController < ApplicationController + # Ensures set_article method is called before specified actions to DRY up finding an article by ID + before_action :set_article, only: [:show, :edit, :update, :destroy] + + # Lists all articles + def index + @articles = Article.all + end + + # Shows a single article, @article is set by set_article method + def show + end + + # Initializes a new article instance for the 'new' form + def new + @article = Article.new + end + + # Creates a new article from the form parameters + def create + @article = Article.new(article_params) + if @article.save # If the article saves successfully + redirect_to @article, notice: 'Article was successfully created.' + else # If the article doesn't save due to validation failures + render :new + end + end + + # Loads the article for editing, @article is set by set_article method + def edit + end + + # Updates the article with the form parameters + def update + if @article.update(article_params) # If the article updates successfully + redirect_to @article, notice: 'Article was successfully updated.' + else # If the update fails due to validation errors + render :edit + end + end + + # Deletes the article + def destroy + @article.destroy + redirect_to articles_url, notice: 'Article was successfully destroyed.' + end + + private + # Sets the @article instance variable from the ID in the parameters + def set_article + @article = Article.find(params[:id]) + end + + # Strong parameters: whitelists the parameters allowed for article creation/updating + def article_params + params.require(:article).permit(:title, :content, :author, :date) + end +end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 00000000..29682775 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end diff --git a/app/javascript/application.js b/app/javascript/application.js index 0d7b4940..efc96aa4 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -1,3 +1,5 @@ // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails import "@hotwired/turbo-rails" import "controllers" +import Rails from "@rails/ujs"; +Rails.start(); diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 00000000..c36d21f8 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,12 @@ +class Article < ApplicationRecord + # Validations + validates :title, presence: true + validates :content, presence: true + + # Search functionality + def self.search(query) + where("title LIKE ? OR content LIKE ?", "%#{query}%", "%#{query}%") + end + + end + \ No newline at end of file diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100644 index 00000000..201e0119 --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,49 @@ + +<%= form_with(model: article, local: true) do |form| %> + + + <% if article.errors.any? %> + +
+ +

<%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:

+ + + +
+ <% end %> + + +
+ <%= form.label :title %> + <%= form.text_field :title %> +
+ + +
+ <%= form.label :content %> + <%= form.text_area :content %> +
+ + +
+ <%= form.label :author %> + <%= form.text_field :author %> +
+ + +
+ <%= form.label :date %> + <%= form.date_field :date %> +
+ + +
+ <%= form.submit %> +
+ +<% end %> diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100644 index 00000000..983dd2f2 --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1,3 @@ +

Edit Article

+<%= render 'form', article: @article %> +<%= link_to 'Back', article_path(@article) %> \ No newline at end of file diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 00000000..2b5b936e --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,42 @@ + +

Listing Articles

+ + +<%= link_to 'New Article', new_article_path %> + + + + + + + + + + + + + + + + + <% @articles.each do |article| %> + + + + + + + + + <% end %> + +
Title Author Date Actions
<%= article.title %> <%= article.author %> <%= article.date.strftime('%b %d, %Y') if article.date %> <%= link_to 'Show', article_path(article) %> <%= link_to 'Edit', edit_article_path(article) %> + + <%= form_with url: article_path(article), method: :delete, local: true do %> + + <%= submit_tag 'Destroy', data: { confirm: 'Are you sure?' } %> + <% end %> +
+ + +<%= link_to 'Back', root_path %> diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100644 index 00000000..c1242cdd --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1,3 @@ +

New Article

+<%= render 'form', article: @article %> +<%= link_to 'Back', articles_path %> \ No newline at end of file diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 00000000..7f3e0a17 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,14 @@ +

<%= @article.title %>

+

<%= @article.content %>

+

+ Author: + <%= @article.author %> +

+ +

+ Date: + <%= @article.date %> +

+ +<%= link_to 'Edit', edit_article_path(@article) %> | +<%= link_to 'Back', articles_path %> diff --git a/config/importmap.rb b/config/importmap.rb index 8dce42d4..140feca4 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -5,3 +5,5 @@ pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true pin_all_from "app/javascript/controllers", under: "controllers" +pin "@rails/ujs", to: "rails-ujs.js" + diff --git a/config/routes.rb b/config/routes.rb index a125ef08..59347c9d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,10 @@ # Can be used by load balancers and uptime monitors to verify that the app is live. get "up" => "rails/health#show", as: :rails_health_check + # Article CRUD routes + resources :articles + root 'articles#index' + # Defines the root path route ("/") # root "posts#index" end diff --git a/db/migrate/20240130012331_create_articles.rb b/db/migrate/20240130012331_create_articles.rb new file mode 100644 index 00000000..610dcb78 --- /dev/null +++ b/db/migrate/20240130012331_create_articles.rb @@ -0,0 +1,12 @@ +class CreateArticles < ActiveRecord::Migration[6.0] + def change + create_table :articles do |t| + t.string :title + t.text :content + t.string :author + t.date :date + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..5eb4d724 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.1].define(version: 2024_01_30_012331) do + create_table "articles", force: :cascade do |t| + t.string "title" + t.text "content" + t.string "author" + t.date "date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb new file mode 100644 index 00000000..0a82cbb1 --- /dev/null +++ b/test/controllers/articles_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ArticlesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end