Sha256: 278217d0c8a7c596647e21cb56434b8058ddccfeeeefcd95e02a87c02ef3c94d

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

require 'virtus'

# TODO: Determine the specific modules that needs to be loaded instead of rails/all
require 'rails/all'

module TransForms
  class FormBase
    include Virtus.model

    extend ActiveModel::Naming
    include ActiveModel::Conversion
    include ActiveModel::Validations
    include ActiveModel::Validations::Callbacks

    include TransForms::Callbacks

    def save
      valid? && run_transaction
    end

    def save!
      save || (raise ActiveRecord::RecordInvalid)
    end

    # ActiveModel support
    def persisted?; false end
    def to_key; nil end

  protected
    def self.transaction(&block)
      class_attribute :_transaction
      self._transaction = block
    end

    def run_transaction
      ActiveRecord::Base.transaction do
        instance_eval &_transaction
        true
      end
    rescue ActiveRecord::ActiveRecordError => e
      # Triggers callback
      after_save_on_error_callback e
      false
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trans_forms-0.1.0 lib/trans_forms/form_base.rb