From 52ca3d6fc3e7c76326aa0773dd0e013478eeb55b Mon Sep 17 00:00:00 2001 From: secretpray Date: Tue, 28 Dec 2021 18:25:39 +0200 Subject: [PATCH] Clean code --- .DS_Store | Bin 6148 -> 0 bytes app/assets/stylesheets/actiontext.scss | 1 + app/helpers/users_helper.rb | 52 ---------------- .../controllers/cards_util_controller.js | 15 ----- .../controllers/comment_reply_controller.js | 1 - .../controllers/dropdown_controller.js | 2 - .../controllers/hovercard_controller.js | 11 ---- .../controllers/likes_controller.js | 2 - .../controllers/modal_controller.js | 11 ---- .../controllers/object_author_controller.js | 1 - .../controllers/popover_controller.js | 6 -- .../controllers/search_controller.js | 7 --- app/models/comment.rb | 4 +- app/models/post.rb | 5 +- .../comments/_comment_with_replies.html.erb | 6 +- app/views/comments/create.turbo_stream.erb | 2 +- .../layouts/_custom_user_styles.html.erb | 13 ---- app/views/posts/_data_comments.html.erb | 1 - app/views/posts/_form.html.erb | 8 +-- app/views/posts/_post.html.erb | 1 - app/views/posts/_post_brc.html.erb | 1 - app/views/posts/index.html.erb | 1 - db/seeds.rb | 57 ------------------ 23 files changed, 13 insertions(+), 195 deletions(-) delete mode 100644 .DS_Store delete mode 100644 app/views/layouts/_custom_user_styles.html.erb diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index a0039df87ca51481088928af05e29afa38af358a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5Z<*_Hx#i4!5;VGt%v@I*n<#aJ$Mr$dQfQ-Qfwe5rAaLsD|rolBcH(6 zab|Zb7JKj{VrO9Yo7tJ!&3uskVT^HS5;PgBGR6cnM2^Z5!QrK@H4}`;agKE4M~M&C zn?EwXzYh5ACFZda8~M|(-yel()@Z!*wNiO~V^auG5nJNM9VSyZ^Rh|mc;hRy&b(~s z8~r>I;>-)au-sIIGLN(*k|J)0)jJvAfqiI_=**Jmycc=NHW{2l|z48=S){D5ku7cN|Aad~saRrw82AYTxIai}h_=B(quM&4!|OBp%ZMnT<68nz z8ng`-8o>j?bt<4v<>raObvleo6K5MNH0pH5)ygoBS$Y0=;c9glmnxibTO;+v05P!0 zKuMcbJpWJNms$GAUrZs37$64z83VlBb-NA}WzN=b<>6T?q4lAmU|fL;2a { .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 { diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 004c81b..830fbb8 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -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 diff --git a/app/javascript/controllers/cards_util_controller.js b/app/javascript/controllers/cards_util_controller.js index 79ff65b..0cf2274 100644 --- a/app/javascript/controllers/cards_util_controller.js +++ b/app/javascript/controllers/cards_util_controller.js @@ -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') - // } - // }) } } diff --git a/app/javascript/controllers/comment_reply_controller.js b/app/javascript/controllers/comment_reply_controller.js index 45867a7..c8e559d 100644 --- a/app/javascript/controllers/comment_reply_controller.js +++ b/app/javascript/controllers/comment_reply_controller.js @@ -25,6 +25,5 @@ export default class extends Controller { } else { event.target.remove() } - // this.toggle(event) } } diff --git a/app/javascript/controllers/dropdown_controller.js b/app/javascript/controllers/dropdown_controller.js index 5d2aebf..ab6a252 100644 --- a/app/javascript/controllers/dropdown_controller.js +++ b/app/javascript/controllers/dropdown_controller.js @@ -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() } } diff --git a/app/javascript/controllers/hovercard_controller.js b/app/javascript/controllers/hovercard_controller.js index 5fc78cf..baa88c0 100644 --- a/app/javascript/controllers/hovercard_controller.js +++ b/app/javascript/controllers/hovercard_controller.js @@ -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); - // }); } } diff --git a/app/javascript/controllers/likes_controller.js b/app/javascript/controllers/likes_controller.js index 66296c0..bcccef9 100644 --- a/app/javascript/controllers/likes_controller.js +++ b/app/javascript/controllers/likes_controller.js @@ -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!') } - } } diff --git a/app/javascript/controllers/modal_controller.js b/app/javascript/controllers/modal_controller.js index 6fc9d35..1ac2c18 100644 --- a/app/javascript/controllers/modal_controller.js +++ b/app/javascript/controllers/modal_controller.js @@ -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) { diff --git a/app/javascript/controllers/object_author_controller.js b/app/javascript/controllers/object_author_controller.js index ee8227d..fac66da 100644 --- a/app/javascript/controllers/object_author_controller.js +++ b/app/javascript/controllers/object_author_controller.js @@ -8,7 +8,6 @@ export default class extends Controller { connect() { if (this.currentUserId === this.authorId) { this.element.classList.remove("hidden") - // this.element.hidden = false } } diff --git a/app/javascript/controllers/popover_controller.js b/app/javascript/controllers/popover_controller.js index 24c5791..716fb0d 100644 --- a/app/javascript/controllers/popover_controller.js +++ b/app/javascript/controllers/popover_controller.js @@ -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') } } diff --git a/app/javascript/controllers/search_controller.js b/app/javascript/controllers/search_controller.js index 7a4b801..9dcda92 100644 --- a/app/javascript/controllers/search_controller.js +++ b/app/javascript/controllers/search_controller.js @@ -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 @@ -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() } } diff --git a/app/models/comment.rb b/app/models/comment.rb index 4cd2a70..57551f9 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 80f172f..fed66fc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 @@ -118,6 +118,7 @@ def update_like(user) end after_destroy_commit do + # binding.pry broadcast_remove_to self update_posts_counter end diff --git a/app/views/comments/_comment_with_replies.html.erb b/app/views/comments/_comment_with_replies.html.erb index 227f0bf..f9d6920 100644 --- a/app/views/comments/_comment_with_replies.html.erb +++ b/app/views/comments/_comment_with_replies.html.erb @@ -1,5 +1,5 @@ <%= turbo_stream_from comment, :comments %> - +
- - <%= 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 %> +
diff --git a/app/views/comments/create.turbo_stream.erb b/app/views/comments/create.turbo_stream.erb index 84862c4..011564b 100644 --- a/app/views/comments/create.turbo_stream.erb +++ b/app/views/comments/create.turbo_stream.erb @@ -1 +1 @@ -<%#= turbo_stream.replace(dom_id_for_records(@commentable, @comment), partial: "comments/form", locals: { comment: @comment, commentable: @commentable}) %> +<%#= # Return handled by cable %> diff --git a/app/views/layouts/_custom_user_styles.html.erb b/app/views/layouts/_custom_user_styles.html.erb deleted file mode 100644 index 230eba8..0000000 --- a/app/views/layouts/_custom_user_styles.html.erb +++ /dev/null @@ -1,13 +0,0 @@ -<% cache current_user do %> - - -<% end %> diff --git a/app/views/posts/_data_comments.html.erb b/app/views/posts/_data_comments.html.erb index bea0e24..db78057 100644 --- a/app/views/posts/_data_comments.html.erb +++ b/app/views/posts/_data_comments.html.erb @@ -2,7 +2,6 @@ <%= pluralize(data[:count], "Comment").split().reverse.join(': ') %> - <% if data[:count] > 0 %> by  diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb index 0c0ec12..03d941e 100644 --- a/app/views/posts/_form.html.erb +++ b/app/views/posts/_form.html.erb @@ -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| %>

