From a9ed789e5229f831b344a95f8fdbb51a1d2b43bb Mon Sep 17 00:00:00 2001 From: StoneFrog Date: Thu, 6 Jun 2019 11:35:38 +0200 Subject: [PATCH 01/26] add config for proc based redirection --- Gemfile.lock | 2 +- .../admin/rentals_controller.rb | 5 +++ .../admin/v2/rentals_controller.rb | 26 +++++++++++ config/routes.rb | 3 ++ lib/bookingsync_portal.rb | 4 ++ .../admin/v2/rentals_controller_spec.rb | 33 ++++++++++++++ spec/dummy/config/routes.rb | 2 +- spec/request/admin_rentals_spec.rb | 44 +++++++++++++++++++ 8 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb create mode 100644 spec/controllers/admin/v2/rentals_controller_spec.rb create mode 100644 spec/request/admin_rentals_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 7df48d8a..1d70a73f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -309,4 +309,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.15.0 + 1.17.2 diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 665f8349..8a488eef 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -1,6 +1,7 @@ module BookingsyncPortal module Admin class RentalsController < Admin::BaseController + before_action :resolve_action, only: :index before_action :synchronize_rentals, only: :index before_action :fetch_remote_rentals, only: :index @@ -31,6 +32,10 @@ def fetch_remote_rentals def rental @rental ||= current_account.rentals.visible.find(params[:id]) end + + def resolve_action + redirect_to admin_v2_rentals_path if BookingsyncPortal.use_paginated_view.call(current_account) + end end end end diff --git a/app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb new file mode 100644 index 00000000..be6c5cf6 --- /dev/null +++ b/app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb @@ -0,0 +1,26 @@ +module BookingsyncPortal + module Admin + module V2 + class RentalsController < Admin::BaseController + before_action :synchronize_rentals, only: :index, if: ->(controller) { controller.request.format.html? } + before_action :fetch_remote_rentals, only: :index, if: ->(controller) { controller.request.format.html? } + + def index + render json: { status: :success } + end + + private + + def synchronize_rentals + BookingsyncPortal.rental_model.constantize.synchronize(scope: current_account) + end + + def fetch_remote_rentals + unless BookingsyncPortal.fetch_remote_rentals(current_account) + @remote_account_not_registered = true + end + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index e842863d..1a5403e6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,5 +5,8 @@ resources :remote_accounts, only: [:new, :create] get 'help', to: 'help#index' root to: 'rentals#index' + namespace :v2 do + resources :rentals, only: :index + end end end diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index e5b124f6..33660787 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -57,6 +57,10 @@ module BookingsyncPortal mattr_accessor :rate_model @@rate_model = 'BookingsyncPortal::Rate' + # whether load-all (false) or paginated (true) view should be used for admin#index + mattr_accessor :use_paginated_view + @@use_paginated_view = ->(_account) { false } + # message bus channel scope mattr_accessor :message_bus_channel_scope diff --git a/spec/controllers/admin/v2/rentals_controller_spec.rb b/spec/controllers/admin/v2/rentals_controller_spec.rb new file mode 100644 index 00000000..11550198 --- /dev/null +++ b/spec/controllers/admin/v2/rentals_controller_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +describe BookingsyncPortal::Admin::V2::RentalsController do + render_views + routes { BookingsyncPortal::Engine.routes } + + let!(:account) { create(:account) } + let!(:remote_account) { create(:remote_account, account: account) } + let(:rental) { create(:rental, account: account) } + let!(:rental_connected) { create(:rental, account: account) } + let!(:connection) { create(:connection, rental: rental_connected) } + + before do + request.env['HTTPS'] = 'on' + allow(controller).to receive(:current_account).and_return(account) + end + + describe 'GET #index' do + context 'when request format is html' do + it 'synchronizes rentals' do + expect(Rental).to receive(:synchronize).with(scope: account) { rental } # pretending to sync rentals :P + expect { get :index, format: :html }.to change { Rental.count } + end + end + + context 'when request format is not html' do + it 'does not synchronize rentals' do + expect(Rental).not_to receive(:synchronize) + expect { get :index, format: :json }.not_to change { Rental.count } + end + end + end +end diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 787824f8..19448cf0 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,3 +1,3 @@ Rails.application.routes.draw do - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + mount BookingsyncPortal::Engine => "/" end diff --git a/spec/request/admin_rentals_spec.rb b/spec/request/admin_rentals_spec.rb new file mode 100644 index 00000000..21f15ca5 --- /dev/null +++ b/spec/request/admin_rentals_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +describe "admin/rentals", type: :request do + let!(:account) { create(:account) } + let!(:remote_account) { create(:remote_account, account: account) } + + describe "GET #index" do + subject(:get_rentals) { get "/admin/rentals", headers: { "HTTPS" => "on" } } + + before do + allow_any_instance_of(BookingsyncPortal::Admin::RentalsController).to receive(:current_account).and_return(account) + end + + context "when config's use_paginated_view returns false (default)" do + it "successfully renders page" do + get_rentals + expect(response).to have_http_status :ok + end + end + + context "when config's use_paginated_view returns true" do + around do |example| + BookingsyncPortal.setup do |config| + config.use_paginated_view = ->(account) { account.persisted? } # anything as long as returns true + end + example.run + BookingsyncPortal.setup do |config| + config.use_paginated_view = ->(_account) { false } + end + end + + before do + allow_any_instance_of(BookingsyncPortal::Admin::V2::RentalsController).to receive(:current_account).and_return(account) + end + + it "successfully renders page" do + get_rentals + expect(response).to redirect_to("/admin/v2/rentals") + follow_redirect! + expect(response).to have_http_status :ok + end + end + end +end From 86251e8031e3e9816015288840951c025eaee660 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Thu, 13 Jun 2019 12:30:43 +0300 Subject: [PATCH 02/26] Add config to handle which fields will be used in rentals search. Move v2/rentals page to admin/rentals_controller.rb --- .../admin/templates/pagination.hbs | 6 +++ .../admin/rentals_controller.rb | 37 +++++++++++++++++-- .../admin/v2/rentals_controller.rb | 26 ------------- config/routes.rb | 5 +-- lib/bookingsync_portal.rb | 13 +++++++ 5 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs delete mode 100644 app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb diff --git a/app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs b/app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs new file mode 100644 index 00000000..0196b4d2 --- /dev/null +++ b/app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs @@ -0,0 +1,6 @@ + diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 8a488eef..c84c17de 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -6,18 +6,47 @@ class RentalsController < Admin::BaseController before_action :fetch_remote_rentals, only: :index def index + index_preparation + @remote_rentals_by_account = @remote_rentals_by_account.group_by(&:remote_account) + end + + def index_with_search + synchronize_rentals if !searchable? + index_preparation + + apply_search + apply_pagination + + @remote_rentals_by_account = @remote_rentals_by_account.group_by(&:remote_account) + render :index + end + + def show + rental + end + + private + + def searchable? + # TODO implement me + true + end + + def index_preparation @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) + .includes(:remote_account, :rental) end - def show - rental + def apply_search + #TODO implement me end - private + def apply_pagination + #TODO implement me + end def synchronize_rentals BookingsyncPortal.rental_model.constantize.synchronize(scope: current_account) diff --git a/app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb deleted file mode 100644 index be6c5cf6..00000000 --- a/app/controllers/bookingsync_portal/admin/v2/rentals_controller.rb +++ /dev/null @@ -1,26 +0,0 @@ -module BookingsyncPortal - module Admin - module V2 - class RentalsController < Admin::BaseController - before_action :synchronize_rentals, only: :index, if: ->(controller) { controller.request.format.html? } - before_action :fetch_remote_rentals, only: :index, if: ->(controller) { controller.request.format.html? } - - def index - render json: { status: :success } - end - - private - - def synchronize_rentals - BookingsyncPortal.rental_model.constantize.synchronize(scope: current_account) - end - - def fetch_remote_rentals - unless BookingsyncPortal.fetch_remote_rentals(current_account) - @remote_account_not_registered = true - end - end - end - end - end -end diff --git a/config/routes.rb b/config/routes.rb index 1a5403e6..b125bfaa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,11 @@ BookingsyncPortal::Engine.routes.draw do namespace :admin do resources :rentals, only: [:index, :show] + get "v2/rentals", to: "rentals#index_with_search", as: "v2_rentals" + resources :connections, only: [:create, :destroy] resources :remote_accounts, only: [:new, :create] get 'help', to: 'help#index' root to: 'rentals#index' - namespace :v2 do - resources :rentals, only: :index - end end end diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index 33660787..3f50883a 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -61,6 +61,19 @@ module BookingsyncPortal mattr_accessor :use_paginated_view @@use_paginated_view = ->(_account) { false } + # rate model class + mattr_accessor :rentals_search + @@rentals_search = { + rentals: %w(name synced_id) + } + mattr_accessor :remote_rentals_search + @@remote_rentals_search = { + rentals: %w(name synced_id), + remote_rentals: %w(uid), + remote_accounts: %w(uid) + } + + # message bus channel scope mattr_accessor :message_bus_channel_scope From cf1424eeeb0693e3197a0c63d7d4e45204545471 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Sun, 16 Jun 2019 15:29:52 +0300 Subject: [PATCH 03/26] Add ransack and kaminari gem dependencies Add list-backend-filter js class to use backend search if use_paginated_view flag is enabled Add loading.hbs and pagination.hbs templates Add back-end search engine to handle search + pagination requests in app/controllers/bookingsync_portal/admin/rentals_controller.rb Add index_with_search.js.erb view + locales + default settings --- .../admin/application.js.coffee | 1 + .../admin/lib/list-backend-filter.js.coffee | 65 ++++++++++ .../admin/rentals.js.coffee | 7 +- .../admin/templates/loading.hbs | 3 + .../admin/rentals_controller.rb | 113 ++++++++++++++---- .../admin/application_helper.rb | 4 + .../admin/rentals/_rentals.html.erb | 7 +- .../admin/rentals/index_with_search.js.erb | 14 +++ .../layouts/bookingsync_portal/admin.html.erb | 2 +- bookingsync_portal.gemspec | 3 + config/locales/en.yml | 1 + lib/bookingsync_portal.rb | 16 ++- 12 files changed, 208 insertions(+), 28 deletions(-) create mode 100644 app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee create mode 100644 app/assets/javascripts/bookingsync_portal/admin/templates/loading.hbs create mode 100644 app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb diff --git a/app/assets/javascripts/bookingsync_portal/admin/application.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/application.js.coffee index 1baaab08..37a54b44 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/application.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/application.js.coffee @@ -21,6 +21,7 @@ #= require bookingsync/form #= require ./vendor/css-contains #= require ./lib/list-filter +#= require ./lib/list-backend-filter #= require_tree ./templates #= require_tree . diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee new file mode 100644 index 00000000..12a9e4f9 --- /dev/null +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -0,0 +1,65 @@ +class @ListBackedFilter + constructor: (header, list, @listElement, @listFilterable, @inputId, formTemplate) -> + @header = $(header) + @list = $(list) + @form = $(formTemplate) + @insertForm() + @observeInputChanges() + + @paginationTemplate = $(HandlebarsTemplates["pagination"]()) + @insertPagination() + @observePageChanges() + + @loadingTemplate = $(HandlebarsTemplates["loading"]()) + + @doneTypingInterval = 1500 # 1.5 seconds wait after each keyup before sending search request + @typingTimer = undefined + + insertForm: -> + @form.appendTo(@header) + @input = $("#" + @inputId) + + insertPagination: -> + @setPage(1) + $(@paginationTemplate).appendTo(@list.parent()) + + setPage: (page)-> + $(@form).data("current-page", page) + + currentPage: -> + $(@form).data("current-page") + + observeInputChanges: -> + @input.on 'keyup', => + @setPage(1) + @displayWaiting() + clearTimeout(@typingTimer) + @typingTimer = setTimeout(@backendSearch, @doneTypingInterval) # make search request only when user is done typing + + observePageChanges: -> + @paginationTemplate.find("[data-type=previous]").on "click", @goToPreviousPage + @paginationTemplate.find("[data-type=next]").on "click", @goToNextPage + + displayWaiting: -> + @list.html(@loadingTemplate) unless @list.children()[0] == @loadingTemplate[0] + + backendSearch: => + $.get(@getSearchQuery()) + + getSearchQuery: -> + if @form.parents(".bookingsync-rentals-list").length > 0 + fieldName = "rentals_search" + else + fieldName = "remote_rentals_search" + searchParams = "#{fieldName}[query]=#{@input.val()}&#{fieldName}[page]=#{@currentPage()}" + "#{document.location.href}.js?#{searchParams}" + + goToPreviousPage: (e) => + e.preventDefault() + $(@form).data("current-page", @currentPage() - 1) + @backendSearch() + + goToNextPage: (e) => + e.preventDefault() + $(@form).data("current-page", @currentPage() + 1) + @backendSearch() diff --git a/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee index a3369a7d..38755451 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee @@ -1,7 +1,12 @@ $ -> + if $("body").data("paginated-view") + Filter = ListBackedFilter + else + Filter = ListFilter + for rentalsList, index in $(".rentals-list") inputId = "rentals-list-filter-#{index}" - new ListFilter( + new Filter( $(rentalsList).children(".rentals-list-header"), $(rentalsList).children(".rentals-list-scroll"), ".panel", diff --git a/app/assets/javascripts/bookingsync_portal/admin/templates/loading.hbs b/app/assets/javascripts/bookingsync_portal/admin/templates/loading.hbs new file mode 100644 index 00000000..b8433532 --- /dev/null +++ b/app/assets/javascripts/bookingsync_portal/admin/templates/loading.hbs @@ -0,0 +1,3 @@ +
+ +
diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index c84c17de..086431bd 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -5,20 +5,28 @@ class RentalsController < Admin::BaseController before_action :synchronize_rentals, only: :index before_action :fetch_remote_rentals, only: :index + helper_method :search_by_rentals? + helper_method :search_by_remote_rentals? + def index index_preparation - @remote_rentals_by_account = @remote_rentals_by_account.group_by(&:remote_account) end def index_with_search - synchronize_rentals if !searchable? - index_preparation + index_preparation do + @search_filter = BookingsyncPortal::SearchFilter.new(params) - apply_search - apply_pagination + apply_search + apply_pagination + end - @remote_rentals_by_account = @remote_rentals_by_account.group_by(&:remote_account) - render :index + respond_to do |format| + format.html do + synchronize_rentals + render :index + end + format.js # view can be app specific + end end def show @@ -27,25 +35,46 @@ def show private - def searchable? - # TODO implement me - true + def search_by_rentals? + params[:rentals_search].present? + end + + def search_by_remote_rentals? + params[:remote_rentals_search].present? + end + + def resolve_action + redirect_to admin_v2_rentals_path if BookingsyncPortal.use_paginated_view.call(current_account) end + def index_preparation @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) + @remote_rentals = current_account.remote_rentals.ordered.joins(:rental, :remote_account) + + yield if block_given? + + @remote_rentals_by_account = current_account.remote_rentals.where(id: @remote_rentals.pluck(:id)).ordered + .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) + .group_by(&:remote_account) end def apply_search - #TODO implement me + if @search_filter.rentals_query.present? + @not_connected_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.rentals_query, records: @not_connected_rentals, source: "rentals_search") + end + + if @search_filter.remote_rentals_query.present? + @remote_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: @remote_rentals, source: "remote_rentals_search") + end end def apply_pagination - #TODO implement me + @not_connected_rentals = @not_connected_rentals.page(@search_filter.rentals_page) + @remote_rentals = @remote_rentals.page(@search_filter.remote_rentals_page) + @remote_accounts = @remote_accounts.where(id: @remote_rentals.pluck(:remote_account_id)) end def synchronize_rentals @@ -53,18 +82,62 @@ def synchronize_rentals end def fetch_remote_rentals - unless BookingsyncPortal.fetch_remote_rentals(current_account) - @remote_account_not_registered = true - end + @remote_account_not_registered = true unless BookingsyncPortal.fetch_remote_rentals(current_account) end def rental @rental ||= current_account.rentals.visible.find(params[:id]) end + end + end - def resolve_action - redirect_to admin_v2_rentals_path if BookingsyncPortal.use_paginated_view.call(current_account) - end + class SearchFilter # TODO add tests + attr_reader :params + def initialize(params) + @params = params + end + + def rentals_query + @rentals_query ||= params.dig(:rentals_search, :query).to_s.strip + end + + def remote_rentals_query + @remote_rentals_query ||= params.dig(:remote_rentals_search, :query).to_s.strip + end + + def rentals_page + @rentals_page ||= (params.dig(:rentals_search, :page).to_i || 1) + end + + def remote_rentals_page + @remote_rentals_page ||= (params.dig(:remote_rentals_search, :page).to_i || 1) end end + + class Searcher # TODO add tests + def self.call(query:, source:, records:) + search_settings = BookingsyncPortal.rentals_search if source == "rentals_search" + search_settings ||= BookingsyncPortal.remote_rentals_search + + conditions = { m: "or" } + + search_settings.each do |type, filtered_fields| + filtered_fields.each do |field| + conditions.merge!(build_search_query(type, field.gsub(".", "_"), query)) + end + end + + records.ransack(conditions).result + end + + private + + def self.build_search_query(type, field, query) + if type == :string + { "#{field}_cont" => query } + else + { "#{field}_eq" => query } + end + end + end end diff --git a/app/helpers/bookingsync_portal/admin/application_helper.rb b/app/helpers/bookingsync_portal/admin/application_helper.rb index edcfef53..5750bc79 100644 --- a/app/helpers/bookingsync_portal/admin/application_helper.rb +++ b/app/helpers/bookingsync_portal/admin/application_helper.rb @@ -29,6 +29,10 @@ def rental_details(rental) safe_join(details, ', ') end + + def use_paginated_view + BookingsyncPortal.use_paginated_view.call(current_account) + end end end end diff --git a/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb b/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb index 7066e77d..d63e8b8d 100644 --- a/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb @@ -6,9 +6,14 @@ <% not_connected_rentals.each do |rental| %> <%= render rental %> <% end %> +<%- elsif search_by_rentals? || search_by_remote_rentals? %> +
+

