Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standard-rails and apply suggested updates #111

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
plugins:
- standard-performance
- standard-rails
ignore:
- 'lib/gpio.rb':
- Lint/Loop
- 'lib/pour_reader.rb' # Used by mruby
- '**/*':
- Rails/EnvironmentVariableAccess

4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ group :test, :development do
gem "pry"
gem "rspec-rails"
gem "selenium-webdriver"
gem "standard", require: false
gem "standard"
gem "standard-performance"
gem "standard-rails"
end

group :no_require do
Expand Down
55 changes: 38 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ GEM
importmap-rails (1.1.5)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
json (2.6.3)
json (2.7.1)
jsonapi-renderer (0.2.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
loofah (2.19.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -205,9 +206,10 @@ GEM
racc (~> 1.4)
oj (3.13.23)
pagy (6.0.2)
parallel (1.22.1)
parser (3.2.1.0)
parallel (1.24.0)
parser (3.3.0.4)
ast (~> 2.4.1)
racc
pg (1.4.5)
propshaft (0.6.4)
actionpack (>= 7.0.0)
Expand Down Expand Up @@ -254,8 +256,8 @@ GEM
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.7.0)
rexml (3.2.5)
regexp_parser (2.9.0)
rexml (3.2.6)
rspec-core (3.12.0)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.1)
Expand All @@ -273,38 +275,55 @@ GEM
rspec-mocks (~> 3.11)
rspec-support (~> 3.11)
rspec-support (3.12.0)
rubocop (1.44.1)
rubocop (1.59.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.0.0)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.24.1, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.26.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-performance (1.15.2)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.11.0)
rubocop-performance (1.20.2)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
rubocop-rails (2.23.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (1.13.0)
ruby-statistics (3.0.1)
rubyzip (2.3.2)
selenium-webdriver (4.7.1)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
stackprof (0.2.23)
standard (1.24.3)
standard (1.33.0)
language_server-protocol (~> 3.17.0.2)
rubocop (= 1.44.1)
rubocop-performance (= 1.15.2)
lint_roller (~> 1.0)
rubocop (~> 1.59.0)
standard-custom (~> 1.0.0)
standard-performance (~> 1.3)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.3.1)
lint_roller (~> 1.1)
rubocop-performance (~> 1.20.2)
standard-rails (1.0.2)
lint_roller (~> 1.0)
rubocop-rails (~> 2.23.1)
thor (1.2.1)
timeout (0.3.1)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
unicode-display_width (2.5.0)
websocket (1.2.9)
websocket-extensions (0.1.5)
xpath (3.2.0)
Expand Down Expand Up @@ -341,6 +360,8 @@ DEPENDENCIES
selenium-webdriver
stackprof
standard
standard-performance
standard-rails
websocket-driver!

RUBY VERSION
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/activity_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ActivityController < ApplicationController
def recent
pours = Pour.where("volume IS NOT NULL").order("created_at desc").limit(params[:limit] || 10)
pours = Pour.where.not(volume: nil).order("created_at desc").limit(params[:limit] || 10)

render json: append_data(pours).to_json
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/pours_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index
def destroy
keg = Keg.find(params[:keg_id])
pour = keg.pours.find(params[:id])
pour.destroy
pour.destroy!
redirect_to :back
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This for example is wrong. It was replaced by redirect_back_or_to

end
end
2 changes: 1 addition & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def update

def destroy
user = User.find(params[:id])
user.destroy
user.destroy!
redirect_to admin_users_path
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/homepage_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class HomepageController < ApplicationController
def index
@beer_taps = BeerTap.order(:display_order)
@pours = Pour.where("volume IS NOT NULL").order("created_at desc").limit(@beer_taps.size * 3)
@pours = Pour.where.not(volume: nil).order("created_at desc").limit(@beer_taps.size * 3)
end
end
6 changes: 3 additions & 3 deletions app/controllers/pours_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def index
end

def new
@users = User.where(["name != ?", "Guest"]).order(:name)
@users = User.where.not(name: "Guest").order(:name)
end

def create
Expand All @@ -27,15 +27,15 @@ def show

def edit
@pour = Pour.find(params[:id])
@users = User.where(["name != ?", "Guest"]).order(:name)
@users = User.where.not(name: "Guest").order(:name)
end

def update
pour = Pour.find(params[:id])
if params.require(:pour).keys == ["finished_at"]
pour.finish_pour(params[:pour][:finished_at])
else
pour.update(pour_params)
pour.update!(pour_params)
end
redirect_to(params[:back_to] || root_path)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/temperature_sensors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def readings
expires_now

sensor = TemperatureSensor.find(params[:sensor_id])
start_time = Time.at(params[:start_timestamp].to_i)
start_time = Time.at(params[:start_timestamp].to_i).utc
end_time = start_time + params[:duration].to_i

render json: Oj.dump(sensor.temp_data(start_time, end_time))
Expand Down
2 changes: 1 addition & 1 deletion app/models/achievement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class << self

def initialize(attributes = {})
attributes&.each do |name, value|
send("#{name}=", value) if respond_to? name.to_sym
send(:"#{name}=", value) if respond_to? name.to_sym
end
end

