diff --git a/app/controllers/concerns/json_api_controller.rb b/app/controllers/concerns/json_api_controller.rb index 45376348..cee58480 100644 --- a/app/controllers/concerns/json_api_controller.rb +++ b/app/controllers/concerns/json_api_controller.rb @@ -146,7 +146,11 @@ def allow_index(allowed = true) return error_forbidden_filters if forbidden_filters.any? # First we gather all records of the model class. - records = model_class_with_includes.all + records = if @records.nil? + params[:include].present? ? model_class_with_includes.all : model_class.all + else + @records # set in the controller, prevents .all + end # Next we reduce this collection with the permanent and requested filters. # @@ -183,7 +187,7 @@ def allow_index(allowed = true) options[:include] = strong_includes if strong_includes # We create a JSON response from the records we collected using the fast and - # JSON API compliant Netflux serializers. + # JSON API compliant Netflix serializers. json = serializer_class.new(records, options).serializable_hash.to_json # Finally we return the JSON with a 200. @@ -270,6 +274,7 @@ def allow_show # To avoid N+1 we include all the includes. def model_class_with_includes + return model_class if params[:include].blank? return model_class unless strong_includes list = [] @@ -283,7 +288,7 @@ def model_class_with_includes value = split[1] hash = {} hash[key] = value - # list.push(**hash) + list.push(**hash) else list.push(symbol) end diff --git a/app/controllers/v1/public/company_markets_controller.rb b/app/controllers/v1/public/company_markets_controller.rb index cf9f91ef..861f40e2 100644 --- a/app/controllers/v1/public/company_markets_controller.rb +++ b/app/controllers/v1/public/company_markets_controller.rb @@ -30,6 +30,19 @@ def model_class def serializer_class V1::Public::CompanyMarketSerializer end + + def permitted_includes + %i[ + company + country + ] + end + + def permitted_filters + %i[ + country + ] + end end end end diff --git a/app/controllers/v1/public/events_controller.rb b/app/controllers/v1/public/events_controller.rb index 2449bc7d..b44e90cc 100644 --- a/app/controllers/v1/public/events_controller.rb +++ b/app/controllers/v1/public/events_controller.rb @@ -2,6 +2,9 @@ module V1 module Public class EventsController < V1::PublicController def index + @records = Event.upcoming if params['upcoming'] == 'true' + @records = Event.past if params['past'] == 'true' + allow_index end @@ -36,6 +39,13 @@ def permitted_includes country ] end + + def permitted_filters + %i[ + upcoming + past + ] + end end end end diff --git a/app/controllers/v1/public_controller.rb b/app/controllers/v1/public_controller.rb index fae6bb1a..9e833e44 100644 --- a/app/controllers/v1/public_controller.rb +++ b/app/controllers/v1/public_controller.rb @@ -1,4 +1,5 @@ module V1 class PublicController < ApplicationController + # TODO: add permanent filter public=true end end diff --git a/app/models/event.rb b/app/models/event.rb index e6228b3e..be1422b3 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -34,13 +34,13 @@ def location "#{city}, #{country.name_english}" end + scope :past, -> { where('start_date < ?', Date.current - 2.days) } + scope :upcoming, -> { where('start_date >= ?', Date.current - 2.days) } + def start_to_end_date return '?' if sd.nil? && ed.nil? - return sd.strftime('%a %-d %b %Y') if sd.present? && ed.nil? - return sd.strftime('%a %-d %b %Y') if ed == sd - return "#{sd.strftime('%a %d')} to #{ed.strftime('%a %-d %b %Y')}" if sd.month == ed.month "#{sd.strftime('%a %-d %b %Y')} to #{ed.strftime('%a %-d %b %Y')}" diff --git a/config/environments/development.rb b/config/environments/development.rb index 400fc3a6..90d8a4bb 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -6,6 +6,13 @@ Bullet.console = true Bullet.rails_logger = true Bullet.add_footer = true + + # Disable only the "AVOID eager loading" warnings + Bullet.unused_eager_loading_enable = false + + # Keep other useful warnings + Bullet.n_plus_one_query_enable = true + Bullet.counter_cache_enable = true end # Settings specified here will take precedence over those in config/application.rb. @@ -68,7 +75,7 @@ end # Return a 404 to all requests who are not these hosts: - config.hosts = ["localhost", "127.0.0.1"] + config.hosts = ['localhost', '127.0.0.1'] # Configure CORS. # Note that the Interflux front-end live on different domains than their backend. diff --git a/scripts/backup.sh b/scripts/backup.sh index 0bf4c37b..8eeae612 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -20,7 +20,7 @@ echo "✅ Downloaded production database" echo "----------" ls -la db/dumps echo "----------" -RAILS_ENV=development rails db:drop db:create +RAILS_ENV=development DISABLE_DATABASE_ENVIRONMENT_CHECK=1 rails db:drop db:create echo "----------" echo "✅ Reset local database" echo "----------"