-
Notifications
You must be signed in to change notification settings - Fork 332
Embedded Scaffolds
Embedding scaffolds allow you to reuse your scaffold and insert it into other views as a widget. You can specify constraints for your scaffold to limit the records it works with.
h3 Rendering 3
To embed a scaffold in another controller, or on a page somewhere, use render :active_scaffold => "controller_id" (it’s a special call that will turn around and call render_component). You may pass ANY combination of parameters you would like: label, sort, sort_direction, search, and even constraints (covered later).
#render UsersController
render :active_scaffold => 'users'
#the same, but this time change the heading to “Active Users”
render :active_scaffold => 'users', :label => 'Active Users'
#sorting by `name`, ascending
render :active_scaffold => 'users', :sort => "name", :sort_direction => "ASC"
h3 Constraints 3
Constraints are simple field/value pairs used as search conditions for the scaffold’s list. Because constrained columns become rather boring to look at, they are automatically removed from column sets so they don’t appear in List, Create, Update, etc. Furthermore, records created through a constrained embedded scaffold will automatically receive the values it is constrained to (a user can not create a record with a value outside of the constraint). Constraints may be the name of a database field (e.g. user_id) or the name of a attribute or association column. If the constraint is the name of an association, then the constraint value will be assumed an ID.
#render all entries for a user
render :active_scaffold => 'entries', :constraints => { :user_id => @user.id }
#render all active users
render :active_scaffold => 'users', :constraints => { :status => "active" }
Important: Constraints are stored in the session. You should therefore only use simple values, e.g. record.id instead of record.
render :active_scaffold => 'users', :constraints => {:active => 1, :user_group => 15}, :params => {:hello => 'world'}, :label => 'Active Editors'
Advanced: Accessing constraint data in your controller
To access constraint data inside of your controller, use active_scaffold_session_storage[:constraints][:constraint_key].
h3 Conditions v1.1 3
Constraints are powerful, but only because they’re limited to equality-based conditions, i.e. a equals b. If you want something simpler and more flexible, add :conditions to your embedded scaffold. Unlike constraints, these conditions will affect what is in the list, but will not affect visible columns and will not automatically apply to all new records. But in exchange you can create greater-than, less-than, in-set, etc., conditions.
#note: example hasn’t been tested for typos or other obvious problems
render :active_scaffold => 'users', :conditions => ['created_at > ?', Time.now - 5.days], :label => 'New Users'