Skip to content

Commit 1c779c8

Browse files
authored
Merge pull request #128 from JudahSan/FT/learning_materials
2 parents f3ccc94 + 7910378 commit 1c779c8

File tree

10 files changed

+34
-30
lines changed

10 files changed

+34
-30
lines changed

app/models/learning_material.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@
2020
# index_learning_materials_on_title (title)
2121
#
2222
class LearningMaterial < ApplicationRecord
23-
# Map app-level attribute names to the current DB column names
24-
# DB columns (per schema.rb): thumbnail, link
25-
# App code expects: thumbnail_url, link_url
26-
alias_attribute :thumbnail_url, :thumbnail
27-
alias_attribute :link_url, :link
28-
2923
enum :level, { beginner: 0, intermediate: 1, expert: 2 }
3024

3125
validates :title, presence: true
3226
validates :level, presence: true
33-
validates :link_url, presence: true, format: URI::DEFAULT_PARSER.make_regexp(%w[http https])
34-
validates :thumbnail_url, allow_blank: true, format: URI::DEFAULT_PARSER.make_regexp(%w[http https])
27+
validates :link, presence: true, format: URI::DEFAULT_PARSER.make_regexp(%w[http https])
28+
validates :thumbnail, allow_blank: true, format: URI::DEFAULT_PARSER.make_regexp(%w[http https])
3529

3630
scope :featured, -> { where(featured: true) }
3731
scope :by_level, ->(lvl) { lvl.present? ? where(level: levels[lvl]) : all }

app/views/learning_materials/index.html.erb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-2 gap-4">
2828
<% @featured_materials.each do |m| %>
2929
<div class="card bg-base-100 shadow-md overflow-hidden w-full transition-transform duration-300 hover:-translate-y-1 hover:shadow-xl">
30-
<% if m.thumbnail_url.present? %>
30+
<% if m.thumbnail.present? %>
3131
<figure class="w-full aspect-[4/3] max-h-40 overflow-hidden">
32-
<img src="<%= m.thumbnail_url %>" alt="<%= m.title %> thumbnail" class="w-full h-full object-cover transition-transform duration-300 ease-out hover:scale-105">
32+
<img src="<%= m.thumbnail %>" alt="<%= m.title %> thumbnail" class="w-full h-full object-cover transition-transform duration-300 ease-out hover:scale-105">
3333
</figure>
3434
<% end %>
3535
<div class="card-body p-2">
3636
<span class="badge badge-secondary capitalize w-fit text-[10px]"><%= m.level %></span>
3737
<h3 class="card-title text-lg text-black mt-1"><%= m.title %></h3>
3838
<div class="card-actions justify-start mt-2">
39-
<%= link_to 'Open resource', m.link_url, class: 'link link-primary text-sm transition-colors duration-200 hover:underline', target: '_blank', rel: 'noopener' %>
39+
<%= link_to 'Open resource', m.link, class: 'link link-primary text-sm transition-colors duration-200 hover:underline', target: '_blank', rel: 'noopener' %>
4040
</div>
4141
</div>
4242
</div>
@@ -50,16 +50,16 @@
5050
<% @materials.each_with_index do |m, i| %>
5151
<div class="pb-6 border-b border-dotted border-gray-300 w-full max-w-md"> <!-- from sm → md -->
5252
<div class="card bg-base-100 shadow-md overflow-hidden w-full max-w-md transition-transform duration-300 hover:-translate-y-1 hover:shadow-xl"> <!-- slightly larger card -->
53-
<% if m.thumbnail_url.present? %>
53+
<% if m.thumbnail.present? %>
5454
<figure class="w-full aspect-video max-h-48 overflow-hidden"> <!-- a little taller thumbnail -->
55-
<img src="<%= m.thumbnail_url %>" alt="<%= m.title %> thumbnail" class="w-full h-full object-cover transition-transform duration-300 ease-out hover:scale-105">
55+
<img src="<%= m.thumbnail %>" alt="<%= m.title %> thumbnail" class="w-full h-full object-cover transition-transform duration-300 ease-out hover:scale-105">
5656
</figure>
5757
<% end %>
5858
<div class="card-body p-3 md:p-4"> <!-- slightly more breathing room -->
5959
<span class="badge badge-ghost capitalize w-fit text-xs md:text-sm"><%= m.level %></span>
6060
<h3 class="card-title text-base mt-1"><%= m.title %></h3>
6161
<div class="card-actions justify-start mt-2">
62-
<%= link_to 'Open resource', m.link_url, class: 'link link-primary text-sm transition-colors duration-200 hover:underline', target: '_blank', rel: 'noopener' %>
62+
<%= link_to 'Open resource', m.link, class: 'link link-primary text-sm transition-colors duration-200 hover:underline', target: '_blank', rel: 'noopener' %>
6363
</div>
6464
</div>
6565
</div>

