Skip to content

Ci cd #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .github/workflows/ci.yml
Empty file.
18 changes: 18 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CommentsController < ApplicationController
def create
# Build a new comment from form inputs
new_comment = Comment.new
new_comment.photo_id = params[:comment_photo_id]
new_comment.author_id = params[:comment_author_id]
new_comment.body = params[:comment_body]

if new_comment.save
# On success, redirect to the photo’s show page
redirect_to "/photos/#{new_comment.photo_id}"
else
# If validations fail, redirect back or handle errors
flash[:alert] = new_comment.errors.full_messages.to_sentence
redirect_to "/photos/#{new_comment.photo_id}"
end
end
end
53 changes: 53 additions & 0 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# app/controllers/photos_controller.rb
class PhotosController < ApplicationController
def index
@photos = Photo.all
@photo = Photo.new # For the inline “Add Photo” form on index
end

def show
@photo = Photo.find(params[:id])
end

def new
@photo = Photo.new
end

def create
@photo = Photo.new(photo_params)
if @photo.save
# Redirect to the photo's show page (i.e., /photos/[PHOTO ID])
redirect_to photo_path(@photo)
else
@photos = Photo.all
render :index
end
end

def update
@photo = Photo.find(params[:id])
# Preserve the existing owner_id if it's not provided in the update form
update_params = photo_params
update_params[:owner_id] ||= @photo.owner_id

if @photo.update(update_params)
redirect_to photo_path(@photo)
else
Rails.logger.info "Update failed: #{@photo.errors.full_messages.join(', ')}"
render :show
end
end

def destroy
@photo = Photo.find(params[:id])
@photo.destroy
redirect_to photos_path
end

private

def photo_params
# Permit :image, :caption, and :owner_id.
params.require(:photo).permit(:image, :caption, :owner_id)
end
end
51 changes: 51 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
# Renders index.html.erb by default
end

def show
# Look up user by username since the test expects /users/[username]
@user = User.find_by!(username: params[:id])
end

def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
# Redirect to /users/[USERNAME] by explicitly building the URL using the username.
redirect_to "/users/#{@user.username}"
else
render :new
end
end

def edit
@user = User.find(params[:id])
end

def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to "/users/#{@user.username}"
else
render :edit
end
end

def destroy
user = User.find(params[:id])
user.destroy
redirect_to "/users"
end

private

def user_params
params.require(:user).permit(:username)
end
end
52 changes: 12 additions & 40 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,31 @@
#

class Photo < ApplicationRecord
validates(:poster, { :presence => true })
# Instead of validating the presence of the poster (which looks up a user by owner_id),
# we validate that an owner_id is provided.
validates :owner_id, presence: true

def poster
my_owner_id = self.owner_id

matching_users = User.where({ :id => my_owner_id })

the_user = matching_users.at(0)

return the_user
# Return the User record corresponding to owner_id (or nil if none exists)
User.find_by(id: owner_id)
end

def comments
my_id = self.id

matching_comments = Comment.where({ :photo_id => self.id })

return matching_comments
Comment.where(photo_id: id)
end

def likes
my_id = self.id

matching_likes = Like.where({ :photo_id => self.id })

return matching_likes
Like.where(photo_id: id)
end

def fans
my_likes = self.likes

array_of_user_ids = Array.new

my_likes.each do |a_like|
array_of_user_ids.push(a_like.fan_id)
end

matching_users = User.where({ :id => array_of_user_ids })

return matching_users
# Collect the fan_ids from likes and find the corresponding Users
user_ids = likes.pluck(:fan_id)
User.where(id: user_ids)
end

def fan_list
my_fans = self.fans

array_of_usernames = Array.new

my_fans.each do |a_user|
array_of_usernames.push(a_user.username)
end

formatted_usernames = array_of_usernames.to_sentence

return formatted_usernames
# Build a sentence of usernames from the fans list
fans.pluck(:username).to_sentence
end
end
12 changes: 7 additions & 5 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Photogram GUI</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<title>MyApp</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<!-- Rails 7 with import maps -->
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>
<body>

<%= yield %>
</body>
</html>
83 changes: 83 additions & 0 deletions app/views/photos/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!-- app/views/photos/index.html.erb -->

<h1>List of photos</h1>

<!-- Display errors if creating a photo fails (e.g., validations) -->
<% if @photo.errors.any? %>
<div style="color: red;">
<h2><%= pluralize(@photo.errors.count, "error") %> prevented this photo from being saved:</h2>
<ul>
<% @photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<!-- Horizontal form at the top to add a new photo -->
<%= form_with(model: @photo, url: photos_path, local: true) do |f| %>
<div>
<%= f.label :image, "Image", for: "photo_image" %><br>
<%= f.text_field :image, id: "photo_image", placeholder: "Enter a URL for the image..." %>
</div>
<div>
<%= f.label :caption, "Caption", for: "photo_caption" %><br>
<%= f.text_area :caption, id: "photo_caption", placeholder: "Enter a caption for the photo..." %>
</div>
<div>
<%= f.label :owner_id, "Owner ID", for: "photo_owner_id" %><br>
<%= f.text_field :owner_id, id: "photo_owner_id", placeholder: "Enter an ID of a User" %>
</div>
<div>
<%= button_tag "Add photo", type: "submit" %>
</div>
<% end %>

<!-- Table of existing photos -->
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Image</th>
<th>Caption</th>
<th>Owner</th>
<th>Posted</th>
<th></th> <!-- "Show details" link -->
</tr>
</thead>
<tbody>
<% @photos.each do |photo| %>
<tr>
<!-- Image column -->
<td>
<% if photo.image.present? %>
<% display_url = photo.image.to_s %>
<% unless display_url =~ /\Ahttps?:\/\//i %>
<% display_url = "http://" + display_url %>
<% end %>
<%= image_tag(display_url, skip_pipeline: true, width: 100, alt: "Photo") %>
<% else %>
(No image)
<% end %>
</td>
<!-- Caption column -->
<td><%= photo.caption %></td>
<!-- Owner column -->
<td>
<% if photo.respond_to?(:poster) && photo.poster.present? %>
<%= photo.poster.username %>
<% else %>
<%= photo.owner_id %>
<% end %>
</td>
<!-- Posted column -->
<td>
<%= photo.created_at.strftime("%Y-%m-%d %H:%M:%S UTC") %>
</td>
<!-- "Show details" link -->
<td>
<%= link_to "Show details", photo_path(photo) %>
</td>
</tr>
<% end %>
</tbody>
</table>
37 changes: 37 additions & 0 deletions app/views/photos/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!-- app/views/photos/new.html.erb -->

<h1>New Photo</h1>

<!-- Display validation errors, if any -->
<% if @photo.errors.any? %>
<div style="color: red;">
<h2><%= pluralize(@photo.errors.count, "error") %> prevented this photo from being saved:</h2>
<ul>
<% @photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_with(model: @photo, local: true) do |f| %>
<p>
<%= f.label :image, "Image URL" %><br>
<%= f.text_field :image %>
</p>

<p>
<%= f.label :caption, "Caption" %><br>
<%= f.text_field :caption %>
</p>

<p>
<%= f.label :owner_id, "Owner ID" %><br>
<%= f.number_field :owner_id %>
</p>

<p><%= f.submit "Create Photo" %></p>
<% end %>

<!-- Link back to the photos index page -->
<p><%= link_to "Back to Photos", photos_path %></p>
Loading