Skip to content
TApicella edited this page Sep 13, 2013 · 8 revisions

Two very useful resources are this introductory book on Grails and the the Grails documentation. Going through the tutorial in the book is a great way to get used to the key concepts of Grails. However, as a quick reference, some of the basic ideas are summarized here and connected to examples in Metridoc-RID. For people experienced with web frameworks, this may seem very basic, however the assumption here is a background in coding but not necessarily a background in web development.

MVC

MVC is a web architecture pattern that helps organize code by its functionality in displaying data on the web. A succinct diagram describing this pattern can be seen here: MVC model. To put this into Metridoc-RID terms, let's briefly go over what RID does. In very simple terms, RID enables users to create transactions, view them, and edit them. They do this through the web interface, which is the view. Buttons in this view, such as save and edit, correspond to methods in the controller. Changes users make are passed from the view to the model by the controller. The model contains information about the data being accessed, and also what type of view should be used. So, let's say a user makes a new transaction on the create page and afterwards the page redirects to the show page. The workflow would be

  • User fills out a form on the "create" page and clicks the create button in the view
  • The create method of the controller is run.
  • This method updates the data in the model, which includes the new transaction and also the instruction to go to the "show" page.
  • The model now contains the new data and the instruction to redirect to show.
  • The view is now the "show" page where the new transaction information is displayed.

Convention over Configuration

Many programming projects and web frameworks require a lot of manual configuration to organize files. Grails operates on convention: controllers are in one folder, domains in another, views in another, all tied together automatically by filename. Even URLs are generated automatically based on controller name and method being used, though they can be changed manually. Plugins are assigned to views by setting up modules with the same name as their folder. This can be very convenient, but it does mean you need to be careful when reorganizing files or changing names around.

GORM

GORM stands for Grails Object-Relational Mapping. What this means is when you create domain classes to store data, they are given tables in the database. You can define relationships between domain classes, which in turn automatically generates links between their tables in the database. This allows you to easily query the database, for example using .list() to easily pull out all of the saved members of that class. One thing to note is that creating tables and entries in those tables is fairly easily, but removing items is much more difficult in order to keep the integrity of the database intact.

Grails command line

When using the command line with grails, there are two options. Single commands can be issued by typing >. However, typing will activate the interactive shell, which removes the need to restart grails every time you want to test an app. Here are some of the most important commands.

  • Creating
  • create-app
  • create-domain
  • create-controller
  • create-service
  • generate-controller <domainClassName> Generates default methods and views for the given domain
  • generate-views <domainClassName> Generates default views for the given domain
  • Managing code
  • clean Forces a recompile
  • refresh-dependencies Installs new plugins and removes deleted ones
  • Locally running the app
  • [env] run-app Runs the application on port 8080 by default. Runs it in development mode unless env is specified. Can be abbreviated as rA.
  • stop-app Stops the application
  • Testing code
  • test-appRuns all tests. Can be abbreviated as tA.
  • test-app [unit:/integration:] Only runs tests of the specified type.
  • test-app <name> Only runs the test with that name