diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..e69a31d --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,70 @@ +class PostsController < ApplicationController + before_action :set_post, only: %i[ show edit update destroy ] + + # GET /posts or /posts.json + def index + @posts = Post.all + end + + # GET /posts/1 or /posts/1.json + def show + end + + # GET /posts/new + def new + @post = Post.new + end + + # GET /posts/1/edit + def edit + end + + # POST /posts or /posts.json + def create + @post = Post.new(post_params) + + respond_to do |format| + if @post.save + format.html { redirect_to @post, notice: "Post was successfully created." } + format.json { render :show, status: :created, location: @post } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @post.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /posts/1 or /posts/1.json + def update + respond_to do |format| + if @post.update(post_params) + format.html { redirect_to @post, notice: "Post was successfully updated." } + format.json { render :show, status: :ok, location: @post } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @post.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /posts/1 or /posts/1.json + def destroy + @post.destroy! + + respond_to do |format| + format.html { redirect_to posts_path, status: :see_other, notice: "Post was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_post + @post = Post.find(params.expect(:id)) + end + + # Only allow a list of trusted parameters through. + def post_params + params.expect(post: [ :title, :body ]) + end +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/post.rb b/app/models/post.rb new file mode 100644 index 0000000..f04075b --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,6 @@ +class Post + include Mongoid::Document + include Mongoid::Timestamps + field :title, type: String + field :body, type: String +end diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb new file mode 100644 index 0000000..90e32bc --- /dev/null +++ b/app/views/posts/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: post) do |form| %> + <% if post.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= form.label :title, style: "display: block" %> + <%= form.text_field :title %> +
+ +
+ <%= form.label :body, style: "display: block" %> + <%= form.textarea :body %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/posts/_post.html.erb b/app/views/posts/_post.html.erb new file mode 100644 index 0000000..c6a0bbf --- /dev/null +++ b/app/views/posts/_post.html.erb @@ -0,0 +1,12 @@ +
+

+ Title: + <%= post.title %> +

+ +

+ Body: + <%= post.body %> +

+ +
diff --git a/app/views/posts/_post.json.jbuilder b/app/views/posts/_post.json.jbuilder new file mode 100644 index 0000000..2d5d444 --- /dev/null +++ b/app/views/posts/_post.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! post, :id, :title, :body, :created_at, :updated_at +json.url post_url(post, format: :json) diff --git a/app/views/posts/edit.html.erb b/app/views/posts/edit.html.erb new file mode 100644 index 0000000..87a4e84 --- /dev/null +++ b/app/views/posts/edit.html.erb @@ -0,0 +1,12 @@ +<% content_for :title, "Editing post" %> + +

Editing post

+ +<%= render "form", post: @post %> + +
+ +
+ <%= link_to "Show this post", @post %> | + <%= link_to "Back to posts", posts_path %> +
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb new file mode 100644 index 0000000..e2c2653 --- /dev/null +++ b/app/views/posts/index.html.erb @@ -0,0 +1,16 @@ +

<%= notice %>

+ +<% content_for :title, "Posts" %> + +

Posts

+ +
+ <% @posts.each do |post| %> + <%= render post %> +

+ <%= link_to "Show this post", post %> +

+ <% end %> +
+ +<%= link_to "New post", new_post_path %> diff --git a/app/views/posts/index.json.jbuilder b/app/views/posts/index.json.jbuilder new file mode 100644 index 0000000..a3c6f4a --- /dev/null +++ b/app/views/posts/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @posts, partial: "posts/post", as: :post diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb new file mode 100644 index 0000000..70c2934 --- /dev/null +++ b/app/views/posts/new.html.erb @@ -0,0 +1,11 @@ +<% content_for :title, "New post" %> + +

New post

+ +<%= render "form", post: @post %> + +
+ +
+ <%= link_to "Back to posts", posts_path %> +
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb new file mode 100644 index 0000000..c3ba772 --- /dev/null +++ b/app/views/posts/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @post %> + +
+ <%= link_to "Edit this post", edit_post_path(@post) %> | + <%= link_to "Back to posts", posts_path %> + + <%= button_to "Destroy this post", @post, method: :delete %> +
diff --git a/app/views/posts/show.json.jbuilder b/app/views/posts/show.json.jbuilder new file mode 100644 index 0000000..5274482 --- /dev/null +++ b/app/views/posts/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "posts/post", post: @post diff --git a/config/routes.rb b/config/routes.rb index 48254e8..327223a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :posts # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. diff --git a/test/controllers/posts_controller_test.rb b/test/controllers/posts_controller_test.rb new file mode 100644 index 0000000..076625f --- /dev/null +++ b/test/controllers/posts_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class PostsControllerTest < ActionDispatch::IntegrationTest + setup do + @post = posts(:one) + end + + test "should get index" do + get posts_url + assert_response :success + end + + test "should get new" do + get new_post_url + assert_response :success + end + + test "should create post" do + assert_difference("Post.count") do + post posts_url, params: { post: { body: @post.body, title: @post.title } } + end + + assert_redirected_to post_url(Post.last) + end + + test "should show post" do + get post_url(@post) + assert_response :success + end + + test "should get edit" do + get edit_post_url(@post) + assert_response :success + end + + test "should update post" do + patch post_url(@post), params: { post: { body: @post.body, title: @post.title } } + assert_redirected_to post_url(@post) + end + + test "should destroy post" do + assert_difference("Post.count", -1) do + delete post_url(@post) + end + + assert_redirected_to posts_url + end +end diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 0000000..6028a9c --- /dev/null +++ b/test/fixtures/posts.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + body: MyText + +two: + title: MyString + body: MyText diff --git a/test/models/post_test.rb b/test/models/post_test.rb new file mode 100644 index 0000000..ff155c4 --- /dev/null +++ b/test/models/post_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/posts_test.rb b/test/system/posts_test.rb new file mode 100644 index 0000000..c3e6dae --- /dev/null +++ b/test/system/posts_test.rb @@ -0,0 +1,43 @@ +require "application_system_test_case" + +class PostsTest < ApplicationSystemTestCase + setup do + @post = posts(:one) + end + + test "visiting the index" do + visit posts_url + assert_selector "h1", text: "Posts" + end + + test "should create post" do + visit posts_url + click_on "New post" + + fill_in "Body", with: @post.body + fill_in "Title", with: @post.title + click_on "Create Post" + + assert_text "Post was successfully created" + click_on "Back" + end + + test "should update Post" do + visit post_url(@post) + click_on "Edit this post", match: :first + + fill_in "Body", with: @post.body + fill_in "Title", with: @post.title + click_on "Update Post" + + assert_text "Post was successfully updated" + click_on "Back" + end + + test "should destroy Post" do + visit post_url(@post) + click_on "Destroy this post", match: :first + + assert_text "Post was successfully destroyed" + end +end