thoughtbot / carnival

An unobtrusive, developer-friendly way to add comments
MIT License
499 stars 30 forks source link

Consider implementing a SiteHandler monad #145

Closed pbrisbin closed 6 years ago

pbrisbin commented 9 years ago

This would give "global" access to a once-looked-up Entity Site by making actions in a SiteHandler monad built with ReaderT:

type SiteHandler = ReaderT (Entity Site) Handler

withSite :: SiteHandler a -> SiteId -> Handler a
withSite f siteId = do
    site <- runDB $ get404 siteId

    runReader f (Entity siteId site)

postComment :: SiteId -> Handler Value
postComment = withSite $ do
    -- ...

    site <- ask

    -- ...

    lift $ sendResponse -- ...
pbrisbin commented 9 years ago

The upside is we can probably remove many SiteId or Entity Site arguments, and take the userCommentSite field off of UserComment. The downside is we can't remove them all (actions in DB that need a Site for example). And we'll have to call lift constantly to perform Handler actions.