aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar √Čtienne Deparis 2022-07-12 19:19:49 +0200
committerGravatar √Čtienne Deparis 2022-07-12 19:24:57 +0200
commit7f6f4517084478b118154f6130f8259aad555e31 (patch)
treeb1166fc94720f5e55df26ee20a6fedf287aecc54
parent338e8d6874d204bd36b7aaef730f77fc649fa415 (diff)
downloadbiberons-7f6f4517084478b118154f6130f8259aad555e31.tar.gz
biberons-7f6f4517084478b118154f6130f8259aad555e31.tar.xz
biberons-7f6f4517084478b118154f6130f8259aad555e31.zip
refactor: Silence rubocop and reek warnings, improve code
-rw-r--r--.reek.yml8
-rw-r--r--lib/biberon.rb26
-rw-r--r--lib/biberon/models/biberon.rb1
-rw-r--r--lib/tasks/assets.rake2
-rw-r--r--lib/tasks/server.rake12
-rw-r--r--views/baby.slim8
6 files changed, 34 insertions, 23 deletions
diff --git a/.reek.yml b/.reek.yml
index b367551..41c4bf1 100644
--- a/.reek.yml
+++ b/.reek.yml
@@ -4,3 +4,11 @@ detectors:
accept:
- _
- e
+ - r
+ DuplicateMethodCall:
+ allow_calls:
+ - R18n.t
+ TooManyStatements:
+ max_statements: 15
+ TooManyInstanceVariables:
+ max_instance_variables: 8
diff --git a/lib/biberon.rb b/lib/biberon.rb
index 0a8df8b..56956cc 100644
--- a/lib/biberon.rb
+++ b/lib/biberon.rb
@@ -13,10 +13,11 @@ module Biberon
plugin :render, engine: 'slim', layout: 'layout'
plugin :public, gzip: true, brotli: true
plugin :route_csrf
- plugin :sessions, secret: ENV['BIBERON_SESSION_SECRET'], key: 'biberon_auth'
- plugin :cookies, domain: ENV['BIBERON_COOKIE_DOMAIN'],
- path: '/', secure: ENV['RACK_ENV'] != 'development',
- httponly: true
+ plugin :sessions, secret: ENV.fetch('BIBERON_SESSION_SECRET'),
+ key: 'biberon_auth'
+ plugin :cookies, domain: ENV.fetch('BIBERON_COOKIE_DOMAIN'),
+ path: '/', httponly: true,
+ secure: ENV.fetch('RACK_ENV', 'development') != 'development' # rubocop:disable Layout/LineLength
# Log to stderr
plugin :common_logger
@@ -25,6 +26,7 @@ module Biberon
r.public
check_csrf!
+ @_cookie = parse_cookie
R18n.set cookie('l', 'fr')
@settings = cookie('c', 'c,t,u,v,w').split(',')
@@ -125,7 +127,7 @@ module Biberon
'%Y%m%d %H:%M'
)
bib = @baby.biberons_dataset.where(datetime: bib_id)
- bib.destroy if bib
+ bib&.destroy
redirect_to_date(r, @baby.id, params['date'])
end
end
@@ -140,22 +142,22 @@ module Biberon
end
def cookie(key, default)
- parse_cookie! unless @_cookie
@_cookie[key] || default
end
- def parse_cookie!
- @_cookie = {}
- return @_cookie unless request.cookies['biberon_settings']
- request.cookies['biberon_settings'].split(';').each do |cook|
- cook.split(':').tap { |data| @_cookie[data[0]] = data[1] || '-' }
+ def parse_cookie
+ bib_settings = request.cookies['biberon_settings'] || ''
+ bib_settings.split(';').to_h do |cook|
+ cook.split(':').tap { |data| data[1] ||= '-' }
end
- @_cookie
end
+ # rubocop:disable Layout/LineLength
+ # :reek:FeatureEnvy
def logger(message, level = :info)
env = request.env
$stderr << "#{env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR'] || '-'} - #{env['REMOTE_USER'] || '-'} [#{Time.now.strftime('%d/%b/%Y:%H:%M:%S %z')}] [#{level.to_s.upcase}] #{message}\n"
end
+ # rubocop:enable Layout/LineLength
end
end
diff --git a/lib/biberon/models/biberon.rb b/lib/biberon/models/biberon.rb
index 26a4c7c..df38469 100644
--- a/lib/biberon/models/biberon.rb
+++ b/lib/biberon/models/biberon.rb
@@ -5,6 +5,7 @@ module Biberon
class Biberon < Sequel::Model
many_to_one :baby
+ # :reek:NilCheck
def validate
super
errors.add(:baby, R18n.t.biberon.validations.baby) if baby.nil?
diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake
index 0ece3a5..525cf68 100644
--- a/lib/tasks/assets.rake
+++ b/lib/tasks/assets.rake
@@ -27,7 +27,7 @@ namespace :assets do
desc 'Compress a file to gzip'
rule '.gz' => [proc { |tn| tn.delete_suffix('.gz') }] do |t|
- IO.write(t.name, Zlib.gzip(IO.read(t.source)))
+ File.write(t.name, Zlib.gzip(File.read(t.source)))
end
desc 'Compress a file to brotli'
diff --git a/lib/tasks/server.rake b/lib/tasks/server.rake
index a8e8c82..08b6eb7 100644
--- a/lib/tasks/server.rake
+++ b/lib/tasks/server.rake
@@ -9,7 +9,7 @@ namespace :server do
warn Rainbow('No pid file found').red
exit 1
end
- pid = IO.read('tmp/puma.pid').strip.to_i
+ pid = File.read('tmp/puma.pid').strip.to_i
Process.kill('TERM', pid)
File.unlink 'tmp/puma.pid'
puts Rainbow('Done').green
@@ -17,11 +17,10 @@ namespace :server do
desc 'Start the server'
task :start do
- ENV['RACK_ENV'] ||= 'development'
+ env = ENV.fetch 'RACK_ENV', 'development'
cmd = ['rackup', '-s', 'puma',
- '-E', ENV['RACK_ENV'],
- '-P', 'tmp/puma.pid']
- if ENV['RACK_ENV'] == 'development'
+ '-E', env, '-P', 'tmp/puma.pid']
+ if env == 'development'
# Ensure we don't keep cached/compressed version of the assets
Rake::Task['assets:clean_compressed'].invoke
Rake::Task['assets:all'].invoke
@@ -36,7 +35,8 @@ namespace :server do
desc 'Publish on upstream server'
task publish: ['assets:all'] do
- cmd = ['rsync', '-rltv', '--delete', '.', ENV['BIBERON_PUBLISH_FOLDER']]
+ cmd = ['rsync', '-rltv', '--delete', '.',
+ ENV.fetch('BIBERON_PUBLISH_FOLDER')]
exclude_files = ['.*', 'node_modules', 'package*.json', 'assets',
'tmp*', 'db/biberon.db']
cmd.insert(3, *exclude_files.map { |f| "--exclude \"#{f}\"" })
diff --git a/views/baby.slim b/views/baby.slim
index f6a4b2e..52b2b42 100644
--- a/views/baby.slim
+++ b/views/baby.slim
@@ -116,18 +116,18 @@ form action="/#{@baby.id}/add" method='post'
= R18n.t.biberon.temperature
input#temperature type='number' name='temperature' min='30' step='0.1'
- div.row
- div.column.column-33
+ .row
+ .column.column-33
input#feces type='checkbox' name='feces'
label.label-inline for='feces' = R18n.t.biberon.feces
- if @settings.include?('u')
- div.column.column-33
+ .column.column-33
input#urines type='checkbox' name='urines'
label.label-inline for='urines' = R18n.t.biberon.urines
- if @settings.include?('v')
- div.column.column-33
+ .column.column-33
input#vitamind type='checkbox' name='vitamind'
label.label-inline for='vitamind' = R18n.t.biberon.vitamind