Skip to content

Commit

Permalink
Admin UI auth logs add default filter
Browse files Browse the repository at this point in the history
  • Loading branch information
senid231 committed Jul 31, 2019
1 parent 85ea00f commit d1a41bf
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 33 deletions.
5 changes: 5 additions & 0 deletions app/admin/cdr/auth_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
config.batch_actions = false
config.sort_order = 'request_time_desc'

with_default_params do
params[:q] = { request_time_gteq_datetime: 1.days.ago.to_date.strftime('%F') }
'Only records from yesterday are displayed by default'
end

acts_as_export :id,
:request_time,
[:gateway_name, proc { |row| row.gateway.try(:name) }],
Expand Down
8 changes: 3 additions & 5 deletions app/admin/logs/api_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
scope :all, default: true
scope :failed, show_count: false

before_action only: [:index] do
if params['q'].blank?
params['q'] = { created_at_gteq: 1.days.ago } # only 1 last days by default
flash.now[:notice] = 'Only records for last day are displayed by default'
end
with_default_params do
params[:q] = { created_at_gteq: 1.days.ago } # only 1 last days by default
'Only records for last day are displayed by default'
end

controller do
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/bad_routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@
filter :internal_disconnect_code
filter :internal_disconnect_reason

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/not_authenticated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
collection: Report::Realtime::Base::INTERVALS,
input_html: { class: 'chosen' }, include_blank: false

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/origination_performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
collection: proc { Contractor.select(:id, :name).reorder(:name) },
input_html: { class: 'chosen' }

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
8 changes: 1 addition & 7 deletions app/admin/reports/realtime/termination_distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
as: :select, collection: proc { Contractor.select(:id, :name).reorder(:name) },
input_html: { class: 'chosen' }

before_action only: [:index] do
params[:q] ||= {}
if params[:q][:time_interval_eq].blank?
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
flash.now[:notice_message] = "Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
with_default_realtime_interval

controller do
def scoped_collection
Expand Down
1 change: 1 addition & 0 deletions config/initializers/yeti.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsDelayedJobLock
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsFilterByRoutingTagIds
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsBelongsTo
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::WithDefaultParams

# ActiveAdmin::CSVBuilder.send(:include, Yeti::CSVBuilder)

Expand Down
28 changes: 28 additions & 0 deletions lib/resource_dsl/with_default_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module ResourceDSL
module WithDefaultParams
# @param opts[:if] [Proc] - passes params into proc, block will be executed if proc returns true
# @param opts[:flash_type] [Proc] - passes params into proc, block will be executed if proc returns true
# yield in context of controller
# @yieldreturn message that will be shown in flash.now
def with_default_params(opts = {}, &block)
if_proc = opts[:if] || proc { |q: nil, **_| q.blank? }
flash_type = opts.fetch(:flash_type, :notice_message)
before_action only: [:index] do
if instance_exec(params.to_unsafe_h, &if_proc)
message = instance_exec(&block)
flash.now[flash_type] = message if message.present?
end
end
end

def with_default_realtime_interval
with_default_params if: proc { |q: nil, **_| q.blank? || q[:time_interval_eq].blank? } do
params[:q] ||= {}
params[:q][:time_interval_eq] = Report::Realtime::Base::DEFAULT_INTERVAL
"Records for time interval #{Report::Realtime::Base::DEFAULT_INTERVAL} seconds are displayed by default"
end
end
end
end

0 comments on commit d1a41bf

Please sign in to comment.