Skip to content

Commit

Permalink
Fix observer/ventable specs
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster committed Apr 5, 2024
1 parent 71eb7dc commit 2233b0e
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 311 deletions.
424 changes: 202 additions & 222 deletions Brewfile.lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ gem 'sentry-raven'
gem 'stripe'
gem 'twitter-bootstrap-rails', git: 'https://github.com/seyhunak/twitter-bootstrap-rails.git'
gem 'uglifier'
gem 'ventable'
gem 'ventable', '>= 1.3'
gem 'yard'

group :development, :test do
Expand Down
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ GEM
unaccent (0.4.0)
unicode-display_width (2.5.0)
uri (0.13.0)
ventable (1.2.0)
ventable (1.3.1)
activesupport (>= 5)
warden (1.2.9)
rack (>= 2.0.9)
websocket-driver (0.7.6)
Expand Down Expand Up @@ -446,7 +447,7 @@ DEPENDENCIES
timeout
twitter-bootstrap-rails!
uglifier
ventable
ventable (>= 1.3)
yard

BUNDLED WITH
Expand Down
4 changes: 4 additions & 0 deletions app/classes/fnf/event_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def metric(event, value)
::NewRelic::Agent.record_metric(event_name(event), value)
end

def handle_event(event)
metric(event, 1)
end

protected

def event_name(event)
Expand Down
4 changes: 2 additions & 2 deletions app/classes/fnf/events.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'ventable'

require_relative 'events/abstract_event'
require_relative 'events/ticket_request_event'
require_relative 'events/ticket_request_approved_event'
require_relative 'events/ticket_request_declined_event'
Expand All @@ -26,5 +28,3 @@ def initialize_events!
end
end
end

FnF::Events.initialize_events!
20 changes: 13 additions & 7 deletions app/classes/fnf/events/abstract_event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'ventable'
require_relative '../event_reporter'

module FnF
module Events
Expand All @@ -13,17 +14,22 @@ def initialize(user: nil, target: nil)
end

class << self
# @param [Object] klass
def inherited(klass)
super(klass)
klass.instance_eval do
include Ventable::Event
# @param [Object] subclass
def inherited(subclass)
# noinspection RubyMismatchedArgumentType
super(subclass)

notifies ->(event) { EventReporter.metric(event, 1) }
subclass.include Ventable::Event

subclass.notifies(EventReporter)

subclass.instance_eval do
# For eg FnF::Events::TicketRequestEvent this should return :ticket_request
def ventable_callback_method_name
name.gsub(/^FnF::Events::/, '').underscore.downcase.to_sym
ActiveSupport::Inflector.underscore(name)
.gsub(%r{^fnf/events/}, '')
.gsub(/_event$/, '')
.to_sym
end
end
end
Expand Down
13 changes: 2 additions & 11 deletions app/controllers/ticket_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
# Manage all pages related to ticket requests.
# rubocop: disable Metrics/ClassLength
class TicketRequestsController < ApplicationController
before_action :authenticate_user!, except: %i[new create]
before_action :set_event
before_action :authenticate_user!, except: %i[new create]
before_action :require_event_admin, except: %i[new create show edit update]
before_action :set_ticket_request, except: %i[index new create download]

# Uncomment this if we start getting too many requests
# http_basic_authenticate_with name: Rails.application.secrets.ticket_request_username,
# password: Rails.application.secrets.ticket_request_password,
# only: :new

def index
@ticket_requests = TicketRequest
.includes({ event: [:price_rules] }, :payment, :user)
Expand Down Expand Up @@ -102,18 +97,14 @@ def create
end

tr_params = permitted_params[:ticket_request].to_h || {}

Rails.logger.debug { "#create: params=#{tr_params.inspect}" }

tr_params[:user_id] = current_user.id if signed_in? && current_user.present?

if tr_params.empty?
flash.now[:error] = 'Please enter some information'
flash.now[:error] = 'Please fill out the form below to request tickets.'
redirect_to new_event_ticket_request_path(@event)
end

@ticket_request = TicketRequest.new(tr_params)

