Skip to content

Commit

Permalink
Added image and slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dalezak committed Feb 7, 2022
1 parent b64cb5e commit 6586c62
Show file tree
Hide file tree
Showing 32 changed files with 276 additions and 74 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ node_modules/
/public/packs
/public/packs-test
/public/assets
/public/uploads

# Ignore yarn files
/yarn-error.log
Expand Down Expand Up @@ -88,4 +89,5 @@ yarn-debug.log*
*.crt
*.key
*.pem
*.py
*.py

8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ gem "faker", "~> 2.19"
gem "gems"

gem 'store_attribute'

gem "shrine", "~> 3.4"

gem "fastimage", "~> 2.2"

gem "aws-sdk-s3", "~> 1.112"

gem "image_processing", "~> 1.12"
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
aws-eventstream (1.2.0)
aws-partitions (1.553.0)
aws-sdk-core (3.126.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.525.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-kms (1.54.0)
aws-sdk-core (~> 3, >= 3.126.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.112.0)
aws-sdk-core (~> 3, >= 3.126.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.4)
aws-sigv4 (1.4.0)
aws-eventstream (~> 1, >= 1.0.2)
bcrypt (3.1.16)
bindex (0.8.1)
bootsnap (1.9.4)
Expand All @@ -88,6 +104,7 @@ GEM
xpath (~> 3.2)
childprocess (4.1.0)
concurrent-ruby (1.1.9)
content_disposition (1.0.0)
crass (1.0.6)
cssbundling-rails (1.0.0)
railties (>= 6.0.0)
Expand All @@ -105,6 +122,8 @@ GEM
actionmailer (>= 5.0)
devise (>= 4.6)
digest (3.1.0)
down (5.2.4)
addressable (~> 2.8)
erubi (1.10.0)
faker (2.19.0)
i18n (>= 1.6, < 2)
Expand All @@ -131,19 +150,25 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
fastimage (2.2.6)
ffi (1.15.5)
gems (1.2.0)
globalid (1.0.0)
activesupport (>= 5.0)
hashie (5.0.0)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
image_processing (1.12.1)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
io-console (0.5.11)
io-wait (0.2.1)
irb (1.4.1)
reline (>= 0.3.0)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jmespath (1.5.0)
jsbundling-rails (1.0.0)
railties (>= 6.0.0)
jwt (2.3.0)
Expand All @@ -155,6 +180,7 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_magick (4.11.0)
mini_mime (1.1.2)
minitest (5.15.0)
msgpack (1.4.2)
Expand Down Expand Up @@ -241,12 +267,17 @@ GEM
actionpack (>= 5.0)
railties (>= 5.0)
rexml (3.2.5)
ruby-vips (2.1.4)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
selenium-webdriver (4.1.0)
childprocess (>= 0.5, < 5.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2)
shrine (3.4.0)
content_disposition (~> 1.0)
down (~> 5.1)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -288,6 +319,7 @@ PLATFORMS
arm64-darwin-20

DEPENDENCIES
aws-sdk-s3 (~> 1.112)
bootsnap
bootstrap_form (~> 5.0)
cancancan (~> 3.3)
Expand All @@ -298,7 +330,9 @@ DEPENDENCIES
devise-bootstrap5 (~> 0.1.3)
devise_invitable (~> 2.0)
faker (~> 2.19)
fastimage (~> 2.2)
gems
image_processing (~> 1.12)
jbuilder
jsbundling-rails
omniauth (~> 2.0)
Expand All @@ -308,6 +342,7 @@ DEPENDENCIES
rails (~> 7.0.1)
redis (~> 4.0)
selenium-webdriver
shrine (~> 3.4)
sprockets-rails
stimulus-rails
store_attribute
Expand Down
2 changes: 1 addition & 1 deletion app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//= link_tree ../images
//= link_tree ../builds
//= link_tree ../builds
Empty file removed app/assets/images/.keep
Empty file.
Binary file added app/assets/images/blank.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/user/user-100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/user/user-25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/user/user-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/user/user-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/user/user-50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/user/user-72.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,22 @@
&.fade {
opacity: 0;
}
}

