Back to Home

Introducing Active Scaffold

active scaffold · ruby ​​on rails · interfaces · prototyping · activescaffold

Introducing Active Scaffold

  • Tutorial
Good day to all! I want to tell you about the wonderful gem (plugin) for Ruby and Rails (I almost did not find any mention of it on the Habr).

The purpose of this plugin is to provide a convenient, standard “out of the box” interface. With the addition, deletion, editing, search, sorting and all this with ajax (or without it).



Such a miracle is done, as in the figure, in a few simple steps:

1. We throw a line in the Gemfile
gem 'active_scaffold'

2. Install gems
bundle install

3. Connect styles and scripts
/* В /app/assets/stylesheets/application.css.scss
 *= require active_scaffold
 */

// В /app/assets/javascripts/application.js
//= require active_scaffold

4. We write in the target controller
class StuffsController < AuditorController
  active_scaffold :stuff
end

All! Next, Active Scaffold itself will pick up the corresponding model (in the example - Stuff), find out about its columns in the database table and related models, select the 15 (default) first records according to the default_scope settings in the model (if specified) and present it to you. Also at the same time (except for displaying HTML) will provide you with APIs in JSON and XML formats.

Active Scaffold defines the active_scaffold and active_scaffold_controller generators that allow you to create a model + migration + controller + route or just one controller, respectively. Like that:
rails generate active_scaffold stub name:string value:integer precise:decimal occurs:date


The result is this:


ActiveScaffold is friends with I18n, so Russification should not cause difficulties:
ru:
  activerecord:
    models:
      stub:
        one: "Заглушка"
        few: "Заглушки"
        many: "Заглушки"
        other: "Заглушки"
    attributes:
      stub:
        name: "Название"
        value: "Значение"
        precise: "Точно"
        occurs: "Состоится"
        created_at: "Создано"
        updated_at: "Изменено"



Very cool, right?

Active Scaffold can also display error messages if model validation fails or something irreparable with the server happens.

But this is all the basis. Most important: Active Scaffold monitors relationships between models (entities) and can load related records. Like this:


In this case, the plugin is well configured:
  • You can specify the order and number of columns displayed (besides columns, you can display model methods), you can set your own attribute sets to create, display and edit
  • You can control sorting and filtering
  • Depending on different conditions, you can generously distribute CSS classes to various entries.
  • You can create links to your actions in the controller.
  • You can enable checkboxes and mark many records in bulk (and in bulk to process them). Approximately in this way .
  • You can redefine how the values ​​of the various columns are displayed (pay attention to the colored boxes):

    This trick is done like this (in the helper file for the desired controller):
    module GroupsHelper
      def group_color_column(record)
        raw "
    " end end
  • You can override form elements . For most cases (password, list, ...) there are already finished ones, look in the API: Column under the heading form_ui.
  • You can set the settings and access rights to each entry separately.
  • And many many others…

The source code for the project is available on github, and in the same place, on the wiki, there is excellent documentation (although a bit scattered and sometimes outdated). If all the same, something is incomprehensible or something does not work - the project has a live Google group , look or ask there, they will help you (but you definitely look at the FAQ in the documentation before that , right?)

Summarizing


The plugin is perfect for prototyping and creating simple interfaces, if there is a lot of data and they are of the same type (for example, many, many numbers). Also good for various admin areas, etc.

However, if you need something quite complicated - write your own better. Active Scaffold is difficult to configure for processing complex forms, complex representations, working with many-to-many relationships, working with intricately related entities. Here it is better to write your own right away.

That's all. Have a happy development and do not waste your routine in vain. Such amazing plugins were created for her. And don't forget to return to open source. (Actually, I was working on such a small return now)

Read Next