<%= icon 'fa', 'search fa-lg' %>

+

<%=t '.no_results' %>

+
<%- else -%>
-

<%= icon 'thumbs-up fa-lg' %>

+

<%= icon 'fa', 'thumbs-up fa-lg' %>

<%=t '.all_synchronized' %>

<%- end -%> diff --git a/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb new file mode 100644 index 00000000..dc7d5c43 --- /dev/null +++ b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb @@ -0,0 +1,14 @@ +<%- if search_by_rentals? %> + list = $(".bookingsync-rentals-list.rentals-list .rentals-list-scroll") + data = $("<%=escape_javascript(render file: 'bookingsync_portal/admin/rentals/index') %>") + newList = $(data).find(".bookingsync-rentals-list.rentals-list .rentals-list-scroll").first() + list.html(newList.html()) +<%- elsif search_by_remote_rentals? %> + list = $(".remote-rentals-list.rentals-list .rentals-list-scroll") + data = $("<%=escape_javascript(render file: 'bookingsync_portal/admin/rentals/index') %>") + newList = $(data).find(".remote-rentals-list.rentals-list .rentals-list-scroll").first() + list.html(newList.html()) +<%- end %> + + + diff --git a/app/views/layouts/bookingsync_portal/admin.html.erb b/app/views/layouts/bookingsync_portal/admin.html.erb index a3afa18b..9227a16a 100644 --- a/app/views/layouts/bookingsync_portal/admin.html.erb +++ b/app/views/layouts/bookingsync_portal/admin.html.erb @@ -9,7 +9,7 @@ <%= stylesheet_link_tag 'admin/application', media: 'all' %> <%= csrf_meta_tags %> - + <%= render partial: '/layouts/bookingsync_portal/menu' %>
<%= render '/layouts/bookingsync_portal/flash' %> diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index db5f052f..1d333762 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -33,6 +33,9 @@ Gem::Specification.new do |s| s.add_dependency 'sass-rails' s.add_dependency 'uglifier' s.add_dependency 'coffee-rails' + + s.add_dependency 'ransack' + s.add_dependency 'kaminari' s.add_development_dependency 'appraisal' s.add_development_dependency 'rspec-rails' diff --git a/config/locales/en.yml b/config/locales/en.yml index 0269e231..557b0662 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -53,6 +53,7 @@ en: rentals: no_published_rentals_html: 'No published rentals present, please make sure to fill all your rentals details.' all_synchronized: Perfect! All your rentals are synchronized. + no_results: No results rental: bedrooms_html: one: '1 Bedroom' diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index 3f50883a..b9613a7b 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -61,19 +61,25 @@ module BookingsyncPortal mattr_accessor :use_paginated_view @@use_paginated_view = ->(_account) { false } - # rate model class + # search by not connected rentals mattr_accessor :rentals_search @@rentals_search = { - rentals: %w(name synced_id) + numeric: %w(synced_id), + string: %w(name) } + + # search by remote rentals rentals mattr_accessor :remote_rentals_search @@remote_rentals_search = { - rentals: %w(name synced_id), - remote_rentals: %w(uid), - remote_accounts: %w(uid) + numeric: %w(uid rental.synced_id remote_account.uid), + string: %w(rental.name) } + # included tables for remote_rentals_by_account + mattr_accessor :remote_rentals_by_account_included_tables + @@remote_rentals_by_account_included_tables = %w(remote_account rental) + # message bus channel scope mattr_accessor :message_bus_channel_scope From 18345b9ca5daaf7a1b32bccf6a86ef6fabffa403 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Mon, 17 Jun 2019 11:36:29 +0300 Subject: [PATCH 04/26] Apply PR feedback --- .../admin/lib/list-backend-filter.js.coffee | 33 +++++++++++++++++-- .../admin/templates/pagination.hbs | 4 +-- .../admin/rentals_controller.rb | 26 +++++++-------- .../admin/rentals/index_with_search.js.erb | 7 ++++ .../layouts/bookingsync_portal/admin.html.erb | 6 +++- bookingsync_portal.gemspec | 4 +-- lib/bookingsync_portal.rb | 4 +++ 7 files changed, 61 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index 12a9e4f9..1441f6c1 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -1,12 +1,12 @@ class @ListBackedFilter - constructor: (header, list, @listElement, @listFilterable, @inputId, formTemplate) -> + constructor: (header, list, @listElement, @listFilterable, @inputId, formTemplate, paginationTemplate) -> @header = $(header) @list = $(list) @form = $(formTemplate) @insertForm() @observeInputChanges() - @paginationTemplate = $(HandlebarsTemplates["pagination"]()) + @paginationTemplate ||= $(HandlebarsTemplates["pagination"]()) @insertPagination() @observePageChanges() @@ -20,6 +20,7 @@ class @ListBackedFilter @input = $("#" + @inputId) insertPagination: -> + @paginationTemplate.find("[data-type=previous]").addClass("disabled") @setPage(1) $(@paginationTemplate).appendTo(@list.parent()) @@ -29,6 +30,15 @@ class @ListBackedFilter currentPage: -> $(@form).data("current-page") + firstPage: -> + @currentPage() == 1 + + lastPage: -> + if @form.parents(".bookingsync-rentals-list").length > 0 + @list.find('.panel.panel-bookingsync.bookingsync-rental').length < $("body").data("items-per-page") + else + @list.find('.panel.panel-connected').length < $("body").data("items-per-page") + observeInputChanges: -> @input.on 'keyup', => @setPage(1) @@ -42,9 +52,22 @@ class @ListBackedFilter displayWaiting: -> @list.html(@loadingTemplate) unless @list.children()[0] == @loadingTemplate[0] + @paginationTemplate.find("[data-type=previous]").addClass("disabled") + @paginationTemplate.find("[data-type=next]").addClass("disabled") backendSearch: => - $.get(@getSearchQuery()) + $.get(@getSearchQuery(), @afterSearch) + + afterSearch: => + if @firstPage() + @paginationTemplate.find("[data-type=previous]").addClass("disabled") + else + @paginationTemplate.find("[data-type=previous]").removeClass("disabled") + + if @lastPage() + @paginationTemplate.find("[data-type=next]").addClass("disabled") + else + @paginationTemplate.find("[data-type=next]").removeClass("disabled") getSearchQuery: -> if @form.parents(".bookingsync-rentals-list").length > 0 @@ -56,10 +79,14 @@ class @ListBackedFilter goToPreviousPage: (e) => e.preventDefault() + return if @firstPage() $(@form).data("current-page", @currentPage() - 1) + @displayWaiting() @backendSearch() goToNextPage: (e) => e.preventDefault() + return if @lastPage() $(@form).data("current-page", @currentPage() + 1) + @displayWaiting() @backendSearch() diff --git a/app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs b/app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs index 0196b4d2..db37df56 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs +++ b/app/assets/javascripts/bookingsync_portal/admin/templates/pagination.hbs @@ -1,6 +1,6 @@ diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 086431bd..f4fb11cd 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -9,11 +9,11 @@ class RentalsController < Admin::BaseController helper_method :search_by_remote_rentals? def index - index_preparation + prepare_index_variables end def index_with_search - index_preparation do + prepare_index_variables do @search_filter = BookingsyncPortal::SearchFilter.new(params) apply_search @@ -48,33 +48,27 @@ def resolve_action end - def index_preparation + def prepare_index_variables @not_connected_rentals = current_account.rentals.visible.ordered.not_connected @visible_rentals = current_account.rentals.visible - @remote_accounts = current_account.remote_accounts @remote_rentals = current_account.remote_rentals.ordered.joins(:rental, :remote_account) yield if block_given? - @remote_rentals_by_account = current_account.remote_rentals.where(id: @remote_rentals.pluck(:id)).ordered + @remote_rentals_by_account = @remote_rentals .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) .group_by(&:remote_account) + @remote_accounts = @remote_rentals_by_account.keys end def apply_search - if @search_filter.rentals_query.present? - @not_connected_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.rentals_query, records: @not_connected_rentals, source: "rentals_search") - end - - if @search_filter.remote_rentals_query.present? - @remote_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: @remote_rentals, source: "remote_rentals_search") - end + @not_connected_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.rentals_query, records: @not_connected_rentals, source: "rentals_search") + @remote_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: @remote_rentals, source: "remote_rentals_search") end def apply_pagination - @not_connected_rentals = @not_connected_rentals.page(@search_filter.rentals_page) - @remote_rentals = @remote_rentals.page(@search_filter.remote_rentals_page) - @remote_accounts = @remote_accounts.where(id: @remote_rentals.pluck(:remote_account_id)) + @not_connected_rentals = @not_connected_rentals.page(@search_filter.rentals_page).per(BookingsyncPortal.items_per_page) + @remote_rentals = @remote_rentals.page(@search_filter.remote_rentals_page).per(BookingsyncPortal.items_per_page) end def synchronize_rentals @@ -116,6 +110,8 @@ def remote_rentals_page class Searcher # TODO add tests def self.call(query:, source:, records:) + return records if query.blank? + search_settings = BookingsyncPortal.rentals_search if source == "rentals_search" search_settings ||= BookingsyncPortal.remote_rentals_search diff --git a/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb index dc7d5c43..93c75aa8 100644 --- a/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb +++ b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb @@ -3,12 +3,19 @@ data = $("<%=escape_javascript(render file: 'bookingsync_portal/admin/rentals/index') %>") newList = $(data).find(".bookingsync-rentals-list.rentals-list .rentals-list-scroll").first() list.html(newList.html()) + + $(".bookingsync-rental").draggableRental() + $(".panel.panel-remote").droppableRemoteRental() <%- elsif search_by_remote_rentals? %> list = $(".remote-rentals-list.rentals-list .rentals-list-scroll") data = $("<%=escape_javascript(render file: 'bookingsync_portal/admin/rentals/index') %>") newList = $(data).find(".remote-rentals-list.rentals-list .rentals-list-scroll").first() list.html(newList.html()) + + $(".bookingsync-rental").draggableRental() + $(".panel.panel-remote").droppableRemoteRental() <%- end %> + diff --git a/app/views/layouts/bookingsync_portal/admin.html.erb b/app/views/layouts/bookingsync_portal/admin.html.erb index 9227a16a..80ede498 100644 --- a/app/views/layouts/bookingsync_portal/admin.html.erb +++ b/app/views/layouts/bookingsync_portal/admin.html.erb @@ -9,7 +9,11 @@ <%= stylesheet_link_tag 'admin/application', media: 'all' %> <%= csrf_meta_tags %> - + <%= render partial: '/layouts/bookingsync_portal/menu' %>
<%= render '/layouts/bookingsync_portal/flash' %> diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index 1d333762..230274af 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -34,8 +34,8 @@ Gem::Specification.new do |s| s.add_dependency 'uglifier' s.add_dependency 'coffee-rails' - s.add_dependency 'ransack' - s.add_dependency 'kaminari' + s.add_runtime_dependency 'ransack' + s.add_runtime_dependency 'kaminari' s.add_development_dependency 'appraisal' s.add_development_dependency 'rspec-rails' diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index b9613a7b..b8010897 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -75,6 +75,10 @@ module BookingsyncPortal string: %w(rental.name) } + # the number of items that will be displayed per page + # works only with enabled use_paginated_view + mattr_accessor :items_per_page + @@items_per_page = 25 # included tables for remote_rentals_by_account mattr_accessor :remote_rentals_by_account_included_tables From b49e35ca59a292d01da51428277492587a9d6fe2 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Mon, 17 Jun 2019 16:47:28 +0300 Subject: [PATCH 05/26] Fix bug with not displaying remote_accounts without remote_rentals. --- .../admin/rentals_controller.rb | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index f4fb11cd..072ac510 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -52,18 +52,37 @@ def prepare_index_variables @not_connected_rentals = current_account.rentals.visible.ordered.not_connected @visible_rentals = current_account.rentals.visible @remote_rentals = current_account.remote_rentals.ordered.joins(:rental, :remote_account) + @remote_accounts = current_account.remote_accounts yield if block_given? @remote_rentals_by_account = @remote_rentals .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) .group_by(&:remote_account) - @remote_accounts = @remote_rentals_by_account.keys + + @remote_accounts = @remote_accounts + .where(id: @remote_rentals_by_account.keys.map(&:id)) + .or(blank_remote_accounts) + end + + def blank_remote_accounts + result = current_account + .remote_accounts + .left_outer_joins(:remote_rentals) + .where(remote_rentals: { id: nil }) + + search_settings = {} + BookingsyncPortal.remote_rentals_search.each do |type, fields| + remote_account_fields = fields.select {|field| field.include?("remote_account.") } + search_settings[type] = remote_account_fields if remote_account_fields.present? + end.compact + result = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: result, search_settings: search_settings) + RemoteAccount.where(id: result.pluck(:id)) end def apply_search - @not_connected_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.rentals_query, records: @not_connected_rentals, source: "rentals_search") - @remote_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: @remote_rentals, source: "remote_rentals_search") + @not_connected_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.rentals_query, records: @not_connected_rentals, search_settings: BookingsyncPortal.rentals_search) + @remote_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: @remote_rentals, search_settings: BookingsyncPortal.remote_rentals_search) end def apply_pagination @@ -109,12 +128,10 @@ def remote_rentals_page end class Searcher # TODO add tests - def self.call(query:, source:, records:) + def self.call(query:, search_settings:, records:) return records if query.blank? + return records if search_settings.blank? - search_settings = BookingsyncPortal.rentals_search if source == "rentals_search" - search_settings ||= BookingsyncPortal.remote_rentals_search - conditions = { m: "or" } search_settings.each do |type, filtered_fields| From 773d23c1e8252fb5f07fd157c3a77ea3289a76eb Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Mon, 17 Jun 2019 19:17:47 +0300 Subject: [PATCH 06/26] Fix search by remote_accounts --- app/controllers/bookingsync_portal/admin/rentals_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 072ac510..13a4f7cd 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -73,7 +73,7 @@ def blank_remote_accounts search_settings = {} BookingsyncPortal.remote_rentals_search.each do |type, fields| - remote_account_fields = fields.select {|field| field.include?("remote_account.") } + remote_account_fields = fields.select {|field| field.include?("remote_account.") }.map {|f| f.gsub("remote_account.", "") } search_settings[type] = remote_account_fields if remote_account_fields.present? end.compact result = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: result, search_settings: search_settings) From 1548221b7c6f6234e351a3f5aa14c79ff96e06fa Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Tue, 18 Jun 2019 10:55:40 +0300 Subject: [PATCH 07/26] Fix tests Add tests for app/controllers/bookingsync_portal/admin/rentals_controller.rb. Fix searching for not connected remote_rentals --- .gitignore | 1 + Gemfile.lock | 128 ++++++++------ .../admin/rentals_controller.rb | 12 +- .../admin/rentals/_connected_rental.html.erb | 4 +- .../rentals/_new_remote_account.html.erb | 2 +- .../admin/rentals/index.html.erb | 2 +- bookingsync_portal.gemspec | 1 + lib/bookingsync_portal.rb | 2 + .../admin/rentals_controller_spec.rb | 158 +++++++++++++++++- .../admin/v2/rentals_controller_spec.rb | 33 ---- spec/rails_helper.rb | 1 + spec/request/admin_rentals_spec.rb | 2 +- 12 files changed, 248 insertions(+), 98 deletions(-) delete mode 100644 spec/controllers/admin/v2/rentals_controller_spec.rb diff --git a/.gitignore b/.gitignore index 25c3136a..1c4e3537 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.byebug_history .bundle/ log/*.log pkg/ diff --git a/Gemfile.lock b/Gemfile.lock index 1d70a73f..efdddcbe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,8 +9,10 @@ PATH handlebars_assets jquery-rails jquery-ui-rails (~> 6.0.1) + kaminari message_bus rails + ransack redis responders sass-rails @@ -67,29 +69,30 @@ GEM thor (>= 0.14.0) arel (7.1.4) ast (2.3.0) - autoprefixer-rails (7.1.1.2) + autoprefixer-rails (9.6.0) execjs - bookingsync-api (0.1.5) + bookingsync-api (0.1.11) addressable faraday (~> 0.9) hashie net-http-persistent (~> 2) - bookingsync-engine (3.0.0) - bookingsync-api (>= 0.0.28) + bookingsync-engine (3.0.2) + bookingsync-api (>= 0.1.7) omniauth-bookingsync (~> 0.5.0) rails (>= 5.0.0) - bookingsync_application (2.0.0) - bookingsync-engine (~> 3.0.0) + bookingsync_application (2.0.2) + bookingsync-engine (~> 3.0.2) dotenv-rails jsonapi-resources (~> 0.1) - rails (>= 5.0, < 5.2) + rails (>= 5.0, < 5.3) synced - bootstrap-bookingsync-sass (1.0.4) + bootstrap-bookingsync-sass (1.0.5) bootstrap-sass (>= 3.3.5) - bootstrap-sass (3.3.7) + bootstrap-sass (3.4.1) autoprefixer-rails (>= 5.2.1) - sass (>= 3.3.4) + sassc (>= 2.0.0) builder (3.2.2) + byebug (11.0.1) coderay (1.1.1) coffee-rails (4.2.2) coffee-script (>= 2.2.0) @@ -102,10 +105,10 @@ GEM crack (0.4.3) safe_yaml (~> 1.0.0) diff-lcs (1.2.5) - dotenv (2.2.1) - dotenv-rails (2.2.1) - dotenv (= 2.2.1) - railties (>= 3.2, < 5.2) + dotenv (2.7.2) + dotenv-rails (2.7.2) + dotenv (= 2.7.2) + railties (>= 3.2, < 6.1) erubis (2.7.0) execjs (2.7.0) factory_girl (4.8.0) @@ -115,43 +118,54 @@ GEM railties (>= 3.0.0) faraday (0.11.0) multipart-post (>= 1.2, < 3) - font-awesome-sass (4.7.0) - sass (>= 3.2) - globalid (0.4.0) + ffi (1.11.1) + font-awesome-sass (5.8.1) + sassc (>= 1.11) + globalid (0.4.2) activesupport (>= 4.2.0) - handlebars_assets (0.23.2) + handlebars_assets (0.23.4) execjs (~> 2.0) sprockets (>= 2.0.0) tilt (>= 1.2) hashdiff (0.3.1) - hashie (3.5.5) + hashie (3.6.0) i18n (0.7.0) - jquery-rails (4.3.1) + jquery-rails (4.3.5) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) jquery-ui-rails (6.0.1) railties (>= 3.2.16) - jsonapi-resources (0.9.0) + jsonapi-resources (0.9.8) activerecord (>= 4.1) concurrent-ruby railties (>= 4.1) jwt (1.5.6) + kaminari (1.1.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) + actionview + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) + activerecord + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) loofah (2.0.3) nokogiri (>= 1.5.9) - mail (2.6.6) - mime-types (>= 1.16, < 4) - message_bus (2.0.2) + mail (2.7.1) + mini_mime (>= 0.1.1) + message_bus (2.2.1) rack (>= 1.1.3) method_source (0.8.2) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) + mini_mime (1.0.1) mini_portile2 (2.1.0) minitest (5.10.1) - multi_json (1.12.1) + multi_json (1.13.1) multi_xml (0.6.0) - multipart-post (2.0.0) + multipart-post (2.1.1) net-http-persistent (2.9.4) nio4r (1.2.1) nokogiri (1.6.8.1) @@ -162,8 +176,8 @@ GEM multi_json (~> 1.3) multi_xml (~> 0.5) rack (>= 1.2, < 3) - omniauth (1.6.1) - hashie (>= 3.4.6, < 3.6.0) + omniauth (1.9.0) + hashie (>= 3.4.6, < 3.7.0) rack (>= 1.6.2, < 3) omniauth-bookingsync (0.5.0) oauth2 (~> 1.3.0) @@ -214,10 +228,18 @@ GEM thor (>= 0.18.1, < 2.0) rainbow (2.1.0) rake (12.0.0) - redis (3.3.3) - responders (2.4.0) - actionpack (>= 4.2.0, < 5.3) - railties (>= 4.2.0, < 5.3) + ransack (2.1.1) + actionpack (>= 5.0) + activerecord (>= 5.0) + activesupport (>= 5.0) + i18n + rb-fsevent (0.10.3) + rb-inotify (0.10.0) + ffi (~> 1.0) + redis (4.1.2) + responders (2.4.1) + actionpack (>= 4.2.0, < 6.0) + railties (>= 4.2.0, < 6.0) rspec-core (3.5.4) rspec-support (~> 3.5.0) rspec-expectations (3.5.0) @@ -243,44 +265,51 @@ GEM unicode-display_width (~> 1.0, >= 1.0.1) ruby-progressbar (1.8.1) safe_yaml (1.0.4) - sass (3.4.24) - sass-rails (5.0.6) + sass (3.7.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + sass-rails (5.0.7) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + sassc (2.0.1) + ffi (~> 1.9) + rake shoulda (3.5.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (>= 1.4.1, < 3.0) shoulda-context (1.2.2) shoulda-matchers (2.8.0) activesupport (>= 3.0.0) - simple_form (3.5.0) - actionpack (> 4, < 5.2) - activemodel (> 4, < 5.2) + simple_form (4.1.0) + actionpack (>= 5.0) + activemodel (>= 5.0) slop (3.6.0) - sprockets (3.7.1) + sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.0) + sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.12) - synced (1.6.0) + synced (1.8.0) bookingsync-api (>= 0.1.4) hashie rails (>= 4.0.0) thor (0.19.4) thread_safe (0.3.5) - tilt (2.0.7) - turbolinks (5.0.1) - turbolinks-source (~> 5) - turbolinks-source (5.0.3) + tilt (2.0.9) + turbolinks (5.2.0) + turbolinks-source (~> 5.2) + turbolinks-source (5.2.0) tzinfo (1.2.2) thread_safe (~> 0.1) - uglifier (3.2.0) + uglifier (4.1.20) execjs (>= 0.3.0, < 3) unicode-display_width (1.1.2) vcr (3.0.3) @@ -290,7 +319,7 @@ GEM hashdiff websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) + websocket-extensions (0.1.4) PLATFORMS ruby @@ -298,6 +327,7 @@ PLATFORMS DEPENDENCIES appraisal bookingsync_portal! + byebug factory_girl_rails pry-rails rails-controller-testing diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 13a4f7cd..766ed379 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -14,8 +14,6 @@ def index def index_with_search prepare_index_variables do - @search_filter = BookingsyncPortal::SearchFilter.new(params) - apply_search apply_pagination end @@ -51,18 +49,19 @@ def resolve_action def prepare_index_variables @not_connected_rentals = current_account.rentals.visible.ordered.not_connected @visible_rentals = current_account.rentals.visible - @remote_rentals = current_account.remote_rentals.ordered.joins(:rental, :remote_account) + @remote_rentals = current_account.remote_rentals.ordered @remote_accounts = current_account.remote_accounts + @search_filter = BookingsyncPortal::SearchFilter.new(params) + yield if block_given? @remote_rentals_by_account = @remote_rentals .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) .group_by(&:remote_account) - @remote_accounts = @remote_accounts - .where(id: @remote_rentals_by_account.keys.map(&:id)) - .or(blank_remote_accounts) + remote_account_ids = @remote_rentals_by_account.keys.map(&:id) + blank_remote_accounts.pluck(:id) + @remote_accounts = @remote_accounts.where(id: remote_account_ids) end def blank_remote_accounts @@ -77,7 +76,6 @@ def blank_remote_accounts search_settings[type] = remote_account_fields if remote_account_fields.present? end.compact result = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: result, search_settings: search_settings) - RemoteAccount.where(id: result.pluck(:id)) end def apply_search diff --git a/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb b/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb index e189d67d..26e9c055 100644 --- a/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb @@ -17,9 +17,9 @@ class: ["btn", "btn-xs", "remove-connection", rental.remote_rental.synchronized? ? "btn-success" : "btn-warning"], data: { disable_with: t('.disconnecting_rental') }, method: :delete, remote: true do %> <% unless rental.remote_rental.synchronized? %> - <%= icon('spinner') %> + <%= icon('fa', 'spinner') %> <% else %> - <%= icon('check') %> + <%= icon('fa', 'check') %> <% end %> <%= t('.disconnect_rental') %> <% end %> diff --git a/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb b/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb index 743a995c..56e5bfa7 100644 --- a/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb @@ -1,3 +1,3 @@ <%= link_to new_admin_remote_account_path, class: "btn btn-default" do %> - <%= icon :plus %> <%= t '.connect_accounts' %> + <%= icon 'fa', :plus %> <%= t '.connect_accounts' %> <% 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..b458e0e9 100644 --- a/app/views/bookingsync_portal/admin/rentals/index.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/index.html.erb @@ -39,7 +39,7 @@ <% end %> <%- elsif !BookingsyncPortal.create_remote_rental -%>
-

