diff --git a/Gemfile b/Gemfile index 1bd5c98af..e3e5d8571 100644 --- a/Gemfile +++ b/Gemfile @@ -98,6 +98,7 @@ group :development do gem "rails-erd" gem "rufo" gem "specs_to_readme" + gem "web_git" end group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index c4571af47..d42785bf8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -83,6 +83,7 @@ GEM annotate (3.2.0) activerecord (>= 3.2, < 8.0) rake (>= 10.4, < 14.0) + ansispan (0.0.2) appdev_support (0.2.1) tabulo awesome_print (1.9.2) @@ -141,6 +142,7 @@ GEM responders warden (~> 1.2.3) diff-lcs (1.5.1) + diffy (3.4.3) domain_name (0.6.20240107) dotenv (3.1.2) draft_generators (0.0.4) @@ -164,6 +166,11 @@ GEM ffi-compiler (1.3.2) ffi (>= 1.15.5) rake + git (3.0.0) + activesupport (>= 5.0) + addressable (~> 2.8) + process_executer (~> 1.3) + rchardet (~> 1.9) globalid (1.2.1) activesupport (>= 6.1) grade_runner (0.0.12) @@ -233,6 +240,8 @@ GEM mini_mime (1.1.5) minitest (5.23.1) msgpack (1.7.2) + mustermann (3.0.3) + ruby2_keywords (~> 0.0.1) mutex_m (0.2.0) net-http (0.4.1) uri @@ -256,6 +265,7 @@ GEM oj (3.13.23) orm_adapter (0.5.0) pg (1.5.6) + process_executer (1.3.0) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -268,6 +278,10 @@ GEM nio4r (~> 2.0) racc (1.8.0) rack (3.0.11) + rack-protection (4.1.1) + base64 (>= 0.1.0) + logger (>= 1.6.0) + rack (>= 3.0.0, < 4) rack-session (2.0.0) rack (>= 3.0.0) rack-test (2.1.0) @@ -323,6 +337,7 @@ GEM activerecord (>= 6.1.5) activesupport (>= 6.1.5) i18n + rchardet (1.9.0) rdoc (6.7.0) psych (>= 4.0.0) redis (4.8.1) @@ -364,6 +379,7 @@ GEM ruby-vips (2.2.2) ffi (~> 1.12) logger + ruby2_keywords (0.0.5) rubyzip (2.3.2) rufo (0.18.0) sawyer (0.9.2) @@ -378,6 +394,13 @@ GEM simple_form (5.3.1) actionpack (>= 5.2) activemodel (>= 5.2) + sinatra (4.1.1) + logger (>= 1.6.0) + mustermann (~> 3.0) + rack (>= 3.0.0, < 4) + rack-protection (= 4.1.1) + rack-session (>= 2.0.0, < 3) + tilt (~> 2.0) specs_to_readme (0.1.0) sprockets (4.2.1) concurrent-ruby (~> 1.0) @@ -400,6 +423,7 @@ GEM terminal-table (3.0.2) unicode-display_width (>= 1.1.1, < 3) thor (1.3.1) + tilt (2.6.0) timeout (0.4.1) tty-screen (0.8.2) turbo-rails (2.0.5) @@ -408,6 +432,8 @@ GEM railties (>= 6.0.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + tzinfo-data (1.2025.1) + tzinfo (>= 1.0.0) unicode-display_width (2.5.0) uri (0.13.0) warden (1.2.9) @@ -417,6 +443,13 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) + web_git (0.1.0) + actionview + ansispan + diffy + git + sinatra + tzinfo-data webdrivers (5.3.1) nokogiri (~> 1.6) rubyzip (>= 1.3.0) @@ -482,6 +515,7 @@ DEPENDENCIES turbo-rails tzinfo-data web-console + web_git webdrivers webmock diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d12a..6b4dcfa85 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::Base + before_action :authenticate_user! end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 000000000..6cae78f6e --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,70 @@ +class CommentsController < ApplicationController + before_action :set_comment, only: %i[ show edit update destroy ] + + # GET /comments or /comments.json + def index + @comments = Comment.all + end + + # GET /comments/1 or /comments/1.json + def show + end + + # GET /comments/new + def new + @comment = Comment.new + end + + # GET /comments/1/edit + def edit + end + + # POST /comments or /comments.json + def create + @comment = Comment.new(comment_params) + + respond_to do |format| + if @comment.save + format.html { redirect_to comment_url(@comment), notice: "Comment was successfully created." } + format.json { render :show, status: :created, location: @comment } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /comments/1 or /comments/1.json + def update + respond_to do |format| + if @comment.update(comment_params) + format.html { redirect_to comment_url(@comment), notice: "Comment was successfully updated." } + format.json { render :show, status: :ok, location: @comment } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /comments/1 or /comments/1.json + def destroy + @comment.destroy! + + respond_to do |format| + format.html { redirect_to comments_url, notice: "Comment was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_comment + @comment = Comment.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def comment_params + params.require(:comment).permit(:author_id, :photo_id, :body) + end +end diff --git a/app/controllers/follow_requests_controller.rb b/app/controllers/follow_requests_controller.rb new file mode 100644 index 000000000..fcbdc78c4 --- /dev/null +++ b/app/controllers/follow_requests_controller.rb @@ -0,0 +1,70 @@ +class FollowRequestsController < ApplicationController + before_action :set_follow_request, only: %i[ show edit update destroy ] + + # GET /follow_requests or /follow_requests.json + def index + @follow_requests = FollowRequest.all + end + + # GET /follow_requests/1 or /follow_requests/1.json + def show + end + + # GET /follow_requests/new + def new + @follow_request = FollowRequest.new + end + + # GET /follow_requests/1/edit + def edit + end + + # POST /follow_requests or /follow_requests.json + def create + @follow_request = FollowRequest.new(follow_request_params) + + respond_to do |format| + if @follow_request.save + format.html { redirect_to follow_request_url(@follow_request), notice: "Follow request was successfully created." } + format.json { render :show, status: :created, location: @follow_request } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @follow_request.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /follow_requests/1 or /follow_requests/1.json + def update + respond_to do |format| + if @follow_request.update(follow_request_params) + format.html { redirect_to follow_request_url(@follow_request), notice: "Follow request was successfully updated." } + format.json { render :show, status: :ok, location: @follow_request } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @follow_request.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /follow_requests/1 or /follow_requests/1.json + def destroy + @follow_request.destroy! + + respond_to do |format| + format.html { redirect_to follow_requests_url, notice: "Follow request was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_follow_request + @follow_request = FollowRequest.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def follow_request_params + params.require(:follow_request).permit(:recipient_id, :sender_id, :status) + end +end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 000000000..9acbe1290 --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -0,0 +1,70 @@ +class LikesController < ApplicationController + before_action :set_like, only: %i[ show edit update destroy ] + + # GET /likes or /likes.json + def index + @likes = Like.all + end + + # GET /likes/1 or /likes/1.json + def show + end + + # GET /likes/new + def new + @like = Like.new + end + + # GET /likes/1/edit + def edit + end + + # POST /likes or /likes.json + def create + @like = Like.new(like_params) + + respond_to do |format| + if @like.save + format.html { redirect_to like_url(@like), notice: "Like was successfully created." } + format.json { render :show, status: :created, location: @like } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /likes/1 or /likes/1.json + def update + respond_to do |format| + if @like.update(like_params) + format.html { redirect_to like_url(@like), notice: "Like was successfully updated." } + format.json { render :show, status: :ok, location: @like } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /likes/1 or /likes/1.json + def destroy + @like.destroy! + + respond_to do |format| + format.html { redirect_to likes_url, notice: "Like was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_like + @like = Like.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def like_params + params.require(:like).permit(:fan_id, :photo_id) + end +end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb new file mode 100644 index 000000000..7b50a7997 --- /dev/null +++ b/app/controllers/photos_controller.rb @@ -0,0 +1,70 @@ +class PhotosController < ApplicationController + before_action :set_photo, only: %i[ show edit update destroy ] + + # GET /photos or /photos.json + def index + @photos = Photo.all + end + + # GET /photos/1 or /photos/1.json + def show + end + + # GET /photos/new + def new + @photo = Photo.new + end + + # GET /photos/1/edit + def edit + end + + # POST /photos or /photos.json + def create + @photo = Photo.new(photo_params) + + respond_to do |format| + if @photo.save + format.html { redirect_to photo_url(@photo), notice: "Photo was successfully created." } + format.json { render :show, status: :created, location: @photo } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @photo.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /photos/1 or /photos/1.json + def update + respond_to do |format| + if @photo.update(photo_params) + format.html { redirect_to photo_url(@photo), notice: "Photo was successfully updated." } + format.json { render :show, status: :ok, location: @photo } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @photo.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /photos/1 or /photos/1.json + def destroy + @photo.destroy! + + respond_to do |format| + format.html { redirect_to photos_url, notice: "Photo was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_photo + @photo = Photo.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def photo_params + params.require(:photo).permit(:image, :comments_count, :likes_count, :caption, :owner_id) + end +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 000000000..59b81cade --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,28 @@ +# == Schema Information +# +# Table name: comments +# +# id :bigint not null, primary key +# body :text not null +# created_at :datetime not null +# updated_at :datetime not null +# author_id :bigint not null +# photo_id :bigint not null +# +# Indexes +# +# index_comments_on_photo_id (photo_id) +# +# Foreign Keys +# +# fk_rails_... (author_id => users.id) +# fk_rails_... (photo_id => photos.id) +# +# app/models/comment.rb + +class Comment < ApplicationRecord + belongs_to :author, class_name: "User", counter_cache: true + belongs_to :photo, counter_cache: true + + validates :body, presence: true +end diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb new file mode 100644 index 000000000..d400204aa --- /dev/null +++ b/app/models/follow_request.rb @@ -0,0 +1,6 @@ +class FollowRequest < ApplicationRecord + belongs_to :recipient, class_name: "User" + belongs_to :sender, class_name: "User" + + enum status: { pending: "pending", rejected: "rejected", accepted: "accepted" } +end diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 000000000..d7a03b761 --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,24 @@ +# == Schema Information +# +# Table name: likes +# +# id :bigint not null, primary key +# created_at :datetime not null +# updated_at :datetime not null +# fan_id :bigint not null +# photo_id :bigint not null +# +# Indexes +# +# index_likes_on_fan_id (fan_id) +# index_likes_on_photo_id (photo_id) +# +# Foreign Keys +# +# fk_rails_... (fan_id => users.id) +# fk_rails_... (photo_id => photos.id) +# +class Like < ApplicationRecord + belongs_to :fan, class_name: "User", counter_cache: true + belongs_to :photo, counter_cache: true +end diff --git a/app/models/photo.rb b/app/models/photo.rb new file mode 100644 index 000000000..04661657c --- /dev/null +++ b/app/models/photo.rb @@ -0,0 +1,15 @@ +# app/models/photo.rb + +class Photo < ApplicationRecord + belongs_to :owner, class_name: "User", counter_cache: true + has_many :comments, dependent: :destroy + has_many :likes, dependent: :destroy + has_many :fans, through: :likes + + validates :caption, presence: true + validates :image, presence: true + + # Scopes + scope :past_week, -> { where(created_at: 1.week.ago..Time.current) } + scope :by_likes, -> { order(likes_count: :desc) } +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 000000000..34a61f549 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,30 @@ +class User < ApplicationRecord + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :validatable + + # Direct associations + has_many :comments, foreign_key: :author_id, dependent: :destroy + has_many :likes, foreign_key: :fan_id, dependent: :destroy + + # FollowRequest associations + has_many :sent_follow_requests, foreign_key: :sender_id, class_name: "FollowRequest", dependent: :destroy + has_many :received_follow_requests, foreign_key: :recipient_id, class_name: "FollowRequest", dependent: :destroy + has_many :accepted_sent_follow_requests, -> { where(status: "accepted") }, + foreign_key: :sender_id, class_name: "FollowRequest" + has_many :accepted_received_follow_requests, -> { where(status: "accepted") }, + foreign_key: :recipient_id, class_name: "FollowRequest" + + # Indirect associations + has_many :leaders, through: :accepted_sent_follow_requests, source: :recipient + has_many :followers, through: :accepted_received_follow_requests, source: :sender + has_many :liked_photos, through: :likes, source: :photo + + # **This is the missing association:** + has_many :own_photos, foreign_key: :owner_id, class_name: "Photo", dependent: :destroy + + # Additional indirect associations for feed and discover, if applicable + has_many :feed, through: :leaders, source: :own_photos + has_many :discover, through: :leaders, source: :liked_photos + + validates :username, presence: true, uniqueness: true +end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb new file mode 100644 index 000000000..e5aac46fe --- /dev/null +++ b/app/views/comments/_comment.html.erb @@ -0,0 +1,17 @@ +
+

