Skip to content

Commit 9367231

Browse files
authored
Merge pull request #169 from joyofrails/publish/color-schemes
Publish color schemes
2 parents a08589e + 342a18f commit 9367231

File tree

6 files changed

+79
-30
lines changed

6 files changed

+79
-30
lines changed

app/content/layouts/article.html.erb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<%= render "application/skip_to_content" %>
22
<%= render_layout "application" do %>
3-
<article>
3+
<article itemscope itemtype="http://schema.org/Article">
44
<%= render Pages::Header.new(
55
title: current_page.data.title!,
66
description: current_page.data.description,
7-
published_on: current_page.data.published&.to_date
7+
published_on: current_page.data.published&.to_date,
8+
updated_on: current_page.data.updated&.to_date
89
) %>
910
<%- if current_page.data.toc -%>
1011
<aside>
@@ -19,7 +20,7 @@
1920
</nav>
2021
</aside>
2122
<%- end -%>
22-
<div class="article-content container">
23+
<div class="article-content container" itemprop="articleBody">
2324
<%= yield %>
2425
</div>
2526
</article>

app/content/pages/about/index.html.mdrb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: Joy of Rails is a Rails application dedicated to teaching and demon
66
updated: 2024-07-15
77
---
88

9-
I’ll never forget that first day of school.
9+
I’ll never forget my first day of teaching.
1010

1111
I was young and inexperienced. As my first-period students filed in from the hallway, the reality of the situation began to settle in.
1212

@@ -16,31 +16,31 @@ _My students are going to see right through me_.
1616

1717
_I’m screwed._
1818

19-
As I closed the door, I surveyed the faces of my students. They were looking back at me eager to find out if I had any worth. Fear tightened its grip around my throat.
19+
I closed the door. I surveyed the faces of my students. They were looking back at me eager to find out if I had any worth. Fear tightened its grip around my throat.
2020

2121
I almost passed out.
2222

2323
My first job out of college was teaching Science in inner-city Houston, Texas. It was, and still is, the hardest job I’ve ever had.
2424

25-
On that first day of teaching, I was under-prepared. I less than ten years older than my 8th-grade students. I had completed less than three months of teacher training through the Teach for America program. Most of my students were first and second generation immigrants whose families hailed from Latin America. I didn’t speak any Spanish.
25+
When I started teaching, I was under-prepared. Less than ten years older than my 8th-grade students, I had only two months of teacher training through the Teach for America program. Most of my students were first and second generation immigrants whose families hailed from Latin America. I didn’t speak any Spanish.
2626

27-
_I_ was supposed to teach _them_.
27+
_I_ was supposed to teach _them_. Yeah, right.
2828

29-
Somehow, I survived my first period class and the others that followed that day. And then something magical happened.
29+
Somehow, I survived my first period class and the rest of the day. And then something magical happened.
3030

3131
Once the students had been dismissed, I was cleaning up in the an equipment closet, which I shared with the department chair, Ms Young, who taught next door. She came in and pointed to some boxes on the shelf.
3232

3333
_"The district gave me these boxes and I’m not sure what to make of all that. Why don’t you take ’em?"_
3434

3535
I didn’t know it at the time, but this moment changed my life.
3636

