Skip to content
Permalink
Browse files

Replace try with safe operator

Improving method calls using ruby safe operator instead of try rails method
  • Loading branch information...
lucianosousa authored and sonalkr132 committed Dec 24, 2018
1 parent 32f00d7 commit fb2441faa73721d5d76f064d025442ec798804dd
@@ -102,13 +102,6 @@ Style/NumericPredicate:
- 'app/helpers/searches_helper.rb'
- 'app/models/rubygem.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: ConvertCodeThatCanStartToReturnNil.
Style/SafeNavigation:
Exclude:
- 'app/jobs/fastly_log_processor.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowSafeAssignment.
@@ -25,7 +25,7 @@ def destroy
owner = @rubygem.owners.find_by_name(params[:email])
if owner
ownership = @rubygem.ownerships.find_by(user_id: owner.id)
if ownership.try(:safe_destroy)
if ownership&.safe_destroy
render plain: "Owner removed successfully."
else
render plain: "Unable to remove owner.", status: :forbidden
@@ -19,8 +19,8 @@ def create
end

def remove
webhook = @api_user.web_hooks.find_by_rubygem_id_and_url(@rubygem.try(:id), @url)
if webhook.try(:destroy)
webhook = @api_user.web_hooks.find_by_rubygem_id_and_url(@rubygem&.id, @url)
if webhook&.destroy
render(plain: webhook.removed_message)
else
render(plain: "No such webhook exists under your account.", status: :not_found)
@@ -3,12 +3,12 @@ class SubscriptionsController < ApplicationController

def create
subscription = @rubygem.subscriptions.build(user: current_user)
redirect_to_rubygem(subscription.try(:save))
redirect_to_rubygem(subscription&.save)
end

def destroy
subscription = @rubygem.subscriptions.find_by_user_id(current_user.try(:id))
redirect_to_rubygem(subscription.try(:destroy))
subscription = @rubygem.subscriptions.find_by_user_id(current_user&.id)
redirect_to_rubygem(subscription&.destroy)
end

protected
@@ -1,6 +1,6 @@
module PagesHelper
def version_number
version.try(:number) || "0.0.0"
version&.number || "0.0.0"
end

def version
@@ -1,6 +1,6 @@
module RubygemsHelper
def pluralized_licenses_header(version)
t("rubygems.show.licenses_header").pluralize(version.try(:licenses).try(:length) || 0)
t("rubygems.show.licenses_header").pluralize(version&.licenses&.length || 0)
end

def formatted_licenses(license_names)
@@ -107,7 +107,7 @@ def show_all_versions_link?(rubygem)

def latest_version_number(rubygem)
return rubygem.version if rubygem.respond_to?(:version)
(rubygem.latest_version || rubygem.versions.last).try(:number)
(rubygem.latest_version || rubygem.versions.last)&.number
end

