Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update has_many.rb #2274

Merged
merged 7 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ association `belongs_to :country`, from your model.

**Field::HasMany**

`:limit` - Set the number of resources to display in the show view. Default is
`5`.
`:limit` - The number of resources (paginated) to display in the show view. To disable pagination,
set this to `0` or `false`. Default is `5`.

`:sort_by` - What to sort the association by in the show view.

Expand Down
11 changes: 9 additions & 2 deletions lib/administrate/field/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def limit
options.fetch(:limit, DEFAULT_LIMIT)
end

def paginate?
limit.respond_to?(:zero?) ? limit.zero? : limit.present?
end

def permitted_attribute
self.class.permitted_attribute(
attribute,
Expand All @@ -53,12 +57,15 @@ def permitted_attribute
end

def resources(page = 1, order = self.order)
resources = order.apply(data).page(page).per(limit)
resources = order.apply(data)
if paginate?
resources = resources.page(page).per(limit)
end
includes.any? ? resources.includes(*includes) : resources
end

def more_than_limit?
data.count(:all) > limit
paginate? && data.count(:all) > limit
end

def data
Expand Down