Module: RedmineCrm::ActsAsDraftable::InstanceMethods

Defined in:
lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#clear_draftObject



140
141
142
143
144
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 140

def clear_draft
  if last_draft && last_draft.destroy
    self.draft_id = nil
  end
end

#draftObject



108
109
110
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 108

def draft
  Draft.find_by_id(draft_id)
end

#dump_to_draftObject



127
128
129
130
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 127

def dump_to_draft
  instance_values.default = nil
  Marshal.dump(instance_values)
end

#last_draftObject



118
119
120
121
122
123
124
125
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 118

def last_draft
  @last_draft ||= RedmineCrm::ActsAsDraftable::Draft.where(
    target_type: self.class.name,
    parent_type: parent_for_draft.try(:class).try(:to_s),
    parent_id:   parent_for_draft.try(:id),
    user_id:     User.current.try(:id)
  ).last
end

#load_from_draft(string) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 132

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

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

#parent_for_draftObject



112
113
114
115
116
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 112

def parent_for_draft
  return self if id.present?

  self.class.draft_parent.present? ? send(self.class.draft_parent) : self
end

#save_draftObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 87

def save_draft
  return false unless new_record? || changed?

  draft = self.draft || Draft.new
  draft.data = dump_to_draft
  draft.user_id = User.current.try(:id)
  draft.parent = parent_for_draft
  draft.target_type = self.class.name

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

#update_draft(attributes) ⇒ Object



101
102
103
104
105
106
# File 'lib/redmine_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 101

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