Skip to content

Commit b07dadf

Browse files
committed
Update seeds to use remote image URLs for production
1 parent fb092e6 commit b07dadf

1 file changed

Lines changed: 75 additions & 55 deletions

File tree

db/seeds.rb

Lines changed: 75 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,97 @@
1-
# This file should ensure the existence of records required to run the application in every environment (production,
2-
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
3-
# The data can then be loaded with the bin/rails db:seed command.
1+
# This file should contain all the record creation needed to seed the database with its default values.
2+
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
43

5-
# Create a dedicated user to own the templates
6-
template_user = User.find_or_create_by!(email: 'templates@example.com') do |user|
7-
user.password = SecureRandom.hex(16)
8-
user.username = 'TemplateUser'
9-
puts "Created template owner user."
4+
# Make sure you have the 'open-uri' library required at the top
5+
require 'open-uri'
6+
7+
# Clear existing records to prevent duplicates
8+
puts "Destroying all posts and users..."
9+
Post.destroy_all
10+
User.destroy_all
11+
12+
# Create a template owner user
13+
puts "Creating template owner user..."
14+
template_user = User.create!(
15+
username: 'template_owner',
16+
email: 'template@example.com',
17+
password: 'password',
18+
password_confirmation: 'password'
19+
)
20+
21+
# Function to attach a remote photo to a post
22+
def attach_remote_photo(post, url, filename)
23+
begin
24+
post.photo.attach(
25+
io: URI.open(url),
26+
filename: filename,
27+
content_type: 'image/jpeg' # Assuming jpeg, but this is often auto-detected
28+
)
29+
puts "Attached #{filename} to post '#{post.title}'"
30+
rescue OpenURI::HTTPError => e
31+
puts "Could not attach #{filename}. Error: #{e.message}"
32+
rescue => e
33+
puts "An unexpected error occurred while attaching #{filename}: #{e.message}"
34+
end
1035
end
1136