<%= icon 'info fa-lg' %>

+

<%= icon 'fa', 'info fa-lg' %>

<%=t '.create_listings_first', portal_name: BookingsyncPortal.portal_name %>

<%- end -%> diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index 230274af..5cd49f2e 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -37,6 +37,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'ransack' s.add_runtime_dependency 'kaminari' + s.add_development_dependency 'byebug' s.add_development_dependency 'appraisal' s.add_development_dependency 'rspec-rails' s.add_development_dependency 'shoulda' diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index b8010897..31623d74 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -16,6 +16,8 @@ require 'simple_form' require 'turbolinks' require 'responders' +require 'kaminari' +require 'ransack' module BookingsyncPortal # portal name diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index 30cc3d70..a5c0b83c 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -5,10 +5,13 @@ routes { BookingsyncPortal::Engine.routes } let!(:account) { create(:account) } - let!(:remote_account) { create(:remote_account, account: account) } - let(:rental) { create(:rental, account: account) } - let!(:rental_connected) { create(:rental, account: account) } - let!(:connection) { create(:connection, rental: rental_connected) } + let!(:remote_account) { create(:remote_account, account: account, uid: 3001) } + let(:rental) { create(:rental, account: account, synced_id: 1001) } + let!(:remote_rental) { create(:remote_rental, remote_account: remote_account, uid: 2001) } + let!(:rental_connected) { create(:rental, account: account, synced_id: 1002) } + let!(:remote_account_connected) { create(:remote_account, account: account, uid: 3002) } + let!(:remote_rental_connected) { create(:remote_rental, remote_account: remote_account_connected, uid: 2002) } + let!(:connection) { create(:connection, rental: rental_connected, remote_rental: remote_rental_connected) } before do request.env['HTTPS'] = 'on' @@ -27,4 +30,151 @@ expect { get :index }.to change { Rental.count } end end + + describe 'GET #index_with_search' do + subject(:index_with_search) do + get :index_with_search, params: params, format: request_format + end + + let(:params) do + { + rentals_search: {query: rentals_search_query, page: 1}, + remote_rentals_search: {query: remote_rentals_search_query, page: 1}, + } + end + let(:rentals_search_query) { "" } + let(:remote_rentals_search_query) { "" } + let(:request_format) { :html } + + context "when format is js" do + let(:request_format) { :js } + + it "does not call Rental.synchronize for current_account" do + expect(Rental).not_to receive(:synchronize).with(scope: account) + index_with_search + end + end + + context "when format is html" do + let(:request_format) { :html } + + before do + allow(Rental).to receive(:synchronize).with(scope: account) do + # pretending to sync rentals :P + rental + end + end + + it "calls Rental.synchronize for current_account" do + expect(Rental).to receive(:synchronize).with(scope: account) + index_with_search + end + end + + context "when there is rentals_search query" do + context "and it's empty string" do + let(:rentals_search_query) { "" } + + it "does not filter rentals" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_connected => [remote_rental_connected], + remote_account => [remote_rental] + }) + end + end + + context "and it's attempt to filter by" do + context "rental.synced_id" do + let(:rentals_search_query) { "#{rental.synced_id}" } + + it "filters not_connected_rentals but does not filter remote_rentals part" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_connected => [remote_rental_connected], + remote_account => [remote_rental] + }) + end + end + + context "rental_connected.synced_id" do + let(:rentals_search_query) { "#{rental_connected.synced_id}" } + + it "filters not_connected_rentals but does not filter remote_rentals part" do + index_with_search + expect(assigns(:not_connected_rentals)).to be_blank + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_connected => [remote_rental_connected], + remote_account => [remote_rental] + }) + end + end + end + end + + context "when there is remote_rentals_search query" do + context "and it's empty string" do + let(:remote_rentals_search_query) { "" } + + it "does not filter rentals" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_connected => [remote_rental_connected], + remote_account => [remote_rental] + }) + end + end + + context "and it's attempt to filter by" do + context "rental.synced_id" do + let(:remote_rentals_search_query) { "#{rental.synced_id}" } + + it "filters remote_rentals but does not filter not_connected_rentals part" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({}) + end + end + + context "rental_connected.synced_id" do + let(:remote_rentals_search_query) { "#{rental_connected.synced_id}" } + + it "filters remote_rentals but does not filter not_connected_rentals part" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_connected => [remote_rental_connected] + }) + end + end + + context "remote_account.uid" do + let(:remote_rentals_search_query) { "#{remote_account.uid}" } + + it "filters remote_rentals but does not filter not_connected_rentals part" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account => [remote_rental] + }) + end + end + + context "remote_account_connected.uid" do + let(:remote_rentals_search_query) { "#{remote_account_connected.uid}" } + + it "filters remote_rentals but does not filter not_connected_rentals part" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_connected => [remote_rental_connected] + }) + end + end + end + end + end end diff --git a/spec/controllers/admin/v2/rentals_controller_spec.rb b/spec/controllers/admin/v2/rentals_controller_spec.rb deleted file mode 100644 index 11550198..00000000 --- a/spec/controllers/admin/v2/rentals_controller_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'rails_helper' - -describe BookingsyncPortal::Admin::V2::RentalsController do - render_views - routes { BookingsyncPortal::Engine.routes } - - let!(:account) { create(:account) } - let!(:remote_account) { create(:remote_account, account: account) } - let(:rental) { create(:rental, account: account) } - let!(:rental_connected) { create(:rental, account: account) } - let!(:connection) { create(:connection, rental: rental_connected) } - - before do - request.env['HTTPS'] = 'on' - allow(controller).to receive(:current_account).and_return(account) - end - - describe 'GET #index' do - context 'when request format is html' do - it 'synchronizes rentals' do - expect(Rental).to receive(:synchronize).with(scope: account) { rental } # pretending to sync rentals :P - expect { get :index, format: :html }.to change { Rental.count } - end - end - - context 'when request format is not html' do - it 'does not synchronize rentals' do - expect(Rental).not_to receive(:synchronize) - expect { get :index, format: :json }.not_to change { Rental.count } - end - end - end -end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b11e0137..54c50193 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -8,6 +8,7 @@ require 'shoulda/matchers' require 'factory_girl_rails' require 'rails-controller-testing' +require 'byebug' Rails::Controller::Testing.install diff --git a/spec/request/admin_rentals_spec.rb b/spec/request/admin_rentals_spec.rb index 21f15ca5..2076fc0b 100644 --- a/spec/request/admin_rentals_spec.rb +++ b/spec/request/admin_rentals_spec.rb @@ -30,7 +30,7 @@ end before do - allow_any_instance_of(BookingsyncPortal::Admin::V2::RentalsController).to receive(:current_account).and_return(account) + allow_any_instance_of(BookingsyncPortal::Admin::RentalsController).to receive(:current_account).and_return(account) end it "successfully renders page" do From f6f29ccbd2e09952bf8799371b862cbe5784280f Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Tue, 18 Jun 2019 14:56:54 +0300 Subject: [PATCH 08/26] Fix js pagination Move SearchFilter and Searcher to separate files and add tests for them. Fix pagiation logic for blank remote_accounts. Add more tests --- .../admin/lib/list-backend-filter.js.coffee | 30 ++++--- .../admin/rentals_controller.rb | 58 ++----------- .../bookingsync_portal/search_filter.rb | 23 +++++ app/services/bookingsync_portal/searcher.rb | 26 ++++++ lib/bookingsync_portal.rb | 2 +- .../admin/rentals_controller_spec.rb | 57 ++++++++++-- .../bookingsync_portal/search_filter_spec.rb | 87 +++++++++++++++++++ .../bookingsync_portal/searcher_spec.rb | 47 ++++++++++ 8 files changed, 260 insertions(+), 70 deletions(-) create mode 100644 app/services/bookingsync_portal/search_filter.rb create mode 100644 app/services/bookingsync_portal/searcher.rb create mode 100644 spec/services/bookingsync_portal/search_filter_spec.rb create mode 100644 spec/services/bookingsync_portal/searcher_spec.rb diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index 1441f6c1..ae9119e6 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -23,6 +23,19 @@ class @ListBackedFilter @paginationTemplate.find("[data-type=previous]").addClass("disabled") @setPage(1) $(@paginationTemplate).appendTo(@list.parent()) + @refreshPagination() + + refreshPagination: -> + if @firstPage() + @paginationTemplate.find("[data-type=previous]").addClass("disabled") + else + @paginationTemplate.find("[data-type=previous]").removeClass("disabled") + + if @lastPage() + @paginationTemplate.find("[data-type=next]").addClass("disabled") + else + @paginationTemplate.find("[data-type=next]").removeClass("disabled") + setPage: (page)-> $(@form).data("current-page", page) @@ -34,10 +47,13 @@ class @ListBackedFilter @currentPage() == 1 lastPage: -> + $(".bookingsync-rentals-list").find(".panel").length < $("body").data("items-per-page") if @form.parents(".bookingsync-rentals-list").length > 0 - @list.find('.panel.panel-bookingsync.bookingsync-rental').length < $("body").data("items-per-page") + itemsCount = @list.find('.panel.bookingsync-rental').length else - @list.find('.panel.panel-connected').length < $("body").data("items-per-page") + itemsCount = @list.find(".panel.panel-connected").length + itemsCount += @list.find('.panel.panel-remote').length + itemsCount < $("body").data("items-per-page") observeInputChanges: -> @input.on 'keyup', => @@ -59,15 +75,7 @@ class @ListBackedFilter $.get(@getSearchQuery(), @afterSearch) afterSearch: => - if @firstPage() - @paginationTemplate.find("[data-type=previous]").addClass("disabled") - else - @paginationTemplate.find("[data-type=previous]").removeClass("disabled") - - if @lastPage() - @paginationTemplate.find("[data-type=next]").addClass("disabled") - else - @paginationTemplate.find("[data-type=next]").removeClass("disabled") + @refreshPagination() getSearchQuery: -> if @form.parents(".bookingsync-rentals-list").length > 0 diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 766ed379..a008bbae 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -60,11 +60,15 @@ def prepare_index_variables .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) .group_by(&:remote_account) - remote_account_ids = @remote_rentals_by_account.keys.map(&:id) + blank_remote_accounts.pluck(:id) - @remote_accounts = @remote_accounts.where(id: remote_account_ids) + @remote_rentals_by_account.merge!( + blank_remote_accounts.each_with_object({}) {|remote_account, res| res[remote_account] = []} + ) + @remote_accounts = @remote_rentals_by_account.keys end def blank_remote_accounts + return [] if @search_filter.remote_rentals_query.blank? && @search_filter.remote_rentals_page > 1 + result = current_account .remote_accounts .left_outer_joins(:remote_rentals) @@ -101,54 +105,4 @@ def rental end end end - - class SearchFilter # TODO add tests - attr_reader :params - def initialize(params) - @params = params - end - - def rentals_query - @rentals_query ||= params.dig(:rentals_search, :query).to_s.strip - end - - def remote_rentals_query - @remote_rentals_query ||= params.dig(:remote_rentals_search, :query).to_s.strip - end - - def rentals_page - @rentals_page ||= (params.dig(:rentals_search, :page).to_i || 1) - end - - def remote_rentals_page - @remote_rentals_page ||= (params.dig(:remote_rentals_search, :page).to_i || 1) - end - end - - class Searcher # TODO add tests - def self.call(query:, search_settings:, records:) - return records if query.blank? - return records if search_settings.blank? - - conditions = { m: "or" } - - search_settings.each do |type, filtered_fields| - filtered_fields.each do |field| - conditions.merge!(build_search_query(type, field.gsub(".", "_"), query)) - end - end - - records.ransack(conditions).result - end - - private - - def self.build_search_query(type, field, query) - if type == :string - { "#{field}_cont" => query } - else - { "#{field}_eq" => query } - end - end - end end diff --git a/app/services/bookingsync_portal/search_filter.rb b/app/services/bookingsync_portal/search_filter.rb new file mode 100644 index 00000000..2fbd181e --- /dev/null +++ b/app/services/bookingsync_portal/search_filter.rb @@ -0,0 +1,23 @@ +class BookingsyncPortal::SearchFilter # TODO add tests + attr_reader :params + def initialize(params) + @params = params + end + + def rentals_query + @rentals_query ||= params.dig(:rentals_search, :query).to_s.strip + end + + def remote_rentals_query + @remote_rentals_query ||= params.dig(:remote_rentals_search, :query).to_s.strip + end + + def rentals_page + @rentals_page ||= [params.dig(:rentals_search, :page).to_i, 1].max + end + + def remote_rentals_page + @remote_rentals_page ||= [params.dig(:remote_rentals_search, :page).to_i, 1].max + end +end + diff --git a/app/services/bookingsync_portal/searcher.rb b/app/services/bookingsync_portal/searcher.rb new file mode 100644 index 00000000..4eb11206 --- /dev/null +++ b/app/services/bookingsync_portal/searcher.rb @@ -0,0 +1,26 @@ +class BookingsyncPortal::Searcher # TODO add tests + def self.call(query:, search_settings:, records:) + return records if query.blank? + return records if search_settings.blank? + + conditions = { m: "or" } + + search_settings.each do |type, filtered_fields| + filtered_fields.each do |field| + conditions.merge!(build_search_query(type, field.gsub(".", "_"), query)) + end + end + + records.ransack(conditions).result + end + + private + + def self.build_search_query(type, field, query) + if type == :string + { "#{field}_cont" => query } + else + { "#{field}_eq" => query } + end + end +end diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index 31623d74..672500ed 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -73,7 +73,7 @@ module BookingsyncPortal # search by remote rentals rentals mattr_accessor :remote_rentals_search @@remote_rentals_search = { - numeric: %w(uid rental.synced_id remote_account.uid), + numeric: %w(uid remote_account.uid), string: %w(rental.name) } diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index a5c0b83c..5275388d 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -12,6 +12,7 @@ let!(:remote_account_connected) { create(:remote_account, account: account, uid: 3002) } let!(:remote_rental_connected) { create(:remote_rental, remote_account: remote_account_connected, uid: 2002) } let!(:connection) { create(:connection, rental: rental_connected, remote_rental: remote_rental_connected) } + let!(:remote_account_empty) { create(:remote_account, account: account, uid: 3003) } before do request.env['HTTPS'] = 'on' @@ -38,12 +39,14 @@ let(:params) do { - rentals_search: {query: rentals_search_query, page: 1}, - remote_rentals_search: {query: remote_rentals_search_query, page: 1}, + rentals_search: {query: rentals_search_query, page: rentals_search_page}, + remote_rentals_search: {query: remote_rentals_search_query, page: remote_rentals_search_page}, } end let(:rentals_search_query) { "" } + let(:rentals_search_page) { 1 } let(:remote_rentals_search_query) { "" } + let(:remote_rentals_search_page) { 1 } let(:request_format) { :html } context "when format is js" do @@ -80,9 +83,25 @@ expect(assigns(:not_connected_rentals)).to contain_exactly(rental) expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], - remote_account => [remote_rental] + remote_account => [remote_rental], + remote_account_empty => [] }) end + + context "and goes to the next page" do + let(:rentals_search_page) { 2 } + + it "appies pagination only for not_connected_rentals" do + index_with_search + expect(assigns(:not_connected_rentals)).to be_blank + + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_connected => [remote_rental_connected], + remote_account => [remote_rental], + remote_account_empty => [] + }) + end + end end context "and it's attempt to filter by" do @@ -94,7 +113,8 @@ expect(assigns(:not_connected_rentals)).to contain_exactly(rental) expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], - remote_account => [remote_rental] + remote_account => [remote_rental], + remote_account_empty => [] }) end end @@ -107,7 +127,8 @@ expect(assigns(:not_connected_rentals)).to be_blank expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], - remote_account => [remote_rental] + remote_account => [remote_rental], + remote_account_empty => [] }) end end @@ -123,9 +144,20 @@ expect(assigns(:not_connected_rentals)).to contain_exactly(rental) expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], - remote_account => [remote_rental] + remote_account => [remote_rental], + remote_account_empty => [] }) end + + context "and goes to the next page" do + let(:remote_rentals_search_page) { 2 } + + it "appies pagination only for remote_rentals" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to be_blank + end + end end context "and it's attempt to filter by" do @@ -174,6 +206,19 @@ }) end end + + context "remote_account_empty.uid" do + let(:remote_rentals_search_query) { "#{remote_account_empty.uid}" } + + it "filters remote_rentals but does not filter not_connected_rentals part" do + index_with_search + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_empty => [] + }) + end + end + end end end diff --git a/spec/services/bookingsync_portal/search_filter_spec.rb b/spec/services/bookingsync_portal/search_filter_spec.rb new file mode 100644 index 00000000..6617ab04 --- /dev/null +++ b/spec/services/bookingsync_portal/search_filter_spec.rb @@ -0,0 +1,87 @@ +require 'rails_helper' + +describe BookingsyncPortal::SearchFilter do + let(:search_filter) { described_class.new(params) } + let(:params) do + { + rentals_search: rentals_search, + remote_rentals_search: remote_rentals_search, + } + end + let(:remote_rentals_search) { {} } + let(:rentals_search) { {} } + + describe "#rentals_query" do + subject(:rentals_query) { search_filter.rentals_query } + + let(:rentals_search) { { query: query } } + + context "when query is blank" do + let(:query) { "" } + + it { is_expected.to be_blank } + end + + context "when query exists" do + let(:query) { "bla-bla" } + + it { is_expected.to eq(query) } + end + end + + describe "#remote_rentals_query" do + subject(:remote_rentals_query) { search_filter.remote_rentals_query } + + let(:remote_rentals_search) { { query: query } } + + context "when remote_rentals_page is blank" do + let(:query) { "" } + + it { is_expected.to be_blank } + end + + context "when query exists" do + let(:query) { "bla-bla" } + + it { is_expected.to eq(query) } + end + end + + describe "#rentals_page" do + subject(:rentals_page) { search_filter.rentals_page } + + let(:rentals_search) { { page: page } } + + context "when page is blank" do + let(:page) { "" } + + it { is_expected.to eq(1) } + end + + context "when page exists" do + let(:page) { 20 } + + it { is_expected.to eq(page) } + end + + end + + describe "#remote_rentals_page" do + subject(:remote_rentals_page) { search_filter.remote_rentals_page } + + let(:remote_rentals_search) { { page: page } } + + context "when page is blank" do + let(:page) { "" } + + it { is_expected.to eq(1) } + end + + context "when page exists" do + let(:page) { 20 } + + it { is_expected.to eq(page) } + end + + end +end diff --git a/spec/services/bookingsync_portal/searcher_spec.rb b/spec/services/bookingsync_portal/searcher_spec.rb new file mode 100644 index 00000000..59a17ab7 --- /dev/null +++ b/spec/services/bookingsync_portal/searcher_spec.rb @@ -0,0 +1,47 @@ +require 'rails_helper' + +describe BookingsyncPortal::Searcher do + describe ".call" do + subject(:call) { described_class.call(query: query, search_settings: search_settings, records: records) } + + let(:records) { Rental.all } + let(:search_settings) do + { + numeric: %w(synced_id), + string: %w(synced_data) + } + end + + context "when query is synced_id" do + let(:query) { "1" } + + context "and there is rental with such synced_id" do + let!(:rental) { create(:rental, synced_id: query) } + + it { is_expected.to contain_exactly(rental) } + end + + context "and there is no rental with such synced_id" do + let!(:rental) { create(:rental, synced_id: query.to_i + 1) } + + it { is_expected.to be_blank } + end + end + + context "when query is string" do + let(:query) { "bla-bla" } + + context "and there is rental with such synced_id" do + let!(:rental) { create(:rental, synced_data: { name: query }) } + + it { is_expected.to contain_exactly(rental) } + end + + context "and there is no rental with such synced_id" do + let!(:rental) { create(:rental, synced_data: { name: "zzz" }) } + + it { is_expected.to be_blank } + end + end + end +end From 708e06398851b66646320ce08922400110c61c90 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Tue, 18 Jun 2019 15:37:35 +0300 Subject: [PATCH 09/26] Small cleaning --- .../admin/lib/list-backend-filter.js.coffee | 1 - .../bookingsync_portal/admin/rentals_controller.rb | 1 - app/services/bookingsync_portal/search_filter.rb | 2 +- app/services/bookingsync_portal/searcher.rb | 2 +- .../bookingsync_portal/admin/rentals/index_with_search.js.erb | 4 ---- bookingsync_portal.gemspec | 4 ++-- 6 files changed, 4 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index ae9119e6..5e617abe 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -36,7 +36,6 @@ class @ListBackedFilter else @paginationTemplate.find("[data-type=next]").removeClass("disabled") - setPage: (page)-> $(@form).data("current-page", page) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index a008bbae..a32a0185 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -45,7 +45,6 @@ def resolve_action redirect_to admin_v2_rentals_path if BookingsyncPortal.use_paginated_view.call(current_account) end - def prepare_index_variables @not_connected_rentals = current_account.rentals.visible.ordered.not_connected @visible_rentals = current_account.rentals.visible diff --git a/app/services/bookingsync_portal/search_filter.rb b/app/services/bookingsync_portal/search_filter.rb index 2fbd181e..e6ab84eb 100644 --- a/app/services/bookingsync_portal/search_filter.rb +++ b/app/services/bookingsync_portal/search_filter.rb @@ -1,4 +1,4 @@ -class BookingsyncPortal::SearchFilter # TODO add tests +class BookingsyncPortal::SearchFilter attr_reader :params def initialize(params) @params = params diff --git a/app/services/bookingsync_portal/searcher.rb b/app/services/bookingsync_portal/searcher.rb index 4eb11206..416c2893 100644 --- a/app/services/bookingsync_portal/searcher.rb +++ b/app/services/bookingsync_portal/searcher.rb @@ -1,4 +1,4 @@ -class BookingsyncPortal::Searcher # TODO add tests +class BookingsyncPortal::Searcher def self.call(query:, search_settings:, records:) return records if query.blank? return records if search_settings.blank? diff --git a/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb index 93c75aa8..b124ab28 100644 --- a/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb +++ b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb @@ -15,7 +15,3 @@ $(".bookingsync-rental").draggableRental() $(".panel.panel-remote").droppableRemoteRental() <%- end %> - - - - diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index 5cd49f2e..1889007e 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -34,8 +34,8 @@ Gem::Specification.new do |s| s.add_dependency 'uglifier' s.add_dependency 'coffee-rails' - s.add_runtime_dependency 'ransack' - s.add_runtime_dependency 'kaminari' + s.add_dependency 'ransack' + s.add_dependency 'kaminari' s.add_development_dependency 'byebug' s.add_development_dependency 'appraisal' From 2df58b8881f3e9580abdc0199287d7eee362a1b3 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Tue, 18 Jun 2019 20:27:27 +0300 Subject: [PATCH 10/26] Change order of displaying remote_accounts - empty are first --- .../admin/rentals_controller.rb | 9 ++++--- .../admin/rentals_controller_spec.rb | 27 ++++--------------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index a32a0185..3d41373c 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -59,14 +59,12 @@ def prepare_index_variables .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) .group_by(&:remote_account) - @remote_rentals_by_account.merge!( - blank_remote_accounts.each_with_object({}) {|remote_account, res| res[remote_account] = []} - ) + @remote_rentals_by_account = blank_remote_accounts.merge(@remote_rentals_by_account) @remote_accounts = @remote_rentals_by_account.keys end def blank_remote_accounts - return [] if @search_filter.remote_rentals_query.blank? && @search_filter.remote_rentals_page > 1 + return {} if @search_filter.remote_rentals_query.blank? && @search_filter.remote_rentals_page > 1 result = current_account .remote_accounts @@ -79,6 +77,9 @@ def blank_remote_accounts search_settings[type] = remote_account_fields if remote_account_fields.present? end.compact result = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: result, search_settings: search_settings) + result.each_with_object({}) do |remote_account, res| + res[remote_account] = [] + end end def apply_search diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index 5275388d..f7e3d5ed 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -86,6 +86,7 @@ remote_account => [remote_rental], remote_account_empty => [] }) + expect(assigns(:remote_rentals_by_account).first).to eq([remote_account_empty, []]) end context "and goes to the next page" do @@ -100,6 +101,7 @@ remote_account => [remote_rental], remote_account_empty => [] }) + expect(assigns(:remote_rentals_by_account).first).to eq([remote_account_empty, []]) end end end @@ -116,6 +118,7 @@ remote_account => [remote_rental], remote_account_empty => [] }) + expect(assigns(:remote_rentals_by_account).first).to eq([remote_account_empty, []]) end end @@ -130,6 +133,7 @@ remote_account => [remote_rental], remote_account_empty => [] }) + expect(assigns(:remote_rentals_by_account).first).to eq([remote_account_empty, []]) end end end @@ -147,6 +151,7 @@ remote_account => [remote_rental], remote_account_empty => [] }) + expect(assigns(:remote_rentals_by_account).first).to eq([remote_account_empty, []]) end context "and goes to the next page" do @@ -161,28 +166,6 @@ end context "and it's attempt to filter by" do - context "rental.synced_id" do - let(:remote_rentals_search_query) { "#{rental.synced_id}" } - - it "filters remote_rentals but does not filter not_connected_rentals part" do - index_with_search - expect(assigns(:not_connected_rentals)).to contain_exactly(rental) - expect(assigns(:remote_rentals_by_account)).to eq({}) - end - end - - context "rental_connected.synced_id" do - let(:remote_rentals_search_query) { "#{rental_connected.synced_id}" } - - it "filters remote_rentals but does not filter not_connected_rentals part" do - index_with_search - expect(assigns(:not_connected_rentals)).to contain_exactly(rental) - expect(assigns(:remote_rentals_by_account)).to eq({ - remote_account_connected => [remote_rental_connected] - }) - end - end - context "remote_account.uid" do let(:remote_rentals_search_query) { "#{remote_account.uid}" } From c7b20547dab40f63ea20582ec94a6f116e030665 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Wed, 19 Jun 2019 08:14:56 +0300 Subject: [PATCH 11/26] Downgrade font-awesome-sass to '4.7.0' to avoid breaking changes --- Gemfile.lock | 6 +++--- .../admin/rentals/_connected_rental.html.erb | 4 ++-- bookingsync_portal.gemspec | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index efdddcbe..b8a34129 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,7 +5,7 @@ PATH bookingsync_application (>= 1, < 3) bootstrap-bookingsync-sass (~> 1.0.0) coffee-rails - font-awesome-sass + font-awesome-sass (= 4.7.0) handlebars_assets jquery-rails jquery-ui-rails (~> 6.0.1) @@ -119,8 +119,8 @@ GEM faraday (0.11.0) multipart-post (>= 1.2, < 3) ffi (1.11.1) - font-awesome-sass (5.8.1) - sassc (>= 1.11) + font-awesome-sass (4.7.0) + sass (>= 3.2) globalid (0.4.2) activesupport (>= 4.2.0) handlebars_assets (0.23.4) diff --git a/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb b/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb index 26e9c055..e189d67d 100644 --- a/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/_connected_rental.html.erb @@ -17,9 +17,9 @@ class: ["btn", "btn-xs", "remove-connection", rental.remote_rental.synchronized? ? "btn-success" : "btn-warning"], data: { disable_with: t('.disconnecting_rental') }, method: :delete, remote: true do %> <% unless rental.remote_rental.synchronized? %> - <%= icon('fa', 'spinner') %> + <%= icon('spinner') %> <% else %> - <%= icon('fa', 'check') %> + <%= icon('check') %> <% end %> <%= t('.disconnect_rental') %> <% end %> diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index 1889007e..0a2607a1 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -25,7 +25,7 @@ Gem::Specification.new do |s| s.add_dependency 'jquery-rails' s.add_dependency 'jquery-ui-rails', '~> 6.0.1' s.add_dependency 'bootstrap-bookingsync-sass', '~> 1.0.0' - s.add_dependency 'font-awesome-sass' + s.add_dependency 'font-awesome-sass', '4.7.0' s.add_dependency 'handlebars_assets' s.add_dependency 'simple_form' s.add_dependency 'message_bus' From 6afad9627594ef8555d286ceee4c93d0a3486791 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Wed, 19 Jun 2019 08:35:11 +0300 Subject: [PATCH 12/26] Fix travis-ci --- gemfiles/rails_5.0.gemfile.lock | 112 ++++++++++++++++++++------------ gemfiles/rails_5.1.gemfile.lock | 112 ++++++++++++++++++++------------ 2 files changed, 144 insertions(+), 80 deletions(-) diff --git a/gemfiles/rails_5.0.gemfile.lock b/gemfiles/rails_5.0.gemfile.lock index 79c5c786..48b2174d 100644 --- a/gemfiles/rails_5.0.gemfile.lock +++ b/gemfiles/rails_5.0.gemfile.lock @@ -5,12 +5,14 @@ PATH bookingsync_application (>= 1, < 3) bootstrap-bookingsync-sass (~> 1.0.0) coffee-rails - font-awesome-sass + font-awesome-sass (= 4.7.0) handlebars_assets jquery-rails jquery-ui-rails (~> 6.0.1) + kaminari message_bus rails + ransack redis responders sass-rails @@ -67,29 +69,30 @@ GEM thor (>= 0.14.0) arel (7.1.4) ast (2.3.0) - autoprefixer-rails (7.1.1.2) + autoprefixer-rails (9.6.0) execjs - bookingsync-api (0.1.5) + bookingsync-api (0.1.11) addressable faraday (~> 0.9) hashie net-http-persistent (~> 2) - bookingsync-engine (3.0.0) - bookingsync-api (>= 0.0.28) + bookingsync-engine (3.0.2) + bookingsync-api (>= 0.1.7) omniauth-bookingsync (~> 0.5.0) rails (>= 5.0.0) - bookingsync_application (2.0.0) - bookingsync-engine (~> 3.0.0) + bookingsync_application (2.0.2) + bookingsync-engine (~> 3.0.2) dotenv-rails jsonapi-resources (~> 0.1) - rails (>= 5.0, < 5.2) + rails (>= 5.0, < 5.3) synced - bootstrap-bookingsync-sass (1.0.4) + bootstrap-bookingsync-sass (1.0.5) bootstrap-sass (>= 3.3.5) - bootstrap-sass (3.3.7) + bootstrap-sass (3.4.1) autoprefixer-rails (>= 5.2.1) - sass (>= 3.3.4) + sassc (>= 2.0.0) builder (3.2.3) + byebug (11.0.1) coderay (1.1.1) coffee-rails (4.2.2) coffee-script (>= 2.2.0) @@ -102,10 +105,10 @@ GEM crack (0.4.3) safe_yaml (~> 1.0.0) diff-lcs (1.3) - dotenv (2.2.1) - dotenv-rails (2.2.1) - dotenv (= 2.2.1) - railties (>= 3.2, < 5.2) + dotenv (2.7.2) + dotenv-rails (2.7.2) + dotenv (= 2.7.2) + railties (>= 3.2, < 6.1) erubis (2.7.0) execjs (2.7.0) factory_girl (4.8.0) @@ -115,33 +118,46 @@ GEM railties (>= 3.0.0) faraday (0.11.0) multipart-post (>= 1.2, < 3) + ffi (1.11.1) font-awesome-sass (4.7.0) sass (>= 3.2) globalid (0.4.0) activesupport (>= 4.2.0) - handlebars_assets (0.23.2) + handlebars_assets (0.23.4) execjs (~> 2.0) sprockets (>= 2.0.0) tilt (>= 1.2) hashdiff (0.3.4) - hashie (3.5.5) + hashie (3.6.0) i18n (0.8.4) - jquery-rails (4.3.1) + jquery-rails (4.3.5) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) jquery-ui-rails (6.0.1) railties (>= 3.2.16) - jsonapi-resources (0.9.0) + jsonapi-resources (0.9.8) activerecord (>= 4.1) concurrent-ruby railties (>= 4.1) jwt (1.5.6) + kaminari (1.1.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) + actionview + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) + activerecord + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.6) mime-types (>= 1.16, < 4) - message_bus (2.0.2) + message_bus (2.2.1) rack (>= 1.1.3) method_source (0.8.2) mime-types (3.1) @@ -149,9 +165,9 @@ GEM mime-types-data (3.2016.0521) mini_portile2 (2.2.0) minitest (5.10.2) - multi_json (1.12.1) + multi_json (1.13.1) multi_xml (0.6.0) - multipart-post (2.0.0) + multipart-post (2.1.1) net-http-persistent (2.9.4) nio4r (2.1.0) nokogiri (1.8.0) @@ -162,8 +178,8 @@ GEM multi_json (~> 1.3) multi_xml (~> 0.5) rack (>= 1.2, < 3) - omniauth (1.6.1) - hashie (>= 3.4.6, < 3.6.0) + omniauth (1.9.0) + hashie (>= 3.4.6, < 3.7.0) rack (>= 1.6.2, < 3) omniauth-bookingsync (0.5.0) oauth2 (~> 1.3.0) @@ -216,10 +232,18 @@ GEM rainbow (2.2.2) rake rake (12.0.0) - redis (3.3.3) - responders (2.4.0) - actionpack (>= 4.2.0, < 5.3) - railties (>= 4.2.0, < 5.3) + ransack (2.1.1) + actionpack (>= 5.0) + activerecord (>= 5.0) + activesupport (>= 5.0) + i18n + rb-fsevent (0.10.3) + rb-inotify (0.10.0) + ffi (~> 1.0) + redis (4.1.2) + responders (2.4.1) + actionpack (>= 4.2.0, < 6.0) + railties (>= 4.2.0, < 6.0) rspec-core (3.6.0) rspec-support (~> 3.6.0) rspec-expectations (3.6.0) @@ -246,22 +270,29 @@ GEM unicode-display_width (~> 1.0, >= 1.0.1) ruby-progressbar (1.8.1) safe_yaml (1.0.4) - sass (3.4.24) - sass-rails (5.0.6) + sass (3.7.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + sass-rails (5.0.7) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + sassc (2.0.1) + ffi (~> 1.9) + rake shoulda (3.5.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (>= 1.4.1, < 3.0) shoulda-context (1.2.2) shoulda-matchers (2.8.0) activesupport (>= 3.0.0) - simple_form (3.5.0) - actionpack (> 4, < 5.2) - activemodel (> 4, < 5.2) + simple_form (4.1.0) + actionpack (>= 5.0) + activemodel (>= 5.0) slop (3.6.0) sprockets (3.7.1) concurrent-ruby (~> 1.0) @@ -271,19 +302,19 @@ GEM activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.13) - synced (1.6.0) + synced (1.8.0) bookingsync-api (>= 0.1.4) hashie rails (>= 4.0.0) thor (0.19.4) thread_safe (0.3.6) - tilt (2.0.7) - turbolinks (5.0.1) - turbolinks-source (~> 5) - turbolinks-source (5.0.3) + tilt (2.0.9) + turbolinks (5.2.0) + turbolinks-source (~> 5.2) + turbolinks-source (5.2.0) tzinfo (1.2.3) thread_safe (~> 0.1) - uglifier (3.2.0) + uglifier (4.1.20) execjs (>= 0.3.0, < 3) unicode-display_width (1.3.0) vcr (3.0.3) @@ -301,6 +332,7 @@ PLATFORMS DEPENDENCIES appraisal bookingsync_portal! + byebug factory_girl_rails pry-rails rails (~> 5.0.0) @@ -313,4 +345,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.15.0 + 1.16.1 diff --git a/gemfiles/rails_5.1.gemfile.lock b/gemfiles/rails_5.1.gemfile.lock index 2c3e8aec..6fae4981 100644 --- a/gemfiles/rails_5.1.gemfile.lock +++ b/gemfiles/rails_5.1.gemfile.lock @@ -5,12 +5,14 @@ PATH bookingsync_application (>= 1, < 3) bootstrap-bookingsync-sass (~> 1.0.0) coffee-rails - font-awesome-sass + font-awesome-sass (= 4.7.0) handlebars_assets jquery-rails jquery-ui-rails (~> 6.0.1) + kaminari message_bus rails + ransack redis responders sass-rails @@ -67,29 +69,30 @@ GEM thor (>= 0.14.0) arel (8.0.0) ast (2.3.0) - autoprefixer-rails (7.1.1.2) + autoprefixer-rails (9.6.0) execjs - bookingsync-api (0.1.5) + bookingsync-api (0.1.11) addressable faraday (~> 0.9) hashie net-http-persistent (~> 2) - bookingsync-engine (3.0.0) - bookingsync-api (>= 0.0.28) + bookingsync-engine (3.0.2) + bookingsync-api (>= 0.1.7) omniauth-bookingsync (~> 0.5.0) rails (>= 5.0.0) - bookingsync_application (2.0.0) - bookingsync-engine (~> 3.0.0) + bookingsync_application (2.0.2) + bookingsync-engine (~> 3.0.2) dotenv-rails jsonapi-resources (~> 0.1) - rails (>= 5.0, < 5.2) + rails (>= 5.0, < 5.3) synced - bootstrap-bookingsync-sass (1.0.4) + bootstrap-bookingsync-sass (1.0.5) bootstrap-sass (>= 3.3.5) - bootstrap-sass (3.3.7) + bootstrap-sass (3.4.1) autoprefixer-rails (>= 5.2.1) - sass (>= 3.3.4) + sassc (>= 2.0.0) builder (3.2.3) + byebug (11.0.1) coderay (1.1.1) coffee-rails (4.2.2) coffee-script (>= 2.2.0) @@ -102,10 +105,10 @@ GEM crack (0.4.3) safe_yaml (~> 1.0.0) diff-lcs (1.3) - dotenv (2.2.1) - dotenv-rails (2.2.1) - dotenv (= 2.2.1) - railties (>= 3.2, < 5.2) + dotenv (2.7.2) + dotenv-rails (2.7.2) + dotenv (= 2.7.2) + railties (>= 3.2, < 6.1) erubi (1.6.1) execjs (2.7.0) factory_girl (4.8.0) @@ -115,33 +118,46 @@ GEM railties (>= 3.0.0) faraday (0.11.0) multipart-post (>= 1.2, < 3) + ffi (1.11.1) font-awesome-sass (4.7.0) sass (>= 3.2) globalid (0.4.0) activesupport (>= 4.2.0) - handlebars_assets (0.23.2) + handlebars_assets (0.23.4) execjs (~> 2.0) sprockets (>= 2.0.0) tilt (>= 1.2) hashdiff (0.3.4) - hashie (3.5.5) + hashie (3.6.0) i18n (0.8.4) - jquery-rails (4.3.1) + jquery-rails (4.3.5) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) jquery-ui-rails (6.0.1) railties (>= 3.2.16) - jsonapi-resources (0.9.0) + jsonapi-resources (0.9.8) activerecord (>= 4.1) concurrent-ruby railties (>= 4.1) jwt (1.5.6) + kaminari (1.1.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) + actionview + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) + activerecord + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.6) mime-types (>= 1.16, < 4) - message_bus (2.0.2) + message_bus (2.2.1) rack (>= 1.1.3) method_source (0.8.2) mime-types (3.1) @@ -149,9 +165,9 @@ GEM mime-types-data (3.2016.0521) mini_portile2 (2.2.0) minitest (5.10.2) - multi_json (1.12.1) + multi_json (1.13.1) multi_xml (0.6.0) - multipart-post (2.0.0) + multipart-post (2.1.1) net-http-persistent (2.9.4) nio4r (2.1.0) nokogiri (1.8.0) @@ -162,8 +178,8 @@ GEM multi_json (~> 1.3) multi_xml (~> 0.5) rack (>= 1.2, < 3) - omniauth (1.6.1) - hashie (>= 3.4.6, < 3.6.0) + omniauth (1.9.0) + hashie (>= 3.4.6, < 3.7.0) rack (>= 1.6.2, < 3) omniauth-bookingsync (0.5.0) oauth2 (~> 1.3.0) @@ -216,10 +232,18 @@ GEM rainbow (2.2.2) rake rake (12.0.0) - redis (3.3.3) - responders (2.4.0) - actionpack (>= 4.2.0, < 5.3) - railties (>= 4.2.0, < 5.3) + ransack (2.1.1) + actionpack (>= 5.0) + activerecord (>= 5.0) + activesupport (>= 5.0) + i18n + rb-fsevent (0.10.3) + rb-inotify (0.10.0) + ffi (~> 1.0) + redis (4.1.2) + responders (2.4.1) + actionpack (>= 4.2.0, < 6.0) + railties (>= 4.2.0, < 6.0) rspec-core (3.6.0) rspec-support (~> 3.6.0) rspec-expectations (3.6.0) @@ -246,22 +270,29 @@ GEM unicode-display_width (~> 1.0, >= 1.0.1) ruby-progressbar (1.8.1) safe_yaml (1.0.4) - sass (3.4.24) - sass-rails (5.0.6) + sass (3.7.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + sass-rails (5.0.7) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + sassc (2.0.1) + ffi (~> 1.9) + rake shoulda (3.5.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (>= 1.4.1, < 3.0) shoulda-context (1.2.2) shoulda-matchers (2.8.0) activesupport (>= 3.0.0) - simple_form (3.5.0) - actionpack (> 4, < 5.2) - activemodel (> 4, < 5.2) + simple_form (4.1.0) + actionpack (>= 5.0) + activemodel (>= 5.0) slop (3.6.0) sprockets (3.7.1) concurrent-ruby (~> 1.0) @@ -271,19 +302,19 @@ GEM activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.13) - synced (1.6.0) + synced (1.8.0) bookingsync-api (>= 0.1.4) hashie rails (>= 4.0.0) thor (0.19.4) thread_safe (0.3.6) - tilt (2.0.7) - turbolinks (5.0.1) - turbolinks-source (~> 5) - turbolinks-source (5.0.3) + tilt (2.0.9) + turbolinks (5.2.0) + turbolinks-source (~> 5.2) + turbolinks-source (5.2.0) tzinfo (1.2.3) thread_safe (~> 0.1) - uglifier (3.2.0) + uglifier (4.1.20) execjs (>= 0.3.0, < 3) unicode-display_width (1.3.0) vcr (3.0.3) @@ -301,6 +332,7 @@ PLATFORMS DEPENDENCIES appraisal bookingsync_portal! + byebug factory_girl_rails pry-rails rails (~> 5.1.0) @@ -313,4 +345,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.15.0 + 1.16.1 From 4ba5a0b5a84fa0d470e21665504ddb312d5cf06c Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Wed, 19 Jun 2019 08:39:52 +0300 Subject: [PATCH 13/26] Rollback changes in app/views/bookingsync_portal/admin/rentals/ templates --- .../admin/rentals/_new_remote_account.html.erb | 2 +- app/views/bookingsync_portal/admin/rentals/_rentals.html.erb | 4 ++-- app/views/bookingsync_portal/admin/rentals/index.html.erb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb b/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb index 56e5bfa7..743a995c 100644 --- a/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/_new_remote_account.html.erb @@ -1,3 +1,3 @@ <%= link_to new_admin_remote_account_path, class: "btn btn-default" do %> - <%= icon 'fa', :plus %> <%= t '.connect_accounts' %> + <%= icon :plus %> <%= t '.connect_accounts' %> <% end %> diff --git a/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb b/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb index d63e8b8d..63583731 100644 --- a/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb @@ -8,12 +8,12 @@ <% end %> <%- elsif search_by_rentals? || search_by_remote_rentals? %>
-

