From 36d2dfd1229c2c05845a294a0a7b0d1e754a7fe5 Mon Sep 17 00:00:00 2001 From: Kohei Sugi Date: Tue, 14 Aug 2018 05:32:11 +0900 Subject: [PATCH] Apply rubocop some rule (#295) * Fix FactoryBot/StaticAttributeDefinedDynamically and Metrics/LineLength * Fix RSpec/NotToNot: Prefer not_to over to_not * Fix RSpec/NotToNot: Prefer not_to over to_not and Metrics/LineLength * Fix Layout/MultilineMethodCallIndentation and Metrics/LineLength * Fix Naming/PredicateName * Fix Style/ConditionalAssignment * Avoid code climate error --- app/controllers/application_controller.rb | 4 +-- app/controllers/chat_channels_controller.rb | 31 ++++++++++------- app/controllers/dashboards_controller.rb | 2 +- .../followed_articles_controller.rb | 34 +++++++++---------- app/controllers/notifications_controller.rb | 2 +- app/helpers/articles_helper.rb | 4 +-- app/models/user.rb | 4 +-- app/policies/organization_policy.rb | 2 +- app/views/articles/_markdown_form.html.erb | 8 ++--- app/views/layouts/application.html.erb | 10 +++--- spec/factories/broadcasts.rb | 8 ++--- spec/models/message_spec.rb | 14 +++++--- spec/models/user_spec.rb | 14 ++++---- spec/views/articles_spec.rb | 2 +- 14 files changed, 75 insertions(+), 64 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bee4edfcd..cb746ef3c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -40,10 +40,10 @@ class ApplicationController < ActionController::Base raise "BANNED" if current_user&.banned end - def is_internal_navigation? + def internal_navigation? params[:i] == "i" end - helper_method :is_internal_navigation? + helper_method :internal_navigation? def valid_request_origin? # This manually does what it was supposed to do on its own. diff --git a/app/controllers/chat_channels_controller.rb b/app/controllers/chat_channels_controller.rb index a4acec8ac..300ea3075 100644 --- a/app/controllers/chat_channels_controller.rb +++ b/app/controllers/chat_channels_controller.rb @@ -24,7 +24,9 @@ class ChatChannelsController < ApplicationController authorize ChatChannel @chat_channel = ChatChannelCreationService.new(current_user, params[:chat_channel]).create if @chat_channel.valid? - render json: { status: "success", chat_channel: @chat_channel.to_json(only: %i[channel_name slug]) }, status: 200 + render json: { status: "success", + chat_channel: @chat_channel.to_json(only: %i[channel_name slug]) }, + status: 200 else render json: { errors: @chat_channel.errors.full_messages } end @@ -35,7 +37,9 @@ class ChatChannelsController < ApplicationController authorize @chat_channel ChatChannelUpdateService.new(@chat_channel, chat_channel_params).update if @chat_channel.valid? - render json: { status: "success", chat_channel: @chat_channel.to_json(only: %i[channel_name slug]) }, status: 200 + render json: { status: "success", + chat_channel: @chat_channel.to_json(only: %i[channel_name slug]) }, + status: 200 else render json: { errors: @chat_channel.errors.full_messages } end @@ -60,7 +64,9 @@ class ChatChannelsController < ApplicationController if banned_user banned_user.add_role :banned banned_user.messages.each(&:destroy!) - Pusher.trigger(@chat_channel.pusher_channels, "user-banned", { userId: banned_user.id }.to_json) + Pusher.trigger(@chat_channel.pusher_channels, + "user-banned", + { userId: banned_user.id }.to_json) render json: { status: "success", message: "banned!" }, status: 200 else render json: { status: "error", message: "username not found" }, status: 400 @@ -88,15 +94,16 @@ class ChatChannelsController < ApplicationController end def render_unopened_json_response - if current_user - @chat_channels_memberships = current_user. - chat_channel_memberships.includes(:chat_channel). - where("has_unopened_messages = ? OR status = ?", true, "pending"). - where(show_global_badge_notification: true). - order("chat_channel_memberships.updated_at DESC") - else - @chat_channels_memberships = [] - end + @chat_channels_memberships = if current_user + current_user. + chat_channel_memberships.includes(:chat_channel). + where("has_unopened_messages = ? OR status = ?", + true, "pending"). + where(show_global_badge_notification: true). + order("chat_channel_memberships.updated_at DESC") + else + [] + end render "index.json" end diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index d80176e2c..915082532 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -4,7 +4,7 @@ class DashboardsController < ApplicationController after_action :verify_authorized def show - @user = if params[:username] && current_user.is_admin? + @user = if params[:username] && current_user.admin? User.find_by_username(params[:username]) else current_user diff --git a/app/controllers/followed_articles_controller.rb b/app/controllers/followed_articles_controller.rb index 99b00c9e1..f5b8e639b 100644 --- a/app/controllers/followed_articles_controller.rb +++ b/app/controllers/followed_articles_controller.rb @@ -6,23 +6,23 @@ class FollowedArticlesController < ApplicationController expires_in: 35.minutes def index - if current_user - @articles = Rails.cache.fetch("user-#{current_user.id}__#{current_user.updated_at}/followed_articles", expires_in: 30.minutes) do - current_user. - followed_articles. - includes(:user). - where("published_at > ?", 5.days.ago). - order("hotness_score DESC"). - limit(25). - map do |a| - unless inappropriate_hiring_instance(a) - article_json(a) - end - end.compact - end - else - @articles = [] - end + @articles = if current_user + Rails.cache.fetch( + "user-#{current_user.id}__#{current_user.updated_at}/followed_articles", + expires_in: 30.minutes, + ) do + current_user.followed_articles. + includes(:user).where("published_at > ?", 5.days.ago). + order("hotness_score DESC"). + limit(25).map do |a| + unless inappropriate_hiring_instance(a) + article_json(a) + end + end.compact + end + else + @articles = [] + end classic_article = Suggester::Articles::Classic.new(current_user).get response.headers["Cache-Control"] = "public, max-age=150" render json: { diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 051e69fc0..2636d5a88 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -5,7 +5,7 @@ class NotificationsController < ApplicationController def index if user_signed_in? @notifications_index = true - @user = if params[:username] && current_user.is_admin? + @user = if params[:username] && current_user.admin? User.find_by_username(params[:username]) else current_user diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb index a891cfee9..14b1d4ec7 100644 --- a/app/helpers/articles_helper.rb +++ b/app/helpers/articles_helper.rb @@ -12,7 +12,7 @@ module ArticlesHelper end def image_tag_or_inline_svg(service_name) - if is_internal_navigation? + if internal_navigation? image_tag("#{service_name}-logo.svg", class: "icon-img") else inline_svg("#{service_name}-logo.svg", class: "icon-img") @@ -41,7 +41,7 @@ module ArticlesHelper host.start_with?("www.") ? host[4..-1] : host end - def is_hiring_form?(tag, article) + def hiring_form?(tag, article) tag.to_s == "hiring" || article.tag_list.include?("hiring") end end diff --git a/app/models/user.rb b/app/models/user.rb index 10cbb8b85..09568aeb7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -247,7 +247,7 @@ class User < ApplicationRecord has_role? :warned end - def is_admin? + def admin? has_role?(:super_admin) end @@ -280,7 +280,7 @@ class User < ApplicationRecord has_any_role?(:workshop_pass, :level_3_member, :level_4_member, :triple_unicorn_member) end - def is_org_admin?(organization) + def org_admin?(organization) user.org_admin && user.organization_id == organization.id end diff --git a/app/policies/organization_policy.rb b/app/policies/organization_policy.rb index 541b1bd26..c7d71b873 100644 --- a/app/policies/organization_policy.rb +++ b/app/policies/organization_policy.rb @@ -4,7 +4,7 @@ class OrganizationPolicy < ApplicationPolicy end def update? - user.is_org_admin?(record) + user.org_admin?(record) end def generate_new_secret? diff --git a/app/views/articles/_markdown_form.html.erb b/app/views/articles/_markdown_form.html.erb index 1ca3b5559..6166537cc 100644 --- a/app/views/articles/_markdown_form.html.erb +++ b/app/views/articles/_markdown_form.html.erb @@ -11,7 +11,7 @@ <% end %> - <% if is_hiring_form?(@tag, @article) %> + <% if hiring_form?(@tag, @article) %>
< beta ❤️ >

The #Hiring Tag

@@ -19,7 +19,7 @@
<% end %>
- <% unless is_hiring_form?(@tag, @article) %> + <% unless hiring_form?(@tag, @article) %>
@@ -28,7 +28,7 @@
<% end %>
- <% if is_hiring_form?(@tag, @article) %> + <% if hiring_form?(@tag, @article) %> <%= render 'articles/hiring_form', f:f %> <% else %> <% if current_user.organization %> @@ -56,7 +56,7 @@
<%= f.text_area :body_markdown %> - <% if is_hiring_form?(@tag, @article) %> + <% if hiring_form?(@tag, @article) %> <% if current_user.organization %>