yesod-pagination 0.2.0.0 → 0.3.0.0
raw patch · 3 files changed
+11/−17 lines, 3 files
Files
- src/Yesod/Paginate.hs +7/−13
- tests/main.hs +3/−3
- yesod-pagination.cabal +1/−1
src/Yesod/Paginate.hs view
@@ -43,7 +43,6 @@ -- | Returned by 'paginate' and friends. data Page r = Page { pageResults :: [r] -- ^ Returned entities.- , pageCount :: Int64 -- ^ Total number of pages. This will be at minimum 1, even for an empty result set. , nextPage :: Maybe Text -- ^ Link to next page, pre-rendered. , previousPage :: Maybe Text -- ^ Link to previous page, pre-rendered. } deriving (Eq, Read, Show)@@ -82,17 +81,12 @@ -> HandlerT site IO (Page r) -- ^ Returned page. paginateWithConfig c sel = do let filterStmt u = limit (pageSize c) >> return u-- [ct] <- runDB $ select $ from $ \u -> do- _ <- filterStmt u -- does nothing, used for type constraint- return (countRows :: SqlExpr (Value Int64))-- let maxPage = max 1 $ (unValue ct + pageSize c - 1) `div` pageSize c- cp = within (1, maxPage) $ currentPage c+ cp = max 1 $ currentPage c es <- runDB $ select $ from $ \u -> do _ <- filterStmt u- offset $ within (0, max 0 $ unValue ct - 1) $ pageSize c * (cp - 1)+ limit (pageSize c + 1)+ offset $ max 0 $ pageSize c * (cp - 1) sel u rt' <- getCurrentRoute@@ -103,13 +97,13 @@ np = rend rt $ updateQs qs ("page", [st|#{cp + 1}|]) pp = rend rt $ updateQs qs ("page", [st|#{cp - 1}|]) - return Page { pageResults = es- , pageCount = maxPage- , nextPage = if cp == maxPage then Nothing else Just np+ return Page { pageResults = take (fromIntegral $ pageSize c) es+ , nextPage = if fromIntegral (length es) == pageSize c + 1+ then Just np+ else Nothing , previousPage = if cp == 1 then Nothing else Just pp } where- unValue (Value a) = a updateQs ((a,b):as) (k,v) | k == a = (k,v):as | otherwise = (a,b):updateQs as (k,v) updateQs [] (k,v) = [(k,v)]
tests/main.hs view
@@ -57,14 +57,14 @@ clearOut pool get ItemsR- wantPage $ Page [] 1 Nothing Nothing+ wantPage $ Page [] Nothing Nothing yit "with some items" $ do clearOut pool k <- liftIO $ runSqlPersistMPool (insert $ Item "hello, world!") pool get ItemsR- wantPage $ Page [Entity k (Item "hello, world!")] 1 Nothing Nothing+ wantPage $ Page [Entity k (Item "hello, world!")] Nothing Nothing yit "with two pages" $ do clearOut pool@@ -86,7 +86,7 @@ get ("/items?page=2" :: Text) cp <- getPage- liftIO $ length (pageResults cp) `shouldBe` 5+ liftIO $ length (pageResults cp) `shouldBe` 0 getPage :: YesodExample TestApp (Page (Entity Item)) getPage = withResponse $ \SResponse { simpleBody } ->
yesod-pagination.cabal view
@@ -1,5 +1,5 @@ name: yesod-pagination-version: 0.2.0.0+version: 0.3.0.0 synopsis: Pagination in Yesod description: Easy pagination for Yesod. homepage: https://github.com/joelteon/yesod-pagination