Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
secretpray authored and secretpray committed Dec 28, 2021
1 parent 391efc4 commit 52ca3d6
Show file tree
Hide file tree
Showing 23 changed files with 13 additions and 195 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions app/assets/stylesheets/actiontext.scss
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ blockquote.twitter-tweet>a {
.trix-button {
// filter: brightness(0.8) sepia(0.5) hue-rotate(334deg) saturate(16);
}
// Redefine icon style
trix-toolbar .trix-button--icon-attach::before {
background-image: url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20fill%3D%22%23e5e5e5%22%20width%3D%2224%22%20height%3D%2224%22%3E%3Cpath%20d%3D%22M16.5%206v11.5a4%204%200%201%201-8%200V5a2.5%202.5%200%200%201%205%200v10.5a1%201%200%201%201-2%200V6H10v9.5a2.5%202.5%200%200%200%205%200V5a4%204%200%201%200-8%200v12.5a5.5%205.5%200%200%200%2011%200V6h-1.5z%22%2F%3E%3C%2Fsvg%3E);}
trix-toolbar .trix-button--icon-bold::before {
Expand Down
52 changes: 0 additions & 52 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,4 @@ module UsersHelper
def is_author?(object, user_id)
user_id == object.user_id
end
# # include Rails.application.routes.url_helpers
#
# def user_avatar(user, size=40)
# # if user.respond_to?(:avatar) && user.avatar.attached? && user.avatar.variable?
# if user.avatar.attached?
# image_tag user.avatar.variant(resize: "#{size}x#{size}!"), class: 'rounded-full'
# # image_tag polymorphic_url(user.avatar.variant(resize: "40x40!"), only_path: true)
# else
# avatar_circle(user, size)
# # gravatar_image_url(user.email, size: size)
# end
# end
#
# def avatar_circle(user, size)
# if gravatar_exists?(user.email)
# image_tag gravatar_image_url(user.email, size: size), class: 'rounded-full'
# # gravatar_id = Digest::MD5.hexdigest(user.email)
# # gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
# # image_tag(gravatar_url, class: 'avatar_circle')
# else
# # inline_svg_pack_tag('media/images/user.svg', class: "rounded", size: "5rem * 5rem")
# initials = [user.first_name&.first, user.last_name&.first].join('')
# initials = user.email.gsub(/[[-.][_.]]+/, '').first(2) if initials.blank?
# style = "background-color: #{avatar_color(initials)}; height: #{size}px; width: #{size}px;"
# content_tag :div, class: 'avatar-circle', style: style do
# content_tag :div, initials, class: 'avatar-text'
# end
# end
# end
#
# def gravatar_exists?(email)
# hash = Digest::MD5.hexdigest(email)
# http = Net::HTTP.new('www.gravatar.com', 80)
# http.read_timeout = 2
# response = http.request_head("/avatar/#{hash}?default=http://gravatar.com/avatar")
# response.code != '302' ? true : false
# rescue StandardError, Timeout::Error
# false
# end
#
# def avatar_color(initials)
# colors = [
# '#00AA55', '#009FD4', '#B381B3', '#939393', '#E3BC00',
# '#047500', '#DC2A2A', '#696969', '#ff0000', '#ff80ed',
# '#407294', '#133337', '#065535', '#c0c0c0', '#5ac18e',
# '#666666', '#f7347a', '#576675', '#696966', '#008080',
# '#ffa500', '#40e0d0', '#8000ff', '#003366', '#fa8072',
# '#800000'
# ]
#
# colors[initials.first.to_s.downcase.ord - 97] || '#000000'
# end
end
15 changes: 0 additions & 15 deletions app/javascript/controllers/cards_util_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ export default class extends Controller {
connect() {
if (this.hasCard_frameTarget && document.body.dataset.actionName == 'index' ) {
this.card_frameTarget.setAttribute("style", "width: 47%;")
// this.element.classList.add("w-2/5")
}
// if (document.body.dataset.actionName == 'index' ) {
// var btns = document.getElementsByClassName('back-button')
// if (btns.length > 0) {
// for (let btn of btns) {
// console.log('btn: ', btn)
// btn.classList.add("hidden")
// }
// }
// }
// btns.forEach(btn => {
// if (!btn.classlist.contains("hidden")) {
// btn.classList.add('hidden')
// }
// })
}
}
1 change: 0 additions & 1 deletion app/javascript/controllers/comment_reply_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ export default class extends Controller {
} else {
event.target.remove()
}
// this.toggle(event)
}
}
2 changes: 0 additions & 2 deletions app/javascript/controllers/dropdown_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ export default class extends Controller {
if (cardUnderDropdown) {
cardUnderDropdown.classList.add('pointer-events-none')
}
// console.log('openValue _show')
this._show()
} else {
if (cardUnderDropdown && cardUnderDropdown.classList.contains('pointer-events-none')) {
cardUnderDropdown.classList.remove('pointer-events-none')
}
// console.log('openValue _hide')
this._hide()
}
}
Expand Down
11 changes: 0 additions & 11 deletions app/javascript/controllers/hovercard_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@ import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ["card"];
// static values = { url: String };

