Use Context#settings instead of loadSystemSettings()

This commit is contained in:
takezoe
2014-03-15 04:07:31 +09:00
parent 02a367fd99
commit f5883abf04
3 changed files with 23 additions and 26 deletions

View File

@@ -1,7 +1,6 @@
package service package service
import model._ import model._
import service.SystemSettingsService.SystemSettings
/** /**
* This service is used for a view helper mainly. * This service is used for a view helper mainly.
@@ -9,28 +8,23 @@ import service.SystemSettingsService.SystemSettings
* It may be called many times in one request, so each method stores * It may be called many times in one request, so each method stores
* its result into the cache which available during a request. * its result into the cache which available during a request.
*/ */
trait RequestCache { trait RequestCache extends SystemSettingsService with AccountService with IssuesService {
def getSystemSettings()(implicit context: app.Context): SystemSettings =
context.cache("system_settings"){
new SystemSettingsService {}.loadSystemSettings()
}
def getIssue(userName: String, repositoryName: String, issueId: String)(implicit context: app.Context): Option[Issue] = { def getIssue(userName: String, repositoryName: String, issueId: String)(implicit context: app.Context): Option[Issue] = {
context.cache(s"issue.${userName}/${repositoryName}#${issueId}"){ context.cache(s"issue.${userName}/${repositoryName}#${issueId}"){
new IssuesService {}.getIssue(userName, repositoryName, issueId) super.getIssue(userName, repositoryName, issueId)
} }
} }
def getAccountByUserName(userName: String)(implicit context: app.Context): Option[Account] = { def getAccountByUserName(userName: String)(implicit context: app.Context): Option[Account] = {
context.cache(s"account.${userName}"){ context.cache(s"account.${userName}"){
new AccountService {}.getAccountByUserName(userName) super.getAccountByUserName(userName)
} }
} }
def getAccountByMailAddress(mailAddress: String)(implicit context: app.Context): Option[Account] = { def getAccountByMailAddress(mailAddress: String)(implicit context: app.Context): Option[Account] = {
context.cache(s"account.${mailAddress}"){ context.cache(s"account.${mailAddress}"){
new AccountService {}.getAccountByMailAddress(mailAddress) super.getAccountByMailAddress(mailAddress)
} }
} }
} }

View File

