first commit
This commit is contained in:
4
app/assets/config/manifest.js
Normal file
4
app/assets/config/manifest.js
Normal file
@@ -0,0 +1,4 @@
|
||||
//= link_tree ../images
|
||||
//= link_directory ../stylesheets .css
|
||||
//= link_tree ../../javascript .js
|
||||
//= link_tree ../../../vendor/javascript .js
|
0
app/assets/images/.keep
Normal file
0
app/assets/images/.keep
Normal file
15
app/assets/stylesheets/application.css
Normal file
15
app/assets/stylesheets/application.css
Normal file
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
||||
* listed below.
|
||||
*
|
||||
* Any CSS (and SCSS, if configured) file within this directory, lib/assets/stylesheets, or any plugin's
|
||||
* vendor/assets/stylesheets directory can be referenced here using a relative path.
|
||||
*
|
||||
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
||||
* compiled file so the styles you add here take precedence over styles defined in any other CSS
|
||||
* 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_tree .
|
||||
*= require_self
|
||||
*/
|
4
app/channels/application_cable/channel.rb
Normal file
4
app/channels/application_cable/channel.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module ApplicationCable
|
||||
class Channel < ActionCable::Channel::Base
|
||||
end
|
||||
end
|
4
app/channels/application_cable/connection.rb
Normal file
4
app/channels/application_cable/connection.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module ApplicationCable
|
||||
class Connection < ActionCable::Connection::Base
|
||||
end
|
||||
end
|
2
app/controllers/application_controller.rb
Normal file
2
app/controllers/application_controller.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
end
|
51
app/controllers/articles_controller.rb
Normal file
51
app/controllers/articles_controller.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
class ArticlesController < ApplicationController
|
||||
http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
|
||||
|
||||
def index
|
||||
@articles = Article.all
|
||||
end
|
||||
|
||||
def show
|
||||
@article = Article.find(params[:id])
|
||||
end
|
||||
|
||||
def new
|
||||
@article = Article.new
|
||||
end
|
||||
|
||||
def create
|
||||
@article = Article.new(article_params)
|
||||
|
||||
if @article.save
|
||||
redirect_to @article
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
@article = Article.find(params[:id])
|
||||
end
|
||||
|
||||
def update
|
||||
@article = Article.find(params[:id])
|
||||
|
||||
if @article.update(article_params)
|
||||
redirect_to @article
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@article = Article.find(params[:id])
|
||||
@article.destroy
|
||||
|
||||
redirect_to root_path, status: :see_other
|
||||
end
|
||||
|
||||
private
|
||||
def article_params
|
||||
params.require(:article).permit(:title, :body, :status)
|
||||
end
|
||||
end
|
21
app/controllers/comments_controller.rb
Normal file
21
app/controllers/comments_controller.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class CommentsController < ApplicationController
|
||||
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
|
||||
|
||||
def create
|
||||
@article = Article.find(params[:article_id])
|
||||
@comment = @article.comments.create(comment_params)
|
||||
redirect_to article_path(@article)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@article = Article.find(params[:article_id])
|
||||
@comment = @article.comments.find(params[:id])
|
||||
comment.destroy
|
||||
redirect_to article_path(@article), status: :see_other
|
||||
end
|
||||
|
||||
private
|
||||
def comment_params
|
||||
params.require(:comment).permit(:commenter, :body, :status)
|
||||
end
|
||||
end
|
0
app/controllers/concerns/.keep
Normal file
0
app/controllers/concerns/.keep
Normal file
2
app/helpers/application_helper.rb
Normal file
2
app/helpers/application_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module ApplicationHelper
|
||||
end
|
2
app/helpers/articles_helper.rb
Normal file
2
app/helpers/articles_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module ArticlesHelper
|
||||
end
|
2
app/helpers/comments_helper.rb
Normal file
2
app/helpers/comments_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module CommentsHelper
|
||||
end
|
3
app/javascript/application.js
Normal file
3
app/javascript/application.js
Normal file
@@ -0,0 +1,3 @@
|
||||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
|
||||
import "@hotwired/turbo-rails"
|
||||
import "controllers"
|
9
app/javascript/controllers/application.js
Normal file
9
app/javascript/controllers/application.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Application } from "@hotwired/stimulus"
|
||||
|
||||
const application = Application.start()
|
||||
|
||||
// Configure Stimulus development experience
|
||||
application.debug = false
|
||||
window.Stimulus = application
|
||||
|
||||
export { application }
|
7
app/javascript/controllers/hello_controller.js
Normal file
7
app/javascript/controllers/hello_controller.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
connect() {
|
||||
this.element.textContent = "Hello World!"
|
||||
}
|
||||
}
|
11
app/javascript/controllers/index.js
Normal file
11
app/javascript/controllers/index.js
Normal file
@@ -0,0 +1,11 @@
|
||||
// Import and register all your controllers from the importmap under controllers/*
|
||||
|
||||
import { application } from "controllers/application"
|
||||
|
||||
// Eager load all controllers defined in the import map under controllers/**/*_controller
|
||||
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
|
||||
eagerLoadControllersFrom("controllers", application)
|
||||
|
||||
// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
|
||||
// import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
|
||||
// lazyLoadControllersFrom("controllers", application)
|
7
app/jobs/application_job.rb
Normal file
7
app/jobs/application_job.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class ApplicationJob < ActiveJob::Base
|
||||
# Automatically retry jobs that encountered a deadlock
|
||||
# retry_on ActiveRecord::Deadlocked
|
||||
|
||||
# Most jobs are safe to ignore if the underlying records are no longer available
|
||||
# discard_on ActiveJob::DeserializationError
|
||||
end
|
4
app/mailers/application_mailer.rb
Normal file
4
app/mailers/application_mailer.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class ApplicationMailer < ActionMailer::Base
|
||||
default from: "from@example.com"
|
||||
layout "mailer"
|
||||
end
|
3
app/models/application_record.rb
Normal file
3
app/models/application_record.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
primary_abstract_class
|
||||
end
|
8
app/models/article.rb
Normal file
8
app/models/article.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Article < ApplicationRecord
|
||||
include Visible
|
||||
|
||||
has_many :comments, dependent: :destroy
|
||||
|
||||
validates :title, presence: true
|
||||
validates :body, presence: true, length: {minimum: 10}
|
||||
end
|
5
app/models/comment.rb
Normal file
5
app/models/comment.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class Comment < ApplicationRecord
|
||||
include Visible
|
||||
|
||||
belongs_to :article
|
||||
end
|
0
app/models/concerns/.keep
Normal file
0
app/models/concerns/.keep
Normal file
19
app/models/concerns/visible.rb
Normal file
19
app/models/concerns/visible.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
module Visible
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
VALID_STATUSES = ['public', 'private', 'archived']
|
||||
|
||||
included do
|
||||
validates :status, inclusion: { in: VALID_STATUSES }
|
||||
end
|
||||
|
||||
class_methods do
|
||||
def public_count
|
||||
where(status: 'public').count
|
||||
end
|
||||
end
|
||||
|
||||
def archived?
|
||||
status == 'archived'
|
||||
end
|
||||
end
|
3
app/models/user.rb
Normal file
3
app/models/user.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
end
|
26
app/views/articles/_form.html.erb
Normal file
26
app/views/articles/_form.html.erb
Normal file
@@ -0,0 +1,26 @@
|
||||
<%= form_with model: article do |form| %>
|
||||
<div>
|
||||
<%= form.label :title %><br>
|
||||
<%= form.text_field :title %>
|
||||
<% article.errors.full_messages_for(:title).each do |message| %>
|
||||
<div><%= message %></div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :body %><br>
|
||||
<%= form.text_area :body %>
|
||||
<% article.errors.full_messages_for(:body).each do |message| %>
|
||||
<div><%= message %></div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :status %><br>
|
||||
<%= form.select :status, Visible::VALID_STATUSES, selected: article.status || 'public' %><br>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% end %>
|
3
app/views/articles/edit.html.erb
Normal file
3
app/views/articles/edit.html.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<h1>Edit Article</h1>
|
||||
|
||||
<%= render "form", article: @article %>
|
15
app/views/articles/index.html.erb
Normal file
15
app/views/articles/index.html.erb
Normal file
@@ -0,0 +1,15 @@
|
||||
<h1>Articles!</h1>
|
||||
|
||||
Our blog has <%= Article.public_count %> articles and counting!
|
||||
|
||||
<ul>
|
||||
<% @articles.each do |article| %>
|
||||
<% unless article.archived? %>
|
||||
<li>
|
||||
<%= link_to article.title, article %>
|
||||
</li>
|
||||
<%end%>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<%= link_to "New Article", new_article_path %>
|
3
app/views/articles/new.html.erb
Normal file
3
app/views/articles/new.html.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<h1>New Article</h1>
|
||||
|
||||
<%= render "form", article: @article %>
|
17
app/views/articles/show.html.erb
Normal file
17
app/views/articles/show.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<h1><%= @article.title %></h1>
|
||||
|
||||
<p><%= @article.body %></p>
|
||||
|
||||
<ul>
|
||||
<li><%= link_to "Edit", edit_article_path(@article) %></li>
|
||||
<li><%= link_to "Destroy", article_path(@article), data: {
|
||||
turbo_method: :delete,
|
||||
turbo_confirm: "Are you sure?"
|
||||
} %></li>
|
||||
</ul>
|
||||
|
||||
<h2>Comments</h2>
|
||||
<%= render @article.comments %>
|
||||
|
||||
<h2>Add a comment</h2>
|
||||
<%= render "comments/form" %>
|
17
app/views/comments/_comment.html.erb
Normal file
17
app/views/comments/_comment.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<% unless comment.archived? %>
|
||||
<p>
|
||||
<strong>Commenter:</strong>
|
||||
<%= comment.commenter %>
|
||||
</p>
|
||||
<p>
|
||||
<strong>Comment:</strong>
|
||||
<%= comment.body %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= link_to "Destroy Comment", [comment.article, comment], data: {
|
||||
turbo_method: :delete,
|
||||
turbo_confirm: "Are you sure?"
|
||||
} %>
|
||||
</p>
|
||||
<% end %>
|
17
app/views/comments/_form.html.erb
Normal file
17
app/views/comments/_form.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
|
||||
<p>
|
||||
<%= form.label :commenter %><br>
|
||||
<%= form.text_field :commenter %><br>
|
||||
</p>
|
||||
<p>
|
||||
<%= form.label :body %><br>
|
||||
<%= form.text_area :body %><br>
|
||||
</p>
|
||||
<p>
|
||||
<%= form.label :status %><br>
|
||||
<%= form.select :status, Visible::VALID_STATUSES, selected: 'public' %><br>
|
||||
</p>
|
||||
<p>
|
||||
<%= form.submit %><br>
|
||||
</p>
|
||||
<% end %>
|
16
app/views/layouts/application.html.erb
Normal file
16
app/views/layouts/application.html.erb
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Blog</title>
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<%= csrf_meta_tags %>
|
||||
<%= csp_meta_tag %>
|
||||
|
||||
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
|
||||
<%= javascript_importmap_tags %>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
13
app/views/layouts/mailer.html.erb
Normal file
13
app/views/layouts/mailer.html.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<style>
|
||||
/* Email styles need to be inline */
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
1
app/views/layouts/mailer.text.erb
Normal file
1
app/views/layouts/mailer.text.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= yield %>
|
Reference in New Issue
Block a user