Sha256: 799d203c6b3157106ad15af98d5cc1ccb6952d19c4816663bc5cba56eba63731

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# kind of the evolution of the FormStruct

module Edifice
  module Forms
    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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
edifice-forms-0.5.1 lib/edifice-forms/form_model.rb
edifice-forms-0.5.0 lib/edifice-forms/form_model.rb
edifice-forms-0.4.0 lib/edifice-forms/form_model.rb