Skip to content

Commit e42d320

Browse files
committed
Initial Commit
1 parent 6b4424d commit e42d320

21 files changed

+829
-3
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ gem "kamal", require: false
3737
gem "thruster", require: false
3838

3939
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
40-
# gem "image_processing", "~> 1.2"
40+
gem "image_processing", "~> 1.2"
4141

4242
group :development, :test do
4343
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ GEM
114114
zeitwerk
115115
i18n (1.14.7)
116116
concurrent-ruby (~> 1.0)
117+
image_processing (1.14.0)
118+
mini_magick (>= 4.9.5, < 6)
119+
ruby-vips (>= 2.0.17, < 3)
117120
importmap-rails (2.1.0)
118121
actionpack (>= 6.0.0)
119122
activesupport (>= 6.0.0)
@@ -150,6 +153,9 @@ GEM
150153
net-pop
151154
net-smtp
152155
marcel (1.0.4)
156+
mini_magick (5.2.0)
157+
benchmark
158+
logger
153159
mini_mime (1.1.5)
154160
minitest (5.25.5)
155161
msgpack (1.8.0)
@@ -279,6 +285,9 @@ GEM
279285
rubocop-performance (>= 1.24)
280286
rubocop-rails (>= 2.30)
281287
ruby-progressbar (1.13.0)
288+
ruby-vips (2.2.3)
289+
ffi (~> 1.12)
290+
logger
282291
securerandom (0.4.1)
283292
solid_cable (3.0.7)
284293
actioncable (>= 7.2)
@@ -365,6 +374,7 @@ DEPENDENCIES
365374
brakeman
366375
debug
367376
hotwire-spark
377+
image_processing (~> 1.2)
368378
importmap-rails
369379
kamal
370380
propshaft

app/assets/stylesheets/actiontext.css

Lines changed: 440 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class PostsController < ApplicationController
2+
before_action :set_post, only: %i[ show edit update destroy ]
3+
4+
# GET /posts
5+
def index
6+
@posts = Post.published.order(published_at: :desc)
7+
end
8+
9+
# GET /posts/1
10+
def show
11+
end
12+
13+
# GET /posts/new
14+
def new
15+
@post = Post.new
16+
end
17+
18+
# GET /posts/1/edit
19+
def edit
20+
end
21+
22+
# POST /posts
23+
def create
24+
@post = Post.new(post_params)
25+
26+
if @post.save
27+
redirect_to @post, notice: "Post was successfully created."
28+
else
29+
render :new, status: :unprocessable_entity
30+
end
31+
end
32+
33+
# PATCH/PUT /posts/1
34+
def update
35+
if @post.update(post_params)
36+
redirect_to @post, notice: "Post was successfully updated.", status: :see_other
37+
else
38+
render :edit, status: :unprocessable_entity
39+
end
40+
end
41+
42+
# DELETE /posts/1
43+
def destroy
44+
@post.destroy!
45+
redirect_to posts_path, notice: "Post was successfully destroyed.", status: :see_other
46+
end
47+
48+
private
49+
# Use callbacks to share common setup or constraints between actions.
50+
def set_post
51+
@post = Post.find(params.expect(:id))
52+
end
53+
54+
# Only allow a list of trusted parameters through.
55+
def post_params
56+
params.expect(post: [ :title, :published_at, :content ])
57+
end
58+
end

app/javascript/application.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
22
import "@hotwired/turbo-rails"
33
import "controllers"
4+
5+
import "trix"
6+
import "@rails/actiontext"

app/models/post.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Post < ApplicationRecord
2+
has_rich_text :content
3+
4+
scope :published, -> { where(published_at: ..Time.current) }
5+
scope :scheduled, -> { where(published_at: Time.current..) }
6+
# scope :scheduled, -> { where("published_at > ?", Time.current) }
7+
scope :unpublished, -> { where(published_at: nil) }
8+
9+
def published?
10+
published_at.present? && published_at.past?
11+
end
12+
13+
def scheduled?
14+
published_at.present? && published_at.future?
15+
end
16+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
2+
<% if blob.representable? %>
3+
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
4+
<% end %>
5+
6+
<figcaption class="attachment__caption">
7+
<% if caption = blob.try(:caption) %>
8+
<%= caption %>
9+
<% else %>
10+
<span class="attachment__name"><%= blob.filename %></span>
11+
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
12+
<% end %>
13+
</figcaption>
14+
</figure>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="trix-content">
2+
<%= yield -%>
3+
</div>

app/views/posts/_form.html.erb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<%= form_with(model: post, class: "contents") do |form| %>
2+
<% if post.errors.any? %>
3+
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-md mt-3">
4+
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
5+
6+
<ul class="list-disc ml-6">
7+
<% post.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="my-5">
15+
<%= form.label :title %>
16+
<%= form.text_field :title, class: ["block shadow-sm rounded-md border px-3 py-2 mt-2 w-full", {"border-gray-400 focus:outline-blue-600": post.errors[:title].none?, "border-red-400 focus:outline-red-600": post.errors[:title].any?}] %>
17+
</div>
18+
19+
<div class="my-5 flex items-center gap-2">
20+
<%= form.label :published_at %>
21+
<%= form.datetime_field :published_at %>
22+
</div>
23+
24+
<div class="my-5">
25+
<%= form.label :content %>
26+
<%= form.rich_textarea :content, class: ["block shadow-sm rounded-md border px-3 py-2 mt-2 w-full", {"border-gray-400 focus:outline-blue-600": post.errors[:content].none?, "border-red-400 focus:outline-red-600": post.errors[:content].any?}] %>
27+
</div>
28+
29+
<div class="inline">
30+
<%= form.submit class: "w-full sm:w-auto rounded-md px-3.5 py-2.5 bg-blue-600 hover:bg-blue-500 text-white inline-block font-medium cursor-pointer" %>
31+
</div>
32+
<% end %>

app/views/posts/_post.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div id="<%= dom_id post %>" class="w-full sm:w-auto my-5 space-y-5">
2+
<h1 class="font-bold text-4xl"><%= post.title %></h1>
3+
4+
<div>
5+
<%= "Published" if post.published? %>
6+
<%= "Scheduled" if post.scheduled? %>
7+
<%= "Unpublished" if !post.scheduled? && !post.published? %>
8+
</div>
9+
10+
<div>
11+
<%= post.content %>
12+
</div>
13+
</div>

0 commit comments

Comments
 (0)