if @ticket_request.save
FnF::Events::TicketRequestEvent.new(
user: current_user,
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/ticket_request_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def to_email

class << self
def mail_config
TicketBooth::Application.configure do
TicketBooth::Application.configure do |config|
return config.action_mailer
end
end
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/ventable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require 'fnf/events'
require 'fnf/event_reporter'

FnF::Events.initialize_events!
22 changes: 12 additions & 10 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
get 'oops', controller: :home, action: :oops
devise_for :users

event_id = 9

get('fnf-tickets', controller: :ticket_requests, action: :new, event_id:)
get('fnf_tickets', controller: :ticket_requests, action: :new, event_id:)
get('fnftickets', controller: :ticket_requests, action: :new, event_id:)
get('fnf', controller: :ticket_requests, action: :new, event_id:)
get('FNF', controller: :ticket_requests, action: :new, event_id:)
get('FnF', controller: :ticket_requests, action: :new, event_id:)

get('eald', controller: :eald_payments, action: :new, event_id:)
# WTF was this for? --@kig
#
# event_id = 9
#
# get('fnf-tickets', controller: :ticket_requests, action: :new, event_id:)
# get('fnf_tickets', controller: :ticket_requests, action: :new, event_id:)
# get('fnftickets', controller: :ticket_requests, action: :new, event_id:)
# get('fnf', controller: :ticket_requests, action: :new, event_id:)
# get('FNF', controller: :ticket_requests, action: :new, event_id:)
# get('FnF', controller: :ticket_requests, action: :new, event_id:)
#
# get('eald', controller: :eald_payments, action: :new, event_id:)

resources :payments, only: %i[new create show] do
collection do
Expand Down
11 changes: 0 additions & 11 deletions lib/fnf/events.rb

This file was deleted.

36 changes: 36 additions & 0 deletions spec/classes/fnf/event_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'rails_helper'

module FnF
RSpec.describe EventReporter do
# noinspection RailsParamDefResolve
let(:handler_name) { ::FnF::Events::BuildingOnFireEvent.send(:ventable_callback_method_name) }

before do
described_class.instance_eval do
def building_on_fire(event)
metric(event, 1)
end
end
end

it 'is correct handler name' do
expect(handler_name).to eq(:building_on_fire)
end

# rubocop: disable RSpec
describe '#event_name' do
before :suite do
::FnF::Events::BuildingOnFireEvent.configure { notifies EventReporter }
end

it 'invokes metric for all events' do
expect(described_class).to receive(:metric).with(FnF::Events::BuildingOnFireEvent, 1).once

Events.set_building_on_fire
end
end
# rubocop: enable RSpec
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# frozen_string_literal: true

require 'rails_helper'

class Worker
@events = {}

class << self
attr_accessor :events
attr_reader :events

def register(event)
events[event.class.name] ||= 0
events[event.class.name] += 1
@events ||= {}
@events[event.class.name] ||= 0
@events[event.class.name] += 1
end

def building_on_fire_event(event)
def building_on_fire(event)
register(event)
end
end
Expand All @@ -27,7 +30,7 @@ def building_on_fire_event(event)
before do
event.configure { notifies Worker }

FnF::Events.set_building_on_fire!
FnF::Events.set_building_on_fire
end

it { is_expected.not_to be_empty }
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@
end

describe 'newly created event' do
subject { Event.last }
subject { Event.find_by(name: new_event_params['name']) }

before { make_request[] }

it { is_expected.to be_valid }
it { is_expected.to be_persisted }
end

describe 'event creation' do
Expand Down
13 changes: 3 additions & 10 deletions spec/controllers/ticket_requests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,19 @@
context 'when event ticket sales are closed' do
it 'has no error message before the request' do
Timecop.freeze(event.end_time + 1.hour) do
expect(flash[:error]).to be_nil
make_request.call
end
end

it 'renders an error message after the request' do
Timecop.freeze(event.end_time + 1.hour) do
make_request.call
expect(flash[:error]).not_to be_nil
expect(flash[:error]).to be('Sorry, but ticket sales have closed')
end
end
end

context 'when viewer already signed in' do
subject { make_request[user_id: viewer.id] }
subject(:response) { make_request[user_id: viewer.id] }

let(:viewer) { create(:user) }

it 'creates a ticket request' do
expect { subject }.to(change(TicketRequest, :count))
expect { response }.to(change(TicketRequest, :count))
end

it 'assigned the ticket request to the viewer' do
Expand Down
8 changes: 4 additions & 4 deletions spec/factories/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
start_time { 1.year.from_now }
end_time { 1.year.from_now + 1.day }

adult_ticket_price { Random.rand(100) }
kid_ticket_price { nil }
adult_ticket_price { Random.rand(100..150) }
kid_ticket_price { Random.rand(40..50) }
cabin_price { nil }
max_adult_tickets_per_request { Random.rand(1..4) }
max_kid_tickets_per_request { nil }
max_adult_tickets_per_request { Random.rand(2..4) }
max_kid_tickets_per_request { 2 }
max_cabins_per_request { nil }
tickets_require_approval { true }

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/ticket_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :ticket_request do
adults { Random.rand(1..4) }
adults { event&.max_adult_tickets_per_request || 1 }
kids { Random.rand(2) }
cabins { Random.rand(2) }
needs_assistance { [true, false].sample }
Expand Down
1 change: 1 addition & 0 deletions spec/lib/fnf/csv_reader_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'spec_helper'
require 'fnf/csv_reader'

module FnF
Expand Down
17 changes: 0 additions & 17 deletions spec/lib/fnf/event_reporter_spec.rb

This file was deleted.

6 changes: 4 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative 'spec_helper'

ENV['RAILS_ENV'] = 'test'

require File.expand_path('../config/environment', __dir__)
Expand All @@ -9,8 +11,6 @@
require 'stripe_mock'
require 'timeout'

require_relative 'spec_helper'

def example_with_timeout(example)
Timeout.timeout(20) { example.run }
end
Expand All @@ -27,6 +27,8 @@ def example_with_timeout(example)

config.around { |example| example_with_timeout(example) }

config.use_transactional_fixtures = true

config.expect_with(:rspec) do |c|
c.syntax = %i[should expect]
end
Expand Down
Loading

0 comments on commit 2233b0e

Please sign in to comment.