<%= icon 'fa', 'search fa-lg' %>

+

<%= icon 'search fa-lg' %>

<%=t '.no_results' %>

<%- else -%>
-

<%= icon 'fa', 'thumbs-up fa-lg' %>

+

<%= icon 'thumbs-up fa-lg' %>

<%=t '.all_synchronized' %>

<%- end -%> diff --git a/app/views/bookingsync_portal/admin/rentals/index.html.erb b/app/views/bookingsync_portal/admin/rentals/index.html.erb index b458e0e9..ff40aadb 100644 --- a/app/views/bookingsync_portal/admin/rentals/index.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/index.html.erb @@ -39,7 +39,7 @@ <% end %> <%- elsif !BookingsyncPortal.create_remote_rental -%>
-

<%= icon 'fa', 'info fa-lg' %>

+

<%= icon 'info fa-lg' %>

<%=t '.create_listings_first', portal_name: BookingsyncPortal.portal_name %>

<%- end -%> From cae3297290f63880e42bb06fe2ddbc18129a6ba1 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Wed, 19 Jun 2019 16:18:59 +0300 Subject: [PATCH 14/26] Fix lastPage method in @ListBackedFilter class --- .../admin/lib/list-backend-filter.js.coffee | 5 ++--- app/views/layouts/bookingsync_portal/admin.html.erb | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index 5e617abe..2d9a5c84 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -48,10 +48,9 @@ class @ListBackedFilter lastPage: -> $(".bookingsync-rentals-list").find(".panel").length < $("body").data("items-per-page") if @form.parents(".bookingsync-rentals-list").length > 0 - itemsCount = @list.find('.panel.bookingsync-rental').length + itemsCount = $("body").data("rentals-records-count") else - itemsCount = @list.find(".panel.panel-connected").length - itemsCount += @list.find('.panel.panel-remote').length + itemsCount = $("body").data("remote-rentals-records-count") itemsCount < $("body").data("items-per-page") observeInputChanges: -> diff --git a/app/views/layouts/bookingsync_portal/admin.html.erb b/app/views/layouts/bookingsync_portal/admin.html.erb index 80ede498..417d4ca7 100644 --- a/app/views/layouts/bookingsync_portal/admin.html.erb +++ b/app/views/layouts/bookingsync_portal/admin.html.erb @@ -13,6 +13,8 @@ data-messagebus-channel="<%= messagebus_channel %>" data-paginated-view="<%=use_paginated_view%>" data-items-per-page="<%= BookingsyncPortal.items_per_page %>" + data-rentals-records-count="<%= @not_connected_rentals.count %>" + data-remote-rentals-records-count="<%= @remote_rentals.count %>" > <%= render partial: '/layouts/bookingsync_portal/menu' %>
From d996899750ed692259a5c0ecec2d2d6f03a5351f Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Wed, 19 Jun 2019 16:45:42 +0300 Subject: [PATCH 15/26] Update data records-count attributes after each index_with_search.js request --- .../bookingsync_portal/admin/lib/list-backend-filter.js.coffee | 2 +- .../bookingsync_portal/admin/rentals/index_with_search.js.erb | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index 2d9a5c84..0c0e50bb 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -51,7 +51,7 @@ class @ListBackedFilter itemsCount = $("body").data("rentals-records-count") else itemsCount = $("body").data("remote-rentals-records-count") - itemsCount < $("body").data("items-per-page") + parseInt(itemsCount) < $("body").data("items-per-page") observeInputChanges: -> @input.on 'keyup', => diff --git a/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb index b124ab28..2a189779 100644 --- a/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb +++ b/app/views/bookingsync_portal/admin/rentals/index_with_search.js.erb @@ -15,3 +15,6 @@ $(".bookingsync-rental").draggableRental() $(".panel.panel-remote").droppableRemoteRental() <%- end %> + +$("body").data("rentals-records-count", "<%= @not_connected_rentals.count %>") +$("body").data("remote-rentals-records-count", "<%= @remote_rentals.count %>") From 62827e75e9fba5efe2f1fde81f65135647b05656 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Thu, 20 Jun 2019 05:30:48 +0300 Subject: [PATCH 16/26] Fix tests --- .../bookingsync_portal/admin/application_helper.rb | 8 ++++++++ app/views/layouts/bookingsync_portal/admin.html.erb | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/helpers/bookingsync_portal/admin/application_helper.rb b/app/helpers/bookingsync_portal/admin/application_helper.rb index 5750bc79..bb819e3e 100644 --- a/app/helpers/bookingsync_portal/admin/application_helper.rb +++ b/app/helpers/bookingsync_portal/admin/application_helper.rb @@ -33,6 +33,14 @@ def rental_details(rental) def use_paginated_view BookingsyncPortal.use_paginated_view.call(current_account) end + + def not_connected_rentals_count + @not_connected_rentals.present? ? @not_connected_rentals.count : 0 + end + + def remote_rentals_count + @remote_rentals.present? ? @remote_rentals.count : 0 + end end end end diff --git a/app/views/layouts/bookingsync_portal/admin.html.erb b/app/views/layouts/bookingsync_portal/admin.html.erb index 417d4ca7..16c54a15 100644 --- a/app/views/layouts/bookingsync_portal/admin.html.erb +++ b/app/views/layouts/bookingsync_portal/admin.html.erb @@ -13,8 +13,8 @@ data-messagebus-channel="<%= messagebus_channel %>" data-paginated-view="<%=use_paginated_view%>" data-items-per-page="<%= BookingsyncPortal.items_per_page %>" - data-rentals-records-count="<%= @not_connected_rentals.count %>" - data-remote-rentals-records-count="<%= @remote_rentals.count %>" + data-rentals-records-count="<%= not_connected_rentals_count %>" + data-remote-rentals-records-count="<%= remote_rentals_count %>" > <%= render partial: '/layouts/bookingsync_portal/menu' %>
From 9093cf2f4caceffd493c977d862e078adb951bcf Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Mon, 24 Jun 2019 14:28:09 +0300 Subject: [PATCH 17/26] Add extend_rentals_index_action gem setting to extend rentals#index_with_search and rentals#index actions Fix sync logic in index and index_with_search actions Skip system keys as a trigger to start searching Add filtered_strategies gem settings Add optional account arg for all filter strategies Update app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee to get search params based on form instead of getting them based on 1 input field --- .../admin/lib/list-backend-filter.js.coffee | 39 +++++++--- .../admin/rentals.js.coffee | 4 +- .../admin/templates/filter_input.hbs | 2 +- .../admin/rentals_controller.rb | 71 ++++++++++++------- .../admin/application_helper.rb | 2 +- .../default_rentals_filter_strategy.rb | 6 ++ .../bookingsync_portal/filter_strategies.rb | 4 ++ .../filter_strategies/base_strategy.rb | 28 ++++++++ .../blank_remote_accounts.rb | 17 +++++ .../filter_strategies/remote_rentals.rb | 11 +++ .../filter_strategies/rentals.rb | 11 +++ .../layouts/bookingsync_portal/admin.html.erb | 2 +- lib/bookingsync_portal.rb | 12 +++- .../admin/rentals_controller_spec.rb | 25 +++++++ .../blank_remote_accounts_spec.rb | 27 +++++++ .../filter_strategies/remote_rentals_spec.rb | 27 +++++++ .../filter_strategies/rentals_spec.rb | 27 +++++++ 17 files changed, 276 insertions(+), 39 deletions(-) create mode 100644 app/services/bookingsync_portal/default_rentals_filter_strategy.rb create mode 100644 app/services/bookingsync_portal/filter_strategies.rb create mode 100644 app/services/bookingsync_portal/filter_strategies/base_strategy.rb create mode 100644 app/services/bookingsync_portal/filter_strategies/blank_remote_accounts.rb create mode 100644 app/services/bookingsync_portal/filter_strategies/remote_rentals.rb create mode 100644 app/services/bookingsync_portal/filter_strategies/rentals.rb create mode 100644 spec/services/bookingsync_portal/filter_strategies/blank_remote_accounts_spec.rb create mode 100644 spec/services/bookingsync_portal/filter_strategies/remote_rentals_spec.rb create mode 100644 spec/services/bookingsync_portal/filter_strategies/rentals_spec.rb diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index 0c0e50bb..d5f5980a 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -4,7 +4,7 @@ class @ListBackedFilter @list = $(list) @form = $(formTemplate) @insertForm() - @observeInputChanges() + @observeFormChanges() @paginationTemplate ||= $(HandlebarsTemplates["pagination"]()) @insertPagination() @@ -53,12 +53,26 @@ class @ListBackedFilter itemsCount = $("body").data("remote-rentals-records-count") parseInt(itemsCount) < $("body").data("items-per-page") - observeInputChanges: -> - @input.on 'keyup', => - @setPage(1) - @displayWaiting() - clearTimeout(@typingTimer) - @typingTimer = setTimeout(@backendSearch, @doneTypingInterval) # make search request only when user is done typing + observeFormChanges: -> + skippedKeyCodes = [13, 16, 17, 18, 37, 38, 39, 40, 17, 91, 93, 224] + @input.on 'keyup', (e) => + if e.keyCode not in skippedKeyCodes + @startSearching() + + @input.bind 'paste', => + @startSearching() + + @input.on 'change', => + @startSearching() + + @form.on 'change', (e) => + @startSearching() + + startSearching: => + @setPage(1) + @displayWaiting() + clearTimeout(@typingTimer) + @typingTimer = setTimeout(@backendSearch, @doneTypingInterval) # make search request only when user is done typing observePageChanges: -> @paginationTemplate.find("[data-type=previous]").on "click", @goToPreviousPage @@ -80,14 +94,20 @@ class @ListBackedFilter fieldName = "rentals_search" else fieldName = "remote_rentals_search" - searchParams = "#{fieldName}[query]=#{@input.val()}&#{fieldName}[page]=#{@currentPage()}" - "#{document.location.href}.js?#{searchParams}" + searchParams = ["#{fieldName}[page]=#{@currentPage()}"] + $(@form.serialize().split("&")).each (_, item) -> + key = item.split("=")[0] + value = item.split("=")[1] + searchParams.push("#{fieldName}[#{key}]=#{value}") + + "#{document.location.href}.js?#{searchParams.join("&")}" goToPreviousPage: (e) => e.preventDefault() return if @firstPage() $(@form).data("current-page", @currentPage() - 1) @displayWaiting() + clearTimeout(@typingTimer) @backendSearch() goToNextPage: (e) => @@ -95,4 +115,5 @@ class @ListBackedFilter return if @lastPage() $(@form).data("current-page", @currentPage() + 1) @displayWaiting() + clearTimeout(@typingTimer) @backendSearch() diff --git a/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee index 38755451..e740a392 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/rentals.js.coffee @@ -6,12 +6,14 @@ $ -> for rentalsList, index in $(".rentals-list") inputId = "rentals-list-filter-#{index}" + filterInput = $(rentalsList).data("filter-input") + filterInput ||= "filter_input" new Filter( $(rentalsList).children(".rentals-list-header"), $(rentalsList).children(".rentals-list-scroll"), ".panel", ".panel h4", inputId, - HandlebarsTemplates["filter_input"] + HandlebarsTemplates[filterInput] inputId: inputId ) diff --git a/app/assets/javascripts/bookingsync_portal/admin/templates/filter_input.hbs b/app/assets/javascripts/bookingsync_portal/admin/templates/filter_input.hbs index e789f280..d8ffddd4 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/templates/filter_input.hbs +++ b/app/assets/javascripts/bookingsync_portal/admin/templates/filter_input.hbs @@ -1,6 +1,6 @@
- +
diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 3d41373c..5c9aee82 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -2,13 +2,14 @@ module BookingsyncPortal module Admin class RentalsController < Admin::BaseController before_action :resolve_action, only: :index - before_action :synchronize_rentals, only: :index - before_action :fetch_remote_rentals, only: :index helper_method :search_by_rentals? helper_method :search_by_remote_rentals? def index + synchronize_rentals + fetch_remote_rentals + prepare_index_variables end @@ -21,6 +22,7 @@ def index_with_search respond_to do |format| format.html do synchronize_rentals + fetch_remote_rentals render :index end format.js # view can be app specific @@ -42,54 +44,65 @@ def search_by_remote_rentals? end def resolve_action - redirect_to admin_v2_rentals_path if BookingsyncPortal.use_paginated_view.call(current_account) + redirect_to admin_v2_rentals_path if use_paginated_view? end def prepare_index_variables - @not_connected_rentals = current_account.rentals.visible.ordered.not_connected - @visible_rentals = current_account.rentals.visible - @remote_rentals = current_account.remote_rentals.ordered - @remote_accounts = current_account.remote_accounts - - @search_filter = BookingsyncPortal::SearchFilter.new(params) + @action_variables = OpenStruct.new + + @action_variables.not_connected_rentals = current_account.rentals.visible.ordered.not_connected + @action_variables.visible_rentals = current_account.rentals.visible + @action_variables.remote_rentals = current_account.remote_rentals.ordered yield if block_given? + BookingsyncPortal.extend_rentals_index_action.call(current_account, @action_variables, params) - @remote_rentals_by_account = @remote_rentals - .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) - .group_by(&:remote_account) + @action_variables.remote_rentals_by_account = @action_variables.remote_rentals + .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) + .group_by(&:remote_account) - @remote_rentals_by_account = blank_remote_accounts.merge(@remote_rentals_by_account) - @remote_accounts = @remote_rentals_by_account.keys + @action_variables.remote_rentals_by_account = blank_remote_accounts.merge(@action_variables.remote_rentals_by_account) + @action_variables.remote_accounts = @action_variables.remote_rentals_by_account.keys + + @action_variables.to_h.each do |variable_name, variable_value| + instance_variable_set("@#{variable_name}", variable_value) + end end def blank_remote_accounts - return {} if @search_filter.remote_rentals_query.blank? && @search_filter.remote_rentals_page > 1 + return {} if search_filter.remote_rentals_query.blank? && search_filter.remote_rentals_page > 1 result = current_account .remote_accounts .left_outer_joins(:remote_rentals) .where(remote_rentals: { id: nil }) - search_settings = {} - BookingsyncPortal.remote_rentals_search.each do |type, fields| - remote_account_fields = fields.select {|field| field.include?("remote_account.") }.map {|f| f.gsub("remote_account.", "") } - search_settings[type] = remote_account_fields if remote_account_fields.present? - end.compact - result = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: result, search_settings: search_settings) + BookingsyncPortal.filter_strategies.each do |strategy| + result = strategy.constantize.call(account: current_account, records: result, search_filter: search_filter) + end result.each_with_object({}) do |remote_account, res| res[remote_account] = [] end end def apply_search - @not_connected_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.rentals_query, records: @not_connected_rentals, search_settings: BookingsyncPortal.rentals_search) - @remote_rentals = BookingsyncPortal::Searcher.call(query: @search_filter.remote_rentals_query, records: @remote_rentals, search_settings: BookingsyncPortal.remote_rentals_search) + BookingsyncPortal.filter_strategies.each do |strategy| + @action_variables.not_connected_rentals = strategy.constantize.call( + account: current_account, + records: @action_variables.not_connected_rentals, + search_filter: search_filter + ) + @action_variables.remote_rentals = strategy.constantize.call( + account: current_account, + records: @action_variables.remote_rentals, + search_filter: search_filter + ) + end end def apply_pagination - @not_connected_rentals = @not_connected_rentals.page(@search_filter.rentals_page).per(BookingsyncPortal.items_per_page) - @remote_rentals = @remote_rentals.page(@search_filter.remote_rentals_page).per(BookingsyncPortal.items_per_page) + @action_variables.not_connected_rentals = @action_variables.not_connected_rentals.page(search_filter.rentals_page).per(BookingsyncPortal.items_per_page) + @action_variables.remote_rentals = @action_variables.remote_rentals.page(search_filter.remote_rentals_page).per(BookingsyncPortal.items_per_page) end def synchronize_rentals @@ -103,6 +116,14 @@ def fetch_remote_rentals def rental @rental ||= current_account.rentals.visible.find(params[:id]) end + + def search_filter + @search_filter ||= BookingsyncPortal::SearchFilter.new(params) + end + + def use_paginated_view? + BookingsyncPortal.use_paginated_view.call(current_account) + end end end end diff --git a/app/helpers/bookingsync_portal/admin/application_helper.rb b/app/helpers/bookingsync_portal/admin/application_helper.rb index bb819e3e..8e0e4809 100644 --- a/app/helpers/bookingsync_portal/admin/application_helper.rb +++ b/app/helpers/bookingsync_portal/admin/application_helper.rb @@ -30,7 +30,7 @@ def rental_details(rental) safe_join(details, ', ') end - def use_paginated_view + def use_paginated_view? BookingsyncPortal.use_paginated_view.call(current_account) end diff --git a/app/services/bookingsync_portal/default_rentals_filter_strategy.rb b/app/services/bookingsync_portal/default_rentals_filter_strategy.rb new file mode 100644 index 00000000..f782fe72 --- /dev/null +++ b/app/services/bookingsync_portal/default_rentals_filter_strategy.rb @@ -0,0 +1,6 @@ +class BookingsyncPortal::DefaultRentalsFilterStrategy + def self.call(records:, search_filter:) + return records if records.table_name != "rentals" + BookingsyncPortal::Searcher.call(query: search_filter.rentals_query, records: records, search_settings: BookingsyncPortal.rentals_search) + end +end diff --git a/app/services/bookingsync_portal/filter_strategies.rb b/app/services/bookingsync_portal/filter_strategies.rb new file mode 100644 index 00000000..66bcda6c --- /dev/null +++ b/app/services/bookingsync_portal/filter_strategies.rb @@ -0,0 +1,4 @@ +module BookingsyncPortal + module FilterStrategies + end +end diff --git a/app/services/bookingsync_portal/filter_strategies/base_strategy.rb b/app/services/bookingsync_portal/filter_strategies/base_strategy.rb new file mode 100644 index 00000000..6fd28380 --- /dev/null +++ b/app/services/bookingsync_portal/filter_strategies/base_strategy.rb @@ -0,0 +1,28 @@ +module BookingsyncPortal + module FilterStrategies + class BaseStrategy + + attr_accessor :account, :records, :search_filter + private :account, :records, :search_filter + + def initialize(account: nil, records:, search_filter:) + @account = account + @records = records + @search_filter = search_filter + end + + def self.call(account: nil, records:, search_filter:) + return records if models_for_filter.all? {|model_name| model_name.constantize.table_name != records.table_name } + new(account: account, records: records, search_filter: search_filter).call + end + + def self.filtered_models(*args) + @filtered_models = Array.wrap(args) + end + + def self.models_for_filter + @filtered_models ||= [] + end + end + end +end diff --git a/app/services/bookingsync_portal/filter_strategies/blank_remote_accounts.rb b/app/services/bookingsync_portal/filter_strategies/blank_remote_accounts.rb new file mode 100644 index 00000000..1b4faee3 --- /dev/null +++ b/app/services/bookingsync_portal/filter_strategies/blank_remote_accounts.rb @@ -0,0 +1,17 @@ +module BookingsyncPortal + module FilterStrategies + class BlankRemoteAccounts < BaseStrategy + + filtered_models BookingsyncPortal.remote_account_model + + def call + search_settings = {} + BookingsyncPortal.remote_rentals_search.each do |type, fields| + remote_account_fields = fields.select {|field| field.include?("remote_account.") }.map {|f| f.gsub("remote_account.", "") } + search_settings[type] = remote_account_fields if remote_account_fields.present? + end.compact + BookingsyncPortal::Searcher.call(query: search_filter.remote_rentals_query, records: records, search_settings: search_settings) + end + end + end +end diff --git a/app/services/bookingsync_portal/filter_strategies/remote_rentals.rb b/app/services/bookingsync_portal/filter_strategies/remote_rentals.rb new file mode 100644 index 00000000..e291cbc1 --- /dev/null +++ b/app/services/bookingsync_portal/filter_strategies/remote_rentals.rb @@ -0,0 +1,11 @@ +module BookingsyncPortal + module FilterStrategies + class RemoteRentals < BaseStrategy + filtered_models BookingsyncPortal.remote_rental_model + + def call + BookingsyncPortal::Searcher.call(query: search_filter.remote_rentals_query, records: records, search_settings: BookingsyncPortal.remote_rentals_search) + end + end + end +end diff --git a/app/services/bookingsync_portal/filter_strategies/rentals.rb b/app/services/bookingsync_portal/filter_strategies/rentals.rb new file mode 100644 index 00000000..e06820f4 --- /dev/null +++ b/app/services/bookingsync_portal/filter_strategies/rentals.rb @@ -0,0 +1,11 @@ +module BookingsyncPortal + module FilterStrategies + class Rentals < BaseStrategy + filtered_models BookingsyncPortal.rental_model + + def call + BookingsyncPortal::Searcher.call(query: search_filter.rentals_query, records: records, search_settings: BookingsyncPortal.rentals_search) + end + end + end +end diff --git a/app/views/layouts/bookingsync_portal/admin.html.erb b/app/views/layouts/bookingsync_portal/admin.html.erb index 16c54a15..b89b864f 100644 --- a/app/views/layouts/bookingsync_portal/admin.html.erb +++ b/app/views/layouts/bookingsync_portal/admin.html.erb @@ -11,7 +11,7 @@ (_account) { false } + @@use_paginated_view = Proc.new { |_account| false } # search by not connected rentals mattr_accessor :rentals_search @@ -77,11 +77,21 @@ module BookingsyncPortal string: %w(rental.name) } + mattr_accessor :filter_strategies + @@filter_strategies = [ + "BookingsyncPortal::FilterStrategies::Rentals", + "BookingsyncPortal::FilterStrategies::RemoteRentals", + "BookingsyncPortal::FilterStrategies::BlankRemoteAccounts" + ] + # the number of items that will be displayed per page # works only with enabled use_paginated_view mattr_accessor :items_per_page @@items_per_page = 25 + mattr_accessor :extend_rentals_index_action + @@extend_rentals_index_action = Proc.new { |_account, _action_variables, _params| } + # included tables for remote_rentals_by_account mattr_accessor :remote_rentals_by_account_included_tables @@remote_rentals_by_account_included_tables = %w(remote_account rental) diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index f7e3d5ed..4d03e832 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -204,5 +204,30 @@ end end + + context "when there is extend_rentals_index_action setting" do + let(:rentals_index_action_extention) do + Proc.new do |account, action_variables, params| + action_variables.not_connected_rentals = Rental.all + action_variables.remote_rentals = RemoteRental.all + action_variables.custom_variable = "BookingSync" + end + end + + before do + BookingsyncPortal.extend_rentals_index_action = rentals_index_action_extention + end + + after do + BookingsyncPortal.extend_rentals_index_action = Proc.new {} + end + + it "applies extended logic" do + index_with_search + expect(assigns(:not_connected_rentals)).to eq(Rental.all) + expect(assigns(:remote_rentals)).to eq(RemoteRental.all) + expect(assigns(:custom_variable)).to eq("BookingSync") + end + end end end diff --git a/spec/services/bookingsync_portal/filter_strategies/blank_remote_accounts_spec.rb b/spec/services/bookingsync_portal/filter_strategies/blank_remote_accounts_spec.rb new file mode 100644 index 00000000..cedeb143 --- /dev/null +++ b/spec/services/bookingsync_portal/filter_strategies/blank_remote_accounts_spec.rb @@ -0,0 +1,27 @@ +require 'rails_helper' + +describe BookingsyncPortal::FilterStrategies::BlankRemoteAccounts do + describe ".call" do + subject(:call) { described_class.call(records: records, search_filter: search_filter) } + + let(:search_filter) { BookingsyncPortal::SearchFilter.new({}) } + + context "when records is RemoteAccount based" do + let(:records) { RemoteAccount.all } + + it "delegates logic to searcher" do + expect(BookingsyncPortal::Searcher).to receive(:call) + call + end + end + + context "when records is not RemoteAccount based" do + let(:records) { RemoteRental.all } + + it "does not delegate logic to searcher and return current records" do + expect(BookingsyncPortal::Searcher).not_to receive(:call) + expect(call).to eq(records) + end + end + end +end diff --git a/spec/services/bookingsync_portal/filter_strategies/remote_rentals_spec.rb b/spec/services/bookingsync_portal/filter_strategies/remote_rentals_spec.rb new file mode 100644 index 00000000..141a95d5 --- /dev/null +++ b/spec/services/bookingsync_portal/filter_strategies/remote_rentals_spec.rb @@ -0,0 +1,27 @@ +require 'rails_helper' + +describe BookingsyncPortal::FilterStrategies::RemoteRentals do + describe ".call" do + subject(:call) { described_class.call(records: records, search_filter: search_filter) } + + let(:search_filter) { BookingsyncPortal::SearchFilter.new({}) } + + context "when records is RemoteRental based" do + let(:records) { RemoteRental.all } + + it "delegates logic to searcher" do + expect(BookingsyncPortal::Searcher).to receive(:call) + call + end + end + + context "when records is not RemoteRental based" do + let(:records) { Rental.all } + + it "does not delegate logic to searcher and return current records" do + expect(BookingsyncPortal::Searcher).not_to receive(:call) + expect(call).to eq(records) + end + end + end +end diff --git a/spec/services/bookingsync_portal/filter_strategies/rentals_spec.rb b/spec/services/bookingsync_portal/filter_strategies/rentals_spec.rb new file mode 100644 index 00000000..616424f6 --- /dev/null +++ b/spec/services/bookingsync_portal/filter_strategies/rentals_spec.rb @@ -0,0 +1,27 @@ +require 'rails_helper' + +describe BookingsyncPortal::FilterStrategies::Rentals do + describe ".call" do + subject(:call) { described_class.call(records: records, search_filter: search_filter) } + + let(:search_filter) { BookingsyncPortal::SearchFilter.new({}) } + + context "when records is Rental based" do + let(:records) { Rental.all } + + it "delegates logic to searcher" do + expect(BookingsyncPortal::Searcher).to receive(:call) + call + end + end + + context "when records is not Rental based" do + let(:records) { RemoteRental.all } + + it "does not delegate logic to searcher and return current records" do + expect(BookingsyncPortal::Searcher).not_to receive(:call) + expect(call).to eq(records) + end + end + end +end From 0af66e498d7f82e6badf79e021b8e821e2101cf1 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Wed, 26 Jun 2019 19:41:18 +0300 Subject: [PATCH 18/26] Add order by remote_account_id in Admin::Rentals index and index_with_search actions --- .../bookingsync_portal/admin/rentals_controller.rb | 1 + lib/bookingsync_portal.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index 5c9aee82..a143fb34 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -59,6 +59,7 @@ def prepare_index_variables @action_variables.remote_rentals_by_account = @action_variables.remote_rentals .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) + .order(:remote_account_id) .group_by(&:remote_account) @action_variables.remote_rentals_by_account = blank_remote_accounts.merge(@action_variables.remote_rentals_by_account) diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index f72302e1..cc39cc41 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -61,7 +61,7 @@ module BookingsyncPortal # whether load-all (false) or paginated (true) view should be used for admin#index mattr_accessor :use_paginated_view - @@use_paginated_view = Proc.new { |_account| false } + @@use_paginated_view = -> (_account) { false } # search by not connected rentals mattr_accessor :rentals_search @@ -90,7 +90,7 @@ module BookingsyncPortal @@items_per_page = 25 mattr_accessor :extend_rentals_index_action - @@extend_rentals_index_action = Proc.new { |_account, _action_variables, _params| } + @@extend_rentals_index_action = -> (_account, _action_variables, _params) { } # included tables for remote_rentals_by_account mattr_accessor :remote_rentals_by_account_included_tables From 42b5624cd1b153acddf45506e2217adaf4341386 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Thu, 27 Jun 2019 17:41:26 +0300 Subject: [PATCH 19/26] Fix ordering issues --- .../admin/rentals_controller.rb | 2 +- .../admin/rentals_controller_spec.rb | 72 +++++++++++++++++-- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index a143fb34..d762bf1d 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -59,7 +59,7 @@ def prepare_index_variables @action_variables.remote_rentals_by_account = @action_variables.remote_rentals .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) - .order(:remote_account_id) + .reorder(:remote_account_id) .group_by(&:remote_account) @action_variables.remote_rentals_by_account = blank_remote_accounts.merge(@action_variables.remote_rentals_by_account) diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index 4d03e832..8ff111b5 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -81,7 +81,7 @@ it "does not filter rentals" do index_with_search expect(assigns(:not_connected_rentals)).to contain_exactly(rental) - expect(assigns(:remote_rentals_by_account)).to eq({ + expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], remote_account => [remote_rental], remote_account_empty => [] @@ -96,7 +96,7 @@ index_with_search expect(assigns(:not_connected_rentals)).to be_blank - expect(assigns(:remote_rentals_by_account)).to eq({ + expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], remote_account => [remote_rental], remote_account_empty => [] @@ -113,7 +113,7 @@ it "filters not_connected_rentals but does not filter remote_rentals part" do index_with_search expect(assigns(:not_connected_rentals)).to contain_exactly(rental) - expect(assigns(:remote_rentals_by_account)).to eq({ + expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], remote_account => [remote_rental], remote_account_empty => [] @@ -128,7 +128,7 @@ it "filters not_connected_rentals but does not filter remote_rentals part" do index_with_search expect(assigns(:not_connected_rentals)).to be_blank - expect(assigns(:remote_rentals_by_account)).to eq({ + expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_connected => [remote_rental_connected], remote_account => [remote_rental], remote_account_empty => [] @@ -229,5 +229,69 @@ expect(assigns(:custom_variable)).to eq("BookingSync") end end + + context "when there are several remote_rentals belonged to several remote_accounts" do + let!(:remote_account1) { remote_account } + let!(:remote_account2) { remote_account_connected } + let!(:remote_account3) { create(:remote_account, account: account) } + + let!(:remote_rental_11) { remote_rental } + let!(:remote_rental_21) { remote_rental_connected } + let!(:remote_rental_31) { create(:remote_rental, remote_account: remote_account3) } + + let!(:remote_rental_12) { create(:remote_rental, remote_account: remote_account1) } + let!(:remote_rental_22) { create(:remote_rental, remote_account: remote_account2) } + let!(:remote_rental_32) { create(:remote_rental, remote_account: remote_account3) } + + let!(:remote_rental_13) { create(:remote_rental, remote_account: remote_account1) } + let!(:remote_rental_23) { create(:remote_rental, remote_account: remote_account2) } + let!(:remote_rental_33) { create(:remote_rental, remote_account: remote_account3) } + + before do + allow(BookingsyncPortal).to receive(:items_per_page).and_return(4) + end + + context "and there is the first page" do + let(:remote_rentals_search_page) { 1 } + + it "displayes recors in corrent order" do + index_with_search + + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account_empty => [], + remote_account1 => [remote_rental_11, remote_rental_12, remote_rental_13], + remote_account2 => [remote_rental_21], + }) + end + end + + context "and there is the second page" do + let(:remote_rentals_search_page) { 2 } + + it "displayes recors in corrent order" do + index_with_search + + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account2 => [remote_rental_22, remote_rental_23], + remote_account3 => [remote_rental_31, remote_rental_32], + }) + end + end + + context "and there is the third page" do + let(:remote_rentals_search_page) { 3 } + + it "displayes recors in corrent order" do + index_with_search + + expect(assigns(:not_connected_rentals)).to contain_exactly(rental) + expect(assigns(:remote_rentals_by_account)).to eq({ + remote_account3 => [remote_rental_33], + }) + end + end + end end end From efeba774264c98e65504662b5cebfa4468040966 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Thu, 27 Jun 2019 21:06:04 +0300 Subject: [PATCH 20/26] Change direction of order to DESC --- .../bookingsync_portal/admin/rentals_controller.rb | 2 +- spec/controllers/admin/rentals_controller_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index d762bf1d..b4a662e4 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -59,7 +59,7 @@ def prepare_index_variables @action_variables.remote_rentals_by_account = @action_variables.remote_rentals .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) - .reorder(:remote_account_id) + .reorder(remote_account_id: :desc) .group_by(&:remote_account) @action_variables.remote_rentals_by_account = blank_remote_accounts.merge(@action_variables.remote_rentals_by_account) diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index 8ff111b5..f3f0fd11 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -260,7 +260,7 @@ expect(assigns(:not_connected_rentals)).to contain_exactly(rental) expect(assigns(:remote_rentals_by_account)).to eq({ remote_account_empty => [], - remote_account1 => [remote_rental_11, remote_rental_12, remote_rental_13], + remote_account3 => [remote_rental_31, remote_rental_32, remote_rental_33], remote_account2 => [remote_rental_21], }) end @@ -275,7 +275,7 @@ expect(assigns(:not_connected_rentals)).to contain_exactly(rental) expect(assigns(:remote_rentals_by_account)).to eq({ remote_account2 => [remote_rental_22, remote_rental_23], - remote_account3 => [remote_rental_31, remote_rental_32], + remote_account1 => [remote_rental_11, remote_rental_12], }) end end @@ -288,7 +288,7 @@ expect(assigns(:not_connected_rentals)).to contain_exactly(rental) expect(assigns(:remote_rentals_by_account)).to eq({ - remote_account3 => [remote_rental_33], + remote_account1 => [remote_rental_13], }) end end From df06dc76e3329f661957713e4d0b96ca98dca07e Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Tue, 2 Jul 2019 16:39:30 +0300 Subject: [PATCH 21/26] Split 1 callback by 2 - before search and after search to handle bsa-airbnb logic. Make some controller methods public to put controller as an argument to callbacks Update app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee to avoid useless search requests small fixes in app/controllers/bookingsync_portal/admin/rentals_controller.rb --- .../admin/lib/list-backend-filter.js.coffee | 9 ++--- .../admin/rentals_controller.rb | 34 +++++++++++----- .../filter_strategies/base_strategy.rb | 2 +- lib/bookingsync_portal.rb | 7 +++- .../admin/rentals_controller_spec.rb | 39 +++++++++++++++---- 5 files changed, 66 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index d5f5980a..152a68b1 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -15,6 +15,8 @@ class @ListBackedFilter @doneTypingInterval = 1500 # 1.5 seconds wait after each keyup before sending search request @typingTimer = undefined + @currentSearchQuery = @input.val() + insertForm: -> @form.appendTo(@header) @input = $("#" + @inputId) @@ -54,12 +56,7 @@ class @ListBackedFilter parseInt(itemsCount) < $("body").data("items-per-page") observeFormChanges: -> - skippedKeyCodes = [13, 16, 17, 18, 37, 38, 39, 40, 17, 91, 93, 224] @input.on 'keyup', (e) => - if e.keyCode not in skippedKeyCodes - @startSearching() - - @input.bind 'paste', => @startSearching() @input.on 'change', => @@ -69,6 +66,8 @@ class @ListBackedFilter @startSearching() startSearching: => + return if @currentSearchQuery == @input.val() + @currentSearchQuery = @input.val() @setPage(1) @displayWaiting() clearTimeout(@typingTimer) diff --git a/app/controllers/bookingsync_portal/admin/rentals_controller.rb b/app/controllers/bookingsync_portal/admin/rentals_controller.rb index b4a662e4..5d74bbf0 100644 --- a/app/controllers/bookingsync_portal/admin/rentals_controller.rb +++ b/app/controllers/bookingsync_portal/admin/rentals_controller.rb @@ -33,6 +33,16 @@ def show rental end + def ignore_blank_remote_accounts? + search_filter.remote_rentals_query.blank? && search_filter.remote_rentals_page > 1 + end + + def action_variables + @action_variables ||= {} + end + + public :current_account + private def search_by_rentals? @@ -53,9 +63,10 @@ def prepare_index_variables @action_variables.not_connected_rentals = current_account.rentals.visible.ordered.not_connected @action_variables.visible_rentals = current_account.rentals.visible @action_variables.remote_rentals = current_account.remote_rentals.ordered + @action_variables.blank_remote_accounts = generate_blank_remote_accounts + BookingsyncPortal.before_rentals_index_action_filter.call(self) yield if block_given? - BookingsyncPortal.extend_rentals_index_action.call(current_account, @action_variables, params) @action_variables.remote_rentals_by_account = @action_variables.remote_rentals .includes(*BookingsyncPortal.remote_rentals_by_account_included_tables) @@ -65,27 +76,30 @@ def prepare_index_variables @action_variables.remote_rentals_by_account = blank_remote_accounts.merge(@action_variables.remote_rentals_by_account) @action_variables.remote_accounts = @action_variables.remote_rentals_by_account.keys + BookingsyncPortal.after_rentals_index_action_filter.call(self) @action_variables.to_h.each do |variable_name, variable_value| instance_variable_set("@#{variable_name}", variable_value) end end def blank_remote_accounts - return {} if search_filter.remote_rentals_query.blank? && search_filter.remote_rentals_page > 1 - - result = current_account - .remote_accounts - .left_outer_joins(:remote_rentals) - .where(remote_rentals: { id: nil }) - + return {} if ignore_blank_remote_accounts? BookingsyncPortal.filter_strategies.each do |strategy| - result = strategy.constantize.call(account: current_account, records: result, search_filter: search_filter) + @action_variables.blank_remote_accounts = strategy.constantize.call(account: current_account, records: @action_variables.blank_remote_accounts, search_filter: search_filter) end - result.each_with_object({}) do |remote_account, res| + @action_variables.blank_remote_accounts.each_with_object({}) do |remote_account, res| res[remote_account] = [] end end + def generate_blank_remote_accounts + return RemoteAccount.none if ignore_blank_remote_accounts? + current_account + .remote_accounts + .left_outer_joins(:remote_rentals) + .where(remote_rentals: { id: nil }) + end + def apply_search BookingsyncPortal.filter_strategies.each do |strategy| @action_variables.not_connected_rentals = strategy.constantize.call( diff --git a/app/services/bookingsync_portal/filter_strategies/base_strategy.rb b/app/services/bookingsync_portal/filter_strategies/base_strategy.rb index 6fd28380..96a023cf 100644 --- a/app/services/bookingsync_portal/filter_strategies/base_strategy.rb +++ b/app/services/bookingsync_portal/filter_strategies/base_strategy.rb @@ -11,7 +11,7 @@ def initialize(account: nil, records:, search_filter:) @search_filter = search_filter end - def self.call(account: nil, records:, search_filter:) + def self.call(account: nil, records:, search_filter: nil) return records if models_for_filter.all? {|model_name| model_name.constantize.table_name != records.table_name } new(account: account, records: records, search_filter: search_filter).call end diff --git a/lib/bookingsync_portal.rb b/lib/bookingsync_portal.rb index cc39cc41..55e96a92 100644 --- a/lib/bookingsync_portal.rb +++ b/lib/bookingsync_portal.rb @@ -89,8 +89,11 @@ module BookingsyncPortal mattr_accessor :items_per_page @@items_per_page = 25 - mattr_accessor :extend_rentals_index_action - @@extend_rentals_index_action = -> (_account, _action_variables, _params) { } + mattr_accessor :before_rentals_index_action_filter + @@before_rentals_index_action_filter = -> (_controller) { } + + mattr_accessor :after_rentals_index_action_filter + @@after_rentals_index_action_filter = -> (_controller) { } # included tables for remote_rentals_by_account mattr_accessor :remote_rentals_by_account_included_tables diff --git a/spec/controllers/admin/rentals_controller_spec.rb b/spec/controllers/admin/rentals_controller_spec.rb index f3f0fd11..78c63ed6 100644 --- a/spec/controllers/admin/rentals_controller_spec.rb +++ b/spec/controllers/admin/rentals_controller_spec.rb @@ -205,21 +205,46 @@ end end - context "when there is extend_rentals_index_action setting" do + context "when there is before_rentals_index_action_filter setting" do let(:rentals_index_action_extention) do - Proc.new do |account, action_variables, params| - action_variables.not_connected_rentals = Rental.all - action_variables.remote_rentals = RemoteRental.all - action_variables.custom_variable = "BookingSync" + Proc.new do |controller| + controller.action_variables.not_connected_rentals = Rental.all + controller.action_variables.remote_rentals = RemoteRental.all + controller.action_variables.custom_variable = "BookingSync" end end before do - BookingsyncPortal.extend_rentals_index_action = rentals_index_action_extention + BookingsyncPortal.before_rentals_index_action_filter = rentals_index_action_extention end after do - BookingsyncPortal.extend_rentals_index_action = Proc.new {} + BookingsyncPortal.before_rentals_index_action_filter = Proc.new {} + end + + it "applies extended logic" do + index_with_search + expect(assigns(:not_connected_rentals)).not_to eq(Rental.all) # will be overridden + expect(assigns(:remote_rentals)).not_to eq(RemoteRental.all) # will be overridden + expect(assigns(:custom_variable)).to eq("BookingSync") + end + end + + context "when there is after_rentals_index_action_filter setting" do + let(:rentals_index_action_extention) do + Proc.new do |controller| + controller.action_variables.not_connected_rentals = Rental.all + controller.action_variables.remote_rentals = RemoteRental.all + controller.action_variables.custom_variable = "BookingSync" + end + end + + before do + BookingsyncPortal.after_rentals_index_action_filter = rentals_index_action_extention + end + + after do + BookingsyncPortal.after_rentals_index_action_filter = Proc.new {} end it "applies extended logic" do From 3a5ca65c3b7d390aaf9a886f8a1c437aa0c14e10 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Wed, 3 Jul 2019 09:07:04 +0300 Subject: [PATCH 22/26] Fix skiping search logic in app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee --- .../admin/lib/list-backend-filter.js.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee index 152a68b1..b64b1b3d 100644 --- a/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee +++ b/app/assets/javascripts/bookingsync_portal/admin/lib/list-backend-filter.js.coffee @@ -15,7 +15,7 @@ class @ListBackedFilter @doneTypingInterval = 1500 # 1.5 seconds wait after each keyup before sending search request @typingTimer = undefined - @currentSearchQuery = @input.val() + @currentSearchQuery = @form.serialize() insertForm: -> @form.appendTo(@header) @@ -66,8 +66,8 @@ class @ListBackedFilter @startSearching() startSearching: => - return if @currentSearchQuery == @input.val() - @currentSearchQuery = @input.val() + return if @currentSearchQuery == @form.serialize() + @currentSearchQuery = @form.serialize() @setPage(1) @displayWaiting() clearTimeout(@typingTimer) From 4f4ab21f031e26002124b02b2f90e488c8060775 Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Mon, 8 Jul 2019 13:54:04 +0300 Subject: [PATCH 23/26] Downgrade message-bus gem to 2.0.2 --- Gemfile.lock | 16 ++++++++-------- bookingsync_portal.gemspec | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b8a34129..89108de1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -10,7 +10,7 @@ PATH jquery-rails jquery-ui-rails (~> 6.0.1) kaminari - message_bus + message_bus (= 2.0.2) rails ransack redis @@ -69,7 +69,7 @@ GEM thor (>= 0.14.0) arel (7.1.4) ast (2.3.0) - autoprefixer-rails (9.6.0) + autoprefixer-rails (9.6.1) execjs bookingsync-api (0.1.11) addressable @@ -105,9 +105,9 @@ GEM crack (0.4.3) safe_yaml (~> 1.0.0) diff-lcs (1.2.5) - dotenv (2.7.2) - dotenv-rails (2.7.2) - dotenv (= 2.7.2) + dotenv (2.7.4) + dotenv-rails (2.7.4) + dotenv (= 2.7.4) railties (>= 3.2, < 6.1) erubis (2.7.0) execjs (2.7.0) @@ -136,7 +136,7 @@ GEM thor (>= 0.14, < 2.0) jquery-ui-rails (6.0.1) railties (>= 3.2.16) - jsonapi-resources (0.9.8) + jsonapi-resources (0.9.9) activerecord (>= 4.1) concurrent-ruby railties (>= 4.1) @@ -157,10 +157,10 @@ GEM nokogiri (>= 1.5.9) mail (2.7.1) mini_mime (>= 0.1.1) - message_bus (2.2.1) + message_bus (2.0.2) rack (>= 1.1.3) method_source (0.8.2) - mini_mime (1.0.1) + mini_mime (1.0.2) mini_portile2 (2.1.0) minitest (5.10.1) multi_json (1.13.1) diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index 0a2607a1..042ac992 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -28,7 +28,7 @@ Gem::Specification.new do |s| s.add_dependency 'font-awesome-sass', '4.7.0' s.add_dependency 'handlebars_assets' s.add_dependency 'simple_form' - s.add_dependency 'message_bus' + s.add_dependency 'message_bus', '2.0.2' s.add_dependency 'turbolinks' s.add_dependency 'sass-rails' s.add_dependency 'uglifier' From d3483ea179f7047d4785edecc3a67b044cc4a4ef Mon Sep 17 00:00:00 2001 From: valexl <17valexl@gmail.com> Date: Mon, 15 Jul 2019 06:32:26 +0300 Subject: [PATCH 24/26] Fix app/views/bookingsync_portal/admin/rentals/_rentals.html.erb --- app/views/bookingsync_portal/admin/rentals/_rentals.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb b/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb index 63583731..de6ed78d 100644 --- a/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb +++ b/app/views/bookingsync_portal/admin/rentals/_rentals.html.erb @@ -6,7 +6,7 @@ <% not_connected_rentals.each do |rental| %> <%= render rental %> <% end %> -<%- elsif search_by_rentals? || search_by_remote_rentals? %> +<%- elsif params[:controller] == "bookingsync_portal/admin/connections" && (search_by_rentals? || search_by_remote_rentals?) %>

