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
This commit is contained in:
Kohei Sugi 2018-08-14 05:32:11 +09:00 committed by Ben Halpern
parent 987e572944
commit 36d2dfd122
14 changed files with 75 additions and 64 deletions

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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: {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -4,7 +4,7 @@ class OrganizationPolicy < ApplicationPolicy
end
def update?
user.is_org_admin?(record)
user.org_admin?(record)
end
def generate_new_secret?

View file

@ -11,7 +11,7 @@
</div>
<% end %>
<span id="error_explanation_anchor"></span>
<% if is_hiring_form?(@tag, @article) %>
<% if hiring_form?(@tag, @article) %>
<div class="job-listing-header">
<h5>< beta ❤️ ></h5>
<h1>The #Hiring Tag</h1>
@ -19,7 +19,7 @@
</div>
<% end %>
<div class="container article markdown-editor" id="markdown-editor">
<% unless is_hiring_form?(@tag, @article) %>
<% unless hiring_form?(@tag, @article) %>
<div class="top-buttons">
<button id="help-butt" class="editor-button help-butt">HELP</button>
<button id="markdownbutt" class="editor-button markdown-butt active">MARKDOWN</button>
@ -28,7 +28,7 @@
</div>
<% end %>
<div id="markdown-editor-main" class="editor-view">
<% 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 @@
</div>
</div>
<%= f.text_area :body_markdown %>
<% if is_hiring_form?(@tag, @article) %>
<% if hiring_form?(@tag, @article) %>
<% if current_user.organization %>
<div class="hiring-form-toggle">
<label class="switch">

View file

@ -4,7 +4,7 @@
<meta charset="utf-8">
<% title = yield(:title) %>
<title><%= title || 'DEV Community' %></title>
<% if is_internal_navigation? %>
<% if internal_navigation? %>
<style>
.universal-page-content-wrapper{
visibility: hidden;
@ -76,13 +76,13 @@
<% end %>
</head>
<body data-user-status="<%= user_logged_in_status %>" data-pusher-key="<%= ApplicationConfig["PUSHER_KEY"] %>">
<% unless is_internal_navigation? %>
<% unless internal_navigation? %>
<div id="audiocontent">
<%= yield(:audio) %>
</div>
<% end %>
<% cache("application-top-bar--#{user_signed_in?}--#{is_internal_navigation?}", :expires_in => 100.hours) do %>
<%= render "layouts/top_bar" unless is_internal_navigation? %>
<% cache("application-top-bar--#{user_signed_in?}--#{internal_navigation?}", :expires_in => 100.hours) do %>
<%= render "layouts/top_bar" unless internal_navigation? %>
<% end %>
<div id="message-notice"></div>
<div id="page-content" class="universal-page-content-wrapper <%= view_class %>" data-current-page="<%= current_page %>">
@ -90,7 +90,7 @@
<%= yield %>
</div>
</div>
<% unless is_internal_navigation? %>
<% unless internal_navigation? %>
<% cache("footer-and-signup-modal--#{user_signed_in?}--#{ApplicationConfig["DEPLOYMENT_SIGNATURE"].to_s}", :expires_in => 200.hours) do %>
<%= render "layouts/footer" %>
<%= render "layouts/signup_modal" unless user_signed_in? %>

View file

@ -1,12 +1,12 @@
FactoryBot.define do
factory :broadcast do
sent { false }
sent false
end
trait :onboarding do
title { "Welcome Notification" }
type_of { "Onboarding" }
processed_html { "Welcome to dev.to! Introduce yourself in our <a href='/welcome'>welcome thread!</a>" }
title "Welcome Notification"
type_of "Onboarding"
processed_html "Welcome! Introduce yourself in our <a href='/welcome'>welcome thread!</a>"
end
trait :sent do

View file

@ -4,6 +4,7 @@ RSpec.describe Message, type: :model do
let(:user) { create(:user) }
let(:chat_channel) { create(:chat_channel) }
let(:user2) { create(:user) }
let(:long_text) { Faker::Hipster.words(1500) }
describe "validations" do
subject { build(:message, :ignore_after_callback) }
@ -27,11 +28,14 @@ RSpec.describe Message, type: :model do
end
it "is invalid if over 1024 chars" do
long_text = Faker::Hipster.words(1500)
p long_text.size
message = build(:message, chat_channel_id: chat_channel.id, user_id: user.id, message_markdown: long_text)
expect(message).to_not be_valid
message.message_markdown = "hello"
message = build(:message, chat_channel_id: chat_channel.id, user_id: user.id,
message_markdown: long_text)
expect(message).not_to be_valid
end
it "is valid if under 1024 chars" do
message = build(:message, chat_channel_id: chat_channel.id, user_id: user.id,
message_markdown: "hello")
expect(message).to be_valid
end
end

View file

@ -155,13 +155,13 @@ RSpec.describe User, type: :model do
it "updates mentor_form_updated_at at appropriate time" do
user.mentor_description = "hello"
user.save
expect(user.mentor_form_updated_at).to_not eq(nil)
expect(user.mentor_form_updated_at).not_to eq(nil)
end
it "updates mentee_form_updated_at at appropriate time" do
user.mentee_description = "hello"
user.save
expect(user.mentee_form_updated_at).to_not eq(nil)
expect(user.mentee_form_updated_at).not_to eq(nil)
end
it "does not allow too short or too long name" do
@ -424,18 +424,18 @@ RSpec.describe User, type: :model do
describe "organization admin privileges" do
it "recognizes an org admin" do
user.update(organization: org, org_admin: true)
expect(user.is_org_admin?(org)).to be true
expect(user.org_admin?(org)).to be true
end
it "forbids an incorrect org admin" do
user.update(organization: org, org_admin: true)
expect(user.is_org_admin?(second_org)).to be false
expect(second_user.is_org_admin?(org)).to be false
expect(user.org_admin?(second_org)).to be false
expect(second_user.org_admin?(org)).to be false
end
it "responds to nil" do
expect(user.is_org_admin?(nil)).to be false
expect(second_user.is_org_admin?(nil)).to be false
expect(user.org_admin?(nil)).to be false
expect(second_user.org_admin?(nil)).to be false
end
end

View file

@ -11,7 +11,7 @@ describe "articles/show", type: :view do
assign(:article, article1.decorate)
assign(:comment, Comment.new)
without_partial_double_verification do
allow(view).to receive(:is_internal_navigation?).and_return(params[:i] == "i")
allow(view).to receive(:internal_navigation?).and_return(params[:i] == "i")
end
end