show() {
if (this.hasCardTarget) {
this.cardTarget.classList.remove("hidden");
// } else {
// fetch(this.urlValue)
// .then((r) => r.text())
// .then((html) => {
// const fragment = document
// .createRange()
// .createContextualFragment(html);
//
// this.element.appendChild(fragment);
// });
}
}

Expand Down
2 changes: 0 additions & 2 deletions app/javascript/controllers/likes_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export default class extends Controller {
const request = new FetchRequest("get", this.urlValue, {responseKind: "turbo-stream"})
const response = await request.perform()
if (response.ok) {
// console.log('Server response OK!')
}

}
}
11 changes: 0 additions & 11 deletions app/javascript/controllers/modal_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ['frame', 'container', 'showButton']
// static targets = ['frame', 'container', 'showButton', 'errors']

// handleSuccess({ detail: { success } }) {
// if (success && !this.hasErrorsTarget) {
//
// console.log('Successed form!')
// // this.close()
// // this.clearErrors()
// // this.formTarget.reset()
// }
// }

// click on background
closeBackground(event) {
Expand Down
1 change: 0 additions & 1 deletion app/javascript/controllers/object_author_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default class extends Controller {
connect() {
if (this.currentUserId === this.authorId) {
this.element.classList.remove("hidden")
// this.element.hidden = false
}
}

Expand Down
6 changes: 0 additions & 6 deletions app/javascript/controllers/popover_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ export default class extends Controller {

// Sets the popover offset using Stimulus data map objects.
initialize() {
// this.contentTarget.setAttribute(
// 'style',
// `transform:translate(${this.data.get('translateX')}, ${this.data.get('translateY')});`,
// )
}

// Show the popover
mouseOver() {
// this.contentTarget.classList.remove('hidden')
this.contentTarget.classList.add('active')
}
// Hide the popover
mouseOut() {
// this.contentTarget.classList.add('hidden')
this.contentTarget.classList.remove('active')
}
}
7 changes: 0 additions & 7 deletions app/javascript/controllers/search_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default class extends Controller {

connect() {
this.search_with_debounce = debounce(this.search_with_debounce.bind(this), 500) // bind debounce to search_debonce
// this.search_with_debounce = debounce(this.search_with_debounce, 500).bind(this) // bind debounce to search_debonce
}

// Hide default browser Errors validations input Search message
Expand All @@ -17,13 +16,7 @@ export default class extends Controller {
}

search_with_debounce() {
// console.log('this.hasSearchInputTarget', this.hasSearchInputTarget)
// console.log('Fired debonce search! (event.target)', event.target) // input -> event.target
// console.log('Fired debonce search! (this.element)', this.element) // form -> this.element
// fired if input > 2 letters
if (this.searchInputTarget.value.length > 1) {
// console.log('event.target.value.lenght)', event.target.value.lenght)
// console.log('this.searchInputTarget.value.length)', this.searchInputTarget.value.length)
this.element.requestSubmit()
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Comment < ApplicationRecord
include ActionView::RecordIdentifier
include ActionView::Helpers::TextHelper

belongs_to :user
belongs_to :commentable, polymorphic: true, counter_cache: true
Expand Down Expand Up @@ -43,7 +44,8 @@ def self.max_nesting
private

def update_comments_counter
broadcast_update_to [commentable, :comments], target: "#{dom_id(commentable)}_comments_counter", html: "Comment#{commentable.comments.size > 1 ? 's: ' : ': '}#{commentable.comments.size}"
broadcast_update_to [commentable, :comments], target: "#{dom_id(commentable)}_comments_counter",
html: "#{pluralize(commentable.comments.size, 'Comment').split().reverse.join(': ')}"
end

def update_post_comments_data
Expand Down
5 changes: 3 additions & 2 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Post < ApplicationRecord
scope :last_week_deleted, -> { where(status: :deleted).where('created_at < ? AND created_at > ?', Time.zone.now, Date.today.beginning_of_week) }
scope :last_month, -> { where('created_at < ? AND created_at > ?', Time.zone.now, Date.today.beginning_of_month) }
scope :last_month_deleted, -> { where(status: :deleted).where('created_at < ? AND created_at > ?', Time.zone.now, Date.today.beginning_of_month) }
scope :commented, -> { order('comments_count') } # Post.commented.pluck(:id, :comments_count)
scope :liked, -> { order('likes_count') } # Post.liked.pluck(:id, :likes_count)
scope :commented, -> { order('comments_count') }
scope :liked, -> { order('likes_count') }

# Save sorted method
def self.saved_sort
Expand Down Expand Up @@ -118,6 +118,7 @@ def update_like(user)
end

after_destroy_commit do
# binding.pry
broadcast_remove_to self
update_posts_counter
end
Expand Down
6 changes: 2 additions & 4 deletions app/views/comments/_comment_with_replies.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<%= turbo_stream_from comment, :comments %>
<!-- border-l-4 -->

<div class="pl-4 ml-4 border-gray-600"
id="<%= dom_id(comment) %>_with_comments"
data-controller="comment-reply "
data-user-id="<%= comment.user.id %>">
<%= render comment %>

<div class="text-xs">
<!-- Nesting <%= comment.nesting %> (<%= comment.set_nesting %>) of <%= Comment.max_nesting %> max nesting! -->
<!-- Parent id: <%= comment.parent&.id %> Nesting: <%= comment.nesting %> -->
<%= link_to "Reply", "javascript:void(0);", data: { action: "click->comment-reply#toggle" } %>
<%#= link_to "Reply", "javascript:void(0);", data: { action: "click->comment-reply#toggle" } if Comment.max_nesting > comment.set_nesting %>

</div>

<div class="" id="<%= dom_id(comment) %>_comments">
Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%#= turbo_stream.replace(dom_id_for_records(@commentable, @comment), partial: "comments/form", locals: { comment: @comment, commentable: @commentable}) %>
<%#= # Return handled by cable %>
13 changes: 0 additions & 13 deletions app/views/layouts/_custom_user_styles.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/posts/_data_comments.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<span class='flex w-full items-center'>
<%= pluralize(data[:count], "Comment").split().reverse.join(': ') %>
<!-- Comment<%= data[:count] > 1 ? 's: ' : ': ' %> <%= data[:count] %> -->
<% if data[:count] > 0 %>
by&nbsp;
<span class='flex w-4/6 ml-2 gap-1 truncate'>
Expand Down
8 changes: 3 additions & 5 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<% uniq_id = post.new_record? ? dom_id(post) : "form_#{dom_id(post)}" %>
<%= form_with(model: post, id: uniq_id,
class: "contents",
data: { turbo_frame_target: :_top }
# data: { action: 'turbo:submit-end->modal#handleSuccess', turbo_frame_target: :_top }
) do |form| %>
<%= form_with( model: post, id: uniq_id,
class: "contents",
data: { turbo_frame_target: :_top } ) do |form| %>
<div class="flex justify-between">
<h1 class="font-bold text-4xl"><%= post.new_record? ? 'New post' : 'Edit post' %></h1>
<a href="javascript:void(0);" class='text-orange-500 text-3xl font-semibold' data-action='modal#close'>&times;</a>
Expand Down
1 change: 0 additions & 1 deletion app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<%= time_ago_in_words(post.created_at) %> ago by
<span id='<%= dom_id(post) %>_user_email'>
<%= render partial: 'users/user_email', locals: { post: post, online_user_ids: @online_user_ids } %>
<%#= post.user&.email %>
</span>
</div>

Expand Down
1 change: 0 additions & 1 deletion app/views/posts/_post_brc.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<%= time_ago_in_words(post.created_at) %> ago by
<span id='<%= dom_id(post) %>_user_email'>
<%= render partial: 'users/user_email', locals: { post: post, online_user_ids: online_user_ids } %>
<%#= post.user&.email %>
</span>
</div>

Expand Down
1 change: 0 additions & 1 deletion app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<%= render 'shared/loader' %>
<div class="flex flex-row my-5">
<div class='flex flex-wrap justify-evenly gap-3 frame-loading-hide' id="post_list">
<%# @posts = @posts.take(20) %>
<%= render @posts %>
</div>
</div>
Expand Down
57 changes: 0 additions & 57 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ def create_user(hero)
print('.')
end

# Fake date for Post
# last_year = Faker::Date.between(from: Date.today.beginning_of_year, to: Date.today)
# last_month = Faker::Date.between(from: Date.today.beginning_of_month, to: Date.today)
# last_week = Faker::Date.between(from: Date.today.beginning_of_week, to: Date.today)

def prev_year_to_last_year
prev_year = Date.today.beginning_of_year - 1.days
Faker::Date.between(from: prev_year.beginning_of_year, to: prev_year)
Expand Down Expand Up @@ -225,55 +220,3 @@ def add_comments(posts, with_reply = nil)
puts "Created #{Post.count} posts!"
puts "Created #{Comment.count} comments (#{Comment.where(parent_id: nil).count} - parent)!"
puts "Created #{Like.count} likes!"

# PS
# last_day: Faker::Date.between(from: 1.days.ago, to: Time.zone.now)
# last week: Faker::Date.between(from: Date.today.beginning_of_week, to: Date.today)
# last month: Faker::Date.between(from: Date.today.beginning_of_month, to: Date.today)
# last_year: Faker::Date.between(from: Date.today.beginning_of_year, to: Date.today)
# prev_year: prev_year = Date.today.beginning_of_year - 1.days
# Faker::Date.between(from: prev_year.beginning_of_year, to: prev_year)
# prev_month: Time.zone.now.beginning_of_month - rand(1..29).days
# rand(10.weeks.ago..1.day.ago)
# Time.zone.now - rand(16..35.years) - rand(1..31).days
# beginning_of_month, beginning_of_quarter, beginning_of_week, beginning_of_year
# end_of_month, end_of_quarter, end_of_week, end_of_year

# Filter
# beginning_of_year_posts = Post.where('created_at < ? AND created_at > ?', Date.today, Date.today.beginning_of_year)


# # Random date between dates
# # Keyword arguments: from, to
# Faker::Date.between(from: '2014-09-23', to: '2014-09-25') #=> #<Date: 2014-09-24>
# # If used with Rails (the Active Support gem), additional options are available:
# Faker::Date.between(from: 2.days.ago, to: Date.today) #=> #<Date: 2014-09-24>
#
# # Random date between dates except for certain date
# # Keyword arguments: from, to, excepted
# Faker::Date.between_except(from: '2014-09-23', to: '2015-09-25', excepted: '2015-01-24') #=> #<Date: 2014-10-03>
# # If used with Rails (the Active Support gem), additional options are available:
# Faker::Date.between_except(from: 1.year.ago, to: 1.year.from_now, excepted: Date.today) #=> #<Date: 2014-10-03>
#
# # Random date in the future (up to maximum of N days)
# # Keyword arguments: days
# Faker::Date.forward(days: 23) # => "Fri, 03 Oct 2014"
#
# # Random date in the past (up to maximum of N days)
# # Keyword arguments: days
# Faker::Date.backward(days: 14) #=> "Fri, 19 Sep 2014"
#
# # Random birthday date (maximum age between 18 and 65)
# # Keyword arguments: min_age, max_age
# Faker::Date.birthday(min_age: 18, max_age: 65) #=> "Mar, 28 Mar 1986"
#
# # Random date in current year
# Faker::Date.in_date_period #=> #<Date: 2019-09-01>
#
# # Random date for range of year 2018 and month 2
# # Keyword arguments: year, month
# Faker::Date.in_date_period(year: 2018, month: 2) #=> #<Date: 2018-02-26>
#
# # Random date for range of current year and month 2
# # Keyword arguments: month
# Faker::Date.in_date_period(month: 2) #=> #<Date: 2019-02-26>

0 comments on commit 52ca3d6

Please sign in to comment.