lib/ninja_model/persistence.rb in ninja-model-0.7.3 vs lib/ninja_model/persistence.rb in ninja-model-0.8.0

- old
+ new

@@ -1,61 +1,64 @@ module NinjaModel - class Base - define_model_callbacks :save, :create, :update, :destroy - end - module Persistence + extend ActiveSupport::Concern - def save(*) - run_callbacks :save do - result = new_record? ? create : update - changed_attributes.clear if result - result - end + included do + define_model_callbacks :save, :create, :update, :destroy end - def create - run_callbacks :create do - if self.class.adapter.create(self) - @persisted = true + module InstanceMethods + def save(*) + run_callbacks :save do + result = new_record? ? create : update + changed_attributes.clear if result + result end - @persisted end - end - def update - run_callbacks :update do - self.class.adapter.update(self) + def create + run_callbacks :create do + if self.class.adapter.create(self) + @persisted = true + end + @persisted + end end - end - def new_record? - !@persisted - end + def update + run_callbacks :update do + self.class.adapter.update(self) + end + end - def destroyed? - @destroyed - end + def new_record? + !@persisted + end - def persisted? - @persisted && !destroyed? - end + def destroyed? + @destroyed + end - def destroy - run_callbacks :destroy do - if self.class.adapter.destroy(self) - @destroyed = true + def persisted? + @persisted && !destroyed? + end + + def destroy + run_callbacks :destroy do + if self.class.adapter.destroy(self) + @destroyed = true + end + @destroyed end - @destroyed end - end - def reload - self.class.adapter.reload(self) - end + def reload + self.class.adapter.reload(self) + end - def update_attributes(attributes) - self.attributes = attributes - save + def update_attributes(attributes) + self.attributes = attributes + save + end end end end