Sha256: 140268e010443764b3d977fb8f07d7061770e6f46ccb48735d6b9167f9fc7bd5

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

module NinjaModel
  module Persistence
    extend ActiveSupport::Concern

    included do
      define_model_callbacks :save, :create, :update, :destroy, :reload
    end

    module InstanceMethods
      def save(*)
        run_callbacks :save do
          result = new_record? ? create : update
          changed_attributes.clear if result
          result
        end
      end

      def create
        run_callbacks :create do
          if self.class.adapter.create(self)
            @persisted = true
          end
          @persisted
        end
      end

      def update
        run_callbacks :update do
          self.class.adapter.update(self)
        end
      end

      def new_record?
        !@persisted
      end

      def destroyed?
        @destroyed
      end

      def persisted?
        @persisted && !destroyed?
      end

      def destroy
        run_callbacks :destroy do
          if self.class.adapter.destroy(self)
            @destroyed = true
          end
          @destroyed
        end
      end

      def reload
        self.class.adapter.reload(self)
      end

      def update_attributes(attributes)
        self.attributes = attributes
        save
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ninja-model-1.0.5 lib/ninja_model/persistence.rb
ninja-model-1.0.4 lib/ninja_model/persistence.rb
ninja-model-1.0.3 lib/ninja_model/persistence.rb
ninja-model-1.0.2 lib/ninja_model/persistence.rb
ninja-model-1.0.1 lib/ninja_model/persistence.rb
ninja-model-1.0.0 lib/ninja_model/persistence.rb