def github_params(link)
@@ -32,7 +32,7 @@ def perform
end
StatsD.gauge("fastly_log_processor.processed_count", processed_count)
rescue
log_ticket.update(status: "failed") if log_ticket
log_ticket&.update(status: "failed")
raise
end
statsd_count_success :perform, "fastly_log_processor.perform"
@@ -18,30 +18,30 @@ def as_indexed_json(_options = {}) # rubocop:disable Metrics/MethodLength
{
name: name,
downloads: downloads,
version: latest_version.try(:number),
version_downloads: latest_version.try(:downloads_count),
platform: latest_version.try(:platform),
authors: latest_version.try(:authors),
info: latest_version.try(:info),
licenses: latest_version.try(:licenses),
metadata: latest_version.try(:metadata),
sha: latest_version.try(:sha256_hex),
version: latest_version&.number,
version_downloads: latest_version&.downloads_count,
platform: latest_version&.platform,
authors: latest_version&.authors,
info: latest_version&.info,
licenses: latest_version&.licenses,
metadata: latest_version&.metadata,
sha: latest_version&.sha256_hex,
project_uri: "#{Gemcutter::PROTOCOL}://#{Gemcutter::HOST}/gems/#{name}",
gem_uri: "#{Gemcutter::PROTOCOL}://#{Gemcutter::HOST}/gems/#{latest_version.try(:full_name)}.gem",
homepage_uri: versioned_links.try(:homepage_uri),
wiki_uri: versioned_links.try(:wiki_uri),
documentation_uri: versioned_links.try(:documentation_uri),
mailing_list_uri: versioned_links.try(:mailing_list_uri),
source_code_uri: versioned_links.try(:source_code_uri),
bug_tracker_uri: versioned_links.try(:bug_tracker_uri),
changelog_uri: versioned_links.try(:changelog_uri),
gem_uri: "#{Gemcutter::PROTOCOL}://#{Gemcutter::HOST}/gems/#{latest_version&.full_name}.gem",
homepage_uri: versioned_links&.homepage_uri,
wiki_uri: versioned_links&.wiki_uri,
documentation_uri: versioned_links&.documentation_uri,
mailing_list_uri: versioned_links&.mailing_list_uri,
source_code_uri: versioned_links&.source_code_uri,
bug_tracker_uri: versioned_links&.bug_tracker_uri,
changelog_uri: versioned_links&.changelog_uri,
yanked: versions.none?(&:indexed?),
summary: latest_version.try(:summary),
description: latest_version.try(:description),
summary: latest_version&.summary,
description: latest_version&.description,
updated: updated_at,
dependencies: {
development: deps.try(:select) { |r| r.rubygem && r.scope == "development" },
runtime: deps.try(:select) { |r| r.rubygem && r.scope == "runtime" }
development: deps&.select { |r| r.rubygem && r.scope == "development" },
runtime: deps&.select { |r| r.rubygem && r.scope == "runtime" }
}
}
end
@@ -29,7 +29,7 @@ def self.runtime
end

def name
unresolved_name || rubygem.try(:name)
unresolved_name || rubygem&.name
end

def payload
@@ -54,7 +54,7 @@ def download_uri

# excluded from metadata_uri_set? check
def homepage_uri
version.metadata["homepage_uri"].presence || linkset.try(:home)
version.metadata["homepage_uri"].presence || linkset&.home
end

# define getters for each of the uris (both short `home` or long `homepage_uri` versions)
@@ -134,11 +134,11 @@ def owned_by?(user)
end

def to_s
versions.most_recent.try(:to_title) || name
versions.most_recent&.to_title || name
end

def downloads
gem_download.try(:count) || 0
gem_download&.count || 0
end

def links(version = versions.most_recent)
@@ -161,7 +161,7 @@ def self.find_from_slug!(rubygem_id, slug)
end

def self.rubygem_name_for(full_name)
find_by(full_name: full_name).try(:rubygem).try(:name)
find_by(full_name: full_name)&.rubygem&.name
end

def self.created_between(start_time, end_time)
@@ -241,7 +241,7 @@ def slug
end

def downloads_count
gem_download.try(:count) || 0
gem_download&.count || 0
end

def payload
@@ -1,5 +1,5 @@
<% @title = t('.title', name: @rubygem.name) %>
<% @subtitle = @latest_version.try(:slug) %>
<% @subtitle = @latest_version&.slug %>

<div class="l-overflow">
<div class="l-colspan--l colspan--l--has-border">
@@ -8,7 +8,7 @@
<%= link_to rubygem_path(rubygem), :class => 't-list__item' do %>
<strong><%= rubygem.name %></strong> <%= dependency.clean_requirements %>
<% end %>
<% elsif dependency.try(:name) %>
<% elsif dependency&.name %>
<p class="t-list__item gem__unregistered" title="unregistered gem">
<strong><%= dependency.name %></strong> <%= dependency.clean_requirements %>
</p>
@@ -1,5 +1,5 @@
<% @title = @rubygem.name %>
<% @subtitle = @latest_version.try(:slug) %>
<% @subtitle = @latest_version&.slug %>

<% content_for :head do %>
<%= auto_discovery_link_tag(:atom, rubygem_versions_path(@rubygem, format: "atom"), {title: "#{@rubygem.name} Version Feed"}) %>

0 comments on commit fb2441f

Please sign in to comment.
You can’t perform that action at this time.