Sha256: 60e04c76746f934a4a5004ca5cda2130c4fe3a28afc28221b5fd21ee36727bfa

Contents?: true

Size: 1.18 KB

Versions: 9

Compression:

Stored size: 1.18 KB

Contents

module NinjaModel
  module Persistence
    extend ActiveSupport::Concern

    included do
      define_model_callbacks :save, :create, :update, :destroy
    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

9 entries across 9 versions & 1 rubygems

Version Path
ninja-model-0.9.6 lib/ninja_model/persistence.rb
ninja-model-0.9.5 lib/ninja_model/persistence.rb
ninja-model-0.9.4 lib/ninja_model/persistence.rb
ninja-model-0.9.3 lib/ninja_model/persistence.rb
ninja-model-0.9.2 lib/ninja_model/persistence.rb
ninja-model-0.9.1 lib/ninja_model/persistence.rb
ninja-model-0.9.0 lib/ninja_model/persistence.rb
ninja-model-0.8.1 lib/ninja_model/persistence.rb
ninja-model-0.8.0 lib/ninja_model/persistence.rb