Module: YeshouaCrm::ActsAsDraftable::Base

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

Constant Summary collapse

ALLOWED_DRAFT_OPTIONS =
[:parent]

Instance Method Summary collapse

Instance Method Details

#draftable?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/yeshoua_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 28

def draftable?
  false
end

#rcrm_acts_as_draftable(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yeshoua_crm/acts_as_draftable/rcrm_acts_as_draftable.rb', line 32

def rcrm_acts_as_draftable(options = {})
  raise ArgumentError unless options.is_a?(Hash)
  raise ArgumentError unless options.keys.all? { |k| ALLOWED_DRAFT_OPTIONS.include?(k) }

  class_attribute :draft_parent

  if options[:parent]
    parent_class = self.reflect_on_all_associations(:belongs_to).find { |a| a.name == options[:parent] }.try(:klass)
    raise ArgumentError unless parent_class

    unless parent_class.method_defined?(:drafts)
      parent_class.class_eval do
        def drafts(user)
          Draft.where(user: user, parent: self)
        end

        def self.child_drafts(user)
          Draft.where(user: user, parent_type: self.base_class.name)
        end
      end
    end

    self.draft_parent = options[:parent]
  end

  attr_accessor :draft_id
  after_create :clear_draft

  extend YeshouaCrm::ActsAsDraftable::ClassMethods
  include YeshouaCrm::ActsAsDraftable::InstanceMethods
end