Module: Anaconda::Model::ClassMethods

Defined in:
lib/anaconda/anaconda_for.rb

Instance Method Summary collapse

Instance Method Details

#anaconda_for(anaconda_column, options = {}) ⇒ Object



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
# File 'lib/anaconda/anaconda_for.rb', line 10

def anaconda_for( anaconda_column, options = {})
  send :include, InstanceMethods
  
  begin
    self.anaconda_columns.present?
  rescue NoMethodError
    class_attribute :anaconda_columns
  end
  self.anaconda_columns = Array.new unless self.anaconda_columns.kind_of? Array
  if self.anaconda_columns.include? anaconda_column.to_sym
    raise AnacondaError, "anaconda_for cannot be called multiple times for the same field"
  end
  self.anaconda_columns << anaconda_column.to_sym
  # Class.anaconda_columns is now an array of symbols

  class_attribute 
  begin
    self.anaconda_options.present?
  rescue NoMethodError
    class_attribute :anaconda_options
  end
  self.anaconda_options = Hash.new unless self.anaconda_options.kind_of? Hash
  self.anaconda_options[anaconda_column.to_sym] = options.reverse_merge(
    aws_access_key_id: Anaconda.aws[:aws_access_key],
    aws_secret_access_key: Anaconda.aws[:aws_secret_key],
    bucket: Anaconda.aws[:aws_bucket],
    acl: "public-read",
    max_file_size: 500.megabytes,
    allowed_file_types: [],
    base_key: "#{self.to_s.pluralize.downcase}/#{anaconda_column.to_s.pluralize}/#{(0...32).map{(65+rand(26)).chr}.join.downcase}"
  )
end