murugaperumal-ramasamy / has_flexiblefields

Rails plugin. Adds extra fields in a has one relationship to an existing model - includes definition and definition entries to label extra fields

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HasFlexibleFields
=================

Added: Forms for Flexifield Definition management.  Uses nested forms.
Added: Wrapped Columns for Flexifield Definition Entry, wraps ActiveRecord::ConnectionAdapters::<Adapter>Column in a
Struct to provide a content attribute and two convenience methods to use in view or form creation

The problem:

How does one add fields to a model (usually another project's models) without permanently altering the model structure?
What if the 'name' of the field was only known at runtime?

This plugin attempts to answer the above questions.

I rejected the idea of Entity-Attribute-Value
I had a look at has_magic_columns but the field labels/aliases need to be defined in code

Features:
Extends ActiveRecord
Uses a has_one association to a flexifields model
The flexifields model is polymorphic, one table to hold all has_one records for any model extended by this plugin
Uses delegation to make some methods appear in the extended model.

To create the flexifield tables put this in your migration for the extended model (only for the first one)
e.g. if Article is the model you are extending
class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles, :force => true do |t|
      t.string :title
      t.text :body
      t.boolean :published
      t.timestamps
    end

    Article.create_ff_tables!
  end

  def self.down
    Article.drop_ff_tables!
    drop_table :articles
  end
end
NOTE: remember the polymorphic nature of the flexifield model, only drop the ff tables if you are dropping the last of the extended models


TODO:
DONE: partials for flexifield definitions forms
DONE: partials for flexifield data entry

NOTE: currently this version requires rails v 2.3.2

Example
=======

class Content < ActiveRecord::Base
  has_flexiblefields
end
where Content has title and body fields

Given a flexifield_def of say:
  flexdef1:
    id: 1
    name: Recipe

and flexifield_def_entries of:
  ffd1e1:
    flexifield_def_id: 1
    flexifield_name: ffs_01
    flexifield_alias: preparation_time
    flexifield_tooltip: Enter time in decimal hours or minutes
    flexifield_order: 1

  ffd1e2:
    flexifield_def_id: 1
    flexifield_name: ffs_02
    flexifield_alias: cooking_time
    flexifield_tooltip: Enter time in H hours M minutes
    flexifield_order: 2

Then this saves the additional content with the main content...
  r = Content.create! :title => 'Roast Pork Belly', :body => 'Ingredients: ...'
  r.ff_def= 1
  r.set_ff_value 'preparation_time', '25 minutes'
  r.set_ff_value 'cooking_time', '2 hours 30 minutes'
  r.assign_ff_values {:preparation_time =>  '25 minutes', :cooking_time => '2 hours 30 minutes'}
  r.get_ff_value(:preparation_time) # -> '25 minutes'
and
  r.ff_aliases # -> ["preparation_time","cooking_time"]
  r.ff_fields  # -> ["ffs_01","ffs_02"]
and
  r.to_ff_alias 'ffs_01'       # -> "preparation time"
  r.to_ff_field 'cooking_time' # -> "ffs_02"

But forms that save to ffs_NN directly will also work.

Copyright (c) 2009 Guy Boertje, released under the MIT license

About

Rails plugin. Adds extra fields in a has one relationship to an existing model - includes definition and definition entries to label extra fields

License:MIT License