-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix pagination of "Page" models #1725
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module Admin | ||
class PagesController < Admin::ApplicationController | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "administrate/base_dashboard" | ||
|
||
class PageDashboard < Administrate::BaseDashboard | ||
ATTRIBUTE_TYPES = { | ||
product: Field::BelongsTo, | ||
id: Field::Number, | ||
title: Field::String, | ||
body: Field::Text, | ||
created_at: Field::DateTime, | ||
updated_at: Field::DateTime, | ||
}.freeze | ||
|
||
COLLECTION_ATTRIBUTES = %i[ | ||
id | ||
title | ||
].freeze | ||
|
||
SHOW_PAGE_ATTRIBUTES = %i[ | ||
product | ||
id | ||
title | ||
body | ||
created_at | ||
updated_at | ||
].freeze | ||
|
||
FORM_ATTRIBUTES = %i[ | ||
product | ||
title | ||
body | ||
].freeze | ||
|
||
COLLECTION_FILTERS = {}.freeze | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Page < ApplicationRecord | ||
belongs_to :product | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class PagePolicy < ApplicationPolicy | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreatePages < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :pages do |t| | ||
t.string :title | ||
t.text :body | ||
t.belongs_to :product, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require "rails_helper" | ||
|
||
search_input_selector = ".search__input" | ||
|
||
RSpec.feature "Pagination", type: :feature do | ||
def expect_to_appear_in_order(*elements) | ||
positions = elements.map { |e| page.body.index(e) } | ||
expect(positions).to eq(positions.sort) | ||
end | ||
|
||
it "paginates records based on a query param" do | ||
customers = create_list(:customer, 2) | ||
|
||
visit admin_customers_path(per_page: 1) | ||
|
||
expect(page).not_to have_content(customers.last.name) | ||
click_on "Next" | ||
expect(page).to have_content(customers.last.name) | ||
end | ||
|
||
describe "sorting" do | ||
it "allows sorting by columns" do | ||
create(:customer, name: "unique name two") | ||
create(:customer, name: "unique name one") | ||
|
||
visit admin_customers_path | ||
click_on "Name" | ||
|
||
expect_to_appear_in_order("unique name one", "unique name two") | ||
end | ||
|
||
it "allows clicking through after sorting", :js do | ||
customer = create(:customer) | ||
create(:order, customer: customer) | ||
|
||
visit admin_customers_path | ||
click_on "Name" | ||
find("[data-url]").click | ||
expect(page).to have_header("Show #{customer.name}") | ||
end | ||
|
||
it "allows reverse sorting" do | ||
create(:customer, name: "unique name one") | ||
create(:customer, name: "unique name two") | ||
|
||
visit admin_customers_path | ||
2.times { click_on "Name" } | ||
|
||
expect_to_appear_in_order("unique name two", "unique name one") | ||
end | ||
|
||
it "toggles the order" do | ||
create(:customer, name: "unique name one") | ||
create(:customer, name: "unique name two") | ||
|
||
visit admin_customers_path | ||
3.times { click_on "Name" } | ||
|
||
expect_to_appear_in_order("unique name one", "unique name two") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
end | ||
|
||
it "preserves search" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
query = "bar@baz.com" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
||
visit admin_customers_path(search: query) | ||
click_on "Name" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
||
expect(find(search_input_selector).value).to eq(query) | ||
end | ||
end | ||
|
||
context "with resources of type Page" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
it "can paginate and sort" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
FactoryBot.create(:page, title: "Page 2") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
FactoryBot.create(:page, title: "Page 4") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
FactoryBot.create(:page, title: "Page 1") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
FactoryBot.create(:page, title: "Page 5") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
FactoryBot.create(:page, title: "Page 3") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
||
visit admin_pages_path(per_page: 3) | ||
click_on "Title" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
expect_to_appear_in_order("Page 1", "Page 2", "Page 3") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
|
||
click_on "Next" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
expect_to_appear_in_order("Page 4", "Page 5") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,6 @@ | |
sequence(:name) { |n| "Country #{n}" } | ||
sequence(:code) { |n| "C#{n}" } | ||
end | ||
|
||
factory :page | ||
end |
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.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.