Skip to content

Commit

Permalink
Merge pull request #1571 from gigorok/YETIWEB-1564-filtering-by-netwo…
Browse files Browse the repository at this point in the history
…rk-ajax

refs #1564, filtering by network ajax
  • Loading branch information
dmitry-sinina authored Oct 4, 2024
2 parents 51fc5e6 + e0e38a4 commit 8d3c023
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ config/pgq_processors.yml
config/system_info.yml
config/click_house.yml
config/secrets.yml
config/ldap.yml
coverage
debian/files
debian/yeti-web
Expand Down
13 changes: 2 additions & 11 deletions app/admin/routing/destinations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,13 @@
filter :network_prefix_country_id_eq,
as: :select,
label: 'Country',
input_html: {
class: 'chosen network_prefix_country_id_eq-filter'
},
input_html: { class: 'chosen' },
collection: -> { System::Country.order(:name) }

association_ajax_filter :network_prefix_network_id_eq,
label: 'Network',
scope: -> { System::Network.order(:name) },
path: '/system_networks/search',
input_html: {
'data-path-params': { 'q[country_id_eq]': '.network_prefix_country_id_eq-filter' }.to_json,
'data-required-param': 'q[country_id_eq]'
},
fill_params: lambda {
{ country_id_eq: params.dig(:q, :network_prefix_country_id_eq) }
}
path: '/system_networks/search'

filter :external_id_eq, label: 'EXTERNAL_ID'
filter :valid_from, as: :date_time_range
Expand Down
13 changes: 2 additions & 11 deletions app/admin/routing/dialpeers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,13 @@ def scoped_collection
filter :network_prefix_country_id_eq,
as: :select,
label: 'Country',
input_html: {
class: 'chosen network_prefix_country_id_eq-filter'
},
input_html: { class: 'chosen' },
collection: -> { System::Country.order(:name) }

association_ajax_filter :network_prefix_network_id_eq,
label: 'Network',
scope: -> { System::Network.order(:name) },
path: '/system_networks/search',
input_html: {
'data-path-params': { 'q[country_id_eq]': '.network_prefix_country_id_eq-filter' }.to_json,
'data-required-param': 'q[country_id_eq]'
},
fill_params: lambda {
{ country_id_eq: params.dig(:q, :network_prefix_country_id_eq) }
}
path: '/system_networks/search'

filter :created_at, as: :date_time_range
filter :external_id
Expand Down
12 changes: 10 additions & 2 deletions app/admin/routing/routing_plan_static_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@
filter :id
filter :routing_plan, collection: -> { Routing::RoutingPlan.having_static_routes }, input_html: { class: 'chosen' }
filter :prefix
filter :country, input_html: { class: 'chosen' }
filter :network, input_html: { class: 'chosen' }
filter :network_prefix_country_id_eq,
as: :select,
label: 'Country',
input_html: { class: 'chosen' },
collection: -> { System::Country.order(:name) }

association_ajax_filter :network_prefix_network_id_eq,
label: 'Network',
scope: -> { System::Network.order(:name) },
path: '/system_networks/search'
contractor_filter :vendor_id_eq, label: 'Vendor', path_params: { q: { vendor_eq: true } }

# after_build do |resource|
Expand Down
5 changes: 4 additions & 1 deletion app/admin/system/network_prefixes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
f.input :number_min_length
f.input :number_max_length
f.input :country, input_html: { class: 'chosen' }
f.input :network, input_html: { class: 'chosen' }
f.association_ajax_input :network_id,
label: 'Network',
scope: System::Network.order(:name),
path: '/system_networks/search'
end
f.actions
end
Expand Down
13 changes: 13 additions & 0 deletions lib/active_admin/fast_count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@ def collection_size(records = collection)
records.fast_count(:all)
end
end

def current_page_collection_size
is_last_page = collection.total_count <= collection_offset + collection.limit_value
if is_last_page
collection.total_count - collection_offset
else
collection.limit_value
end
end

def collection_offset
(collection.current_page - 1) * collection.limit_value
end
end
end
46 changes: 45 additions & 1 deletion lib/active_admin/views/components/paginated_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class PaginatedCollection < ActiveAdmin::Component
#
def build(collection, options = {})
@collection = collection
@params = options.delete(:params)
@param_name = options.delete(:param_name)
@download_links = options.delete(:download_links)
@display_total = options.delete(:pagination_total) { true }
Expand All @@ -26,7 +27,7 @@ def build(collection, options = {})
@built = true
end

protected
private

def build_top_pagination_panel
div class: 'pagination_top' do
Expand All @@ -36,6 +37,49 @@ def build_top_pagination_panel
end
end
end

def page_entries_info(options = {})
if options[:entry_name]
entry_name = options[:entry_name]
entries_name = options[:entries_name] || entry_name.pluralize
elsif collection_is_empty?
entry_name = I18n.t 'active_admin.pagination.entry', count: 1, default: 'entry'
entries_name = I18n.t 'active_admin.pagination.entry', count: 2, default: 'entries'
else
key = "activerecord.models.#{collection.first.class.model_name.i18n_key}"
entry_name = I18n.t key, count: 1, default: collection.first.class.name.underscore.sub('_', ' ')
entries_name = I18n.t key, count: collection.size, default: entry_name.pluralize
end

if @display_total
if collection.total_pages < 2
case collection_size
when 0
I18n.t('active_admin.pagination.empty', model: entries_name)
when 1
I18n.t('active_admin.pagination.one', model: entry_name)
else
I18n.t('active_admin.pagination.one_page', model: entries_name, n: collection.total_count)
end
else
offset = collection_offset
total = collection.total_count
I18n.t 'active_admin.pagination.multiple',
model: entries_name,
total:,
from: offset + 1,
to: offset + current_page_collection_size
end
else
# Do not display total count, in order to prevent a `SELECT count(*)`.
# To do so we must not call `collection.total_pages`
offset = (collection.current_page - 1) * collection.limit_value
I18n.t 'active_admin.pagination.multiple_without_total',
model: entries_name,
from: offset + 1,
to: offset + collection_size
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
visit new_system_network_prefix_path

aa_form.set_text 'Prefix', '123'
aa_form.select_chosen 'Network', network.display_name
fill_in_chosen 'Network', with: network.name, ajax: true
end

it 'creates record' do
Expand Down

0 comments on commit 8d3c023

Please sign in to comment.