Column Options
Unlike UI-level declarations, column options are made on individual columns.
Renaming a column in the view
By default, column names are rendered in the view by uppercasing the first letter in the name and replacing underscores with spaces. For example, :fname would be rendered as "Fname." If you want to use a custom name instead, set the :human_name option on user_columns like this:
Streamlined.ui_for(Person) do
user_columns :fname, { :human_name => 'First Name' },
:lname, { :human_name => 'Last Name' }
end
Making a column read only
What if you have a column that shouldn't be editable? Mark it as read only. It will still show up when you view the model, but it will no longer be available when editing the model.
Streamlined.ui_for(Person) do
user_columns :guid, { :read_only => true },
:ssn, { :read_only => true, :human_name => 'SS#' }
end
Making a column create only
A create only column can only be set once, during creation of the record. The column will not be editable after the record has been created.
Streamlined.ui_for(Person) do
user_columns :surname, { :create_only => true },
:suffix, { :create_only => true, :human_name => 'Name Suffix' }
end
Making a column update only
An update only column can only be set once the record has been created. (It can't be set when creating a new record.)
Streamlined.ui_for(Person) do
user_columns :surname, { :update_only => true },
:suffix, { :update_only => true, :human_name => 'Name Suffix' }
end
Allowing HTML in a column
By default, Streamlined renders column values using the #h method for safety. This replaces any potentially dangerous HTML characters with their symbolic equivalents. If your column contains raw HTML, or your column is an addition that has HTML in it, you can use the :allow_html option to force Streamlined to render the column correctly.
Streamlined.ui_for(Person) do
user_columns :post_body, { :allow_html => true }
end
Marking a column as not editable from the list view
Streamlined renders columns that are associations or enumerations with an "Edit" link next to them in the list view. This allows the column to be updated via an AJAX call. You can turn off these "Edit" links on a per-column basis by setting the :edit_in_list option to false.
Streamlined.ui_for(Person) do
user_columns :salutation, { :edit_in_list => false, :enumeration => Person::SALUTATIONS }
end
Making a boolean column use a check box
By default Streamlined renders boolean columns, in edit views, using drop down lists. To use a check box instead use the :check_box option. You can not combine this with :edit_in_list => true
Streamlined.ui_for(Person) do
user_columns :inactive_fl, { :check_box => true }
end
Using enumerations for column values
New in version 0.1.0 is the ability to use arrays as enumerations for column values. For example, your model might have a rate type column that will always have a value of "Basic," "Standard," or "Premium." You could place these strings in their own database table, but devoting an entire table to these three values is not always the best option to take, especially if these values are unlikely to change over the lifetime of the application.
Instead, Streamlined enables you to declare that a column should use an enumeration:
Streamlined.ui_for(Person) do
user_columns :rate_type, {:enumeration => RateType::TYPES}
end
class RateType
TYPES = ['Basic', 'Standard', 'Premium']
end
The above example would modify the UI so the rate column can only be set via a select field populated with strings from the TYPES array. Enumerations can also accept two-dimensional arrays and hashes as arguments:
class RateType
TYPES = [['Basic', 'BSC'], ['Standard', 'STD'], ['Premium', 'PRM']]
MORE_TYPES = { 'Basic' => 'BSC', 'Standard' => 'STD', 'Premium' => 'PRM' }
end
In these cases, the hash/array key ("Basic" for example) will be used as the label in the generated drop-down and the value ("BSC" for example) is what will actually be stored in the database table.
Using a custom method to render select options
If your UI file references an association and you want to control the select options that are displayed for that association, use the :options_for_select option:
Streamlined.ui_for(Person) do
user_columns :specialty, { :options_for_select => :custom_options_method }
end
The code above will trigger a call to the #custom_options_method (a class method) on the Specialty class. As long as #custom_options_method returns a valid options argument (typically a hash or a 2-dimensional array), the custom options will be rendered inside the select.
An example of the custom options method:
class Specialty < ActiveRecord::Base
def self.custom_options_method
self.find(:all).each { |e| "#{e.name} - #{e.location}" }
end
end
Making a column link to something
If you want a column to be rendered as a link, use the :link_to option on the user_columns declaration:
Streamlined.ui_for(Person) do
user_columns :first_name, { :link_to => { :controller => 'foo', :action => 'bar' } }
end
Any of the Streamlined's default CRUD actions can also be linked to. For example, to link to the edit page for the existing model:
Streamlined.ui_for(Person) do
user_columns :first_name, { :link_to => { :action => 'edit' } }
end
(The action could just as easily have been show or list.)
Specifying that columns open with popups
Streamlined ships with the Overlib JavaScript library for popup tips. Use the :popup option to set one or more columns to trigger the popup.
Streamlined.ui_for(Person) do
user_columns :first_name, { :popup => true },
:last_name, { :popup => true }
end
To change the default popup view, add a partial called _popup.rhtml to the app/view/[your_model]s folder. In that partial, you can access the current item via the @streamlined_item variable.
Customizing unassigned values
When an association column has no value, Streamlined renders it as "unassigned." If this default is not desirable, it can be changed using the :unassigned_value option. For example, to render unassigned associations as "none" use this syntax:
Streamlined.ui_for(Person) do
user_columns :address, { :unassigned_value => 'none' }
end
Wrapping input fields
Wrappers offer a convenient way to place custom HTML before or after input fields. For example, what if you wanted to display a specific icon after certain form fields? You could do something like this:
Streamlined.ui_for(Person) do
user_columns :first_name, { :wrapper => Proc.new { |e| "#{e} <img src='foo.gif'/>" }},
:last_name, { :wrapper => Proc.new { |e| "#{e} <img src='bar.gif'/>" }}
end
The :wrapper option takes a Proc as an argument. The Proc is passed the HTML for the form field when it gets called. The UI syntax gets even cleaner when the Procs are extracted into their own class, like this:
module Wrapper
FOO_IMAGE = Proc.new { |e| "#{e} <img src='foo.gif'/>" }
BAR_IMAGE = Proc.new { |e| "#{e} <img src='bar.gif'/>" }
end
Streamlined.ui_for(Person) do
user_columns :first_name, { :wrapper => Wrapper::FOO_IMAGE },
:last_name, { :wrapper => Wrapper::BAR_IMAGE }
end
