From 28f91f355f0858c9b0e07f21073ed0d925d93059 Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Tue, 8 Aug 2023 17:34:08 +0300 Subject: [PATCH] upto 3.2 --- ...cop-https---relaxed-ruby-style-rubocop-yml | 153 ++++++++++++++++++ .rubocop.yml | 75 +++++++++ app/controllers/beers_controller.rb | 19 +-- app/controllers/breweries_controller.rb | 32 ++-- app/models/beer.rb | 2 +- app/models/brewery.rb | 2 +- app/models/concerns/rating_average.rb | 28 ++-- 7 files changed, 270 insertions(+), 41 deletions(-) create mode 100644 .rubocop-https---relaxed-ruby-style-rubocop-yml create mode 100644 .rubocop.yml diff --git a/.rubocop-https---relaxed-ruby-style-rubocop-yml b/.rubocop-https---relaxed-ruby-style-rubocop-yml new file mode 100644 index 0000000..10f1193 --- /dev/null +++ b/.rubocop-https---relaxed-ruby-style-rubocop-yml @@ -0,0 +1,153 @@ +# Relaxed.Ruby.Style +## Version 2.5 + +Style/Alias: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylealias + +Style/AsciiComments: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styleasciicomments + +Style/BeginBlock: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylebeginblock + +Style/BlockDelimiters: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styleblockdelimiters + +Style/CommentAnnotation: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylecommentannotation + +Style/Documentation: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styledocumentation + +Layout/DotPosition: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#layoutdotposition + +Style/DoubleNegation: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styledoublenegation + +Style/EndBlock: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styleendblock + +Style/FormatString: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styleformatstring + +Style/IfUnlessModifier: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styleifunlessmodifier + +Style/Lambda: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylelambda + +Style/ModuleFunction: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylemodulefunction + +Style/MultilineBlockChain: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylemultilineblockchain + +Style/NegatedIf: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylenegatedif + +Style/NegatedWhile: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylenegatedwhile + +Style/NumericPredicate: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylenumericpredicate + +Style/ParallelAssignment: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styleparallelassignment + +Style/PercentLiteralDelimiters: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylepercentliteraldelimiters + +Style/PerlBackrefs: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styleperlbackrefs + +Style/Semicolon: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylesemicolon + +Style/SignalException: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylesignalexception + +Style/SingleLineBlockParams: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylesinglelineblockparams + +Style/SingleLineMethods: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylesinglelinemethods + +Layout/SpaceBeforeBlockBraces: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#layoutspacebeforeblockbraces + +Layout/SpaceInsideParens: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#layoutspaceinsideparens + +Style/SpecialGlobalVars: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylespecialglobalvars + +Style/StringLiterals: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylestringliterals + +Style/TrailingCommaInArguments: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styletrailingcommainarguments + +Style/TrailingCommaInArrayLiteral: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styletrailingcommainarrayliteral + +Style/TrailingCommaInHashLiteral: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#styletrailingcommainhashliteral + +Style/SymbolArray: + Enabled: false + StyleGuide: http://relaxed.ruby.style/#stylesymbolarray + +Style/WhileUntilModifier: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylewhileuntilmodifier + +Style/WordArray: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#stylewordarray + +Lint/AmbiguousRegexpLiteral: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#lintambiguousregexpliteral + +Lint/AssignmentInCondition: + Enabled: false + StyleGuide: https://relaxed.ruby.style/#lintassignmentincondition + +Layout/LineLength: + Enabled: false + +Metrics: + Enabled: false + diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..9ef8ade --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,75 @@ +inherit_from: +- https://relaxed.ruby.style/rubocop.yml + +AllCops: + Exclude: + - __test/** + - test/** + - test/channels/application_cable/** # for whatever reason this directory was not excluded even though two previous exclusion lines + - bin/** + - spec/**/* + - config/**/* + - db/**/* + - Gemfile + - "*.gemspec" + - bundler/**/* + - vendor/bundle/**/* # travis with --deployment + TargetRubyVersion: 3.1.2 + NewCops: enable + +Style/PercentLiteralDelimiters: + PreferredDelimiters: + default: () + '%i': '()' + '%I': '()' + '%r': '{}' + '%w': '()' + '%W': '()' + +Style/FormatString: + EnforcedStyle: percent + +Style/FrozenStringLiteralComment: + Enabled: false + +Style/WordArray: + Enabled: true + MinSize: 3 + +Style/EmptyMethod: + Enabled: false + +Style/SymbolArray: + Enabled: false + +Gemspec/OrderedDependencies: + Enabled: false + +Style/PerlBackrefs: + Enabled: true + +Layout/SpaceInsideParens: + Enabled: true + +Style/SpecialGlobalVars: + Enabled: true + +Style/Alias: + Enabled: true + +Style/BeginBlock: + Enabled: true + +Style/OpenStructUse: + Enabled: false + +Naming/MethodParameterName: + AllowedNames: + - cn + +Metrics/BlockLength: + Enabled: false + +Metrics/MethodLength: + Enabled: true + Max: 15 diff --git a/app/controllers/beers_controller.rb b/app/controllers/beers_controller.rb index 2e75144..550f056 100644 --- a/app/controllers/beers_controller.rb +++ b/app/controllers/beers_controller.rb @@ -1,5 +1,5 @@ class BeersController < ApplicationController - before_action :set_beer, only: %i[ show edit update destroy ] + before_action :set_beer, only: %i[show edit update destroy] # GET /beers or /beers.json def index @@ -62,13 +62,14 @@ class BeersController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_beer - @beer = Beer.find(params[:id]) - end - # Only allow a list of trusted parameters through. - def beer_params - params.require(:beer).permit(:name, :style, :brewery_id) - end + # Use callbacks to share common setup or constraints between actions. + def set_beer + @beer = Beer.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def beer_params + params.require(:beer).permit(:name, :style, :brewery_id) + end end diff --git a/app/controllers/breweries_controller.rb b/app/controllers/breweries_controller.rb index bc95fe7..639d151 100644 --- a/app/controllers/breweries_controller.rb +++ b/app/controllers/breweries_controller.rb @@ -1,5 +1,5 @@ class BreweriesController < ApplicationController - before_action :set_brewery, only: %i[ show edit update destroy ] + before_action :set_brewery, only: %i[show edit update destroy] before_action :authenticate, only: [:destroy] # GET /breweries or /breweries.json @@ -59,21 +59,23 @@ class BreweriesController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_brewery - @brewery = Brewery.find(params[:id]) - end - # Only allow a list of trusted parameters through. - def brewery_params - params.require(:brewery).permit(:name, :year) - end + # Use callbacks to share common setup or constraints between actions. + def set_brewery + @brewery = Brewery.find(params[:id]) + end - def authenticate - admin_accounts = {"martti" => "anaatti", "moi"=> "hei"} - authenticate_or_request_with_http_basic do |username, password| - raise "Wrong username or password" unless admin_accounts.key? username and admin_accounts[username] == password - return true - end + # Only allow a list of trusted parameters through. + def brewery_params + params.require(:brewery).permit(:name, :year) + end + + def authenticate + admin_accounts = { "martti" => "anaatti", "moi" => "hei" } + authenticate_or_request_with_http_basic do |username, password| + raise "Wrong username or password" unless admin_accounts.key?(username) && admin_accounts[username] == password + + return true end + end end diff --git a/app/models/beer.rb b/app/models/beer.rb index 4ce2afd..4cafe00 100644 --- a/app/models/beer.rb +++ b/app/models/beer.rb @@ -5,6 +5,6 @@ class Beer < ApplicationRecord has_many :ratings, dependent: :destroy def to_s - "#{self.name}, #{(Brewery.find_by_id self.brewery_id).name}" + "#{name}, #{(Brewery.find_by_id brewery_id).name}" end end diff --git a/app/models/brewery.rb b/app/models/brewery.rb index c97d374..cc722c9 100644 --- a/app/models/brewery.rb +++ b/app/models/brewery.rb @@ -12,6 +12,6 @@ class Brewery < ApplicationRecord def restart self.year = 2023 - puts "changed year to #{2023}" + puts "changed year to 2023" end end diff --git a/app/models/concerns/rating_average.rb b/app/models/concerns/rating_average.rb index 1151769..72d756b 100644 --- a/app/models/concerns/rating_average.rb +++ b/app/models/concerns/rating_average.rb @@ -5,11 +5,11 @@ module RatingAverage total_score = 0 count = 0 - if defined? self.beers - beers = self.beers - else - beers = self - end + beers = if defined? self.beers + self.beers + else + self + end beers.each do |beer| count += beer.ratings.count @@ -18,24 +18,22 @@ module RatingAverage end end - if count == 0 - return 0 - end + return 0 unless count != 0 - return total_score / count + total_score / count end def count_ratings - if defined? self.beers - beers = self.beers - else - beers = self - end + beers = if defined? self.beers + self.beers + else + self + end count = 0 beers.each do |beer| count += beer.ratings.count end - return count + count end end