<%= post.new_record? ? 'New post' : 'Edit post' %>

× diff --git a/app/views/posts/_post.html.erb b/app/views/posts/_post.html.erb index 5a55d55..34732aa 100644 --- a/app/views/posts/_post.html.erb +++ b/app/views/posts/_post.html.erb @@ -22,7 +22,6 @@ <%= time_ago_in_words(post.created_at) %> ago by <%= render partial: 'users/user_email', locals: { post: post, online_user_ids: @online_user_ids } %> - <%#= post.user&.email %>
diff --git a/app/views/posts/_post_brc.html.erb b/app/views/posts/_post_brc.html.erb index 9f7fe06..755e8cb 100644 --- a/app/views/posts/_post_brc.html.erb +++ b/app/views/posts/_post_brc.html.erb @@ -21,7 +21,6 @@ <%= time_ago_in_words(post.created_at) %> ago by <%= render partial: 'users/user_email', locals: { post: post, online_user_ids: online_user_ids } %> - <%#= post.user&.email %>
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 8126b03..5611f6c 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -38,7 +38,6 @@ <%= render 'shared/loader' %>
- <%# @posts = @posts.take(20) %> <%= render @posts %>
diff --git a/db/seeds.rb b/db/seeds.rb index 08c2850..a349695 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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) @@ -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') #=> # -# # If used with Rails (the Active Support gem), additional options are available: -# Faker::Date.between(from: 2.days.ago, to: Date.today) #=> # -# -# # 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') #=> # -# # 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) #=> # -# -# # 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 #=> # -# -# # Random date for range of year 2018 and month 2 -# # Keyword arguments: year, month -# Faker::Date.in_date_period(year: 2018, month: 2) #=> # -# -# # Random date for range of current year and month 2 -# # Keyword arguments: month -# Faker::Date.in_date_period(month: 2) #=> #