-
Notifications
You must be signed in to change notification settings - Fork 479
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
Add filters and paging to the workshop materials admin list #16309
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
dashboard/app/controllers/pd/teacher_application_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Helper for simple paging controls | ||
module Pd::PageHelper | ||
# Renders a page header with paging buttons and a page size drop down. | ||
# Each page button is a link to the same controller action with the appropriate page: param, | ||
# and existing values of permitted_params. | ||
# Each page_size button is similar, but with page_size: set. | ||
# @param collection_name [String] Name to be displayed in the page header, e.g. "Applications" | ||
# @param collection [ActiveRecord::Relation] collection of models with paging applied | ||
# @param permitted_params [Array<String, Symbol>] params to be preserved in paging urls | ||
def page_header(collection_name, collection, permitted_params: []) | ||
current_page = collection.current_page | ||
|
||
base_params = params.permit(permitted_params + [:page_size]) | ||
page_buttons = [ | ||
new_page_button('<<', base_params.merge(page: 1), disabled: collection.first_page?), | ||
new_page_button('<', base_params.merge(page: current_page - 1), disabled: collection.first_page?), | ||
new_page_button('>', base_params.merge(page: current_page + 1), disabled: collection.last_page?), | ||
new_page_button('>>', base_params.merge(page: collection.total_pages), disabled: collection.last_page?) | ||
] | ||
|
||
page_size = params[:page_size] || collection.limit_value | ||
page_size_buttons = %w(25 50 All).map do |page_size_option| | ||
new_page_size_button page_size_option, base_params | ||
end | ||
|
||
locals = { | ||
collection: collection, | ||
collection_name: collection_name, | ||
page_buttons: page_buttons, | ||
page_size: page_size, | ||
page_size_buttons: page_size_buttons | ||
} | ||
|
||
render partial: 'pd/page_header', locals: locals | ||
end | ||
|
||
def new_page_button(text, params, disabled: false) | ||
link_to text, params, class: btn_class(disabled: disabled) | ||
end | ||
|
||
def new_page_size_button(page_size, base_params) | ||
link_to page_size, base_params.merge(page_size: page_size) | ||
end | ||
|
||
def btn_class(disabled: false) | ||
"btn btn-default#{(disabled && ' disabled').presence}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
%h5.page-header | ||
Page | ||
= local_assigns[:collection].current_page | ||
of | ||
= local_assigns[:collection].total_pages | ||
( | ||
= local_assigns[:collection].current_page | ||
of | ||
= local_assigns[:collection].total_count | ||
= local_assigns[:collection_name].pluralize | ||
) | ||
|
||
- local_assigns[:page_buttons].each do |page_button| | ||
= page_button | ||
|
||
%span.dropdown{style: 'position: absolute; right: 25px'} | ||
Show | ||
%button.btn.btn-default.dropdown-toggle{'data-toggle': 'dropdown'} | ||
= local_assigns[:page_size] | ||
%span.caret | ||
%ul.dropdown-menu | ||
- local_assigns[:page_size_buttons].each do |page_size_button| | ||
%li | ||
= page_size_button |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
require 'test_helper' | ||
|
||
class Pd::PageHelperTest < ActionView::TestCase | ||
include Pd::PageHelper | ||
|
||
test 'btn_class default' do | ||
assert_equal 'btn btn-default', btn_class | ||
end | ||
|
||
test 'btn_class disabled' do | ||
assert_equal 'btn btn-default disabled', btn_class(disabled: true) | ||
end | ||
|
||
test 'new_page_button' do | ||
expects(:link_to).with('<', {page: 1}, class: 'btn btn-default') | ||
new_page_button '<', page: 1 | ||
end | ||
|
||
test 'new_page_size_button' do | ||
expects(:link_to).with(50, {key: 'value', page_size: 50}) | ||
new_page_size_button 50, {key: 'value'} | ||
end | ||
|
||
test 'page_header first page' do | ||
assert_page_header( | ||
{ | ||
current_page: 1, | ||
first_page?: true, | ||
last_page?: false, | ||
total_pages: 10, | ||
limit_value: 25 | ||
}, | ||
[ | ||
{text: '<<', page: 1, disabled: true}, | ||
{text: '<', page: 0, disabled: true}, | ||
{text: '>', page: 2, disabled: false}, | ||
{text: '>>', page: 10, disabled: false} | ||
] | ||
) | ||
end | ||
|
||
test 'page_header middle page' do | ||
assert_page_header( | ||
{ | ||
current_page: 5, | ||
first_page?: false, | ||
last_page?: false, | ||
total_pages: 10, | ||
limit_value: 25 | ||
}, | ||
[ | ||
{text: '<<', page: 1, disabled: false}, | ||
{text: '<', page: 4, disabled: false}, | ||
{text: '>', page: 6, disabled: false}, | ||
{text: '>>', page: 10, disabled: false} | ||
] | ||
) | ||
end | ||
|
||
test 'page_header last page' do | ||
assert_page_header( | ||
{ | ||
current_page: 10, | ||
first_page?: false, | ||
last_page?: true, | ||
total_pages: 10, | ||
limit_value: 25 | ||
}, | ||
[ | ||
{text: '<<', page: 1, disabled: false}, | ||
{text: '<', page: 9, disabled: false}, | ||
{text: '>', page: 11, disabled: true}, | ||
{text: '>>', page: 10, disabled: true} | ||
] | ||
) | ||
end | ||
|
||
private | ||
|
||
# Sets up expectations for, and then calls, page_header | ||
# @param collection_params [Hash] param hash for mock collection class | ||
# Expected keys are [:current_page, :first_page?, :last_page?, :total_pages, :limit_value] | ||
# @param page_button_params [Array<Hash>] An array of hashes, each with keys | ||
# [:text, :page, :disabled] representing expected params to the new_page_button method | ||
def assert_page_header(collection_params, page_button_params) | ||
collection = OpenStruct.new(collection_params) | ||
page_buttons = set_page_button_expectations page_button_params | ||
|
||
mock_params = mock | ||
stubs(:params).returns(mock_params) | ||
mock_params.expects(:permit).with([:allow, :page_size]).returns({}) | ||
mock_params.expects(:[], :page_size).returns(nil) | ||
|
||
page_size_buttons = 3.times.map {mock} | ||
expects(:new_page_size_button).with('25', {}).returns(page_size_buttons[0]) | ||
expects(:new_page_size_button).with('50', {}).returns(page_size_buttons[1]) | ||
expects(:new_page_size_button).with('All', {}).returns(page_size_buttons[2]) | ||
|
||
expects(:render).with(partial: 'pd/page_header', locals: | ||
{ | ||
collection: collection, | ||
collection_name: 'collection name', | ||
page_buttons: page_buttons, | ||
page_size: 25, | ||
page_size_buttons: page_size_buttons | ||
} | ||
) | ||
|
||
# Call page_header helper method which should meet the above expectations | ||
page_header 'collection name', collection, permitted_params: [:allow] | ||
end | ||
|
||
# Creates expectations for new_page_button method calls with params based on the supplied param set, | ||
# and returns the generated mock buttons | ||
# @param buttons_param_set [Array<Hash>] An array of hashes, each with keys | ||
# [:text, :page, :disabled] representing expected params to the new_page_button method | ||
# @return array of mock buttons that will be returned from the expectations | ||
def set_page_button_expectations(buttons_param_set) | ||
buttons_param_set.map do |button_params| | ||
text = button_params[:text] | ||
page = button_params[:page] | ||
disabled = button_params[:disabled] | ||
|
||
mock.tap do |mock_button| | ||
expects(:new_page_button).with(text, {page: page}, disabled: disabled).returns(mock_button) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is standard practice, but should we also have a param helper method here to filter out params other than email / filter and such?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is for Strong Parameters since these are being passed into the model in Create. Without specifying them explicitly Rails won't allow them in. This method is called by CanCan's load_and_authorize_resource in the
create
action and then used to populate the model.We don't need this for params we reference explicitly in the controller that aren't passed to a model, and I'm not sure what it would add.