Skip to content

Commit

Permalink
Merge branch 'paul-oms-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Feb 25, 2025
2 parents b052c00 + f5c618b commit bcb4b9c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
8 changes: 7 additions & 1 deletion app/views/madmin/fields/has_many/_show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<% field.value(record).each do |object| %>
<% pagy, records = field.paginated_value(record, params) %>
<% records.each do |object| %>
<div>
<%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object), class: "text-blue-500 underline" %>
</div>
<% end %>

<div class="pagination">
<%== pagy_nav pagy if pagy.pages > 1 %>
<span>Showing <%= tag.strong pagy.in %> of <%= tag.strong pagy.count %></span>
</div>
8 changes: 7 additions & 1 deletion app/views/madmin/fields/nested_has_many/_show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<% field.value(record).each do |object| %>
<% pagy, records = field.paginated_value(record, params) %>
<% records.each do |object| %>
<div>
<%= link_to Madmin.resource_for(object).display_name(object), Madmin.resource_for(object).show_path(object), class: "text-blue-500 underline" %>
</div>
<% end %>

<div class="pagination">
<%== pagy_nav pagy if pagy.pages > 1 %>
<span>Showing <%= tag.strong pagy.in %> of <%= tag.strong pagy.count %></span>
</div>
4 changes: 4 additions & 0 deletions lib/madmin/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ def required?
def searchable?
false
end

def paginateable?
false
end
end
end
17 changes: 17 additions & 0 deletions lib/madmin/fields/has_many.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Madmin
module Fields
class HasMany < Field
include Pagy::Backend

def options_for_select(record)
if (records = record.send(attribute_name))
return [] unless records.first
Expand All @@ -18,6 +20,21 @@ def to_param
def index_path
Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass).index_path(format: :json)
end

def paginateable?
true
end

def paginated_value(record, params)
pagy value(record), params: params, page_param: "#{attribute_name}_page"
end

# Override to access params from vars since we're not in a controller/view
def pagy_get_page(vars, force_integer: true)
params = vars[:params]
page = params[vars[:page_param] || DEFAULT[:page_param]]
force_integer ? (page || 1).to_i : page
end
end
end
end
6 changes: 5 additions & 1 deletion lib/madmin/fields/nested_has_many.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Madmin
module Fields
class NestedHasMany < Field
class NestedHasMany < HasMany
DEFAULT_ATTRIBUTES = %w[_destroy id].freeze
def nested_attributes
resource.attributes.except(*skipped_fields)
Expand All @@ -26,6 +26,10 @@ def to_model
attribute_name.to_s.singularize.classify.constantize
end

def paginateable?
true
end

private

def permitted_fields
Expand Down
12 changes: 10 additions & 2 deletions test/dummy/db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
100.times do
User.create!(
user = User.create!(
first_name: FFaker::Name.first_name,
last_name: FFaker::Name.last_name,
birthday: Date.today,
password: "password"
)
end

# For pagination testing
100.times do
Post.create!(
title: FFaker::Lorem.sentence,
user:,
)
end
end
6 changes: 6 additions & 0 deletions test/madmin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ class Madmin::Test < ActiveSupport::TestCase
test "can set custom field for attribute" do
assert_equal CustomField, PostResource.get_attribute(:title).field.class
end

test "has many and nested has many are set to paginateable, others are not" do
assert UserResource.get_attribute(:posts).field.paginateable?
assert UserResource.get_attribute(:comments).field.paginateable?
refute UserResource.get_attribute(:id).field.paginateable?
end
end

0 comments on commit bcb4b9c

Please sign in to comment.