+ Author: + <%= comment.author_id %> +

+ +

+ Photo: + <%= comment.photo_id %> +

+ +

+ Body: + <%= comment.body %> +

+ +
diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder new file mode 100644 index 000000000..64674aec1 --- /dev/null +++ b/app/views/comments/_comment.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! comment, :id, :author_id, :photo_id, :body, :created_at, :updated_at +json.url comment_url(comment, format: :json) diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb new file mode 100644 index 000000000..2bbce7acd --- /dev/null +++ b/app/views/comments/_form.html.erb @@ -0,0 +1,15 @@ + +<%= simple_form_for(@comment) do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +
+ <%= f.association :author %> + <%= f.association :photo %> + <%= f.input :body %> +
+ +
+ <%= f.button :submit %> +
+<% end %> diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb new file mode 100644 index 000000000..9720435b0 --- /dev/null +++ b/app/views/comments/edit.html.erb @@ -0,0 +1,10 @@ +

Editing comment

+ +<%= render "form", comment: @comment %> + +
+ +
+ <%= link_to "Show this comment", @comment %> | + <%= link_to "Back to comments", comments_path %> +
diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb new file mode 100644 index 000000000..c4935157a --- /dev/null +++ b/app/views/comments/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Comments

+ +
+ <% @comments.each do |comment| %> + <%= render comment %> +

