Module: Refile::Neo4j::Attachment

Includes:
Attachment
Defined in:
lib/refile/neo4j/attachment.rb

Instance Method Summary collapse

Instance Method Details

#attachment(name, raise_errors: false, destroy: true, **options) ⇒ void

This method returns an undefined value.

Attachment method which hooks into Neo4j::ActiveNode models

Parameters:

  • destroy (true, false) (defaults to: true)

    Whether to remove the stored file if its model is destroyed

See Also:

  • Attachment#attachment


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
# File 'lib/refile/neo4j/attachment.rb', line 11

def attachment(name, raise_errors: false, destroy: true, **options)
  super(name, raise_errors: raise_errors, **options)

  attacher = :"#{name}_attacher"
  property :"#{name}_id", type: String

  validate do
    if send(attacher).present?
      send(attacher).valid?
      send(attacher).errors.each do |error|
        errors.add(name, error)
      end
    end
  end

  define_method "#{name}=" do |value|
    send("#{name}_id_will_change!")
    super(value)
  end

  define_method "remove_#{name}=" do |value|
    send("#{name}_id_will_change!")
    super(value)
  end

  define_method "remote_#{name}_url=" do |value|
    send("#{name}_id_will_change!")
    super(value)
  end

  before_save do
    send(attacher).store!
  end

  after_destroy do
    send(attacher).delete! if destroy
  end
end