config/motor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ resources:
1919
columns:
2020
- name: title
2121
- name: level
22-
- name: link_url
23-
- name: thumbnail_url
22+
- name: link
23+
- name: thumbnail
2424
- name: featured
2525
updated_at: 2025-09-10 22:08:00.000000000 +00:00
2626
configs:

db/migrate/20250907005500_create_learning_materials.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def change
99
create_table :learning_materials do |t|
1010
t.string :title, null: false
1111
t.integer :level, null: false, default: 0
12-
t.string :thumbnail_url
13-
t.string :link_url, null: false
12+
t.string :thumbnail
13+
t.string :link, null: false
1414
t.boolean :featured, null: false, default: false
1515
t.text :description
1616

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class RenameLearningMaterialsUrlColumns < ActiveRecord::Migration[7.2]
2+
def change
3+
if column_exists?(:learning_materials, :link_url)
4+
rename_column :learning_materials, :link_url, :link
5+
end
6+
7+
if column_exists?(:learning_materials, :thumbnail_url)
8+
rename_column :learning_materials, :thumbnail_url, :thumbnail
9+
end
10+
end
11+
end

db/schema.rb

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

db/seeds.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
if defined?(LearningMaterial)
88
LearningMaterial.find_or_create_by!(title: 'Rails Guides') do |lm|
99
lm.level = :beginner
10-
lm.thumbnail_url = 'https://rubyonrails.org/images/rails-logo.svg'
11-
lm.link_url = 'https://guides.rubyonrails.org/'
10+
lm.thumbnail = 'https://rubyonrails.org/images/rails-logo.svg'
11+
lm.link = 'https://guides.rubyonrails.org/'
1212
lm.featured = true
1313
lm.description = 'Official Rails Guides for beginners to experts.'
1414
end
1515

1616
LearningMaterial.find_or_create_by!(title: 'Ruby Official') do |lm|
1717
lm.level = :intermediate
18-
lm.thumbnail_url = 'https://www.ruby-lang.org/images/header-ruby-logo.png'
19-
lm.link_url = 'https://www.ruby-lang.org/en/documentation/'
18+
lm.thumbnail = 'https://www.ruby-lang.org/images/header-ruby-logo.png'
19+
lm.link = 'https://www.ruby-lang.org/en/documentation/'
2020
lm.featured = false
2121
lm.description = 'Official Ruby documentation and resources.'
2222
end

test/controllers/learning_materials_controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class LearningMaterialsControllerTest < ActionDispatch::IntegrationTest
4444
# LearningMaterial.create!(
4545
# title: "Material #{i}",
4646
# level: :beginner,
47-
# link_url: "https://example.com/#{i}"
47+
# link: "https://example.com/#{i}"
4848
# )
4949
# end
5050
#

test/models/learning_material_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class LearningMaterialTest < ActiveSupport::TestCase
3535
assert_not lm.valid?
3636
assert_includes lm.errors.attribute_names, :title
3737
assert_includes lm.errors.attribute_names, :level
38-
# model uses alias_attribute :link_url, :link
39-
assert_includes lm.errors.attribute_names, :link_url
38+
assert_includes lm.errors.attribute_names, :link
4039
end
4140

4241
test 'enum levels' do

test/system/learning_materials_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
class LearningMaterialsTest < ApplicationSystemTestCase
66
test 'view learning materials index with search and filter' do
7-
LearningMaterial.create!(title: 'Ruby Basics', level: :beginner, link_url: 'https://ruby-lang.org',
8-
thumbnail_url: 'https://example.com/a.png')
9-
LearningMaterial.create!(title: 'Rails Advanced', level: :expert, link_url: 'https://rubyonrails.org',
10-
thumbnail_url: 'https://example.com/b.png', featured: true)
7+
LearningMaterial.create!(title: 'Ruby Basics', level: :beginner, link: 'https://ruby-lang.org',
8+
thumbnail: 'https://example.com/a.png')
9+
LearningMaterial.create!(title: 'Rails Advanced', level: :expert, link: 'https://rubyonrails.org',
10+
thumbnail: 'https://example.com/b.png', featured: true)
1111

1212
visit learning_materials_path
1313

0 commit comments

Comments
 (0)