Sha256: 69f42cd5bc40daa147db77e9f392053196e5eae4ae6c5458e8810b473beadb71

Contents?: true

Size: 1004 Bytes

Versions: 2

Compression:

Stored size: 1004 Bytes

Contents

# kind of the evolution of the FormStruct

module Edifice
  class FormModel
    include ActiveModel::Validations
    include ActiveModel::Conversion
    extend  ActiveModel::Naming
    extend ActiveModel::Callbacks
    define_model_callbacks :save
    
    # more or less the same as activerecord's one
    class RecordInvalid < Exception
      attr_reader :record
      def initialize(record)
        @record = record
        errors = @record.errors.full_messages.join(", ")
        super(errors)
      end
    end
    
    def initialize(attributes = {})
        attributes.each { |n, v| send("#{n}=", v) if respond_to?("#{n}=") }
    end
    
    # default implementation, override as necessary
    def save
      run_callbacks :save do
        valid?
      end
    end
    
    def save!
      save || raise(RecordInvalid.new(self))
    end
    
    def self.create(attributes = {})
      form = new(attributes)
      form.save
      form
    end
    
    def persisted?
      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
edifice-0.10.3 lib/edifice/form_model.rb
edifice-0.10.2 lib/edifice/form_model.rb