Skip to content

Commit

Permalink
Add tableNameToViewName and make view generator use it.
Browse files Browse the repository at this point in the history
  • Loading branch information
liammcdermott committed Oct 9, 2024
1 parent e02a4e3 commit db98a1f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 12 additions & 0 deletions IHP/NameSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module IHP.NameSupport
, fieldNameToColumnName
, escapeHaskellKeyword
, tableNameToControllerName
, tableNameToViewName
, enumValueToControllerName
, toSlug
, module IHP.NameSupport.Inflections
Expand Down Expand Up @@ -67,6 +68,17 @@ tableNameToControllerName tableName = do
else ucfirst tableName
{-# INLINABLE tableNameToControllerName #-}

-- | Transforms an underscore table name to a name for a view
--
-- >>> tableNameToViewName "users"
--
-- >>> tableNameToViewName "projects"
--
-- >>> tableNameToViewName "user_projects"
tableNameToViewName :: Text -> Text
tableNameToViewName = tableNameToControllerName
{-# INLINABLE tableNameToViewName #-}

-- | Transforms a enum value to a name for a model
--
-- >>> enumValueToControllerName "happy"
Expand Down
11 changes: 11 additions & 0 deletions Test/NameSupportSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ tests = do
tableNameToControllerName "users_projects" `shouldBe` "UsersProjects"
tableNameToControllerName "people" `shouldBe` "People"

describe "tableNameToViewName" do
it "should deal with empty input" do
tableNameToViewName "" `shouldBe` ""

it "should transform table names to controller names" do
tableNameToViewName "users" `shouldBe` "Users"
tableNameToViewName "projects" `shouldBe` "Projects"
tableNameToViewName "user_projects" `shouldBe` "UserProjects"
tableNameToViewName "users_projects" `shouldBe` "UsersProjects"
tableNameToViewName "people" `shouldBe` "People"

describe "enumValueToControllerName" do
it "should handle spaces in table names" do
enumValueToControllerName "very happy" `shouldBe` "VeryHappy"
Expand Down
5 changes: 3 additions & 2 deletions ihp-ide/IHP/IDE/CodeGen/ViewGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ data ViewConfig = ViewConfig
} deriving (Eq, Show)

buildPlan :: Text -> Text -> Text -> IO (Either Text [GeneratorAction])
buildPlan viewName applicationName controllerName' =
if (null viewName || null controllerName')
buildPlan viewName' applicationName controllerName' =
if (null viewName' || null controllerName')
then pure $ Left "Neither view name nor controller name can be empty"
else do
schema <- SchemaDesigner.parseSchemaSql >>= \case
Left parserError -> pure []
Right statements -> pure statements
let modelName = tableNameToModelName controllerName'
let controllerName = tableNameToControllerName controllerName'
let viewName = tableNameToViewName viewName'
let paginationEnabled = False
let viewConfig = ViewConfig { .. }
pure $ Right $ buildPlan' schema viewConfig
Expand Down

0 comments on commit db98a1f

Please sign in to comment.