<%= icon 'search fa-lg' %>

<%=t '.no_results' %>

From 0c0ecd2ee5af69941e35c8d3919903b83ebc5733 Mon Sep 17 00:00:00 2001 From: StoneFrog Date: Wed, 19 Feb 2020 17:11:49 +0100 Subject: [PATCH 25/26] lock sass rails to avoid breaking change on sprockets --- Gemfile.lock | 20 ++++++++------------ bookingsync_portal.gemspec | 4 ++-- gemfiles/rails_5.0.gemfile.lock | 18 +++++++----------- gemfiles/rails_5.1.gemfile.lock | 18 +++++++----------- 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f636f361..085a3a57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,7 +16,7 @@ PATH ransack redis responders - sass-rails + sass-rails (~> 5) simple_form sprockets-rails turbolinks @@ -287,16 +287,12 @@ GEM sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - sass-rails (6.0.0) - sassc-rails (~> 2.1, >= 2.1.1) - sassc (2.2.1) - ffi (~> 1.9) - sassc-rails (2.1.2) - railties (>= 4.0.0) - sassc (>= 2.0) - sprockets (> 3.0) - sprockets-rails - tilt + sass-rails (5.1.0) + railties (>= 5.2.0) + sass (~> 3.1) + sprockets (>= 2.8, < 4.0) + sprockets-rails (>= 2.0, < 4.0) + tilt (>= 1.1, < 3) shoulda (3.6.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (~> 3.0) @@ -306,7 +302,7 @@ GEM simple_form (5.0.2) actionpack (>= 5.0) activemodel (>= 5.0) - sprockets (4.0.0) + sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.1) diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index 8db82117..0467d60d 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -31,10 +31,10 @@ Gem::Specification.new do |s| s.add_dependency 'simple_form' s.add_dependency 'message_bus', '2.0.2' s.add_dependency 'turbolinks' - s.add_dependency 'sass-rails' + s.add_dependency 'sass-rails', '~> 5' s.add_dependency 'uglifier' s.add_dependency 'coffee-rails' - + s.add_dependency 'ransack' s.add_dependency 'kaminari' diff --git a/gemfiles/rails_5.0.gemfile.lock b/gemfiles/rails_5.0.gemfile.lock index d541b2f7..da9719d2 100644 --- a/gemfiles/rails_5.0.gemfile.lock +++ b/gemfiles/rails_5.0.gemfile.lock @@ -16,7 +16,7 @@ PATH ransack redis responders - sass-rails + sass-rails (~> 5) simple_form sprockets-rails turbolinks @@ -280,16 +280,12 @@ GEM sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - sass-rails (6.0.0) - sassc-rails (~> 2.1, >= 2.1.1) - sassc (2.2.1) - ffi (~> 1.9) - sassc-rails (2.1.2) - railties (>= 4.0.0) - sassc (>= 2.0) - sprockets (> 3.0) - sprockets-rails - tilt + sass-rails (5.0.7) + railties (>= 4.0.0, < 6) + sass (~> 3.1) + sprockets (>= 2.8, < 4.0) + sprockets-rails (>= 2.0, < 4.0) + tilt (>= 1.1, < 3) shoulda (3.6.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (~> 3.0) diff --git a/gemfiles/rails_5.1.gemfile.lock b/gemfiles/rails_5.1.gemfile.lock index 3a04d1a0..9eff96a6 100644 --- a/gemfiles/rails_5.1.gemfile.lock +++ b/gemfiles/rails_5.1.gemfile.lock @@ -16,7 +16,7 @@ PATH ransack redis responders - sass-rails + sass-rails (~> 5) simple_form sprockets-rails turbolinks @@ -280,16 +280,12 @@ GEM sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - sass-rails (6.0.0) - sassc-rails (~> 2.1, >= 2.1.1) - sassc (2.2.1) - ffi (~> 1.9) - sassc-rails (2.1.2) - railties (>= 4.0.0) - sassc (>= 2.0) - sprockets (> 3.0) - sprockets-rails - tilt + sass-rails (5.0.7) + railties (>= 4.0.0, < 6) + sass (~> 3.1) + sprockets (>= 2.8, < 4.0) + sprockets-rails (>= 2.0, < 4.0) + tilt (>= 1.1, < 3) shoulda (3.6.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (~> 3.0) From 2a595b0d017d56b700bc5b5167866144560f7074 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 15 Feb 2021 10:21:49 +0300 Subject: [PATCH 26/26] Soften bookingsync-application gem version --- Gemfile.lock | 4 +++- bookingsync_portal.gemspec | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index c2a96ecf..270bcce0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ PATH remote: . specs: bookingsync_portal (1.0.1) - bookingsync_application (>= 3, < 4) + bookingsync_application (>= 3) bootstrap-bookingsync-sass (~> 1.0.0) bootstrap-sass (< 3.5) coffee-rails @@ -293,6 +293,8 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + sassc (2.4.0) + ffi (~> 1.9) shoulda (3.6.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (~> 3.0) diff --git a/bookingsync_portal.gemspec b/bookingsync_portal.gemspec index 2617f3f6..18f3837c 100644 --- a/bookingsync_portal.gemspec +++ b/bookingsync_portal.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.add_dependency 'rails' s.add_dependency 'sprockets-rails' s.add_dependency 'responders' - s.add_dependency 'bookingsync_application', ['>= 3', '< 4'] + s.add_dependency 'bookingsync_application', '>= 3' s.add_dependency 'redis' # FIXME: Will no longer be needed once UI moved to Ember s.add_dependency 'jquery-rails'