Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing data from dynamic forms

I am working on dynamic form generator. Someone can create fields like: string, text, boolean, number, file etc.

Are there any gems or guidelines for storing data from such dynamic forms?

I mean I can create a number of tables for each datatype, or I can store all of them as a TEXT with flag wich type it should be converted.

UPD

or I'd better use nosql here?

like image 481
fl00r Avatar asked Feb 22 '26 08:02

fl00r


2 Answers

I believe Mongodb is a right choice for this application, since it doesn't enforce any schema, its a good choice for arbitrary data.

As well, it does support all the datatype you have expected. so it is easy.

Have a form collection which looks like this (Ruby Mongoid code)

  class XForm
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

       field :name, :type => String
       field :user, :type => BSON::ObjectId     

       embeds_many :formfields
  end

and

 class Formfields
  include Mongoid::Document

     field :name, :type => String
     field :kind, :type => String
     #field :value, :type => String -> dont add it in formfields, make it dynamic sine the type varies

  embedded_in :xform
  end

To add value field as a dynamic field, you need to enable allow_dynamic_fields: true in mongoid.yml

and create a new field like this

  form = XForm.new(:name=>'test form',:user => current_user.id)
   #for integer field
   form.formfields << Formfields.new(:name => "Age",:kind=>"Integer", :value => 21)
   #for bool field
   form.formfields << Formfields.new(:name => "isMarried",:kind=>"Boolean",:value => true)
   #for string field
   form.formfields << Formfields.new(:name => "name",:kind=>"String",:value => "ram")

Hope this helps

like image 111
RameshVel Avatar answered Feb 24 '26 21:02

RameshVel


I like this approach.

class User < ActiveRecord::Base
  [:field_1, :field_2, :field_3].each do |method|
    define_method method do
      workable_arbitrary_content[method]
    end

    define_method "#{method}=" do |value|
      data = workable_arbitrary_content.merge(method => value)
      self.arbitrary_content = YAML.dump(data)
    end
  end

  private
    def workable_arbitrary_content
      YAML.load(arbitrary_content || "") || {}
    end
end

I this case you create 3 virtual fields that is being saved as YAML. Create a field in the users called arbitrary_content that is of type text.

Here are some specs for the code above.

describe User do
  context "virtual fields" do
    before(:each) do
      @user = User.new
    end

    it "should be able to handle strings" do
      @user.field_1 = "Data"
      @user.field_1.should eq("Data")
    end

    it "should be able to handle integers" do
      @user.field_2 = 1
      @user.field_2.should eq(1)
    end

    it "should be able to handle symbols" do
      @user.field_3 = :symbol
      @user.field_3.should eq(:symbol)
    end

    it "should be possible to override a field" do
      @user.field_3 = :symbol
      @user.field_3 = "string"

      @user.field_3.should eq("string")
    end

    it "should be possible to set more then one field" do
      @user.field_1 = :symbol
      @user.field_2 = "string"

      @user.field_1.should eq(:symbol)
      @user.field_2.should eq("string")
    end
  end
end
like image 27
Linus Oleander Avatar answered Feb 24 '26 21:02

Linus Oleander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!