ActsAsContactable ================= Easy administration of per-model contact fields. This Plugin allows any model to have a set of contact_fields, each having the following fields: # Table name: contact_fields # # id :integer(11) not null, primary key # created_on :datetime not null # name :string(255) default(""), not null # field_type :string(255) default("text"), not null # required :boolean(1) not null # contact_form_id :integer(11) not null # defaults :text INSTALL: script/generate acts_as_contactable_migration rake db:migrate USAGE: class Article acts_as_contactable end @article = Article.find( params[:id] ) @article.contact_field_list = params[:article][:contact_fields] #destroys previous and adds new fields @article.contact_field_list << params[:article][:contact_fields] # adds new fields # array like [{:name=>'Your name',:field_type=>:text},{:name=>'Your age',:field_type=>:int,:required=>1}] OR if you want to associate some mandatory fields on model creation: class Article acts_as_contactable :preset => [ {:name=>'Name',:field_type=>:text,:required=>true}, {:name=>'Email',:field_type=>:email,:required=>true} ] end ContactFields "Name" and "Email" will be created on Article#afterCreate and will have a flag "mandatory" set to true. You can use this flag to prevent users from deleting these default fields, if you want. @article.destroy_not_mandatory_fields #this doesn't destroy the mandatory ones You can fetch fields according mandatory and required statuses @article.contact_fields.mandatory #the ones you can't delete @article.contact_fields.not_mandatory #the ones you can @article.contact_fields.required #must be filled in @article.contact_fields.optional #can be left blank RECIPIENTS Contact fields are not of much use without recipients for contacts. You can add a recipients field to a table add_column :articles, :recipients, :text @article.recipients = [ {:name => "Ismael Celis", :email => "ismaelct@gmail.com"}, {:name => "Tomás Pollak", :email => "tomas@aardvark.cl"} ] @article.save @article.recipients # -> "Ismael Celis ,Tomás Pollak " @article.parsed_recipients # [ # {:name => "Ismael Celis", :email => "ismaelct@gmail.com"}, # {:name => "Tomás Pollak", :email => "tomas@aardvark.cl"} # ] RHTML In a GUI, to present a form with contact fields for an article, use the contact_field helper: <% form_tag :action => 'send_contact', :id => @article do %> <% for field in @article.contact_fields %>

<%= contact_field 'article',field %>

<% end %> <%= submit_tag 'Send' %> <% end %>