Expand Down
3 changes: 3 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
12 changes: 6 additions & 6 deletions app/models/beer_tap.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class BeerTap < ActiveRecord::Base
class BeerTap < ApplicationRecord
FLOZ_PER_ML = BigDecimal("0.033814")

belongs_to :temperature_sensor, optional: true
belongs_to :kegerator, optional: true

has_many :kegs, inverse_of: :beer_tap
has_one :active_keg, lambda { where(active: true) }, class_name: "Keg", inverse_of: :beer_tap
has_many :kegs, inverse_of: :beer_tap, dependent: :nullify
has_one :active_keg, lambda { where(active: true) }, class_name: "Keg", inverse_of: :beer_tap, dependent: nil

scope :unused, -> { joins("LEFT JOIN kegs ON kegs.beer_tap_id = beer_taps.id AND kegs.active = true").where(kegs: {id: nil}) }

Expand All @@ -14,19 +14,19 @@ def self.for_select
end

def activate
return true unless valve_pin.present?
return true if valve_pin.blank?
valve_pin_gpio.on
true
end

def deactivate
return true unless valve_pin.present?
return true if valve_pin.blank?
valve_pin_gpio.off
true
end

def toggle_cleaning
return true unless valve_pin.present?
return true if valve_pin.blank?
if valve_pin_gpio.off?
valve_pin_gpio.on
else
Expand Down
25 changes: 13 additions & 12 deletions app/models/keg.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Keg < ActiveRecord::Base
class Keg < ApplicationRecord
# Capacity is stored in fluid ounces
KEG_CAPACITIES = [
["1/2 Barrel", 1980],
Expand All @@ -9,17 +9,17 @@ class Keg < ActiveRecord::Base

belongs_to :beer_tap, optional: true

has_many :pours, inverse_of: :keg
has_one :active_pour, lambda { where(finished_at: nil) }, class_name: "Pour", inverse_of: :keg
has_many :pours, inverse_of: :keg, dependent: :nullify
has_one :active_pour, lambda { where(finished_at: nil) }, class_name: "Pour", inverse_of: :keg, dependent: nil

validates :srm, numericality: {greater_than: 0, less_than: 41, allow_blank: true}

def display_name
[name, brewery].reject(&:blank?).join(" by ")
[name, brewery].compact_blank.join(" by ")
end

def completed_pours
pours.where("finished_at IS NOT NULL")
pours.where.not(finished_at: nil)
end

def poured
Expand All @@ -43,7 +43,7 @@ def srm_rgb
def start_pour(user = User.guest)
pour = active_pour || pours.new
pour.user = user if user
pour.save
pour.save!
beer_tap.activate
pour
end
Expand All @@ -55,16 +55,16 @@ def tap_it(tap_id)
end

self.beer_tap_id = tap_id
self.started_at ||= Time.now
self.started_at ||= Time.current
self.finished_at = nil
self.active = true
save
save # standard:disable Rails/SaveBang
end

def untap_it
self.finished_at = Time.now
self.finished_at = Time.current
self.active = false
save
save # standard:disable Rails/SaveBang
end

def temp_data
Expand All @@ -84,12 +84,13 @@ def projected_empty
if poured == 0 || remaining == 0
"No projection available"
else
Time.now + (Time.now - started_at) / poured * remaining
now = Time.current
now + (now - started_at) / poured * remaining
end
end

def days_on_tap
return 0 unless started_at
(((finished_at || Time.now) - started_at) / 1.day).round
(((finished_at || Time.current) - started_at) / 1.day).round
end
end
12 changes: 6 additions & 6 deletions app/models/kegerator.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Kegerator < ActiveRecord::Base
class Kegerator < ApplicationRecord
belongs_to :temperature_sensor
has_many :beer_taps
has_many :beer_taps, dependent: :destroy

def check(reading)
return if control_pin.blank? || min_temp.blank? || max_temp.blank?

if pin.on? && reading.temp_f < min_temp
self.last_shutdown = Time.now
save
self.last_shutdown = Time.current
save!
pin.off
elsif pin.off? && reading.temp_f > max_temp && (last_shutdown.nil? || last_shutdown < 5.minutes.ago)
pin.on
Expand Down Expand Up @@ -35,7 +35,7 @@ def cooling?
# This should send a message on the first alarm reading and every 30 minutes after that
def send_alarm_message(reading)
last_good = temperature_sensor.temperature_readings.where(["temp_f < ?", alarm_temp]).order("created_at DESC").first.try(:created_at)
return if last_good.nil? || (((Time.now - last_good) / 60).round % 30) != 1
return if last_good.nil? || (((Time.current - last_good) / 60).round % 30) != 1

# Try to reset the GFCI
pin.off
Expand All @@ -59,7 +59,7 @@ def report_dms(reading)
http.request(Net::HTTP::Get.new(uri.request_uri))
end
rescue => e
puts "Failed to connect to DMS with: #{e.inspect} (#{Time.now})"
Rails.logger.debug { "Failed to connect to DMS with: #{e.inspect} (#{Time.current})" }
end
end

Expand Down
Loading