Sha256: 8501652e938b5766c8c72033c2497644295846c6889487c40cf1385008418e72

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 KB

Contents

module Drafting
  module InstanceMethods
    def save_draft(user=nil)
      return false unless self.new_record?

      draft = Draft.find_by_id(self.draft_id) || Draft.new

      draft.data = dump_to_draft
      draft.target_type = self.class.name
      draft.user = user
      draft.parent = self.send(self.class.draft_parent) if self.class.draft_parent

      result = draft.save
      self.draft_id = draft.id if result
      result
    end

    def update_draft(user, attributes)
      with_transaction_returning_status do
        assign_attributes(attributes)
        save_draft(user)
      end
    end

    # Override this two methods if you want to change the way to dump/load data
    def dump_to_draft
      Marshal.dump(instance_values)
    end

    def load_from_draft(string)
      values = Marshal.load(string)

      values.each do |name, value|
        instance_variable_set("@#{name}", value)
      end
    end

  private

    def clear_draft
      if draft = Draft.find_by_id(self.draft_id)
        self.draft_id = nil if draft.destroy
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
drafting-0.4.2 lib/drafting/instance_methods.rb
drafting-0.4.1 lib/drafting/instance_methods.rb
drafting-0.4.0 lib/drafting/instance_methods.rb
drafting-0.3.1 lib/drafting/instance_methods.rb
drafting-0.3.0 lib/drafting/instance_methods.rb
drafting-0.2.0 lib/drafting/instance_methods.rb
drafting-0.1.1 lib/drafting/instance_methods.rb
drafting-0.1.0 lib/drafting/instance_methods.rb