Add reminder feature

This commit is contained in:
crstin 2020-02-12 13:57:03 +01:00
parent 9ec7b3ef50
commit e4a317111c
26 changed files with 414 additions and 3 deletions

View File

@ -0,0 +1,3 @@
// Place all the styles related to the Reminders controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/

View File

@ -0,0 +1,65 @@
body {
background-color: #fff;
color: #333;
margin: 33px; }
body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }
pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }
a {
color: #000; }
a:visited {
color: #666; }
a:hover {
color: #fff;
background-color: #000; }
th {
padding-bottom: 5px; }
td {
padding: 0 5px 7px; }
div.field,
div.actions {
margin-bottom: 10px; }
#notice {
color: green; }
.field_with_errors {
padding: 2px;
background-color: red;
display: table; }
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0; }
#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff; }
#error_explanation ul li {
font-size: 12px;
list-style: square; }
label {
display: block; }

View File

@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
before_action :authenticate_user!
end end

View File

@ -0,0 +1,51 @@
class RemindersController < ApplicationController
before_action :set_reminder, only: %i[show edit update destroy]
def index
@reminders = Reminder.all
end
def show; end
def new
@reminder = Reminder.new
end
def edit; end
def create
@reminder = Reminder.new(reminder_params)
@reminder.user_id = current_user.id
if @reminder.save
redirect_to @reminder, notice: 'Reminder was successfully created.'
else
render :new
end
end
def update
if @reminder.update(reminder_params)
redirect_to @reminder, notice: 'Reminder was successfully updated.'
else
render :edit
end
end
def destroy
@reminder.destroy
redirect_to reminders_url, notice: 'Reminder was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reminder
@reminder = Reminder.find(params[:id])
end
# Only allow a list of trusted parameters through.
def reminder_params
params.require(:reminder).permit(:title, :body, :date, :user_id)
end
end

View File

@ -0,0 +1,2 @@
module RemindersHelper
end

7
app/models/reminder.rb Normal file
View File

@ -0,0 +1,7 @@
class Reminder < ApplicationRecord
belongs_to :user
validates :title, presence: true
validates :body, presence: true
validates :date, presence: true
end

View File

@ -11,5 +11,10 @@
<body> <body>
<%= yield %> <%= yield %>
<hr>
<% if user_signed_in? %>
<p><%= link_to('Logout', destroy_user_session_path) %></p>
<% end %>
</body> </body>
</html> </html>

View File

@ -0,0 +1,32 @@
<%= form_with(model: reminder, local: true) do |form| %>
<% if reminder.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(reminder.errors.count, "error") %> prohibited this reminder from being saved:</h2>
<ul>
<% reminder.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,6 @@
<h1>Editing Reminder</h1>
<%= render 'form', reminder: @reminder %>
<%= link_to 'Show', @reminder %> |
<%= link_to 'Back', reminders_path %>

View File

@ -0,0 +1,31 @@
<p id="notice"><%= notice %></p>
<h1>Reminders</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
<th>Date</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @reminders.each do |reminder| %>
<tr>
<td><%= reminder.title %></td>
<td><%= reminder.body %></td>
<td><%= reminder.date %></td>
<td><%= link_to 'Show', reminder %></td>
<td><%= link_to 'Edit', edit_reminder_path(reminder) %></td>
<td><%= link_to 'Destroy', reminder, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Reminder', new_reminder_path %>

View File

@ -0,0 +1,5 @@
<h1>New Reminder</h1>
<%= render 'form', reminder: @reminder %>
<%= link_to 'Back', reminders_path %>

View File

@ -0,0 +1,19 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @reminder.title %>
</p>
<p>
<strong>Body:</strong>
<%= @reminder.body %>
</p>
<p>
<strong>Date:</strong>
<%= @reminder.date %>
</p>
<%= link_to 'Edit', edit_reminder_path(@reminder) %> |
<%= link_to 'Back', reminders_path %>

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :reminders
devise_for :users devise_for :users
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root to: 'reminders#index'
end end

View File

@ -0,0 +1,12 @@
class CreateReminders < ActiveRecord::Migration[6.0]
def change
create_table :reminders do |t|
t.string :title
t.text :body
t.datetime :date
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end

View File

@ -10,7 +10,17 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_02_12_122504) do ActiveRecord::Schema.define(version: 2020_02_12_122724) do
create_table "reminders", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "date"
t.integer "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_reminders_on_user_id"
end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false t.string "email", default: "", null: false
@ -24,4 +34,5 @@ ActiveRecord::Schema.define(version: 2020_02_12_122504) do
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end end
add_foreign_key "reminders", "users"
end end

View File

@ -0,0 +1,8 @@
FactoryBot.define do
factory :reminder do
title { 'TitleTestMsg' }
body { 'BodyTestMsg' }
date { '2020-02-20 02:02:02' }
user
end
end

