Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 97 lines (76 sloc) 3.064 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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
Something went wrong with that request. Please try again.