+ <%= link_to "Show this comment", comment %> +

+ <% end %> +
+ +<%= link_to "New comment", new_comment_path %> diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder new file mode 100644 index 000000000..e3322af65 --- /dev/null +++ b/app/views/comments/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @comments, partial: "comments/comment", as: :comment diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 000000000..6ba6dd8a4 --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,9 @@ +

New comment

+ +<%= render "form", comment: @comment %> + +
+ +
+ <%= link_to "Back to comments", comments_path %> +
diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb new file mode 100644 index 000000000..b90af0a6f --- /dev/null +++ b/app/views/comments/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @comment %> + +
+ <%= link_to "Edit this comment", edit_comment_path(@comment) %> | + <%= link_to "Back to comments", comments_path %> + + <%= button_to "Destroy this comment", @comment, method: :delete %> +
diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder new file mode 100644 index 000000000..78a9099a3 --- /dev/null +++ b/app/views/comments/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "comments/comment", comment: @comment diff --git a/app/views/follow_requests/_follow_request.html.erb b/app/views/follow_requests/_follow_request.html.erb new file mode 100644 index 000000000..3b9f8c078 --- /dev/null +++ b/app/views/follow_requests/_follow_request.html.erb @@ -0,0 +1,17 @@ +
+

+ Recipient: + <%= follow_request.recipient_id %> +

+ +

+ Sender: + <%= follow_request.sender_id %> +

+ +

+ Status: + <%= follow_request.status %> +

+ +
diff --git a/app/views/follow_requests/_follow_request.json.jbuilder b/app/views/follow_requests/_follow_request.json.jbuilder new file mode 100644 index 000000000..d7dd54cc5 --- /dev/null +++ b/app/views/follow_requests/_follow_request.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! follow_request, :id, :recipient_id, :sender_id, :status, :created_at, :updated_at +json.url follow_request_url(follow_request, format: :json) diff --git a/app/views/follow_requests/_form.html.erb b/app/views/follow_requests/_form.html.erb new file mode 100644 index 000000000..2488a66af --- /dev/null +++ b/app/views/follow_requests/_form.html.erb @@ -0,0 +1,15 @@ + +<%= simple_form_for(@follow_request) do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +
+ <%= f.association :recipient %> + <%= f.association :sender %> + <%= f.input :status %> +
+ +
+ <%= f.button :submit %> +
+<% end %> diff --git a/app/views/follow_requests/edit.html.erb b/app/views/follow_requests/edit.html.erb new file mode 100644 index 000000000..7c42ec88d --- /dev/null +++ b/app/views/follow_requests/edit.html.erb @@ -0,0 +1,10 @@ +

Editing follow request

+ +<%= render "form", follow_request: @follow_request %> + +
+ +
+ <%= link_to "Show this follow request", @follow_request %> | + <%= link_to "Back to follow requests", follow_requests_path %> +
diff --git a/app/views/follow_requests/index.html.erb b/app/views/follow_requests/index.html.erb new file mode 100644 index 000000000..20fd53284 --- /dev/null +++ b/app/views/follow_requests/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Follow requests

+ +
+ <% @follow_requests.each do |follow_request| %> + <%= render follow_request %> +

+ <%= link_to "Show this follow request", follow_request %> +

+ <% end %> +
+ +<%= link_to "New follow request", new_follow_request_path %> diff --git a/app/views/follow_requests/index.json.jbuilder b/app/views/follow_requests/index.json.jbuilder new file mode 100644 index 000000000..b45255d0f --- /dev/null +++ b/app/views/follow_requests/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @follow_requests, partial: "follow_requests/follow_request", as: :follow_request diff --git a/app/views/follow_requests/new.html.erb b/app/views/follow_requests/new.html.erb new file mode 100644 index 000000000..f13075ef6 --- /dev/null +++ b/app/views/follow_requests/new.html.erb @@ -0,0 +1,9 @@ +

New follow request

+ +<%= render "form", follow_request: @follow_request %> + +
+ +
+ <%= link_to "Back to follow requests", follow_requests_path %> +
diff --git a/app/views/follow_requests/show.html.erb b/app/views/follow_requests/show.html.erb new file mode 100644 index 000000000..6db53cce2 --- /dev/null +++ b/app/views/follow_requests/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @follow_request %> + +
+ <%= link_to "Edit this follow request", edit_follow_request_path(@follow_request) %> | + <%= link_to "Back to follow requests", follow_requests_path %> + + <%= button_to "Destroy this follow request", @follow_request, method: :delete %> +
diff --git a/app/views/follow_requests/show.json.jbuilder b/app/views/follow_requests/show.json.jbuilder new file mode 100644 index 000000000..3fb15c16d --- /dev/null +++ b/app/views/follow_requests/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "follow_requests/follow_request", follow_request: @follow_request diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9a595c93a..d97e7a53a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,12 +5,27 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> - + + + <%= render partial: "shared/cdn_assets" %> + <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> <%= javascript_importmap_tags %> - <%= yield %> + <%= render partial: "shared/navbar" %> + +
+ <% if notice.present? %> + <%= render partial: "shared/flash", locals: { message: notice, css_class: "success" } %> + <% end %> + + <% if alert.present? %> + <%= render partial: "shared/flash", locals: { message: alert, css_class: "danger" } %> + <% end %> + + <%= yield %> +
diff --git a/app/views/likes/_form.html.erb b/app/views/likes/_form.html.erb new file mode 100644 index 000000000..3c5026035 --- /dev/null +++ b/app/views/likes/_form.html.erb @@ -0,0 +1,14 @@ + +<%= simple_form_for(@like) do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +
+ <%= f.association :fan %> + <%= f.association :photo %> +
+ +
+ <%= f.button :submit %> +
+<% end %> diff --git a/app/views/likes/_like.html.erb b/app/views/likes/_like.html.erb new file mode 100644 index 000000000..f10d1d01a --- /dev/null +++ b/app/views/likes/_like.html.erb @@ -0,0 +1,12 @@ +
+