View File

@ -1,5 +1,6 @@
FactoryBot.define do FactoryBot.define do
factory :user do factory :user do
email { 'test@example.com' }
password { '123456' }
end end
end end

View File

@ -0,0 +1,15 @@
# require 'rails_helper'
# # Specs in this file have access to a helper object that includes
# # the RemindersHelper. For example:
# #
# # describe RemindersHelper do
# # describe "string concat" do
# # it "concats two strings with spaces" do
# # expect(helper.concat_strings("this","that")).to eq("this that")
# # end
# # end
# # end
# RSpec.describe RemindersHelper, type: :helper do
# pending "add some examples to (or delete) #{__FILE__}"
# end

View File

@ -0,0 +1,17 @@
require 'rails_helper'
RSpec.describe Reminder, type: :model do
subject { build(:reminder) }
describe 'validations' do
%w[title body date].each do |attribute|
describe attribute do
it 'must be present' do
expect(subject).to be_valid
subject[attribute] = nil
expect(subject).to_not be_valid
end
end
end
end
end

View File

@ -61,4 +61,7 @@ RSpec.configure do |config|
config.filter_rails_from_backtrace! config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via: # arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name") # config.filter_gems_from_backtrace("gem name")
config.include FactoryBot::Syntax::Methods
config.include Devise::Test::IntegrationHelpers, type: :request
end end

View File

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe 'Reminders', type: :request do
describe 'GET /reminders' do
it 'checks redirection after login' do
get reminders_path
expect(response).to have_http_status(302)
user = build(:user)
sign_in user
get reminders_path
expect(response).to have_http_status(200)
end
end
end

View File

@ -0,0 +1,37 @@
require 'rails_helper'
RSpec.describe RemindersController, type: :routing do
describe 'routing' do
it 'routes to #index' do
expect(get: '/reminders').to route_to('reminders#index')
end
it 'routes to #new' do
expect(get: '/reminders/new').to route_to('reminders#new')
end
it 'routes to #show' do
expect(get: '/reminders/1').to route_to('reminders#show', id: '1')
end
it 'routes to #edit' do
expect(get: '/reminders/1/edit').to route_to('reminders#edit', id: '1')
end
it 'routes to #create' do
expect(post: '/reminders').to route_to('reminders#create')
end
it 'routes to #update via PUT' do
expect(put: '/reminders/1').to route_to('reminders#update', id: '1')
end
it 'routes to #update via PATCH' do
expect(patch: '/reminders/1').to route_to('reminders#update', id: '1')
end
it 'routes to #destroy' do
expect(delete: '/reminders/1').to route_to('reminders#destroy', id: '1')
end
end
end

View File

@ -0,0 +1,17 @@
require 'rails_helper'
RSpec.describe 'reminders/edit', type: :view do
before(:each) do
@reminder = assign(:reminder, create(:reminder))
end
it 'renders the edit reminder form' do
render
assert_select 'form[action=?][method=?]', reminder_path(@reminder), 'post' do
assert_select 'input[name=?]', 'reminder[title]'
assert_select 'textarea[name=?]', 'reminder[body]'
assert_select 'select[name=?]', 'reminder[date(1i)]'
end
end
end

View File

@ -0,0 +1,18 @@
require 'rails_helper'
RSpec.describe 'reminders/index', type: :view do
let(:user) { create :user }
before(:each) do
assign(:reminders, [
Reminder.create(title: 'aaa', body: 'bbb', date: '2020-02-20 02:02:02', user_id: user.id),
Reminder.create(title: 'aaa', body: 'bbb', date: '2020-02-20 02:02:02', user_id: user.id)
])
end
it 'renders a list of reminders' do
render
assert_select 'tr>td', text: 'aaa'.to_s, count: 2
assert_select 'tr>td', text: 'bbb'.to_s, count: 2
assert_select 'tr>td', text: '2020-02-20 02:02:02 UTC'.to_s, count: 2
end
end

View File

@ -0,0 +1,16 @@
require 'rails_helper'
RSpec.describe 'reminders/new', type: :view do
before(:each) do
assign(:reminder, build(:reminder))
end
it 'renders new reminder form' do
render
assert_select 'form[action=?][method=?]', reminders_path, 'post' do
assert_select 'input[name=?]', 'reminder[title]'
assert_select 'textarea[name=?]', 'reminder[body]'
end
end
end

View File

@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe 'reminders/show', type: :view do
before(:each) do
@reminder = create(:reminder)
end
it 'renders attributes in <p>' do
render
expect(rendered).to match(/TitleTestMsg/)
expect(rendered).to match(/BodyTestMsg/)
expect(rendered).to match(/2020-02-20 02:02:02/)
end
end