From f6166ff6538644fb3b8c94425ac6ffb31f0e4b8e Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Tue, 16 Jan 2024 15:12:44 -0500 Subject: [PATCH] Add standard-rails and apply suggested updates --- .standard.yml | 6 ++ Gemfile | 4 +- Gemfile.lock | 55 +++++++++++++------ app/controllers/activity_controller.rb | 2 +- app/controllers/admin/pours_controller.rb | 2 +- app/controllers/admin/users_controller.rb | 2 +- app/controllers/homepage_controller.rb | 2 +- app/controllers/pours_controller.rb | 6 +- .../temperature_sensors_controller.rb | 2 +- app/models/achievement.rb | 2 +- app/models/application_record.rb | 3 + app/models/beer_tap.rb | 12 ++-- app/models/keg.rb | 25 +++++---- app/models/kegerator.rb | 12 ++-- app/models/pour.rb | 21 +++---- app/models/temperature_reading.rb | 2 +- app/models/temperature_sensor.rb | 18 +++--- app/models/user.rb | 8 +-- ..._add_started_at_and_finished_at_to_kegs.rb | 6 +- ...0223060306_remove_change_type_from_pour.rb | 2 +- ...130314210817_add_beer_attributes_to_keg.rb | 8 ++- lib/faye_notifier.rb | 2 +- lib/hubot.rb | 2 +- lib/tap_monitor.rb | 22 ++++---- lib/temp.rb | 4 +- spec/factories/pour.rb | 2 +- spec/models/achievement_spec.rb | 4 +- spec/models/pour_spec.rb | 7 +-- spec/rails_helper.rb | 4 +- spec/requests/api/kegs_spec.rb | 4 +- spec/requests/api/pours_spec.rb | 10 ++-- spec/requests/api/taps_spec.rb | 2 +- spec/requests/api/users_spec.rb | 4 +- 33 files changed, 152 insertions(+), 115 deletions(-) create mode 100644 app/models/application_record.rb diff --git a/.standard.yml b/.standard.yml index a41cde5..89c0411 100644 --- a/.standard.yml +++ b/.standard.yml @@ -1,4 +1,10 @@ +plugins: + - standard-performance + - standard-rails ignore: - 'lib/gpio.rb': - Lint/Loop - 'lib/pour_reader.rb' # Used by mruby + - '**/*': + - Rails/EnvironmentVariableAccess + diff --git a/Gemfile b/Gemfile index 2fff1a0..1d52228 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 6123898..1aefd72 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -273,22 +275,28 @@ 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) @@ -296,15 +304,26 @@ GEM 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) @@ -341,6 +360,8 @@ DEPENDENCIES selenium-webdriver stackprof standard + standard-performance + standard-rails websocket-driver! RUBY VERSION diff --git a/app/controllers/activity_controller.rb b/app/controllers/activity_controller.rb index ada8a5f..693e90e 100644 --- a/app/controllers/activity_controller.rb +++ b/app/controllers/activity_controller.rb @@ -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 diff --git a/app/controllers/admin/pours_controller.rb b/app/controllers/admin/pours_controller.rb index a89ead9..e12c775 100644 --- a/app/controllers/admin/pours_controller.rb +++ b/app/controllers/admin/pours_controller.rb @@ -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 end end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 7e5d96f..22a00ab 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -21,7 +21,7 @@ def update def destroy user = User.find(params[:id]) - user.destroy + user.destroy! redirect_to admin_users_path end diff --git a/app/controllers/homepage_controller.rb b/app/controllers/homepage_controller.rb index ae6e2d0..cd97060 100644 --- a/app/controllers/homepage_controller.rb +++ b/app/controllers/homepage_controller.rb @@ -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 diff --git a/app/controllers/pours_controller.rb b/app/controllers/pours_controller.rb index f380bf6..8d389a3 100644 --- a/app/controllers/pours_controller.rb +++ b/app/controllers/pours_controller.rb @@ -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 @@ -27,7 +27,7 @@ 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 @@ -35,7 +35,7 @@ def update 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 diff --git a/app/controllers/temperature_sensors_controller.rb b/app/controllers/temperature_sensors_controller.rb index 6107a88..5078367 100644 --- a/app/controllers/temperature_sensors_controller.rb +++ b/app/controllers/temperature_sensors_controller.rb @@ -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)) diff --git a/app/models/achievement.rb b/app/models/achievement.rb index 12e9f40..d11dc80 100644 --- a/app/models/achievement.rb +++ b/app/models/achievement.rb @@ -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 diff --git a/app/models/application_record.rb b/app/models/application_record.rb new file mode 100644 index 0000000..b63caeb --- /dev/null +++ b/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + primary_abstract_class +end diff --git a/app/models/beer_tap.rb b/app/models/beer_tap.rb index 9b5a2c9..9679b11 100644 --- a/app/models/beer_tap.rb +++ b/app/models/beer_tap.rb @@ -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}) } @@ -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 diff --git a/app/models/keg.rb b/app/models/keg.rb index 56ce21e..5f4defd 100644 --- a/app/models/keg.rb +++ b/app/models/keg.rb @@ -1,4 +1,4 @@ -class Keg < ActiveRecord::Base +class Keg < ApplicationRecord # Capacity is stored in fluid ounces KEG_CAPACITIES = [ ["1/2 Barrel", 1980], @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/models/kegerator.rb b/app/models/kegerator.rb index c4c4d85..baf4362 100644 --- a/app/models/kegerator.rb +++ b/app/models/kegerator.rb @@ -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 @@ -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 @@ -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 diff --git a/app/models/pour.rb b/app/models/pour.rb index 72e747e..ba094a2 100644 --- a/app/models/pour.rb +++ b/app/models/pour.rb @@ -5,16 +5,16 @@ # Swissflow: between 5400 to 6100 pulses/L # ml * 0.033814 = fl oz -class Pour < ActiveRecord::Base +class Pour < ApplicationRecord belongs_to :keg belongs_to :user before_save :calculate_duration after_save :set_last_pour_at - scope :finished, lambda { where("finished_at IS NOT NULL") } + scope :finished, lambda { where.not(finished_at: nil) } scope :non_guest, lambda { where("user_id > 0") } - scope :for_listing, lambda { where("volume IS NOT NULL").includes(:keg, :user).order("created_at desc") } + scope :for_listing, lambda { where.not(volume: nil).includes(:keg, :user).order("created_at desc") } scope :recent, lambda { where("created_at > ?", 30.days.ago) } def self.between_dates(start_time:, end_time:) @@ -28,14 +28,14 @@ def finish_pour(time) self.finished_at = time if sensor_ticks.to_i == 0 keg.beer_tap.deactivate - destroy + destroy! else - save + save! end end def complete? - finished_at.nil? ? false : (Time.now - finished_at) > Setting.pour_timeout + finished_at.nil? ? false : (Time.current - finished_at) > Setting.pour_timeout end private @@ -45,10 +45,11 @@ def calculate_duration end def set_last_pour_at - return unless finished_at && (user_id_changed? || finished_at_changed?) - User.find(changes["user_id"] || [user_id]).each do |user| - user.last_pour_at = user.pours.finished.maximum(:created_at) - user.save + if finished_at && (user_id_changed? || finished_at_changed?) + User.find(changes["user_id"] || [user_id]).each do |user| + user.last_pour_at = user.pours.finished.maximum(:created_at) + user.save! + end end end end diff --git a/app/models/temperature_reading.rb b/app/models/temperature_reading.rb index 4c6b2f9..5af04ae 100644 --- a/app/models/temperature_reading.rb +++ b/app/models/temperature_reading.rb @@ -1,4 +1,4 @@ -class TemperatureReading < ActiveRecord::Base +class TemperatureReading < ApplicationRecord belongs_to :temperature_sensor after_create :check_kegerator diff --git a/app/models/temperature_sensor.rb b/app/models/temperature_sensor.rb index 7308e9d..9047c0c 100644 --- a/app/models/temperature_sensor.rb +++ b/app/models/temperature_sensor.rb @@ -1,7 +1,7 @@ -class TemperatureSensor < ActiveRecord::Base - has_many :temperature_readings - has_one :latest_reading, lambda { order("created_at DESC") }, class_name: "TemperatureReading" - has_one :kegerator +class TemperatureSensor < ApplicationRecord + has_many :temperature_readings, dependent: :destroy + has_one :latest_reading, lambda { order("created_at DESC") }, class_name: "TemperatureReading", inverse_of: false, dependent: nil + has_one :kegerator, dependent: :nullify def self.for_select all.map { |tap| [tap.name, tap.id] } @@ -10,15 +10,15 @@ def self.for_select def self.monitor loop do Temp.discover.each do |id, sensor| - temp_sensor = where(code: id).first || create(name: id, code: id) + temp_sensor = where(code: id).first || create!(name: id, code: id) begin - temp_sensor.temperature_readings.create(temp_c: sensor.c) + temp_sensor.temperature_readings.create!(temp_c: sensor.c) rescue Temp::ReadingFailed => e - puts "Could not read temperature on #{id}\n#{e.message}" + Rails.logger.debug { "Could not read temperature on #{id}\n#{e.message}" } end end - sleep(60 - Time.now.sec) + sleep(60 - Time.current.sec) end end @@ -26,7 +26,7 @@ def sensor @sensor ||= Temp::Sensor.from_id(code) end - def temp_data(start_time = 24.hours.ago, end_time = Time.now) + def temp_data(start_time = 24.hours.ago, end_time = Time.current) temperature_readings .where(created_at: (start_time...end_time)) .order(:created_at) diff --git a/app/models/user.rb b/app/models/user.rb index e23da12..ee0e165 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ -class User < ActiveRecord::Base - has_many :pours +class User < ApplicationRecord + has_many :pours, dependent: :nullify scope :active, -> { where(hidden: false).where.not(name: nil) } @@ -32,13 +32,13 @@ def gravatar_base_url def decrement_credits(volume) if credits.present? - update(credits: [credits - volume, 0].max) + update!(credits: [credits - volume, 0].max) end end def increment_credits(volume) if credits.present? - update(credits: credits + volume) + update!(credits: credits + volume) end end diff --git a/db/migrate/20130203212003_add_started_at_and_finished_at_to_kegs.rb b/db/migrate/20130203212003_add_started_at_and_finished_at_to_kegs.rb index cad8b75..7db1e6e 100644 --- a/db/migrate/20130203212003_add_started_at_and_finished_at_to_kegs.rb +++ b/db/migrate/20130203212003_add_started_at_and_finished_at_to_kegs.rb @@ -1,6 +1,8 @@ class AddStartedAtAndFinishedAtToKegs < ActiveRecord::Migration[4.2] def change - add_column :kegs, :started_at, :datetime - add_column :kegs, :finished_at, :datetime + change_table :kegs, bulk: true do |t| + t.datetime :started_at + t.datetime :finished_at + end end end diff --git a/db/migrate/20130223060306_remove_change_type_from_pour.rb b/db/migrate/20130223060306_remove_change_type_from_pour.rb index 46cace9..464fa76 100644 --- a/db/migrate/20130223060306_remove_change_type_from_pour.rb +++ b/db/migrate/20130223060306_remove_change_type_from_pour.rb @@ -1,5 +1,5 @@ class RemoveChangeTypeFromPour < ActiveRecord::Migration[4.2] def change - remove_column :pours, :change_type + remove_column :pours, :change_type, type: :string end end diff --git a/db/migrate/20130314210817_add_beer_attributes_to_keg.rb b/db/migrate/20130314210817_add_beer_attributes_to_keg.rb index 9946a65..d5b4e18 100644 --- a/db/migrate/20130314210817_add_beer_attributes_to_keg.rb +++ b/db/migrate/20130314210817_add_beer_attributes_to_keg.rb @@ -1,7 +1,9 @@ class AddBeerAttributesToKeg < ActiveRecord::Migration[4.2] def change - add_column :kegs, :brewery, :string - add_column :kegs, :style, :string - add_column :kegs, :abv, :decimal + change_table :kegs, bulk: true do |t| + t.string :brewery + t.string :style + t.decimal :abv + end end end diff --git a/lib/faye_notifier.rb b/lib/faye_notifier.rb index ef56129..89889c8 100644 --- a/lib/faye_notifier.rb +++ b/lib/faye_notifier.rb @@ -12,7 +12,7 @@ def self.send_message(channel, data) message: {channel: channel, data: data}.to_json ) rescue => e - puts "Encountered #{e.message} (#{e.class}) while trying to connect to faye" + puts "Encountered #{e.message} (#{e.class}) while trying to connect to faye" # standard:disable Rails/Output end end end diff --git a/lib/hubot.rb b/lib/hubot.rb index ce99e26..fb7b78d 100644 --- a/lib/hubot.rb +++ b/lib/hubot.rb @@ -12,7 +12,7 @@ def self.send_message(message) http.request(req) end rescue => e - puts "Encountered #{e.message} (#{e.class}) while trying to report to rosie" + puts "Encountered #{e.message} (#{e.class}) while trying to report to rosie" # standard:disable Rails/Output end end diff --git a/lib/tap_monitor.rb b/lib/tap_monitor.rb index 78cd3e5..b6fb5f6 100644 --- a/lib/tap_monitor.rb +++ b/lib/tap_monitor.rb @@ -45,7 +45,7 @@ def check_dead def start_missing return unless @running - BeerTap.all.each do |tap| + BeerTap.all.each do |tap| # standard:disable Rails/FindEach tap_monitors[tap.id] ||= start(tap) end end @@ -60,7 +60,7 @@ def monitor_loop if ios ios[0].each do |io| monitor = monitors.detect { |mon| mon.io == io } - monitor.update + monitor.update # standard:disable Rails/SaveBang end end @@ -77,9 +77,9 @@ def initialize(tap) @tap = tap @running = true @finished = true - @last_started_at = Time.now.to_f.to_s + @last_started_at = Time.current.to_f.to_s - @io = IO.popen("#{ruby_version} #{Rails.root.join("lib", "pour_reader.rb").to_s.inspect} #{@tap.gpio_pin} #{Setting.pour_timeout}", "r+") + @io = IO.popen("#{ruby_version} #{Rails.root.join("lib/pour_reader.rb").to_s.inspect} #{@tap.gpio_pin} #{Setting.pour_timeout}", "r+") @io.sync = true end @@ -104,7 +104,7 @@ def finish_if_needed @timeout = 2 if @active_pour.finished_at.present? end - if (@last_tick + @timeout) < Time.now + if (@last_tick + @timeout) < Time.current finish end end @@ -112,7 +112,7 @@ def finish_if_needed def update # Read update _, @first_tick, @last_tick, @ticks = @io.readline.strip.split(",") - @last_tick = Time.at(@last_tick.to_f) + @last_tick = Time.at(@last_tick.to_f).utc # Is this a new pour if @last_started_at != @first_tick @@ -131,7 +131,7 @@ def update else @active_pour.reload end - @active_pour.started_at ||= Time.at(@first_tick.to_f) + @active_pour.started_at ||= Time.at(@first_tick.to_f).utc # fast timeout as the user has said they are done @timeout = 2 if @active_pour.finished_at.present? @@ -141,12 +141,12 @@ def update @active_pour.volume = @active_pour.sensor_ticks * @tap.floz_per_tick # is the pour done - if (@last_tick + @timeout) < Time.now + if (@last_tick + @timeout) < Time.current finish @io.puts "reset" @io.flush else - @active_pour.save + @active_pour.save! end rescue EOFError finish_if_needed @@ -155,10 +155,10 @@ def update def finish @finished = true if @active_pour.volume < 0.5 - @active_pour.destroy + @active_pour.destroy! else @active_pour.finished_at = @last_tick - @active_pour.save + @active_pour.save! end @active_pour.keg.beer_tap.deactivate end diff --git a/lib/temp.rb b/lib/temp.rb index 3b630a3..83bf97d 100644 --- a/lib/temp.rb +++ b/lib/temp.rb @@ -56,10 +56,10 @@ def read if parts.detect { |p| TEMP_REGEX =~ p } @value = $1.to_i / 1000.0 else - raise ReadingFailed, "No temp found #{Time.now} #{@id}\n#{output}\n\n" + raise ReadingFailed, "No temp found #{Time.current} #{@id}\n#{output}\n\n" end else - raise ReadingFailed, "CRC Failed #{Time.now} #{@id}" + raise ReadingFailed, "CRC Failed #{Time.current} #{@id}" end rescue ReadingFailed => error @tries += 1 diff --git a/spec/factories/pour.rb b/spec/factories/pour.rb index 2a41d78..838ba09 100644 --- a/spec/factories/pour.rb +++ b/spec/factories/pour.rb @@ -2,7 +2,7 @@ factory :pour do volume { rand * 16 } started_at { 5.seconds.ago } - finished_at { Time.now } + finished_at { Time.current } duration { rand * 30 } keg user diff --git a/spec/models/achievement_spec.rb b/spec/models/achievement_spec.rb index d2c7e40..3bd32c3 100644 --- a/spec/models/achievement_spec.rb +++ b/spec/models/achievement_spec.rb @@ -59,7 +59,7 @@ name: "Foo", description: "Bar", reverse: true, - time_gt: Time.now - 30.days}).value.to_d).to eq(BigDecimal("46")) + time_gt: 30.days.ago}).value.to_d).to eq(BigDecimal("46")) end it "limits calculation by multiple options" do @@ -68,7 +68,7 @@ description: "Bar", reverse: true, keg_id: keg1.id, - time_gt: Time.now - 30.days}).value.to_d).to eq(BigDecimal("12")) + time_gt: 30.days.ago}).value.to_d).to eq(BigDecimal("12")) end end diff --git a/spec/models/pour_spec.rb b/spec/models/pour_spec.rb index 2ed9d2d..fe71658 100644 --- a/spec/models/pour_spec.rb +++ b/spec/models/pour_spec.rb @@ -14,7 +14,6 @@ end after do - travel_back end it "returns true if the pour is completed and the current time is after the pour timeout" do @@ -38,8 +37,8 @@ expect(Hubot).to receive(:send_message).with("Someone just poured a 12.0oz #{pour.keg.name}.") - pour.finished_at = Time.now - pour.save + pour.finished_at = Time.current + pour.save! end it "sends a hubot message on claiming an anonymous pour" do @@ -49,7 +48,7 @@ expect(Hubot).to receive(:send_message).with("Jim has claimed the 12.0oz pour.") pour.user = user - pour.save + pour.save! end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 39e0bb3..ec9a44d 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -20,7 +20,7 @@ # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # -Dir[Rails.root.join("spec", "support", "**", "*.rb")].sort.each { |f| require f } +Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. @@ -32,7 +32,7 @@ end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - config.fixture_path = "#{::Rails.root}/spec/fixtures" + config.fixture_path = Rails.root.join("spec/fixtures").to_s # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false diff --git a/spec/requests/api/kegs_spec.rb b/spec/requests/api/kegs_spec.rb index fbed501..d181c4b 100644 --- a/spec/requests/api/kegs_spec.rb +++ b/spec/requests/api/kegs_spec.rb @@ -9,8 +9,8 @@ expect(response.status).to eq(200) json_response = JSON.parse(response.body) - expect(json_response.map { |u| u["name"] }).to include("PBR") - tap_names = json_response.map { |u| u["beer_tap"] }.flatten.map { |t| t["name"] } + expect(json_response.pluck("name")).to include("PBR") + tap_names = json_response.pluck("beer_tap").flatten.pluck("name") expect(tap_names).to include(keg.beer_tap.name) end end diff --git a/spec/requests/api/pours_spec.rb b/spec/requests/api/pours_spec.rb index 61e7f12..997ada5 100644 --- a/spec/requests/api/pours_spec.rb +++ b/spec/requests/api/pours_spec.rb @@ -13,7 +13,7 @@ expect(response.status).to eq(200) json_response = JSON.parse(response.body) - expect(json_response.map { |u| u["volume"] }).to include(pour.volume.to_s) + expect(json_response.pluck("volume")).to include(pour.volume.to_s) end it "returns pours for a date range as json" do @@ -24,8 +24,8 @@ expect(response.status).to eq(200) json_response = JSON.parse(response.body) - expect(json_response.map { |u| u["volume"] }).to include(pour_in.volume.to_s) - expect(json_response.map { |u| u["volume"] }).not_to include(pour_out.volume.to_s) + expect(json_response.pluck("volume")).to include(pour_in.volume.to_s) + expect(json_response.pluck("volume")).not_to include(pour_out.volume.to_s) end end @@ -50,8 +50,8 @@ expect(response.status).to eq(200) json_response = JSON.parse(response.body) - expect(json_response.map { |u| u["email"] }).to include(pour_in.user.email) - expect(json_response.map { |u| u["email"] }).not_to include(pour_out.user.email) + expect(json_response.pluck("email")).to include(pour_in.user.email) + expect(json_response.pluck("email")).not_to include(pour_out.user.email) end end end diff --git a/spec/requests/api/taps_spec.rb b/spec/requests/api/taps_spec.rb index 50278a1..ffd41b0 100644 --- a/spec/requests/api/taps_spec.rb +++ b/spec/requests/api/taps_spec.rb @@ -9,7 +9,7 @@ expect(response.status).to eq(200) json_response = JSON.parse(response.body) - expect(json_response.map { |u| u["name"] }).to include(keg.beer_tap.name) + expect(json_response.pluck("name")).to include(keg.beer_tap.name) end end end diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index deeaaad..6ed2de3 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -10,8 +10,8 @@ expect(response.status).to eq(200) json_response = JSON.parse(response.body) - expect(json_response.map { |u| u["id"] }).to include(user.id) - expect(json_response.map { |u| u["id"] }).not_to include(hidden_user.id) + expect(json_response.pluck("id")).to include(user.id) + expect(json_response.pluck("id")).not_to include(hidden_user.id) end end