+ Fan: + <%= like.fan_id %> +

+ +

+ Photo: + <%= like.photo_id %> +

+ +
diff --git a/app/views/likes/_like.json.jbuilder b/app/views/likes/_like.json.jbuilder new file mode 100644 index 000000000..f89aea4a0 --- /dev/null +++ b/app/views/likes/_like.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! like, :id, :fan_id, :photo_id, :created_at, :updated_at +json.url like_url(like, format: :json) diff --git a/app/views/likes/edit.html.erb b/app/views/likes/edit.html.erb new file mode 100644 index 000000000..39beff900 --- /dev/null +++ b/app/views/likes/edit.html.erb @@ -0,0 +1,10 @@ +

Editing like

+ +<%= render "form", like: @like %> + +
+ +
+ <%= link_to "Show this like", @like %> | + <%= link_to "Back to likes", likes_path %> +
diff --git a/app/views/likes/index.html.erb b/app/views/likes/index.html.erb new file mode 100644 index 000000000..3032ac648 --- /dev/null +++ b/app/views/likes/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Likes

+ +
+ <% @likes.each do |like| %> + <%= render like %> +

+ <%= link_to "Show this like", like %> +

+ <% end %> +
+ +<%= link_to "New like", new_like_path %> diff --git a/app/views/likes/index.json.jbuilder b/app/views/likes/index.json.jbuilder new file mode 100644 index 000000000..721d47627 --- /dev/null +++ b/app/views/likes/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @likes, partial: "likes/like", as: :like diff --git a/app/views/likes/new.html.erb b/app/views/likes/new.html.erb new file mode 100644 index 000000000..706f8f9d8 --- /dev/null +++ b/app/views/likes/new.html.erb @@ -0,0 +1,9 @@ +

New like

+ +<%= render "form", like: @like %> + +
+ +
+ <%= link_to "Back to likes", likes_path %> +
diff --git a/app/views/likes/show.html.erb b/app/views/likes/show.html.erb new file mode 100644 index 000000000..b635317e8 --- /dev/null +++ b/app/views/likes/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @like %> + +
+ <%= link_to "Edit this like", edit_like_path(@like) %> | + <%= link_to "Back to likes", likes_path %> + + <%= button_to "Destroy this like", @like, method: :delete %> +
diff --git a/app/views/likes/show.json.jbuilder b/app/views/likes/show.json.jbuilder new file mode 100644 index 000000000..587277f0d --- /dev/null +++ b/app/views/likes/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "likes/like", like: @like diff --git a/app/views/photos/_form.html.erb b/app/views/photos/_form.html.erb new file mode 100644 index 000000000..d58d4c2ae --- /dev/null +++ b/app/views/photos/_form.html.erb @@ -0,0 +1,17 @@ + +<%= simple_form_for(@photo) do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +
+ <%= f.input :image %> + <%= f.input :comments_count %> + <%= f.input :likes_count %> + <%= f.input :caption %> + <%= f.association :owner %> +
+ +
+ <%= f.button :submit %> +
+<% end %> diff --git a/app/views/photos/_photo.html.erb b/app/views/photos/_photo.html.erb new file mode 100644 index 000000000..163412972 --- /dev/null +++ b/app/views/photos/_photo.html.erb @@ -0,0 +1,27 @@ +
+

+ Image: + <%= photo.image %> +

+ +

+ Comments count: + <%= photo.comments_count %> +

+ +

+ Likes count: + <%= photo.likes_count %> +

+ +

+ Caption: + <%= photo.caption %> +

+ +

+ Owner: + <%= photo.owner_id %> +

+ +
diff --git a/app/views/photos/_photo.json.jbuilder b/app/views/photos/_photo.json.jbuilder new file mode 100644 index 000000000..fad93a0a9 --- /dev/null +++ b/app/views/photos/_photo.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! photo, :id, :image, :comments_count, :likes_count, :caption, :owner_id, :created_at, :updated_at +json.url photo_url(photo, format: :json) diff --git a/app/views/photos/edit.html.erb b/app/views/photos/edit.html.erb new file mode 100644 index 000000000..334e57176 --- /dev/null +++ b/app/views/photos/edit.html.erb @@ -0,0 +1,10 @@ +

Editing photo

+ +<%= render "form", photo: @photo %> + +
+ +
+ <%= link_to "Show this photo", @photo %> | + <%= link_to "Back to photos", photos_path %> +
diff --git a/app/views/photos/index.html.erb b/app/views/photos/index.html.erb new file mode 100644 index 000000000..127d6cf9d --- /dev/null +++ b/app/views/photos/index.html.erb @@ -0,0 +1,13 @@ + +

Photos

+ +
+ <% @photos.each do |photo| %> + <%= render photo %> +

+ <%= link_to "Show this photo", photo %> +

+ <% end %> +
+ +<%= link_to "New photo", new_photo_path %> diff --git a/app/views/photos/index.json.jbuilder b/app/views/photos/index.json.jbuilder new file mode 100644 index 000000000..24d84b804 --- /dev/null +++ b/app/views/photos/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @photos, partial: "photos/photo", as: :photo diff --git a/app/views/photos/new.html.erb b/app/views/photos/new.html.erb new file mode 100644 index 000000000..4649e7ad5 --- /dev/null +++ b/app/views/photos/new.html.erb @@ -0,0 +1,9 @@ +

New photo

