Module: Conscript::ActiveRecord

Defined in:
lib/conscript/orm/activerecord.rb

Instance Method Summary collapse

Instance Method Details

#register_for_draft(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/conscript/orm/activerecord.rb', line 8

def register_for_draft(options = {})

  cattr_accessor :conscript_options, :instance_accessor => false do
    {
      associations: [],
      ignore_attributes: [self.primary_key, 'type', 'created_at', 'updated_at', 'draft_parent_id', 'is_draft']
    }
  end

  self.conscript_options.each_pair {|key, value| self.conscript_options[key] = Array(value) | Array(options[key]) }
  self.conscript_options[:associations].map!(&:to_sym)
  self.conscript_options[:ignore_attributes].map!(&:to_s)

  default_scope { where(is_draft: false) }

  belongs_to :draft_parent, class_name: self
  has_many :drafts, conditions: {is_draft: true}, class_name: self, foreign_key: :draft_parent_id, dependent: :destroy, inverse_of: :draft_parent

  before_save :check_no_drafts_exist

  # Prevent deleting CarrierWave uploads which may be used by other instances. Uploaders must be mounted beforehand.
  if self.respond_to? :uploaders
    self.uploaders.keys.each {|attribute| skip_callback :commit, :after, :"remove_#{attribute}!" }
    after_commit :clean_uploaded_files_for_draft!, :on => :destroy
  end

  class_eval <<-RUBY
    def self.drafts
      where(is_draft: true)
    end

    def save_as_draft!
      raise Conscript::Exception::AlreadyDraft if is_draft?
      draft = new_record? ? self : dup(include: self.class.conscript_options[:associations])
      draft.is_draft = true
      draft.draft_parent = self unless new_record?
      self.class.base_class.unscoped { draft.save! }
      draft
    end

    def publish_draft
      raise Conscript::Exception::NotADraft unless is_draft?
      return self.update_attribute(:is_draft, false) if !draft_parent_id
      ::ActiveRecord::Base.transaction do
        draft_parent.assign_attributes attributes_to_publish, without_protection: true

        self.class.conscript_options[:associations].each do |association|
          case reflections[association].macro
            when :has_many
              draft_parent.send(association.to_s + "=", self.send(association).collect {|child| child.dup })
          end
        end

        draft_parent.drafts.destroy_all
        draft_parent.save!
      end
      draft_parent
    end

    def uploader_store_param
      draft_parent_id.nil? ? to_param : draft_parent.to_param
    end

    private
      def check_no_drafts_exist
        drafts.count == 0
      end

      def attributes_to_publish
        attributes.reject {|attribute| self.class.conscript_options[:ignore_attributes].include?(attribute) }
      end

      # Clean up CarrierWave uploads if there are no other instances using the files.
      #
      def clean_uploaded_files_for_draft!
        self.class.uploaders.keys.each do |attribute|
          filename = attributes[attribute.to_s]
          self.send("remove_" + attribute.to_s + "!") if !draft_parent_id or draft_parent.drafts.where(attribute => filename).count == 0
        end
      end
  RUBY
end