Skip to content

Commit

Permalink
Merge pull request #7 from gakuzzzz/feature/play26
Browse files Browse the repository at this point in the history
release 0.2.0
  • Loading branch information
gakuzzzz authored Dec 2, 2018
2 parents 89f15e7 + fc175a4 commit be3ed6f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ jdk:
- openjdk8
- oraclejdk8
scala:
- 2.12.4
- 2.12.7
- 2.11.12
109 changes: 61 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ Pager support for Play2 application.
![sample app screenshot](doc/img/play2-pager-sample-ss.png?raw=true)


## Target

Scala 2.11.x & 2.12.x
Play 2.5.x & 2.6.x

## Setup

```scala
libraryDependencies += "jp.t2v" %% "play2-pager" % "0.1.0"
libraryDependencies += "jp.t2v" %% "play2-pager-scalikejdbc" % "0.1.0" // optional. it is useful when you use scalikejdbc.
libraryDependencies += "jp.t2v" %% "play2-pager" % "0.2.0"
libraryDependencies += "jp.t2v" %% "play2-pager-scalikejdbc" % "0.2.0" // optional. it is useful when you use scalikejdbc.
```

## How to use

1. add imports into `templateImports` and `routesImport`
1. add imports into `templateImports` and `routesImport` in your Play Project `.sbt`.

```scala
TwirlKeys.templateImports += "jp.t2v.lab.play2.pager._"
play.PlayImport.PlayKeys.routesImport += "jp.t2v.lab.play2.pager.Pager"
play.PlayImport.PlayKeys.routesImport += "jp.t2v.lab.play2.pager.Bindables._"
lazy val sample = (project in file("sample")).
enablePlugins(PlayScala).
settings(
// ...snip
templateImports ++= Seq(
"jp.t2v.lab.play2.pager._"
),
routesImport ++= Seq(
"jp.t2v.lab.play2.pager.Pager",
"jp.t2v.lab.play2.pager.Bindables._"
),
)
```

1. define implicit `Sortable` value of your entities.
Expand All @@ -30,66 +43,66 @@ libraryDependencies += "jp.t2v" %% "play2-pager-scalikejdbc" % "0.1.0" // option
* `acceptableKeys` is sortable keys of the entity.

```scala
package models.account

import scalikejdbc._
import org.joda.time.{DateTime, LocalDate}
import jp.t2v.lab.play2.pager.{OrderType, Sortable}

case class Account(id: Int, name: Name, email: EMail, birthday: LocalDate, createdAt: DateTime)

object Account extends SQLSyntaxSupport[Account] {

def apply(s: SyntaxProvider[Account])(rs: WrappedResultSet): Account = autoConstruct(rs, s)

implicit object sortable extends Sortable[Account] {
val default: (String, OrderType) = ("id", OrderType.Descending)
val acceptableKeys: Set[String] = Set("id", "name", "email", "birthday", "createdAt")
}

}
package models.account
import scalikejdbc._
import org.joda.time.{DateTime, LocalDate}
import jp.t2v.lab.play2.pager.{OrderType, Sortable}
case class Account(id: Int, name: Name, email: EMail, birthday: LocalDate, createdAt: DateTime)
object Account extends SQLSyntaxSupport[Account] {
def apply(s: SyntaxProvider[Account])(rs: WrappedResultSet): Account = autoConstruct(rs, s)
implicit object sortable extends Sortable[Account] {
val default: (String, OrderType) = ("id", OrderType.Descending)
val acceptableKeys: Set[String] = Set("id", "name", "email", "birthday", "createdAt")
}
}
```

1. define controller that receive a `Pager` of your entity.

```scala
def index(pager: Pager[Account]) = Action {
Ok(views.html.index(accountService.findAll(pager)))
}
def index(pager: Pager[Account]) = Action {
Ok(views.html.index(accountService.findAll(pager)))
}
```

1. define service that returns a `SearchResult` of your entity.

```scala
package models.account

import jp.t2v.lab.play2.pager.{SearchResult, Pager}

class AccountService(implicit accountDao: AccountDao) {

def findAll(pager: Pager[Account]): SearchResult[Account] = {
SearchResult(pager, accountDao.countAll()) { pager =>
accountDao.findAll(pager.allSorters, pager.limit, pager.offset)
package models.account

import jp.t2v.lab.play2.pager.{SearchResult, Pager}

class AccountService(implicit accountDao: AccountDao) {

def findAll(pager: Pager[Account]): SearchResult[Account] = {
SearchResult(pager, accountDao.countAll()) { pager =>
accountDao.findAll(pager.allSorters, pager.limit, pager.offset)
}
}

}
}

}
```

1. define template that receive a `SearchResult` of your entity.

You can use `@pagination` to render pagination.

```scala
@(result: SearchResult[Account])

@main("Welcome to Play2 Pager") {

...snip

@pagination(result, routes.Application.index)

}
@(result: SearchResult[Account])
@main("Welcome to Play2 Pager") {
...snip
@pagination(result, routes.Application.index)
}
```

More details, see [Sample App](sample)
Expand Down
15 changes: 10 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
scalaVersion := "2.11.12"
scalaVersion := "2.12.7"

val _version = "0.1.0"
val _version = "0.2.0"

val _crossScalaVersions = Seq("2.12.4", "2.11.12")
val _crossScalaVersions = Seq("2.12.7", "2.11.12")

val _org = "jp.t2v"

Expand Down Expand Up @@ -60,7 +60,7 @@ lazy val commonSettings = Seq(
organization := _org,
name := "play2-pager",
version := _version,
scalaVersion := "2.11.12",
scalaVersion := "2.12.7",
crossScalaVersions := _crossScalaVersions,
publishMavenStyle := _publishMavenStyle,
publishArtifact in Test := _publishArtifactInTest,
Expand Down Expand Up @@ -118,5 +118,10 @@ lazy val sample = (project in file("sample")).
TwirlKeys.templateImports ++= Seq(
"jp.t2v.lab.play2.pager._",
"models.account._"
)
),
publish := { },
publishArtifact := false,
packagedArtifacts := Map.empty,
publishTo := _publishTo(_version),
pomExtra := _pomExtra
)
12 changes: 1 addition & 11 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.11")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.20")

// web plugins

addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.2")

addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.2")

addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.6")

addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.10")

addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.3")

addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.2")

0 comments on commit be3ed6f

Please sign in to comment.