diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 665f8349..751fa911 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -5,19 +5,44 @@ class RentalsController < Admin::BaseController before_action :fetch_remote_rentals, only: :index def index - @not_connected_rentals = current_account.rentals.visible.ordered.not_connected - @visible_rentals = current_account.rentals.visible - @remote_accounts = current_account.remote_accounts - @remote_rentals_by_account = current_account.remote_rentals.ordered - .includes(:remote_account, :rental).group_by(&:remote_account) + render :index, locals: { **index_arguments } end def show rental end + def index_arguments + BookingsyncPortal.custom_arguments.dig("Admin", "RentalsController", "index") || { + not_connected_rentals: not_connected_rentals.call(current_account), + visible_rentals: visible_rentals.call(current_account), + remote_accounts: remote_accounts.call(current_account), + remote_rentals_by_account: remote_rentals_by_account.call(current_account) + } + end + private + def not_connected_rentals + BookingsyncPortal.not_connected_rentals || lambda { + |account| account.rentals.visible.ordered.not_connected + } + end + + def visible_rentals + BookingsyncPortal.visible_rentals || lambda { |account| account.rentals.visible } + end + + def remote_accounts + BookingsyncPortal.remote_accounts || lambda { |account| account.remote_accounts } + end + + def remote_rentals_by_account + BookingsyncPortal.remote_rentals_by_account || lambda { + |account| account.remote_rentals.ordered.includes(:remote_account, :rental).group_by(&:remote_account) + } + end + def synchronize_rentals BookingsyncPortal.rental_model.constantize.synchronize(scope: current_account) end diff --git a/app/views/bookingsync_portal/admin/rentals/index.html.erb b/app/views/bookingsync_portal/admin/rentals/index.html.erb index ff40aadb..9c818e59 100644 --- a/app/views/bookingsync_portal/admin/rentals/index.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/index.html.erb @@ -10,8 +10,8 @@ <%= image_tag('bookingsync_portal/bookingsync.png', alt: 'BookingSync') %>
- <%= render "rentals", visible_rentals: @visible_rentals, - not_connected_rentals: @not_connected_rentals %> + <%= render "rentals", visible_rentals: visible_rentals, + not_connected_rentals: not_connected_rentals %>
@@ -26,11 +26,11 @@
- <% @remote_accounts.each do |remote_account| %> + <% remote_accounts.each do |remote_account| %>

<%=t '.remote_header', portal_name: BookingsyncPortal.portal_name, account_name: remote_account.name %>

- <%- if Array(@remote_rentals_by_account[remote_account]).length > 0 -%> - <% Array(@remote_rentals_by_account[remote_account]).each do |remote_rental| %> + <%- if Array(remote_rentals_by_account[remote_account]).length > 0 -%> + <% Array(remote_rentals_by_account[remote_account]).each do |remote_rental| %> <% if remote_rental.connected? %> <%= render "connected_rental", remote_rental: remote_rental, rental: remote_rental.rental %> <% else %> diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index e5b124f6..d0295ee7 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -60,6 +60,14 @@ module BookingsyncPortal # message bus channel scope mattr_accessor :message_bus_channel_scope + mattr_accessor :not_connected_rentals + mattr_accessor :visible_rentals + mattr_accessor :remote_accounts + mattr_accessor :remote_rentals_by_account + + mattr_accessor :custom_arguments + @@custom_arguments = {} + # fetch remote rentals def self.fetch_remote_rentals(account) # return false if remote account is not present or not valid diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index 30cc3d70..14c37b80 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -1,7 +1,6 @@ -require 'rails_helper' +require "rails_helper" describe BookingsyncPortal::Admin::RentalsController do - render_views routes { BookingsyncPortal::Engine.routes } let!(:account) { create(:account) } @@ -11,11 +10,11 @@ let!(:connection) { create(:connection, rental: rental_connected) } before do - request.env['HTTPS'] = 'on' + request.env["HTTPS"] = "on" allow(controller).to receive(:current_account).and_return(account) end - describe 'GET #index' do + describe "GET #index" do before do expect(Rental).to receive(:synchronize).with(scope: account) do # pretending to sync rentals :P @@ -23,8 +22,56 @@ end end - it 'synchronizes rentals' do - expect { get :index }.to change { Rental.count } + context "methods from default config" do + render_views + + it "synchronizes rentals" do + expect { get :index }.to change { Rental.count } + end + end + + context "using custom methods" do + let(:fake_connected_rental) { create(:rental, account: account) } + let(:fake_visible_rental) { create(:rental, account: account) } + let(:fake_remote_account) { create(:remote_account, account: account) } + let(:fake_remote_rental) { create(:remote_rental, account: account) } + + let(:fake_connected_rentals_value) { [fake_connected_rental] } + let(:fake_visible_rental_value) { [fake_visible_rental] } + let(:fake_remote_accounts_value) { [fake_remote_account] } + let(:fake_remote_rentals_by_account_value) do + RemoteRental.where(id: fake_remote_rental).includes(:remote_account, :rental).group_by(&:remote_account) + end + + before do + BookingsyncPortal.setup do |config| + config.not_connected_rentals = lambda { |account| fake_connected_rentals_value } + config.visible_rentals = lambda { |account| fake_visible_rental_value } + config.remote_accounts = lambda { |account| fake_remote_accounts_value } + config.remote_rentals_by_account = lambda { |account| fake_remote_rentals_by_account_value } + end + + allow(controller).to receive(:render) + allow(controller).to receive(:render).with(:index, an_instance_of(Hash)) + end + + it "calls custom methods" do + get :index + + expect(BookingsyncPortal.not_connected_rentals.call(account)).to eq(fake_connected_rentals_value) + expect(BookingsyncPortal.visible_rentals.call(account)).to eq(fake_visible_rental_value) + expect(BookingsyncPortal.remote_accounts.call(account)).to eq(fake_remote_accounts_value) + expect(BookingsyncPortal.remote_rentals_by_account.call(account)).to eq(fake_remote_rentals_by_account_value) + + expect(controller).to have_received(:render).at_least(1).times do |method, options| + if method == :index + expect(options.dig(:locals, :not_connected_rentals)).to eq(fake_connected_rentals_value) + expect(options.dig(:locals, :visible_rentals)).to eq(fake_visible_rental_value) + expect(options.dig(:locals, :remote_accounts)).to eq(fake_remote_accounts_value) + expect(options.dig(:locals, :remote_rentals_by_account)).to eq(fake_remote_rentals_by_account_value) + end + end + end end end end