img {
&.img-50 {
width: 50px;
}

&.img-72 {
width: 72px;
}

&.img-100 {
width: 100px;
}

&.img-150 {
width: 150px;
}
}
9 changes: 9 additions & 0 deletions app/controllers/gems_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ def index

def show
authorize! :show, @gem
@offset = params.fetch(:offset, 0).to_i
@limit = [params.fetch(:limit, 24).to_i, 48].min
@users = @gem.users.limit(@limit).offset(@offset).order(created_at: :asc).all
@users_count = @gem.likes_count
respond_to do |format|
format.html { }
format.json { }
format.turbo_stream { }
end
end

def new
Expand Down
12 changes: 12 additions & 0 deletions app/models/concerns/imageable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Imageable
extend ActiveSupport::Concern

included do
before_save :image_resize
end

def image_resize
self.image_derivatives! if self.image_changed?
end

end
35 changes: 35 additions & 0 deletions app/models/concerns/slugable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Slugable
extend ActiveSupport::Concern

included do
before_validation :set_slug

def self.with_slug(id_or_slug)
where(id: id_or_slug).or(where(slug: id_or_slug.to_s))
end

def self.find(id_or_slug)
where(id: id_or_slug).or(where(slug: id_or_slug.to_s)).first
end

def self.find!(id_or_slug)
where(id: id_or_slug).or(where(slug: id_or_slug.to_s)).first!
end
end

def to_param
self.slug.presence || self.id
end

def set_slug
if self.slug.blank?
prefix = (self.try(:name) || self.try(:title) || SecureRandom.hex).parameterize
suffix = nil
while self.class.where(slug: [prefix, suffix].compact.join('-')).count.positive? do
suffix = suffix.to_i + 1
end
self.slug = [prefix, suffix].compact.join('-')
end
end

end
26 changes: 26 additions & 0 deletions app/models/gems/gemm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Gemm < ApplicationRecord
self.table_name = "gems"

include ::Typeable
include ::Slugable

store_attribute :details, :homepage_uri, :string
store_attribute :details, :project_uri, :string
Expand All @@ -21,6 +22,31 @@ class Gemm < ApplicationRecord

def type_name
"Gem"
end

def self.import(data)
name = (data["name"] || data["full_name"]).gsub("-#{data['number']}", "")
gem = Gemm.find_or_initialize_by(slug: name.parameterize)
gem.name = name
gem.title = data["info"] || data["summary"]
gem.description = data["description"]
gem.version = data["version"]
gem.platform = data["platform"]
gem.downloads_count = data["downloads"].to_i
gem.authors = data["authors"].split(", ")
gem.licenses = data["licenses"]
gem.homepage_uri = data["homepage_uri"]
gem.project_uri = data["project_uri"]
gem.funding_uri = data["funding_uri"]
gem.wiki_uri = data["wiki_uri"]
gem.source_code_uri = data["source_code_uri"]
gem.documentation_uri = data["documentation_uri"]
gem.bug_tracker_uri = data["bug_tracker_uri"]
gem.mailing_list_uri = data["mailing_list_uri"]
gem.built_at = data["built_at"] if data["built_at"].present?
gem.created_at = data["created_at"] if data["created_at"].present?
gem.updated_at = data["updated_at"] if data["updated_at"].present?
gem.save
end

end
7 changes: 6 additions & 1 deletion app/models/users/user.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
class User < ApplicationRecord

include ::Typeable
include ::Slugable
include ::Imageable
include ImageUploader::Attachment(:image)

# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :invitable, :database_authenticatable, :omniauthable, :registerable, :recoverable, :rememberable, :validatable

# store_attribute :details, :extra, :string
store_attribute :details, :homepage_uri, :string
store_attribute :details, :github_uri, :string
store_attribute :details, :twitter_uri, :string

has_many :identities, dependent: :destroy

Expand Down
27 changes: 27 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'image_processing/mini_magick'
require 'fastimage'

class ImageUploader < Shrine
plugin :derivatives
plugin :pretty_location
plugin :store_dimensions
plugin :validation_helpers
plugin :determine_mime_type

