Sha256: 1bc8d7ffc2f48dd8b4822685484d2fc2aa57922f87a2597cc3765aa26003b65d

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

require 'active_support/concern'

module Ardm
  module Ar
    module Persistence
      extend ActiveSupport::Concern

      included do
        class_attribute :raise_on_save_failure, instance_accessor: true
        self.raise_on_save_failure = false
      end

      module ClassMethods
        def update(*a)
          options = dump_properties_hash(a.first)
          options = dump_associations_hash(options)
          assert_valid_attributes(options)
          update_all(options) != 0
        end

        def update!(*a)
          options = dump_properties_hash(a.first)
          options = dump_associations_hash(options)
          assert_valid_attributes(options)
          update_all(options) != 0
        end

        def destroy(*a)
          destroy_all
        end

        def destroy!(*a)
          delete_all
        end
      end

      def destroy
        self.class.delete(self.send(self.class.primary_key))
      end

      def new?
        new_record?
      end

      def saved?
        !new_record?
      end

      def save_self(run_callbacks=true)
        save(run_callbacks)
      end

      def save(run_callbacks=true)
        unless run_callbacks
          raise Ardm::NotImplemented, "ActiveRecord doesn't support saving without callbacks"
        end

        super() # no args!
      end

      def save!(*args)
        save(*args) || (raise_on_save_failure && raise(Ardm::SaveFailureError, "Save Failed"))
      end

      def update(*a)
        if a.size == 1
          update_attributes(*a)
        else
          super
        end
      end

      def update!(*a)
        update_attributes!(*a)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ardm-0.4.0.ar427 lib/ardm/ar/persistence.rb
ardm-0.4.0 lib/ardm/ar/persistence.rb
ardm-0.3.2 lib/ardm/ar/persistence.rb
ardm-0.3.1 lib/ardm/ar/persistence.rb
ardm-0.3.0 lib/ardm/ar/persistence.rb