+ +<%= render "form", photo: @photo %> + +
+ +
+ <%= link_to "Back to photos", photos_path %> +
diff --git a/app/views/photos/show.html.erb b/app/views/photos/show.html.erb new file mode 100644 index 000000000..2d4f05255 --- /dev/null +++ b/app/views/photos/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @photo %> + +
+ <%= link_to "Edit this photo", edit_photo_path(@photo) %> | + <%= link_to "Back to photos", photos_path %> + + <%= button_to "Destroy this photo", @photo, method: :delete %> +
diff --git a/app/views/photos/show.json.jbuilder b/app/views/photos/show.json.jbuilder new file mode 100644 index 000000000..5c0f6ffc9 --- /dev/null +++ b/app/views/photos/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "photos/photo", photo: @photo diff --git a/app/views/shared/_cdn_assets.html.erb b/app/views/shared/_cdn_assets.html.erb new file mode 100644 index 000000000..985bd7950 --- /dev/null +++ b/app/views/shared/_cdn_assets.html.erb @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/app/views/shared/_flash.html.erb b/app/views/shared/_flash.html.erb new file mode 100644 index 000000000..46cfd77c5 --- /dev/null +++ b/app/views/shared/_flash.html.erb @@ -0,0 +1,5 @@ + + diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb new file mode 100644 index 000000000..21bcdbc34 --- /dev/null +++ b/app/views/shared/_navbar.html.erb @@ -0,0 +1,50 @@ + + + diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb new file mode 100644 index 000000000..720e2ce3e --- /dev/null +++ b/config/initializers/devise.rb @@ -0,0 +1,313 @@ +# frozen_string_literal: true + +# Assuming you have not yet modified this file, each configuration option below +# is set to its default value. Note that some are commented out while others +# are not: uncommented lines are intended to protect your configuration from +# breaking changes in upgrades (i.e., in the event that future versions of +# Devise change the default values for those options). +# +# Use this hook to configure devise mailer, warden hooks and so forth. +# Many of these configuration options can be set straight in your model. +Devise.setup do |config| + # The secret key used by Devise. Devise uses this key to generate + # random tokens. Changing this key will render invalid all existing + # confirmation, reset password and unlock tokens in the database. + # Devise will use the `secret_key_base` as its `secret_key` + # by default. You can change it below and use your own secret key. + # config.secret_key = 'b3cb2d9868eee07c82562091a02dd29c3aa29a22bac4dda42b7588cb1072eb901a7c34cecdf34c2afb11628169b9b827cdaefab36117cf769ac310fe45e19774' + + # ==> Controller configuration + # Configure the parent class to the devise controllers. + # config.parent_controller = 'DeviseController' + + # ==> Mailer Configuration + # Configure the e-mail address which will be shown in Devise::Mailer, + # note that it will be overwritten if you use your own mailer class + # with default "from" parameter. + config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' + + # Configure the class responsible to send e-mails. + # config.mailer = 'Devise::Mailer' + + # Configure the parent class responsible to send e-mails. + # config.parent_mailer = 'ActionMailer::Base' + + # ==> ORM configuration + # Load and configure the ORM. Supports :active_record (default) and + # :mongoid (bson_ext recommended) by default. Other ORMs may be + # available as additional gems. + require 'devise/orm/active_record' + + # ==> Configuration for any authentication mechanism + # Configure which keys are used when authenticating a user. The default is + # just :email. You can configure it to use [:username, :subdomain], so for + # authenticating a user, both parameters are required. Remember that those + # parameters are used only when authenticating and not when retrieving from + # session. If you need permissions, you should implement that in a before filter. + # You can also supply a hash where the value is a boolean determining whether + # or not authentication should be aborted when the value is not present. + # config.authentication_keys = [:email] + + # Configure parameters from the request object used for authentication. Each entry + # given should be a request method and it will automatically be passed to the + # find_for_authentication method and considered in your model lookup. For instance, + # if you set :request_keys to [:subdomain], :subdomain will be used on authentication. + # The same considerations mentioned for authentication_keys also apply to request_keys. + # config.request_keys = [] + + # Configure which authentication keys should be case-insensitive. + # These keys will be downcased upon creating or modifying a user and when used + # to authenticate or find a user. Default is :email. + config.case_insensitive_keys = [:email] + + # Configure which authentication keys should have whitespace stripped. + # These keys will have whitespace before and after removed upon creating or + # modifying a user and when used to authenticate or find a user. Default is :email. + config.strip_whitespace_keys = [:email] + + # Tell if authentication through request.params is enabled. True by default. + # It can be set to an array that will enable params authentication only for the + # given strategies, for example, `config.params_authenticatable = [:database]` will + # enable it only for database (email + password) authentication. + # config.params_authenticatable = true + + # Tell if authentication through HTTP Auth is enabled. False by default. + # It can be set to an array that will enable http authentication only for the + # given strategies, for example, `config.http_authenticatable = [:database]` will + # enable it only for database authentication. + # For API-only applications to support authentication "out-of-the-box", you will likely want to + # enable this with :database unless you are using a custom strategy. + # The supported strategies are: + # :database = Support basic authentication with authentication key + password + # config.http_authenticatable = false + + # If 401 status code should be returned for AJAX requests. True by default. + # config.http_authenticatable_on_xhr = true + + # The realm used in Http Basic Authentication. 'Application' by default. + # config.http_authentication_realm = 'Application' + + # It will change confirmation, password recovery and other workflows + # to behave the same regardless if the e-mail provided was right or wrong. + # Does not affect registerable. + # config.paranoid = true + + # By default Devise will store the user in session. You can skip storage for + # particular strategies by setting this option. + # Notice that if you are skipping storage for all authentication paths, you + # may want to disable generating routes to Devise's sessions controller by + # passing skip: :sessions to `devise_for` in your config/routes.rb + config.skip_session_storage = [:http_auth] + + # By default, Devise cleans up the CSRF token on authentication to + # avoid CSRF token fixation attacks. This means that, when using AJAX + # requests for sign in and sign up, you need to get a new CSRF token + # from the server. You can disable this option at your own risk. + # config.clean_up_csrf_token_on_authentication = true + + # When false, Devise will not attempt to reload routes on eager load. + # This can reduce the time taken to boot the app but if your application + # requires the Devise mappings to be loaded during boot time the application + # won't boot properly. + # config.reload_routes = true + + # ==> Configuration for :database_authenticatable + # For bcrypt, this is the cost for hashing the password and defaults to 12. If + # using other algorithms, it sets how many times you want the password to be hashed. + # The number of stretches used for generating the hashed password are stored + # with the hashed password. This allows you to change the stretches without + # invalidating existing passwords. + # + # Limiting the stretches to just one in testing will increase the performance of + # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use + # a value less than 10 in other environments. Note that, for bcrypt (the default + # algorithm), the cost increases exponentially with the number of stretches (e.g. + # a value of 20 is already extremely slow: approx. 60 seconds for 1 calculation). + config.stretches = Rails.env.test? ? 1 : 12 + + # Set up a pepper to generate the hashed password. + # config.pepper = 'b24540a3b2cbd2a011c20770cda32a6bd52e898c567d67b9dd46619ff99761741b5e3e29a8c6fee5dbcadd0cb160d34397a473f60b47b7103b357d2cb5ce0444' + + # Send a notification to the original email when the user's email is changed. + # config.send_email_changed_notification = false + + # Send a notification email when the user's password is changed. + # config.send_password_change_notification = false + + # ==> Configuration for :confirmable + # A period that the user is allowed to access the website even without + # confirming their account. For instance, if set to 2.days, the user will be + # able to access the website for two days without confirming their account, + # access will be blocked just in the third day. + # You can also set it to nil, which will allow the user to access the website + # without confirming their account. + # Default is 0.days, meaning the user cannot access the website without + # confirming their account. + # config.allow_unconfirmed_access_for = 2.days + + # A period that the user is allowed to confirm their account before their + # token becomes invalid. For example, if set to 3.days, the user can confirm + # their account within 3 days after the mail was sent, but on the fourth day + # their account can't be confirmed with the token any more. + # Default is nil, meaning there is no restriction on how long a user can take + # before confirming their account. + # config.confirm_within = 3.days + + # If true, requires any email changes to be confirmed (exactly the same way as + # initial account confirmation) to be applied. Requires additional unconfirmed_email + # db field (see migrations). Until confirmed, new email is stored in + # unconfirmed_email column, and copied to email column on successful confirmation. + config.reconfirmable = true + + # Defines which key will be used when confirming an account + # config.confirmation_keys = [:email] + + # ==> Configuration for :rememberable + # The time the user will be remembered without asking for credentials again. + # config.remember_for = 2.weeks + + # Invalidates all the remember me tokens when the user signs out. + config.expire_all_remember_me_on_sign_out = true + + # If true, extends the user's remember period when remembered via cookie. + # config.extend_remember_period = false + + # Options to be passed to the created cookie. For instance, you can set + # secure: true in order to force SSL only cookies. + # config.rememberable_options = {} + + # ==> Configuration for :validatable + # Range for password length. + config.password_length = 6..128 + + # Email regex used to validate email formats. It simply asserts that + # one (and only one) @ exists in the given string. This is mainly + # to give user feedback and not to assert the e-mail validity. + config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ + + # ==> Configuration for :timeoutable + # The time you want to timeout the user session without activity. After this + # time the user will be asked for credentials again. Default is 30 minutes. + # config.timeout_in = 30.minutes + + # ==> Configuration for :lockable + # Defines which strategy will be used to lock an account. + # :failed_attempts = Locks an account after a number of failed attempts to sign in. + # :none = No lock strategy. You should handle locking by yourself. + # config.lock_strategy = :failed_attempts + + # Defines which key will be used when locking and unlocking an account + # config.unlock_keys = [:email] + + # Defines which strategy will be used to unlock an account. + # :email = Sends an unlock link to the user email + # :time = Re-enables login after a certain amount of time (see :unlock_in below) + # :both = Enables both strategies + # :none = No unlock strategy. You should handle unlocking by yourself. + # config.unlock_strategy = :both + + # Number of authentication tries before locking an account if lock_strategy + # is failed attempts. + # config.maximum_attempts = 20 + + # Time interval to unlock the account if :time is enabled as unlock_strategy. + # config.unlock_in = 1.hour + + # Warn on the last attempt before the account is locked. + # config.last_attempt_warning = true + + # ==> Configuration for :recoverable + # + # Defines which key will be used when recovering the password for an account + # config.reset_password_keys = [:email] + + # Time interval you can reset your password with a reset password key. + # Don't put a too small interval or your users won't have the time to + # change their passwords. + config.reset_password_within = 6.hours + + # When set to false, does not sign a user in automatically after their password is + # reset. Defaults to true, so a user is signed in automatically after a reset. + # config.sign_in_after_reset_password = true + + # ==> Configuration for :encryptable + # Allow you to use another hashing or encryption algorithm besides bcrypt (default). + # You can use :sha1, :sha512 or algorithms from others authentication tools as + # :clearance_sha1, :authlogic_sha512 (then you should set stretches above to 20 + # for default behavior) and :restful_authentication_sha1 (then you should set + # stretches to 10, and copy REST_AUTH_SITE_KEY to pepper). + # + # Require the `devise-encryptable` gem when using anything other than bcrypt + # config.encryptor = :sha512 + + # ==> Scopes configuration + # Turn scoped views on. Before rendering "sessions/new", it will first check for + # "users/sessions/new". It's turned off by default because it's slower if you + # are using only default views. + # config.scoped_views = false + + # Configure the default scope given to Warden. By default it's the first + # devise role declared in your routes (usually :user). + # config.default_scope = :user + + # Set this configuration to false if you want /users/sign_out to sign out + # only the current scope. By default, Devise signs out all scopes. + # config.sign_out_all_scopes = true + + # ==> Navigation configuration + # Lists the formats that should be treated as navigational. Formats like + # :html should redirect to the sign in page when the user does not have + # access, but formats like :xml or :json, should return 401. + # + # If you have any extra navigational formats, like :iphone or :mobile, you + # should add them to the navigational formats lists. + # + # The "*/*" below is required to match Internet Explorer requests. + # config.navigational_formats = ['*/*', :html, :turbo_stream] + + # The default HTTP method used to sign out a resource. Default is :delete. + config.sign_out_via = :delete + + # ==> OmniAuth + # Add a new OmniAuth provider. Check the wiki for more information on setting + # up on your models and hooks. + # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' + + # ==> Warden configuration + # If you want to use other strategies, that are not supported by Devise, or + # change the failure app, you can configure them inside the config.warden block. + # + # config.warden do |manager| + # manager.intercept_401 = false + # manager.default_strategies(scope: :user).unshift :some_external_strategy + # end + + # ==> Mountable engine configurations + # When using Devise inside an engine, let's call it `MyEngine`, and this engine + # is mountable, there are some extra configurations to be taken into account. + # The following options are available, assuming the engine is mounted as: + # + # mount MyEngine, at: '/my_engine' + # + # The router that invoked `devise_for`, in the example above, would be: + # config.router_name = :my_engine + # + # When using OmniAuth, Devise cannot automatically set OmniAuth path, + # so you need to do it manually. For the users scope, it would be: + # config.omniauth_path_prefix = '/my_engine/users/auth' + + # ==> Hotwire/Turbo configuration + # When using Devise with Hotwire/Turbo, the http status for error responses + # and some redirects must match the following. The default in Devise for existing + # apps is `200 OK` and `302 Found` respectively, but new apps are generated with + # these new defaults that match Hotwire/Turbo behavior. + # Note: These might become the new default in future versions of Devise. + config.responder.error_status = :unprocessable_entity + config.responder.redirect_status = :see_other + + # ==> Configuration for :registerable + + # When set to false, does not sign a user in automatically after their password is + # changed. Defaults to true, so a user is signed in automatically after changing a password. + # config.sign_in_after_change_password = true +end diff --git a/config/locales/devise.en.yml b/config/locales/devise.en.yml new file mode 100644 index 000000000..260e1c4ba --- /dev/null +++ b/config/locales/devise.en.yml @@ -0,0 +1,65 @@ +# Additional translations at https://github.com/heartcombo/devise/wiki/I18n + +en: + devise: + confirmations: + confirmed: "Your email address has been successfully confirmed." + send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes." + failure: + already_authenticated: "You are already signed in." + inactive: "Your account is not activated yet." + invalid: "Invalid %{authentication_keys} or password." + locked: "Your account is locked." + last_attempt: "You have one more attempt before your account is locked." + not_found_in_database: "Invalid %{authentication_keys} or password." + timeout: "Your session expired. Please sign in again to continue." + unauthenticated: "You need to sign in or sign up before continuing." + unconfirmed: "You have to confirm your email address before continuing." + mailer: + confirmation_instructions: + subject: "Confirmation instructions" + reset_password_instructions: + subject: "Reset password instructions" + unlock_instructions: + subject: "Unlock instructions" + email_changed: + subject: "Email Changed" + password_change: + subject: "Password Changed" + omniauth_callbacks: + failure: "Could not authenticate you from %{kind} because \"%{reason}\"." + success: "Successfully authenticated from %{kind} account." + passwords: + no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided." + send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes." + updated: "Your password has been changed successfully. You are now signed in." + updated_not_active: "Your password has been changed successfully." + registrations: + destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon." + signed_up: "Welcome! You have signed up successfully." + signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated." + signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked." + signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account." + update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirmation link to confirm your new email address." + updated: "Your account has been updated successfully." + updated_but_not_signed_in: "Your account has been updated successfully, but since your password was changed, you need to sign in again." + sessions: + signed_in: "Signed in successfully." + signed_out: "Signed out successfully." + already_signed_out: "Signed out successfully." + unlocks: + send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes." + send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes." + unlocked: "Your account has been unlocked successfully. Please sign in to continue." + errors: + messages: + already_confirmed: "was already confirmed, please try signing in" + confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" + expired: "has expired, please request a new one" + not_found: "not found" + not_locked: "was not locked" + not_saved: + one: "1 error prohibited this %{resource} from being saved:" + other: "%{count} errors prohibited this %{resource} from being saved:" diff --git a/config/routes.rb b/config/routes.rb index 262ffd547..e83228160 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,13 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + # Root route is set first + root "photos#index" - # Defines the root path route ("/") - # root "articles#index" + # Devise routes + devise_for :users + + # Resource routes (alphabetically ordered) + resources :comments + resources :follow_requests + resources :likes + resources :photos end diff --git a/db/migrate/20250314025416_devise_create_users.rb b/db/migrate/20250314025416_devise_create_users.rb new file mode 100644 index 000000000..7ea95452f --- /dev/null +++ b/db/migrate/20250314025416_devise_create_users.rb @@ -0,0 +1,34 @@ +# db/migrate/_devise_create_users.rb + +# frozen_string_literal: true + +class DeviseCreateUsers < ActiveRecord::Migration[7.1] + def change + create_table :users do |t| + ## Database authenticatable + t.string :email, null: false, default: "" + t.string :encrypted_password, null: false, default: "" + + ## Recoverable + t.string :reset_password_token + t.datetime :reset_password_sent_at + + ## Rememberable + t.datetime :remember_created_at + + ## [Optional Devise modules omitted for brevity] + + # Your custom columns + t.string :username + t.boolean :private, default: false + t.integer :likes_count, default: 0 + t.integer :comments_count, default: 0 + + t.timestamps null: false + end + + add_index :users, :email, unique: true + add_index :users, :reset_password_token, unique: true + add_index :users, :username, unique: true # <--- Added! + end +end diff --git a/db/migrate/20250314032943_create_photos.rb b/db/migrate/20250314032943_create_photos.rb new file mode 100644 index 000000000..3290dad8c --- /dev/null +++ b/db/migrate/20250314032943_create_photos.rb @@ -0,0 +1,13 @@ +class CreatePhotos < ActiveRecord::Migration[7.0] + def change + create_table :photos do |t| + t.string :image + t.integer :comments_count, default: 0 + t.integer :likes_count, default: 0 + t.text :caption + t.references :owner, null: false, foreign_key: { to_table: :users } + + t.timestamps + end + end +end diff --git a/db/migrate/20250314034312_create_comments.rb b/db/migrate/20250314034312_create_comments.rb new file mode 100644 index 000000000..a759f6462 --- /dev/null +++ b/db/migrate/20250314034312_create_comments.rb @@ -0,0 +1,13 @@ +# db/migrate/-create-comments.rb + +class CreateComments < ActiveRecord::Migration[7.0] + def change + create_table :comments do |t| + t.references :author, null: false, foreign_key: { to_table: :users }, index: false + t.references :photo, null: false, foreign_key: true + t.text :body, null: false + + t.timestamps + end + end +end diff --git a/db/migrate/20250314041326_create_follow_requests.rb b/db/migrate/20250314041326_create_follow_requests.rb new file mode 100644 index 000000000..048b8fa9e --- /dev/null +++ b/db/migrate/20250314041326_create_follow_requests.rb @@ -0,0 +1,12 @@ +# db/migrate/XXXXXXXXXXXX_create_follow_requests.rb +class CreateFollowRequests < ActiveRecord::Migration[7.0] + def change + create_table :follow_requests do |t| + t.references :recipient, null: false, foreign_key: { to_table: :users } + t.references :sender, null: false, foreign_key: { to_table: :users } + t.string :status, default: "pending" + + t.timestamps + end + end +end diff --git a/db/migrate/20250314041715_create_likes.rb b/db/migrate/20250314041715_create_likes.rb new file mode 100644 index 000000000..e75656e5a --- /dev/null +++ b/db/migrate/20250314041715_create_likes.rb @@ -0,0 +1,10 @@ +class CreateLikes < ActiveRecord::Migration[7.0] + def change + create_table :likes do |t| + t.references :fan, null: false, foreign_key: { to_table: :users } + t.references :photo, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20250314043604_add_counter_caches_to_users_and_photos.rb b/db/migrate/20250314043604_add_counter_caches_to_users_and_photos.rb new file mode 100644 index 000000000..8f7255168 --- /dev/null +++ b/db/migrate/20250314043604_add_counter_caches_to_users_and_photos.rb @@ -0,0 +1,9 @@ +class AddCounterCachesToUsersAndPhotos < ActiveRecord::Migration[7.0] + def change + # The following lines are commented out because the columns already exist: + # add_column :users, :comments_count, :integer, default: 0 + # add_column :users, :likes_count, :integer, default: 0 + # add_column :photos, :comments_count, :integer, default: 0 + # add_column :photos, :likes_count, :integer, default: 0 + end +end diff --git a/db/migrate/20250314043920_add_photos_count_to_users.rb b/db/migrate/20250314043920_add_photos_count_to_users.rb new file mode 100644 index 000000000..4b3501b81 --- /dev/null +++ b/db/migrate/20250314043920_add_photos_count_to_users.rb @@ -0,0 +1,5 @@ +class AddPhotosCountToUsers < ActiveRecord::Migration[7.1] + def change + add_column :users, :photos_count, :integer + end +end diff --git a/db/migrate/20250314044847_add_default_to_private.rb b/db/migrate/20250314044847_add_default_to_private.rb new file mode 100644 index 000000000..bed61220e --- /dev/null +++ b/db/migrate/20250314044847_add_default_to_private.rb @@ -0,0 +1,7 @@ +# db/migrate/XXXXXXXXXX_add_default_to_private.rb + +class AddDefaultToPrivate < ActiveRecord::Migration[7.0] + def change + change_column_default :users, :private, true + end +end diff --git a/db/schema.rb b/db/schema.rb index b783f9866..a4b8000f3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,8 +10,71 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 0) do +ActiveRecord::Schema[7.1].define(version: 2025_03_14_044847) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "comments", force: :cascade do |t| + t.bigint "author_id", null: false + t.bigint "photo_id", null: false + t.text "body", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["photo_id"], name: "index_comments_on_photo_id" + end + + create_table "follow_requests", force: :cascade do |t| + t.bigint "recipient_id", null: false + t.bigint "sender_id", null: false + t.string "status", default: "pending" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["recipient_id"], name: "index_follow_requests_on_recipient_id" + t.index ["sender_id"], name: "index_follow_requests_on_sender_id" + end + + create_table "likes", force: :cascade do |t| + t.bigint "fan_id", null: false + t.bigint "photo_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["fan_id"], name: "index_likes_on_fan_id" + t.index ["photo_id"], name: "index_likes_on_photo_id" + end + + create_table "photos", force: :cascade do |t| + t.string "image" + t.integer "comments_count", default: 0 + t.integer "likes_count", default: 0 + t.text "caption" + t.bigint "owner_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["owner_id"], name: "index_photos_on_owner_id" + end + + create_table "users", force: :cascade do |t| + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.string "username" + t.boolean "private", default: true + t.integer "likes_count" + t.integer "comments_count" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "photos_count" + t.index ["email"], name: "index_users_on_email", unique: true + t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + end + + add_foreign_key "comments", "photos" + add_foreign_key "comments", "users", column: "author_id" + add_foreign_key "follow_requests", "users", column: "recipient_id" + add_foreign_key "follow_requests", "users", column: "sender_id" + add_foreign_key "likes", "photos" + add_foreign_key "likes", "users", column: "fan_id" + add_foreign_key "photos", "users", column: "owner_id" end diff --git a/erd.png b/erd.png new file mode 100644 index 000000000..aed87e5da Binary files /dev/null and b/erd.png differ diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index ca4ae8ef4..ea7673e70 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -1,3 +1,93 @@ +# lib/tasks/dev.rake + desc "Fill the database tables with some sample data" -task({ :sample_data => :environment }) do +task sample_data: :environment do + p "Creating sample data..." + starting = Time.now + + if Rails.env.development? + FollowRequest.destroy_all + Comment.destroy_all + Like.destroy_all + Photo.destroy_all + User.destroy_all + end + + # Create an array of usernames with known values included + usernames = [] + usernames << "alice" + usernames << "bob" + # Add additional random usernames so total users equals 12 + (12 - 2).times do + usernames << Faker::Name.first_name.downcase + end + + usernames.each do |username| + User.create!( + email: "#{username}@example.com", + password: "password", + password_confirmation: "password", + username: username, + private: [true, false].sample + ) + end + + p "There are now #{User.count} users." + + users = User.all + + # Create follow requests between every pair of users (skipping self) + users.each do |first_user| + users.each do |second_user| + next if first_user == second_user + # With 75% chance, create a follow request from first_user to second_user + if rand < 0.75 + first_user.sent_follow_requests.create!( + recipient: second_user, + status: FollowRequest.statuses.keys.sample + ) + end + # And with 75% chance, create one in the reverse direction + if rand < 0.75 + second_user.sent_follow_requests.create!( + recipient: first_user, + status: FollowRequest.statuses.keys.sample + ) + end + end + end + + p "There are now #{FollowRequest.count} follow requests." + + # For each user, create 0-15 photos with random captions and images + users.each do |user| + rand(15).times do + photo = user.own_photos.create!( + caption: Faker::Quote.jack_handey, + image: "https://robohash.org/#{rand(9999)}" + ) + + # For each follower of the user, decide if they like or comment on the photo + user.followers.each do |follower| + # With 50% chance, add a like (only if they haven't liked it already) + if rand < 0.5 && !photo.fans.include?(follower) + photo.likes.create!(fan: follower) + end + + # With 25% chance, add a comment by the follower on this photo + if rand < 0.25 + photo.comments.create!( + body: Faker::Quote.jack_handey, + author: follower + ) + end + end + end + end + + p "There are now #{Photo.count} photos." + p "There are now #{Like.count} likes." + p "There are now #{Comment.count} comments." + + p "Sample data created in #{Time.now - starting} seconds." end