Attacher.validate do
validate_mime_type %w[image/jpeg image/png]
validate_extension %w[jpg jpeg png]
validate_max_size 10*1024*1024
end

Attacher.derivatives do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
thumb: magick.resize_to_limit!(100, 100),
small: magick.resize_to_limit!(300, 300),
medium: magick.resize_to_limit!(600, 600),
large: magick.resize_to_limit!(1200, 1200),
}
end

end
2 changes: 1 addition & 1 deletion app/views/gems/_gem.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="card h-100">
<div class="card-body">
<h5 class="card-title"><%= link_to gem.name, gem_path(gem), data: { turbo_frame: "_top" } %></h5>
<p class="card-text"><%= gem.title %></p>
<p class="card-text"><%= gem.title.to_s.truncate(150) %></p>
</div>
<div class="card-footer">
<% if user_signed_in? %>
Expand Down
10 changes: 9 additions & 1 deletion app/views/gems/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@
<% end %>
<span class="badge bg-secondary"><%= @gem.version %></span>
</div>
</div>
</div>

<%= turbo_frame_tag "users" do %>
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 row-cols-xl-4 g-4">
<%= render partial: '/users/user', collection: @users, as: :user %>
<div id="user_cards"></div>
</div>
<%= render partial: "/partials/load_more", locals: { total: @users.length, count: @users_count, offset: @offset + @limit } %>
<% end %>
7 changes: 7 additions & 0 deletions app/views/gems/show.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= turbo_stream.replace "user_cards" do %>
<%= render partial: '/users/user', collection: @users, as: :user %>
<div id="user_cards"></div>
<% end %>
<%= turbo_stream.replace "load_more" do %>
<%= render partial: "/partials/load_more", locals: { total: @users.length, count: @users_count, offset: @offset + @limit } %>
<% end %>
6 changes: 5 additions & 1 deletion app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
<%= turbo_frame_tag user do %>
<div class="card h-100">
<div class="card-body">
<div class="ms-2 float-end">
<%= image_tag user.image_url, alt: user.name, class: 'img-72 rounded-circle', onerror: "this.src='/assets/user/user-72.png'" if user.image.present? && user.image.exists? %>
<%= image_tag 'user/user-72.png', class: 'img-72 rounded-circle', alt: user.name if user.image.nil? %>
</div>
<h5 class="card-title"><%= link_to user.name, user, data: { turbo_frame: "_top" } %></h5>
<p class="card-text"><%= user.title || user.description %></p>
<p class="card-text"><%= (user.title || user.description).to_s.truncate(150) %></p>
</div>
<div class="card-footer">
<small class="text-muted"><%= pluralize(user.likes_count, "like") %></small>
Expand Down
13 changes: 9 additions & 4 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

<div class="card mb-4">
<div class="card-body">
<div class="ms-2 float-end">
<%= image_tag @user.image_url, alt: @user.name, class: 'img-100 rounded-circle', onerror: "this.src='/assets/user/user-100.png'" if @user.image.present? && @user.image.exists? %>
<%= image_tag 'user/user-100.png', class: 'img-100 rounded-circle', alt: @user.name if @user.image.nil? %>
</div>
<h1 class="card-title"><%= @user.name %></h1>
<h6 class="card-subtitle mb-2 text-muted"><%= @user.title %></h6>
<p class="card-text"><%= @user.description %></p>
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
<%= tag.h5 @user.title, class: "card-subtitle mb-2 text-muted" if @user.title.present? %>
<%= tag.p @user.description, class: "card-text" if @user.description.present? %>
<%= link_to "Homepage", @user.homepage_uri, class: "card-link", target: "_blank" if @user.homepage_uri.present? %>
<%= link_to "Github", @user.github_uri, class: "card-link", target: "_blank" if @user.github_uri.present? %>
<%= link_to "Twitter", @user.twitter_uri, class: "card-link", target: "_blank" if @user.twitter_uri.present? %>
</div>
</div>

Expand Down
Loading

0 comments on commit 6586c62

Please sign in to comment.