37-
These boxes were full of Legos. Not just any Legos—the (at the time) recently released [Lego Mindstorms](https://en.wikipedia.org/wiki/Lego_Mindstorms) kit. Basically Lego Mindstorms was what you’d get if Legos, Scratch, and Arduino made a baby.
37+
These boxes were full of Legos. Not just any Legos—these were [Lego Mindstorms](https://en.wikipedia.org/wiki/Lego_Mindstorms) kits. Basically Lego Mindstorms was what you’d get if Legos, Scratch, and Arduino made a baby.
3838

39-
_Damn right_. I took those boxes. I felt like a kid again. I thought I had been given Legos but the real gift was computer programming.
39+
_Cool_. I took those boxes. I felt like a kid again. I tore into the tutorials and taught myself just enough to pass on to my students. Later that month, I started an after school club. Later that year, we entered Robotic competitions. It was a lot of fun.
4040

41-
I tore into the tutorials and taught myself just enough to pass on to my students. I started an after school club. We entered Robotic competitions. It was a lot of fun.
41+
I thought I had been given Legos but the real gift was computer programming.
4242

43-
Looking back on these memories and my teaching experience, I remember it has being very challenging—emotionally, mentally, physically. But I also remember all the joy. Helping a student learn always brought me joy. How Miguel’s eyes lit up during a demonstration. The way Juana pumped her fist when she balanced a difficult chemistry equation. When our rocket project helped Jerry transform from my worst nightmare and to my biggest supporter.
43+
Looking back on these memories and my teaching experience, I remember it has being very challenging—emotionally, mentally, physically. But I also remember all the joy. Helping a student learn brought me so much. How Miguel’s eyes lit up during a demonstration. The way Juana pumped her fist when she balanced a difficult chemistry equation. When our rocket project helped Jerry transform from my worst nightmare and to my biggest supporter.
4444

4545
I often say my first day of teaching was also my first day as a programmer. In the [first article for this site](https://joyofrails.com/articles/introducing-joy-of-rails), I talked about the joy I felt when I experienced Ruby on Rails for the first time. Looking back, it seems likely I might never have encountered Ruby or Rails or even started a career in computers had it not been for my humble beginnings as a teacher and Ms. Young’s Lego Mindstorms kits. I may have left teaching for tech, but the teacher in me never left. Teaching and programming for me are inexorably linked.
4646

app/content/pages/articles/custom-color-schemes-with-ruby-on-rails.html.mdrb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: Ross Kaffenberger
44
layout: article
55
description: You can edit the color scheme of this website right in content of this blog post. Play with the controls while we highlight the benefits of Rails, Hotwire, and CSS variables.
66
summary: You can edit the color scheme of this website right in content of this blog post. Play with the controls while we highlight the benefits of Rails, Hotwire, and CSS variables.
7-
# published:
7+
published: 2024-07-15
88
image: articles/custom-color-schemes-with-ruby-on-rails/rainbows.jpg
99
uuid: d99f045b-f3f7-4408-811e-9701b1a13ce8
1010
tags:
@@ -31,7 +31,7 @@ Pretty cool, huh? Here are the key ingredients:
3131
- [Cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies): A session cookie to store your saved color scheme selection
3232
- [Hotwire](https://hotwired.dev/): Server-rendered HTML and a single-page app experience powered by Rails, [Turbo Drive](https://turbo.hotwired.dev/handbook/introduction#turbo-drive%3A-navigate-within-a-persistent-process), and [Turbo Frames](https://turbo.hotwired.dev/handbook/introduction#turbo-frames%3A-decompose-complex-pages)
3333

34-
I started with the premise of using a monochromatic color scheme based on the 11 color scale Tailwind uses for each of [its default color sets](https://tailwindcss.com/docs/customizing-colors). The curated options are all generated from [uicolors.app](https://uicolors.app/create).
34+
I started with the premise of using a monochromatic color scheme based on the eleven-item color scale [Tailwind](https://tailwindcss.com) uses for each of [its default color sets](https://tailwindcss.com/docs/customizing-colors). The curated options are all generated from [uicolors.app](https://uicolors.app/create).
3535

3636
Each color scheme is a row in the `color_schemes` table, with a name and CSS hex code values for each of the eleven weights.
3737

@@ -51,7 +51,7 @@ end
5151

5252
CSS variables make it easy to change repeated CSS in a lot of places. You can set a CSS variable with double dashes, `--`. The CSS variable can be accessed using the `var()` expression. CSS variables can be overridden and can be defined in terms of other variables.
5353

54-
Here’s a simplified a view of how I used CSS variables to define the main background color of the `<body>` element.
54+
Here’s a simplified a view of how I used CSS variables to define the main background color of the `<body>` element. Using the pseudo-class `:root` means the CSS variable can be accessed from any scope in CSS.
5555

5656
```css:{"show_header": false}
5757
:root {
@@ -66,9 +66,11 @@ body {
6666
}
6767
```
6868

69-
Even though Joy of Rails does not use Tailwind, this approach is consistent with [the Tailwind docs for using CSS variables to customize Tailwind colors](https://tailwindcss.com/docs/customizing-colors#using-css-variables).
69+
This approach is consistent with [the Tailwind docs for using CSS variables to customize Tailwind colors](https://tailwindcss.com/docs/customizing-colors#using-css-variables) for those of you that may be using (or interested in using) Tailwind.
7070

71-
When you click the "Save" button, the application stores the `id` of the chosen color scheme in your Rails session:
71+
Most of the buttons in the color scheme demo are connected to `<form>` elements with actions that point to the [`ColorSchemesController`](https://github.com/joyofrails/joyofrails.com/blob/a08589e1cbe2accf4a20713829df56533e31755e/app/controllers/settings/color_schemes_controller.rb) in the Joy of Rails application.
72+
73+
When you click the "Save" button, [the application stores the `id` of the chosen color scheme in your Rails session](https://github.com/joyofrails/joyofrails.com/blob/a08589e1cbe2accf4a20713829df56533e31755e/app/controllers/settings/color_schemes_controller.rb#L31):
7274

7375
```ruby:{"show_header":false}
7476
session[:color_scheme_id] = @color_scheme.id
@@ -77,12 +79,10 @@ session[:color_scheme_id] = @color_scheme.id
7779
When the page is rendered, the application checks for the presence of the session data to query for the desired color scheme:
7880

7981
```ruby:{"show_header":false}
80-
if session[:color_scheme_id]
81-
@color_scheme = ColorScheme.find(session[:color_scheme_id])
82-
end
82+
session[:color_scheme_id] && ColorScheme.find(session[:color_scheme_id])
8383
```
8484

85-
The color scheme CSS variables can be written directly into a style tag
85+
The color scheme CSS variables are written directly into a style tag:
8686

8787
```erb:{"filename":"app/view/layouts/application.html.erb"}
8888
<style>

app/controllers/concerns/color_scheming.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ def custom_color_scheme?
1717
end
1818

1919
def find_color_scheme
20-
preview_color_scheme || session_color_scheme || default_color_scheme
20+
@color_scheme ||= preview_color_scheme || session_color_scheme || default_color_scheme
2121
end
2222

2323
def preview_color_scheme_id = params.dig(:settings, :color_scheme_id)
2424

25-
def preview_color_scheme = preview_color_scheme_id && ColorScheme.find(preview_color_scheme_id)
26-
2725
def session_color_scheme_id = session[:color_scheme_id]
2826

29-
def session_color_scheme = session_color_scheme_id && ColorScheme.find(session_color_scheme_id)
27+
def preview_color_scheme = @preview_color_scheme ||= preview_color_scheme_id && ColorScheme.find(preview_color_scheme_id)
28+
29+
def session_color_scheme = @session_color_scheme ||= session_color_scheme_id && ColorScheme.find(session_color_scheme_id)
3030

3131
def default_color_scheme = @default_color_scheme ||= ColorScheme.cached_default
3232
end

app/views/components/pages/header.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ class Pages::Header < ApplicationComponent
44

55
attr_accessor :current_page
66

7-
def initialize(title: nil, description: nil, published_on: nil)
7+
def initialize(title: nil, description: nil, published_on: nil, updated_on: nil)
88
@title = title
99
@description = description
1010
@published_on = published_on
11+
@updated_on = updated_on
1112
end
1213

1314
def view_template
@@ -19,11 +20,27 @@ def view_template
1920
h1 { @title }
2021
end
2122
p(class: "description") { @description } if @description
22-
if @published_on
23+
if @published_on || @updated_on
2324
span(class: "block") do
24-
# <time datetime="2024-03-13T00:00:00Z" itemprop="datePublished" class="dt-published"> March 13th, 2024 </time>
25-
em do
26-
time_tag @published_on, itemprop: "datePublished", class: "dt-published"
25+
if @published_on && @updated_on
26+
plain "Published:"
27+
whitespace
28+
end
29+
if @published_on
30+
em do
31+
time_tag @published_on, itemprop: "datePublished", class: "dt-published"
32+
end
33+
end
34+
if @updated_on
35+
if @published_on
36+
plain " // "
37+
end
38+
plain "Updated:"
39+
whitespace
40+
41+
em do
42+
time_tag @updated_on, itemprop: "dateModified", class: "dt-modified"
43+
end
2744
end
2845
end
2946
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Pages::Header, type: :view do
4+
def render(**kwargs)
5+
described_class.new(**kwargs).call(view_context: view)
6+
end
7+
8+
describe "#call" do
9+
it "renders the header" do
10+
expect(render(title: "Hello")).to have_css(".page-header h1", text: "Hello")
11+
end
12+
13+
it "renders the description" do
14+
expect(render(description: "Describe me")).to have_css(".page-header p", text: "Describe me")
15+
end
16+
17+
it "renders the published date" do
18+
expect(render(published_on: Date.today)).to have_css("time.dt-published", text: Date.today.to_fs(:long))
19+
end
20+
21+
it "renders the updated date" do
22+
expect(render(updated_on: Date.today)).to have_css("time.dt-modified", text: Date.today.to_fs(:long))
23+
end
24+
25+
it "renders the published and updated date" do
26+
rendered = render(published_on: Date.yesterday, updated_on: Date.today)
27+
expect(rendered).to have_css("time.dt-published", text: Date.yesterday.to_fs(:long))
28+
expect(rendered).to have_css("time.dt-modified", text: Date.today.to_fs(:long))
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)