Module: Roachclip::ClassMethods

Defined in:
lib/roachclip.rb

Instance Method Summary collapse

Instance Method Details

#roachclip(name, options = {}) ⇒ Object

Raises:



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
# File 'lib/roachclip.rb', line 26

def roachclip name, options = {}
  self.attachment name

  raise InvalidAttachment unless attachment_names.include?(name)

  path = options.delete(:path) || "/gridfs/fs/%s-%s"
  self.roaches << {:name => name, :options => options}

  options[:styles] ||= {}
  options[:styles].each { |k,v| self.attachment "#{name}_#{k}"}

  before_save :process_roaches
  before_save :destroy_nil_roaches

  self.send(:define_method, "#{name}_path") do
    time = self.attributes['updated_at'] || Time.now
    time = time.to_i
    (path % [self.send(name).id.to_s, time]).chomp('-')
  end
 
  options[:styles].each do |k,v|
    self.send(:define_method, "#{name}_#{k}_path") do
      time = self.attributes['updated_at'] || Time.now
      time = time.to_i
      (path % [self.send("#{name}_#{k}").id.to_s, time]).chomp('-')
    end
  end
end

#validates_roachclip(*args) ⇒ Object



55
56
57
# File 'lib/roachclip.rb', line 55

def validates_roachclip(*args)
  add_validations(args, Roachclip::Validations::ValidatesPresenceOf)
end