Class: Attached::Attatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/attached/attatcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options = {}) ⇒ Attatcher

Returns a new instance of Attatcher.



9
10
11
12
13
# File 'lib/attached/attatcher.rb', line 9

def initialize(klass, name, options = {})
  @klass = klass
  @name = name
  @options = options
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/attached/attatcher.rb', line 5

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/attached/attatcher.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/attached/attatcher.rb', line 7

def options
  @options
end

Class Method Details

.define(klass, name, options = {}) ⇒ Object



15
16
17
# File 'lib/attached/attatcher.rb', line 15

def self.define(klass, name, options = {})
  new(klass, name, options).define
end

Instance Method Details

#defineObject

Define the model methods required to run.

Usage:

attacher.define


25
26
27
28
29
30
31
32
33
34
# File 'lib/attached/attatcher.rb', line 25

def define
  saving
  destroying
  getters
  setters
  query
  url
  validations
  flusher
end

#destroyingObject

Define the destroying callbacks.

Usage:

attacher.destroying


56
57
58
59
60
61
62
# File 'lib/attached/attatcher.rb', line 56

def destroying
  name = @name
  @klass.send(:before_destroy) do
    logger.info "[attached] destroying #{name}"
    send(name).destroy
  end
end

#flusherObject

Flush validations.

Usage:

attacher.flusher


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/attached/attatcher.rb', line 146

def flusher
  name = @name
  [:before_validation,:after_validation].each do |operation|
    @klass.send(operation) do

      %w(size extension identifier).each do |attribute|
        if errors.include?(:"#{name}_#{attribute}")
          errors[:"#{name}_#{attribute}"].each do |message|
            errors.add(name, message)
          end
          errors[:"#{name}_#{attribute}"].clear
        end
      end
    end
  end
end

#gettersObject

Define the getter.

Usage:

attacher.getters


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/attached/attatcher.rb', line 70

def getters
  name = @name
  options = @options
  @klass.send(:define_method, name) do
    ivar = :"@_attachment_#{name}"
    attachment = instance_variable_get(ivar)

    if attachment.nil?
      attachment = Attachment.new(name, self, options)
      instance_variable_set(ivar, attachment)
    end

    return attachment
  end
end

#queryObject

Define the query.

Usage:

attacher.getters


105
106
107
108
109
110
# File 'lib/attached/attatcher.rb', line 105

def query
  name = @name
  @klass.send(:define_method, "#{name}?") do
    send(name).attached?
  end
end

#savingObject

Define the saving callbacks.

Usage:

attacher.saving


42
43
44
45
46
47
48
# File 'lib/attached/attatcher.rb', line 42

def saving
  name = @name
  @klass.send(:before_save) do
    logger.info "[attached] saving #{name}"
    send(name).save
  end
end

#settersObject

Define the setter.

Usage:

attacher.setters


92
93
94
95
96
97
# File 'lib/attached/attatcher.rb', line 92

def setters
  name = @name
  @klass.send(:define_method, "#{name}=") do |file|
    send(name).assign(file)
  end
end

#urlObject

Define the URL.

Usage:

attacher.getters


118
119
120
121
122
123
# File 'lib/attached/attatcher.rb', line 118

def url
  name = @name
  @klass.send(:define_method, "#{name}_url=") do |url|
    send(name).url = url
  end
end

#validationsObject

Forward validations.

Usage:

attacher.validations


131
132
133
134
135
136
137
138
# File 'lib/attached/attatcher.rb', line 131

def validations
  name = @name
  @klass.send(:validates_each, name) do |record, attr, value|
    record.send(name).errors.each do |error|
      record.errors.add(name, error)
    end
  end
end