12-
# Define template posts
13-
template_posts = [
37+
# Array of posts with their corresponding image URLs
38+
posts_to_create = [
1439
{
15-
title: "Exploring the Serene Temples of Kyoto",
16-
content: "Discover the peace and beauty of ancient Japanese temples. This journey takes you through mossy gardens, towering pagodas, and tranquil koi ponds. A must-see for any traveler seeking a moment of calm.",
17-
image: "1.jpg"
40+
title: "Vibrant Abstract Dimensions",
41+
content: "Dive into a world where color knows no bounds. This piece explores the fluidity of digital art and the beauty of chaos.",
42+
image_url: "https://drive.google.com/uc?export=download&id=1zwgg2b10jjUWnMKlT8ssHlCtgWsnL_SJ",
43+
filename: "wallpaper-1.jpg"
1844
},
1945
{
20-
title: "Riding the Retro Wave",
21-
content: "A dive into the aesthetics of synthwave and retro-futurism. The iconic imagery of a setting sun, palm trees, and classic cars creates a powerful sense of nostalgia for a future that never was.",
22-
image: "2.jpg"
46+
title: "Cyberpunk Cityscape at Night",
47+
content: "A look into a neon-drenched metropolis of the future. The towering skyscrapers and bustling streets tell a story of technological marvels.",
48+
image_url: "https://drive.google.com/uc?export=download&id=1Pif1G2q6zZwcpR9rbyhe4yT5MRnDwj7k",
49+
filename: "wallpaper-2.jpg"
2350
},
2451
{
25-
title: "The Way of the Warrior",
26-
content: "A look into the discipline and artistry of the samurai. This post explores the history, philosophy, and enduring legacy of Japan's legendary warriors, whose code of honor continues to inspire.",
27-
image: "3.jpg"
52+
title: "Minimalist Design Philosophy",
53+
content: "Exploring the concept of 'less is more'. This post delves into how minimalism can lead to more impactful and clear designs.",
54+
image_url: "https://drive.google.com/uc?export=download&id=1kHMi0T9HPcwVP7YWEG68wIfnRe2PlhCY",
55+
filename: "wallpaper-3.png"
2856
},
2957
{
30-
title: "Neon Nights in a Cyberpunk City",
31-
content: "The vibrant, rain-slicked streets of a futuristic city come alive after dark. Towering skyscrapers, glowing advertisements, and hidden alleyways create a world of high-tech marvels and noir mysteries.",
32-
image: "4.jpg"
58+
title: "The Solitude of the Cosmos",
59+
content: "A visual journey through the silent, vast expenses of space. Witness distant galaxies, nebulae, and the quiet dance of celestial bodies.",
60+
image_url: "https://drive.google.com/uc?export=download&id=1HHlD7YFeRr20B1El6dzC8gUpbii7SuYR",
61+
filename: "wallpaper-4.jpg"
3362
},
3463
{
35-
title: "The Mark of the Akatsuki",
36-
content: "A symbol recognized by fans worldwide. The red cloud represents the rain of blood that fell in Amegakure during its wars, and serves as a reminder of the pain that drives this shadowy organization.",
37-
image: "5.jpg"
64+
title: "Architectural Wonders",
65+
content: "From ancient structures to modern marvels, a celebration of the human ingenuity behind the world's most stunning buildings.",
66+
image_url: "https://drive.google.com/uc?export=download&id=15xTFmgCn2RXZOOyuRjHxZTXrO2EYctUe",
67+
filename: "wallpaper-5.jpg"
3868
},
3969
{
40-
title: "The Anti-Coding Coding Club",
41-
content: "A tongue-in-cheek look at developer culture. Sometimes, the best code is the code you don't write. This post celebrates the art of simplicity, refactoring, and finding elegant solutions to complex problems.",
42-
image: "6.jpg"
70+
title: "Nature's Unseen Patterns",
71+
content: "A microscopic look at the intricate patterns found in nature, from the veins of a leaf to the structure of a snowflake.",
72+
image_url: "https://drive.google.com/uc?export=download&id=1pqn-mB_ErrXTl700cW91UmnsKlCasYpT",
73+
filename: "wallpaper-6.jpg"
4374
},
4475
{
45-
title: "A Journey into Abstract Patterns",
46-
content: "Exploring the mesmerizing world of generative art. These intricate, flowing patterns are created by algorithms, blending mathematics and creativity to produce something truly unique and beautiful.",
47-
image: "7.jpg"
76+
title: "The Art of Street Photography",
77+
content: "Capturing candid moments in the urban jungle. This collection showcases the raw, unfiltered stories of city life.",
78+
image_url: "https://drive.google.com/uc?export=download&id=1q0XdTuJrnaDbYAoyJDJehp2w5me97l2e",
79+
filename: "wallpaper-7.jpg"
4880
}
4981
]
5082

5183
puts "Seeding template posts..."
52-
53-
template_posts.each do |post_attrs|
54-
# Find existing template post or create a new one
55-
post = Post.find_or_initialize_by(title: post_attrs[:title], user: template_user)
56-
57-
# Only proceed if it's a new record to avoid re-attaching photos
58-
if post.new_record?
59-
post.content = post_attrs[:content]
60-
post.is_template = true
61-
62-
image_path = Rails.root.join('app', 'assets', 'images', 'seed_photos', post_attrs[:image])
63-
if File.exist?(image_path)
64-
post.photo.attach(
65-
io: File.open(image_path),
66-
filename: post_attrs[:image]
67-
)
68-
else
69-
puts "Warning: Image not found at #{image_path}"
70-
end
71-
72-
post.save!
73-
puts "Created template post: #{post.title}"
74-
end
84+
posts_to_create.each do |post_attrs|
85+
post = Post.new(
86+
title: post_attrs[:title],
87+
content: post_attrs[:content],
88+
user: template_user,
89+
is_template: true
90+
)
91+
92+
# The attachment must happen before save for validation, or handle it after
93+
post.save! # Save the post first to get an ID
94+
attach_remote_photo(post, post_attrs[:image_url], post_attrs[:filename])
7595
end
7696

77-
puts "Seeding complete."
97+
puts "Seeding complete. Created #{User.count} user and #{Post.count} posts."

0 commit comments

Comments
 (0)