Skip to content

Commit

Permalink
Merge pull request #10 from nickel/more-redesigning
Browse files Browse the repository at this point in the history
More redesigning
  • Loading branch information
nickel authored Apr 11, 2024
2 parents 5a58df8 + 5a8f881 commit b1cb841
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 13 deletions.
7 changes: 6 additions & 1 deletion app/assets/stylesheets/notes.css
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ul.note-actions, ul#notes {
}

ul.note-actions {
margin-bottom: 40px;
margin-bottom: 20px;
border: 1px solid #777;
padding: 20px;
background-color: #ccc;
Expand All @@ -76,3 +76,8 @@ ul#notes li {
padding-bottom: 10px;
margin-bottom: 10px;
}

ul#notes li h3 {
display: inline-block;
margin: 0;
}
11 changes: 8 additions & 3 deletions app/packages/notes/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ class NotesController < ApplicationController
before_action :require_user_if_private, only: %i(show)

def index
by_visibility = logged_in? ? "private" : "public"

@notes = Note::FindAll.call(
by_visibility: logged_in? ? "private" : "public",
by_tag: params[:tag]
).value!
by_visibility:, by_tag: params[:tag]
).value

@tags = Tag::FindAll.call(
by_visibility:
).value
end

def show
Expand Down
13 changes: 12 additions & 1 deletion app/packages/notes/models/note.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# frozen_string_literal: true

class Note < ApplicationRecord
def intro
return "" unless content.present?

content
.split("\n")
.first
end

def to_struct
CustomStruct.new(attributes)
CustomStruct.new(
attributes
.merge(intro:)
)
end

def to_param
Expand Down
2 changes: 1 addition & 1 deletion app/packages/notes/operations/note/find_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def call
def find_all
notes = by_visibility == "private" ? Note.all : Note.where(private: false)
notes = notes.where("? = ANY(tags)", by_tag) if by_tag.present?
notes.order(updated_at: :desc)
notes.order(created_at: :desc)
end
end
32 changes: 32 additions & 0 deletions app/packages/notes/operations/tag/find_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class Tag::FindAll < CommandHandler::Command
BY_VISIBILITY_FILTERS = Note::FindAll::BY_VISIBILITY_FILTERS

class Form
include CommandHandler::Form

attribute :by_visibility, :string, default: "public"

validates :by_visibility, inclusion: { in: BY_VISIBILITY_FILTERS }
end

delegate(*Form.new.attributes.keys, to: :form)

def call
Response.success(find_all)
end

private

def find_all
notes = Note.select(:tags)
notes = by_visibility == "private" ? notes : Note.where(private: false)

notes
.map(&:tags)
.flatten
.tally
.sort_by { |_, count| -count }
end
end
4 changes: 0 additions & 4 deletions app/packages/notes/views/notes/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,3 @@
<%= form.label :content, style: "display: block" %>
<%= form.text_area :content, rows: 45 %>
</div>

<div>
<%= form.submit "Crear" %>
</div>
7 changes: 5 additions & 2 deletions app/packages/notes/views/notes/_note.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div id="note-<%= note.id %>">
<h1>
<%= note.title %> <%= "🔒" if note.private? %>
<%= note.title %>
</h1>

<% if logged_in? %>
Expand All @@ -10,11 +10,14 @@
</ul>
<% end %>


<div>
Tags: <%= note.tags.map { |tag| link_to "##{tag}", notes_path(tag: tag) }.join(" ").html_safe %>
</div>

<% if note.private? %>
<small>This note is private 🔒</small>
<% end %>

<p>
<%= MarkdownRenderer.convert_markdown(note.content).html_safe %>
</p>
Expand Down
4 changes: 4 additions & 0 deletions app/packages/notes/views/notes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<%= form_for @note_form, url: note_path(@note.id), method: :put, html: { class: "max-w-sm mx-auto mt-20" } do |form| %>
<%= render "form", note: @note_form, form: form %>

<div>
<%= form.submit "Edit note" %>
</div>
<% end %>
15 changes: 14 additions & 1 deletion app/packages/notes/views/notes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@
</ul>
<% end %>

<p>
Browse by tag:

<%= @tags
.map { |tag, count| "#{link_to tag, notes_path(tag: tag)} #{count}" }
.join(" · ")
.html_safe %>
</p>

<h2>Recent notes</h2>

<ul id="notes">
<% @notes.each do |note| %>
<li>
<%= link_to note.title, note %>
<h3><%= link_to note.title, note %> - <%= note.created_at.strftime("%Y-%m-%d") %></h3>
<%= "🔒" if note.private? %>
<%= note.tags.map { |tag| link_to "##{tag}", notes_path(tag: tag) }.join(" ").html_safe %>

<p><%= truncate(note.intro, length: 200) %></p>
</li>
<% end %>
</ul>
4 changes: 4 additions & 0 deletions app/packages/notes/views/notes/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<%= form_for @note_form, url: notes_path, html: { class: "max-w-sm mx-auto mt-20" } do |form| %>
<%= render "form", note: @note_form, form: form %>

<div>
<%= form.submit "Create note" %>
</div>
<% end %>
18 changes: 18 additions & 0 deletions test/packages/notes/operations/tag/find_all_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "test_helper"

class Tag::FindAllTest < ActiveSupport::TestCase
test "tags are grouped" do
3.times { Factory.generate_note(private: false, tags: "foo, bar") }

response = Tag::FindAll.call

assert response.success?
assert_equal 2, response.value.length

response.value.to_h.each_value do |count|
assert_equal 3, count
end
end
end

0 comments on commit b1cb841

Please sign in to comment.