0.0
No commit activity in last 3 years
No release in over 3 years
Objective form is for whoever miss dynamic nested fields adding in form object using nested_form gem by Ryan Bates. Beside that it is lightweight abstraction of form objects for Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

objective_form

Form objects compatible with nested_form

Setup

Just place gem 'objective_form' in your Gemfile and run bundle install command

Usage

To use objective form, just inherit from ObjectiveForm::Base, i.e.

class MyFormObject < ObjectiveForm::Base
end

Defining attributes is rather self-explanatory. You can use validations as well.

class MyFormObject < ObjectiveForm::Base
  attribute :name, String 
  attribute :user_id, Integer      
  attribute :product_ids, Array
  
  validates :name, :presence => true
end

Calling save on form object, checks if object is valid, and then calls persist! method, you need to implement yourself, i.e.

class MyFormObject < ObjectiveForm::Base  
  attribute :user_id, Integer      
  attribute :product_ids, Array
  #...
  
  def persist!
    user = User.find(user_id)
    Product.where(:id => product_ids).each do |product|
      user.observe_product!(product)
    end
  end
end

nested_form

To use nested_form, you need to define pseudo has_many relation on form object, i.e.

class MyFormObject < ObjectiveForm::Base 
  has_many :emails, ObjectiveForm::PseudoRecord.new(:email => String)
end

From now on, it is a matter of passing the same ObjectiveForm::PseudoRecord object as :model_object attribute of link_to_add helper, i.e.

<%= nested_form_for MyFormObject.new, :url => user_emails_path do |f| %>
  <%= f.fields_for :emails do |email_form| %>
    <div class="item">
      <%= email_form.text_field :email %>
      <%= email_form.link_to_remove "Remove this email"%>
    </div>
  <% end %>

  <%= f.link_to_add "Add an email", :emails, :model_object => ObjectiveForm::PseudoRecord.new(:email => String).new %>    
    
  <%= f.submit "Save"%>        
<% end %>