Module: Anaconda::Model::ClassMethods

Defined in:
lib/anaconda/anaconda_for.rb

Instance Method Summary collapse

Instance Method Details

#anaconda_fields_for(anaconda_column) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/anaconda/anaconda_for.rb', line 50

def anaconda_fields_for( anaconda_column )
  if self.anaconda_columns.include? anaconda_column.to_sym
    Anaconda::FieldSuffixes.collect do |suffix|
      "#{anaconda_column}_#{suffix}".to_sym
    end
  else
    raise "#{anaconda_column} not configured for anaconda. Misspelling or did you forget to add the anaconda_for call for this field?"
  end
end

#anaconda_fields_for_all_columnsObject



60
61
62
63
64
# File 'lib/anaconda/anaconda_for.rb', line 60

def anaconda_fields_for_all_columns
  self.anaconda_columns.collect do |column|
    anaconda_fields_for column
  end.flatten
end

#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
42
43
44
45
46
47
48
# 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: "#{anaconda_column}_anaconda_default_base_key".to_sym,
    host: false,
    protocol: "http",
    expiry_length: 1.hour,
    remove_previous_s3_files_on_change: true,
    remove_previous_s3_files_on_destroy: true
  )
  
  self.after_commit :anaconda_remove_previous_s3_files_on_change_or_destroy
end