@@ -16,7 +16,7 @@ trait AvatarImageProvider { self: RequestCache =>
val src = if(mailAddress.isEmpty){ val src = if(mailAddress.isEmpty){
// by user name // by user name
getAccountByUserName(userName).map { account => getAccountByUserName(userName).map { account =>
if(account.image.isEmpty && getSystemSettings().gravatar){ if(account.image.isEmpty && context.settings.gravatar){
s"""https://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress.toLowerCase)}?s=${size}""" s"""https://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress.toLowerCase)}?s=${size}"""
} else { } else {
s"""${context.path}/${account.userName}/_avatar""" s"""${context.path}/${account.userName}/_avatar"""
@@ -27,13 +27,13 @@ trait AvatarImageProvider { self: RequestCache =>
} else { } else {
// by mail address // by mail address
getAccountByMailAddress(mailAddress).map { account => getAccountByMailAddress(mailAddress).map { account =>
if(account.image.isEmpty && getSystemSettings().gravatar){ if(account.image.isEmpty && context.settings.gravatar){
s"""https://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress.toLowerCase)}?s=${size}""" s"""https://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress.toLowerCase)}?s=${size}"""
} else { } else {
s"""${context.path}/${account.userName}/_avatar""" s"""${context.path}/${account.userName}/_avatar"""
} }
} getOrElse { } getOrElse {
if(getSystemSettings().gravatar){ if(context.settings.gravatar){
s"""https://www.gravatar.com/avatar/${StringUtil.md5(mailAddress.toLowerCase)}?s=${size}""" s"""https://www.gravatar.com/avatar/${StringUtil.md5(mailAddress.toLowerCase)}?s=${size}"""
} else { } else {
s"""${context.path}/_unknown/_avatar""" s"""${context.path}/_unknown/_avatar"""

View File

@@ -10,53 +10,58 @@ import twirl.api.Html
class AvatarImageProviderSpec extends Specification { class AvatarImageProviderSpec extends Specification {
implicit val context = app.Context("", None, null)
"getAvatarImageHtml" should { "getAvatarImageHtml" should {
"show Gravatar image for no image account if gravatar integration is enabled" in { "show Gravatar image for no image account if gravatar integration is enabled" in {
val provider = new AvatarImageProviderImpl(Some(createAccount(None)), createSystemSettings(true)) implicit val context = app.Context(createSystemSettings(true), None, null)
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
provider.toHtml("user", 20).toString mustEqual provider.toHtml("user", 20).toString mustEqual
"<img src=\"https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=20\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />" "<img src=\"https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=20\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />"
} }
"show uploaded image even if gravatar integration is enabled" in { "show uploaded image even if gravatar integration is enabled" in {
val provider = new AvatarImageProviderImpl(Some(createAccount(Some("icon.png"))), createSystemSettings(true)) implicit val context = app.Context(createSystemSettings(true), None, null)
val provider = new AvatarImageProviderImpl(Some(createAccount(Some("icon.png"))))
provider.toHtml("user", 20).toString mustEqual provider.toHtml("user", 20).toString mustEqual
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />" "<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />"
} }
"show local image for no image account if gravatar integration is disabled" in { "show local image for no image account if gravatar integration is disabled" in {
val provider = new AvatarImageProviderImpl(Some(createAccount(None)), createSystemSettings(false)) implicit val context = app.Context(createSystemSettings(false), None, null)
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
provider.toHtml("user", 20).toString mustEqual provider.toHtml("user", 20).toString mustEqual
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />" "<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />"
} }
"show Gravatar image for specified mail address if gravatar integration is enabled" in { "show Gravatar image for specified mail address if gravatar integration is enabled" in {
val provider = new AvatarImageProviderImpl(None, createSystemSettings(true)) implicit val context = app.Context(createSystemSettings(true), None, null)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual
"<img src=\"https://www.gravatar.com/avatar/4712f9b0e63f56ad952ad387eaa23b9c?s=20\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />" "<img src=\"https://www.gravatar.com/avatar/4712f9b0e63f56ad952ad387eaa23b9c?s=20\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />"
} }
"show unknown image for unknown user if gravatar integration is enabled" in { "show unknown image for unknown user if gravatar integration is enabled" in {
val provider = new AvatarImageProviderImpl(None, createSystemSettings(true)) implicit val context = app.Context(createSystemSettings(true), None, null)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20).toString mustEqual provider.toHtml("user", 20).toString mustEqual
"<img src=\"/_unknown/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />" "<img src=\"/_unknown/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />"
} }
"show unknown image for specified mail address if gravatar integration is disabled" in { "show unknown image for specified mail address if gravatar integration is disabled" in {
val provider = new AvatarImageProviderImpl(None, createSystemSettings(false)) implicit val context = app.Context(createSystemSettings(false), None, null)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual
"<img src=\"/_unknown/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />" "<img src=\"/_unknown/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" />"
} }
"add tooltip if it's enabled" in { "add tooltip if it's enabled" in {
val provider = new AvatarImageProviderImpl(None, createSystemSettings(false)) implicit val context = app.Context(createSystemSettings(false), None, null)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20, "hoge@hoge.com", true).toString mustEqual provider.toHtml("user", 20, "hoge@hoge.com", true).toString mustEqual
"<img src=\"/_unknown/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" data-toggle=\"tooltip\" title=\"user\"/>" "<img src=\"/_unknown/_avatar\" class=\"avatar\" style=\"width: 20px; height: 20px;\" data-toggle=\"tooltip\" title=\"user\"/>"
@@ -80,7 +85,7 @@ class AvatarImageProviderSpec extends Specification {
private def createSystemSettings(useGravatar: Boolean) = private def createSystemSettings(useGravatar: Boolean) =
SystemSettings( SystemSettings(
baseUrl = None, baseUrl = Some(""),
allowAccountRegistration = false, allowAccountRegistration = false,
gravatar = useGravatar, gravatar = useGravatar,
notification = false, notification = false,
@@ -93,15 +98,13 @@ class AvatarImageProviderSpec extends Specification {
/** /**
* Adapter to test AvatarImageProviderImpl. * Adapter to test AvatarImageProviderImpl.
*/ */
class AvatarImageProviderImpl(account: Option[Account], settings: SystemSettings) class AvatarImageProviderImpl(account: Option[Account]) extends AvatarImageProvider with RequestCache {
extends AvatarImageProvider with RequestCache {
def toHtml(userName: String, size: Int, mailAddress: String = "", tooltip: Boolean = false) def toHtml(userName: String, size: Int, mailAddress: String = "", tooltip: Boolean = false)
(implicit context: app.Context): Html = getAvatarImageHtml(userName, size, mailAddress, tooltip) (implicit context: app.Context): Html = getAvatarImageHtml(userName, size, mailAddress, tooltip)
override def getAccountByMailAddress(mailAddress: String)(implicit context: app.Context): Option[Account] = account override def getAccountByMailAddress(mailAddress: String)(implicit context: app.Context): Option[Account] = account
override def getAccountByUserName(userName: String)(implicit context: app.Context): Option[Account] = account override def getAccountByUserName(userName: String)(implicit context: app.Context): Option[Account] = account
override def getSystemSettings()(implicit context: app.Context): SystemSettings = settings
} }
} }