Sha256: 9bfd16bf843a9a4c678b0de64c56247609d071533b2065cb3f5d6c64f0a0bd63

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require 'forwardable'
require 'ostruct'
require 'active_model'
require 'freeform/form/form_input_key'
require 'freeform/form/model'
require 'freeform/form/nested'
require 'freeform/form/property'
require 'freeform/form/validation'

module FreeForm
  class FormInvalid < StandardError; end
  class Form
    extend Forwardable
    extend ActiveModel::Naming
    extend ActiveModel::Callbacks
    include ActiveModel::Conversion
    include ActiveModel::Validations
    include FreeForm::FormInputKey
    include FreeForm::Model
    include FreeForm::Nested
    include FreeForm::Property
    include FreeForm::Validation

    # Default Validations
    #----------------------------------------------------------------------------
    validate_nested_forms

    # Instance Methods
    #----------------------------------------------------------------------------
    # Required for ActiveModel
    def persisted?; false end

    def initialize(h={})
      h.each {|k,v| send("#{k}=",v)}
      initialize_child_models
    end

    def save(*args)
      if valid?
        before_save
        persist_models
        after_save
        true
      else
        false
      end
    end

    def save!(*args)
      if valid?
        save
      else
        raise FreeForm::FormInvalid, "form invalid." 
      end
    end

    def destroy
      models.each { |m| m.destroy }
    end

    def marked_for_destruction?
      respond_to?(:_destroy) ? _destroy : false
    end

    def before_save
    end

    def after_save
    end

  private
    def initialize_child_models
      self.class.child_models.each do |c|
        send("initialize_#{c}")
      end
    end

    def persist_models
      models.each { |m| m.destroy if m.marked_for_destruction? }
      # We skip validation for underlying models, since we've already run our validation.
      models.each { |m| m.save(:validate => false) unless m.marked_for_destruction? }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
freeform-2.0.2 lib/freeform/form.rb
freeform-2.0.1 lib/freeform/form.rb