module InlineForms
InlineForms
is a Rails Engine
that let you setup an admin interface quick and easy. Please install it as a gem or include it in your Gemfile.
Constants
- DEFAULT_COLUMN_TYPES
DEFAULT_COLUMN_TYPES
holds the standard ActiveRecord::Migration column types. This list provides compatability with the standard types, but we add our own later in 'Special Column Types'.These types will override Special Column Types of the same name.\
Example: rails g inline_forms Example name:string price:integer will result in:
class InlineFormsCreateExamples < ActiveRecord::Migration def self.up create_table :examples do |t| t.string :name t.integer :price t.timestamps end end def self.down drop_table :examples end end
- DEFAULT_FORM_ELEMENTS
DEFAULT_FORM_ELEMENTS
holds a mapping from Default Column Types to Form Elements. Form Elements are defined in lib/app/helpers/form_elements and are pieces of code that display a form for a field.Example:
rails g inline_forms Example name:string price:integer
will result in the following model:
class Example < ApplicationRecord def inline_forms_attribute_list { :name => [ "name", :text_field ], :price => [ "price", :text_field ], } end end
as you see, both :string and :integer are mapped to a :text_field
- RELATIONS
RELATIONS
defines a mapping between AR::Migrations columns and the Model.When a column has the type of :references or :belongs_to, then there will be a line in the migration reflecting that, but not in the model.
Why?¶ ↑
-
Let's say we have a customer that has_many phone_numbers.
-
Let's say that a phone_number belongs_to a customer.
-
Let's say that every number has_one type_of_number (like 'private','gsm' etc.)
-
Let's say a type_of_number belongs_to a number.
Wait a minute… thats sounds right… but it ain't!
In fact, a type_of_number has_many phone_numbers and a phone_number belongs_to a type_of_number!
In a form, it's quite logical to use a dropdown for type_of_number. So, in the generator, use
type_of_number:dropdown
This creates the correct migration (t.integer :type_of_number_id) and the correct model. (It adds 'belongs_to :type_of_number' and adds a dropdown in the inline_forms_attribute_list)
But, you also want to have a client_id in the migration, and a 'belongs_to :client' in the model. In such cases, you need to use :belongs_to, like this:
rails g inline_forms Example phone_number:string type_of_number:dropdown client:belongs_to
-
- SPECIAL_COLUMN_TYPES
SPECIAL_COLUMN_TYPES
maps the column types that we define here and in lib/app/helpers/form_elements to the standard ActiveRecord::Migration column typesExample: in lib/app/helpers/form_elements/dropdown.rb
InlineForms::SPECIAL_COLUMN_TYPES[:dropdown]=:belongs_to
this maps the :dropdown form element to the :belongs_to column type.
If you call the generator with country:dropdown, it will add
t.belongs_to :country
to the migration. (In fact AR will add t.integer :country_id). And it will add
:country => [ "country", :dropdown ],
to the inline_forms_attribute_list in the model.
- SPECIAL_RELATIONS
SPECIAL_RELATIONS
maps AR relations to migrations. In most cases, these relations have no migration at all, but they do need a line in the model.- VERSION