From 721df3c44904b491d8b6a3fd017371fe5d3be3fd Mon Sep 17 00:00:00 2001 From: Julia Nguyen Date: Sun, 4 Nov 2018 15:26:48 -0500 Subject: [PATCH] Refactor ban method in ProfileController --- app/controllers/application_controller.rb | 293 ---------------------- app/controllers/profile_controller.rb | 19 +- app/views/shared/_comments.html.erb | 75 ------ client/yarn.lock | 70 ------ 4 files changed, 9 insertions(+), 448 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8d78ca6b47..51036e8d9f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -64,297 +64,4 @@ def configure_for_accept_invitation password_confirmation invitation_token] end -<<<<<<< HEAD -======= - - helper_method :avatar_url, :viewer_of?, - :are_allies?, :get_uid, :most_focus, - :tag_usage, :can_notify, :if_not_signed_in, - :generate_comment, :get_stories, :moments_stats - - def if_not_signed_in - return if user_signed_in? - - respond_to do |format| - format.html { redirect_to new_user_session_path } - format.json { head :no_content } - end - end - - def are_allies?(user_id1, user_id2) - users = User.where(id: [user_id1, user_id2]) - users.first.mutual_allies?(users.last) - end - - def viewer_of?(viewers) - viewers.include? current_user.id - end - - def get_uid(user_id) - User.find(user_id).uid - end - - # rubocop:disable MethodLength - # TODO: move this method out of the controller + refactor - def most_focus(data_type, profile_id) - data = [] - user_id = profile_id || current_user.id - moments = - if current_user.id == profile_id - user_moments(user_id) - else - user_moments(user_id).where.not(published_at: nil) - end - if data_type == 'category' - strategies = user_strategies(user_id) - [moments, strategies].each do |records| - records.where(user_id: user_id).find_each do |r| - if r.category.any? && (profile_id.blank? || - profile_exists?(profile_id, r)) - data += r.category - end - end - end - elsif data_type.in?(%w[mood strategy]) - moments.find_each do |m| - data_obj = m[data_type] - if data_obj.any? && (profile_id.blank? || - profile_exists?(profile_id, m)) - data += data_obj - end - end - end - - data.empty? ? {} : top_three_focus(data) - end - # rubocop:enable MethodLength - - # rubocop:disable MethodLength - def tag_usage(data_id, data_type, user_id) - result = [] - moments = user_moments(user_id).order('created_at DESC') - if data_type == 'category' - strategies = user_strategies(user_id).order('created_at DESC') - [moments, strategies].each do |records| - objs = [] - records.find_each do |r| - objs.push(r) if data_included?(data_type, data_id, r) - end - result << objs - end - elsif data_type.in?(%w[mood strategy]) - moments.find_each do |m| - result << m if data_included?(data_type, data_id, m) - end - end - result - end - # rubocop:enable MethodLength - - # rubocop:disable MethodLength - def get_stories(user, include_allies) - if user.id == current_user.id - my_moments = user.moments.all.recent - my_strategies = user.strategies.all.recent - end - - if include_allies && user.id == current_user.id - allies = user.allies_by_status(:accepted) - allies.each do |ally| - my_moments += user_stories(ally, 'moments') - my_strategies += user_stories(ally, 'strategies') - end - elsif !include_allies && user.id != current_user.id - my_moments = user_stories(user, 'moments') - my_strategies = user_stories(user, 'strategies') - end - - moments = Moment.where(id: my_moments.map(&:id)).order(created_at: :desc) - strategies = Strategy.where(id: my_strategies.map(&:id)) - .order(created_at: :desc) - - stories = - if moments.count.positive? - moments.zip(strategies).flatten.compact - else - strategies.compact - end - - stories.sort_by(&:created_at).reverse! - end - # rubocop:enable MethodLength - - # rubocop:disable MethodLength - # TODO: move this logic out of the controller and into a helper method - def moments_stats - total_count = current_user.moments.all.count - monthly_count = current_user.moments.where( - created_at: Time.current.beginning_of_month..Time.current - ).count - - return '' if total_count <= 1 - - result = '
' - result += if total_count == 1 - t('stats.total_moment', count: total_count.to_s) - else - t('stats.total_moments', count: total_count.to_s) - end - - if total_count != monthly_count - result += ' ' - result += if monthly_count == 1 - t('stats.monthly_moment', count: monthly_count.to_s) - else - t('stats.monthly_moments', count: monthly_count.to_s) - end - end - - result + '
' - end - # rubocop:enable MethodLength - - private - - def data_included?(data_type, data_id, data) - return false unless data_type.in?(%w[category mood strategy]) - - data_id.in?(data[data_type]) - end - - # TODO: refactor calling method to pass a hash to start with - def top_three_focus(data) - # Turn data array into hash of value => freq_of_value - freq = data.each_with_object(Hash.new(0)) do |value, hash| - hash[value] += 1 - end - - # Sort hash and take highest 3 values - freq.sort_by do |occurrences, _value| - occurrences - end[0..2].to_h - end - - def profile_exists?(profile, data) - profile.present? && - (current_user.id == profile || - data.viewers.include?(current_user.id)) - end - - # rubocop:disable MethodLength - def user_created_data?(id, data_type) - case data_type - when 'moment' - Moment.where(id: id, user_id: current_user.id).exists? - when 'strategy' - Strategy.where(id: id, user_id: current_user.id).exists? - when 'meeting' - MeetingMember.where(meeting_id: id, leader: true, - user_id: current_user.id).exists? - else - false - end - end - # rubocop:enable MethodLength - - def comment_deletable?(data, data_type) - data.comment_by == current_user.id || - user_created_data?(data.commentable_id, data_type) - end - - def comment_for(model_name) - @comment = Comment.create_from!(params) - @comment.notify_of_creation!(current_user) - - respond_with_json( - generate_comment(@comment, model_name) - ) - rescue ActiveRecord::RecordInvalid - respond_not_saved - end - - def comment_reportable?(data) - data.comment_by != current_user.id - end - - def show_with_comments(subject) - model_name = record_model_name(subject) - - if current_user.id != subject.user_id && hide_page?(subject) - path = send("#{model_name.pluralize}_path") - return redirect_to_path(path) - end - - set_show_with_comments_variables(subject, model_name) - end - - # rubocop:disable MethodLength - def set_show_with_comments_variables(subject, model_name) - @page_author = if current_user.id != subject.user_id - User.find(subject.user_id) - else - User.find(current_user.id) - end - @no_hide_page = true - # rubocop:disable GuardClause - if subject.comment - @comment = Comment.new - @comments = Comment.where( - commentable_id: subject.id, - commentable_type: model_name - ).order(created_at: :desc) - end - # rubocop:enable GuardClause - end - # rubocop:enable MethodLength - - def record_model_name(record) - record.class.name.downcase - end - - def redirect_to_path(path) - respond_to do |format| - format.html { redirect_to path } - format.json { head :no_content } - end - end - - def respond_not_saved - respond_with_json(no_save: true) - end - - def respond_with_json(reponse) - respond_to do |format| - format.html { render json: reponse } - format.json { render json: reponse } - end - end - - def hide_page?(subject) - (!current_user.mutual_allies?(subject.user) \ - && !subject.viewer?(current_user)) \ - || !subject.published? - end - - def user_strategies(user_id) - Strategy.where(user_id: user_id) - end - - def user_moments(user_id) - Moment.where(user_id: user_id) - end - - def user_stories(user, collection) - case collection - when 'moments' - query = Moment.published - when 'strategies' - query = Strategy.published - end - - query.where(user_id: user.id).all.recent.map do |story| - story if story.viewers.include?(current_user.id) - end.compact - end ->>>>>>> e2ce9622... Report Feature added for Profile and Comments, Admin dashboard implemented end diff --git a/app/controllers/profile_controller.rb b/app/controllers/profile_controller.rb index 6fdd7f2a79..8f6ceef1af 100644 --- a/app/controllers/profile_controller.rb +++ b/app/controllers/profile_controller.rb @@ -22,21 +22,20 @@ def remove_ban private - def ban_mailer(user, banned) - return unless user.any? - + def update_and_email(user, banned) + user.update(banned: banned) if banned - BannedMailer.add_ban_email(user.first).deliver_now + BannedMailer.add_ban_email(user).deliver_now else - BannedMailer.remove_ban_email(user.first).deliver_now + BannedMailer.remove_ban_email(user).deliver_now end end def ban(banned) return unless current_user.admin - user = User.where(id: params[:user_id]).update(banned: banned) - ban_mailer(user, banned) + user = User.find_by(id: params[:user_id]) + update_and_email(user, banned) if user.present? redirect_to( admin_dashboard_path, notice_or_alert( @@ -47,9 +46,9 @@ def ban(banned) end def notice_or_alert(user, notice) - name = user&.first&.name || params[:user_id] - return { alert: t("reports.#{notice}_error", name: name) } unless user.any? + name = user&.name || params[:user_id] + return { notice: t("reports.#{notice}", name: name) } if user.present? - { notice: t("reports.#{notice}", name: name) } + { alert: t("reports.#{notice}_error", name: name) } end end diff --git a/app/views/shared/_comments.html.erb b/app/views/shared/_comments.html.erb index abe5292076..60740a5fed 100644 --- a/app/views/shared/_comments.html.erb +++ b/app/views/shared/_comments.html.erb @@ -4,79 +4,4 @@ local_assigns[:commentable], local_assigns[:commentable].class.name.downcase ) -<<<<<<< HEAD }) %> -======= -) %> -
- <%= form_for local_assigns[:comment], url: { action: "comment" }, html: { method: "post" } do |f| %> - <% if local_assigns[:comment].errors.any? %> -
- <%= t('common.form.error_explanation') %> -
- <% end %> - - <%= f.text_area :comment, class: 'comment_textarea' %> - - <%= f.hidden_field :commentable_type, value: local_assigns[:commentable_type] %> - <%= f.hidden_field :comment_by, value: current_user.id %> - <%= f.hidden_field :commentable_id, value: local_assigns[:data].id %> - -
- <% if (local_assigns[:commentable_type] == 'moment' || local_assigns[:commentable_type] == 'strategy') && local_assigns[:data].user_id != current_user.id %> - <%= f.select :visibility, [ - [t('shared.comments.share_everyone'), 'all'], - [t('shared.comments.share_with', name: User.where(id: local_assigns[:data].user_id).first.name), 'private'] - ] %> - <% else %> - <%= f.hidden_field :visibility, value: 'all' %> - <% if (local_assigns[:commentable_type] == 'moment' || local_assigns[:commentable_type] == 'strategy') && !local_assigns[:data].viewers.blank? && local_assigns[:data].viewers.length > 0 %> - <%= f.select :viewers, local_assigns[:data].viewers.collect { |v| - [ t('shared.comments.share_with', name: User.where(id: v).first.name), v ] - }, include_blank: t('shared.comments.share_everyone') %> - <% end %> - <% end %> -
- - <%= f.submit t('comment.singular'), class: 'smallerMarginTop buttonDarkS', id: 'add_comment_button' %> -
- <% end %> - -
- <% local_assigns[:comments].each do |comment| %> - <% if comment.visibility == 'all' || - ( - comment.visibility == 'private' && - User.where(id: comment.comment_by).exists? && - ( - comment.comment_by == current_user.id || - local_assigns[:is_member] || - current_user.id == local_assigns[:data].user_id || - ( - !comment.viewers.blank? && - comment.viewers.include?(current_user.id) - ) - ) - ) - %> - <% result = generate_comment(comment, comment.commentable_type) %> -
> -
-
- <%= raw result[:comment_info] %> -
- <%= raw result[:delete_comment] %> - <%= raw result[:report_comment] %> -
-
- <%= raw result[:comment_text] %> -
-
- <%= raw result[:visibility] %> -
-
- <% end %> - <% end %> -
-<% end %> ->>>>>>> aaefd3cd... Report block final (#4) diff --git a/client/yarn.lock b/client/yarn.lock index 311db1de36..29031e649a 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -4122,17 +4122,10 @@ enzyme-adapter-utils@^1.8.0: object.assign "^4.1.0" prop-types "^15.6.2" -<<<<<<< HEAD enzyme@^3.7.0: version "3.7.0" resolved "https://registry.yarnpkg.com/enzyme/-/enzyme-3.7.0.tgz#9b499e8ca155df44fef64d9f1558961ba1385a46" integrity sha512-QLWx+krGK6iDNyR1KlH5YPZqxZCQaVF6ike1eDJAOg0HvSkSCVImPsdWaNw6v+VrnK92Kg8jIOYhuOSS9sBpyg== -======= -enzyme@^3.6.0: - version "3.6.0" - resolved "https://registry.yarnpkg.com/enzyme/-/enzyme-3.6.0.tgz#d213f280a258f61e901bc663d4cc2d6fd9a9dec8" - integrity sha512-onsINzVLGqKIapTVfWkkw6bYvm1o4CyJ9s8POExtQhAkVa4qFDW6DGCQGRy/5bfZYk+gmUbMNyayXiWDzTkHFQ== ->>>>>>> 2a9cd1de... Commit dependencies: array.prototype.flat "^1.2.1" cheerio "^1.0.0-rc.2" @@ -5963,40 +5956,7 @@ interpret@^1.0.0, interpret@^1.1.0: resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614" integrity sha1-ftGxQQxqDg94z5XTuEQMY/eLhhQ= -<<<<<<< HEAD invariant@^2.2.2, invariant@^2.2.4: -======= -intl-format-cache@^2.0.5: - version "2.1.0" - resolved "https://registry.yarnpkg.com/intl-format-cache/-/intl-format-cache-2.1.0.tgz#04a369fecbfad6da6005bae1f14333332dcf9316" - integrity sha1-BKNp/sv61tpgBbrh8UMzMy3PkxY= - -intl-messageformat-parser@1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/intl-messageformat-parser/-/intl-messageformat-parser-1.4.0.tgz#b43d45a97468cadbe44331d74bb1e8dea44fc075" - integrity sha1-tD1FqXRoytvkQzHXS7Ho3qRPwHU= - -intl-messageformat@^2.0.0, intl-messageformat@^2.1.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/intl-messageformat/-/intl-messageformat-2.2.0.tgz#345bcd46de630b7683330c2e52177ff5eab484fc" - integrity sha1-NFvNRt5jC3aDMwwuUhd/9eq0hPw= - dependencies: - intl-messageformat-parser "1.4.0" - -intl-relativeformat@^2.0.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/intl-relativeformat/-/intl-relativeformat-2.1.0.tgz#010f1105802251f40ac47d0e3e1a201348a255df" - integrity sha1-AQ8RBYAiUfQKxH0OPhogE0iiVd8= - dependencies: - intl-messageformat "^2.0.0" - -intl@^1.2.5: - version "1.2.5" - resolved "https://registry.yarnpkg.com/intl/-/intl-1.2.5.tgz#82244a2190c4e419f8371f5aa34daa3420e2abde" - integrity sha1-giRKIZDE5Bn4Nx9ao02qNCDiq94= - -invariant@^2.1.1, invariant@^2.2.2, invariant@^2.2.4: ->>>>>>> 2a9cd1de... Commit version "2.2.4" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6" integrity sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA== @@ -9335,19 +9295,6 @@ react-inspector@^2.3.0: babel-runtime "^6.26.0" is-dom "^1.0.9" -<<<<<<< HEAD -======= -react-intl@^2.4.0: - version "2.4.0" - resolved "https://registry.yarnpkg.com/react-intl/-/react-intl-2.4.0.tgz#66c14dc9df9a73b2fbbfbd6021726e80a613eb15" - integrity sha1-ZsFNyd+ac7L7v71gIXJugKYT6xU= - dependencies: - intl-format-cache "^2.0.5" - intl-messageformat "^2.1.0" - intl-relativeformat "^2.0.0" - invariant "^2.1.1" - ->>>>>>> 2a9cd1de... Commit react-is@^16.4.2, react-is@^16.5.2: version "16.5.2" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.5.2.tgz#e2a7b7c3f5d48062eb769fcb123505eb928722e3" @@ -9373,17 +9320,10 @@ react-modal@^3.5.1: react-lifecycles-compat "^3.0.0" warning "^3.0.0" -<<<<<<< HEAD react-on-rails@11.1.8: version "11.1.8" resolved "https://registry.yarnpkg.com/react-on-rails/-/react-on-rails-11.1.8.tgz#6d5ba72a514cac4ea4efddd8a80f61909d3e9bd7" integrity sha512-vc4uw9IiDZcU4v8CwZEXOd4fVyp1Tu3LCcp9nKS4q3ffr/Ea+jphc+TrqR3nkVOAfcl67UciIDAjXrrnFezJVA== -======= -react-on-rails@11.1.4: - version "11.1.4" - resolved "https://registry.yarnpkg.com/react-on-rails/-/react-on-rails-11.1.4.tgz#7370c1005f8c19ed00cbfd056a9a42201dd4dd98" - integrity sha512-6jdrMliju93aZi3N4ol/qy+XAxIPZBGctZQKFY0zzc1KakN+9fKPHujSt1477VXIZvFLEfi5fQpgO53Mz35qdA== ->>>>>>> 2a9cd1de... Commit dependencies: "@babel/runtime-corejs2" "^7.0.0" @@ -10494,16 +10434,6 @@ stealthy-require@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks= -<<<<<<< HEAD -======= - -storybook-addon-intl@^2.3.0: - version "2.3.0" - resolved "https://registry.yarnpkg.com/storybook-addon-intl/-/storybook-addon-intl-2.3.0.tgz#94711276cefeb0b9ef13b1e70a96516c1fe0cfd2" - integrity sha512-RSBcNIERuMKTGFRmsb3pPn0rAXF50JJYWYvMUn5Zpp3d6v0V2IJfmGnU9Wm4mtmjSby7mCGU175FM4tIUbP9mw== - dependencies: - prop-types "^15.5.0" ->>>>>>> 2a9cd1de... Commit stream-browserify@^2.0.1: version "2.0.1"