FlexScaffold

Flex Scaffold should allow you to swap in and out different view layers on your REST controller. For example, to use the model contact, use flex_scaffold: contact

class ContactsController < ApplicationController

layout ‘flex_scaffold’

  flex_scaffold :contact               (optional - :size => '800x600')

  # The following line can be used instead of the generated definitions
  # of index, show, new, edit, update, destroy. There is also the action
  # schema to look at the data model
  ...
end

Other files that are created:

/app/flex/contacts/_list.mxml
/app/flex/contacts/_contacts.mxml         <-- these are compiled to a swf file

/public/swfs/flex_scaffold/_contacts.swf  <-- created via rake flex:app mxml=contacts

/app/views/layout/flex_scaffold.rhtml     <-- templated pages
/app/views/flex_scaffold/index.rhtml      <-- loads up the swf

USAGE

Summary

To generate a scaffold for the model message with fields title and body:

> ruby script/generate flex_scaffold_resource message title:string body:string
> rake db:migrate
> ruby script/generate flex_scaffold message
> rake flex:app mxml=messages
> ruby script/server start

1. Install a REST service from flex_scaffold_resource

Create an Active Resource (REST service)

> ruby script/generate flex_scaffold_resource message title:string body:string

This simple command will generate a lot of stuff, in detail:

  • A controller with REST actions and an action for getting the schema

  • A model named message

  • A migration named xxx_create_messages

  • Fixture and unit tests for the model and the controller

  • A line in your config/routes.rb file map.resources :messages

Alternatively, you can add scaffolding to any REST resources by adding to any controller:

flex_scaffold :message

Note: You can then delete or extend all the other REST actions as they are implicitly scaffolded

2. Migrate the database

> db:migrate

This updates the database ready for the scaffold to pick up the columns.

3. Create the scaffolds

> ruby script/generate flex_scaffold message

This will generate:

  • A scaffold view for the controller which loads the swf

  • A template layout for the controller

  • Scaffolded mxml files (in app/flex)

4. Compile the scaffolds

> rake flex:app mxml=messages

This will generate:

  • copy assets into the /app/flex/<model> folder ready for compilation (see below)

  • compile the swf files based on the configuration in the related controller to /app/flex/<model>

  • save the resulting swf in /public/swfs/flex_scaffold

  • ensure that all needed public assets are copied to /public/ [stylesheets, javascript] (also see Restart server below)

This Rake task infact copies the necessary assets into the build folder </tt>/app/flex</tt> and then remove when finished. To avoid you compile only. This would involved steps either side for use:

> rake flex:build:install mxml=contacts  [move the assets into the build structure]
> rake flex:compile mxml=contacts        [repeat this as many times as needed]

Optionally you may wish to clean up:

> rake flex:build:clobber mxml=contacts

Note: There is currently no way to build multiple scaffolds at one time.

5. (Re)Start server?

Just check this. You should only need to restart the start to init the plugin. This will also copy the correct files to public which is equivalent to the Rake task: rake flex:public:install

General Design Notes:

Key features:

  • Easily scaffold

  • Be able to manually compile

  • Getting model validation generated out to the client (still working out on full validation conversion)

To do:

  • Be able to cutomise views

  • Automatic (re)complilation

  • Perhaps, auto install flex_sdk2 (this will probably break license - but could have local repository/or checked into a library)

Long run:

I suspect that there will need to split out flex_scaffold and ActionView::Helpers::FlexObjectHelper. I would hope that flex have an equivalent set of helpers as javascript.

Phase I (complete)

  • tie in REST (resource) generation for flex_scaffold

  • scaffold model for flex viewing (flex_scaffold)

  • generate mxml files for model

  • compile swf

  • transfer swf to public folder

  • load page which points flash file (use JavascriptHelper in openLaszlo)

  • add CRUD actions on controller

  • add return schema onto controller

Phase II

  • remove js, styles, etc as generator and put as rake command at startup

  • add the view layer as the plugin rather than generator

  • duplicate model validation (at compile time) into client (as well as server)

  • autocompilation (generator knows whether there were changes)

Phase III

flex_scaffold :task do table :only => [ :name, :role ] form :except => [ :created_at, :updated_at ] columns do name do sort(“name.downcase”) is_required end end end

  • template the flex code created in generator using helpers (develop ActiveView::Helpers::FlexHelper)

  • that is use, rhtml to embed flex within pages (scaffold as a plugin) [unsure whether this will work and/or is desirable]

Phase IV

  • multiple models on a page

Credits

Released under the MIT license (included)

  • toddb — Flex Scaffold plugin

The initial code was inspired by (or lifted from)

  • flex_engenial_scaffold: This is the basis for the CRUD look and feel and early code basis

  • flexible_rails: I have used his section from FlexibleRails for using REST services

  • ajax_scaffold: basically reproduced their tables and hook code

  • laszlo-plugin: this separates out the generators well and has a good javascript helper