Module: Anaconda::Model::InstanceMethods

Defined in:
lib/anaconda/anaconda_for.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/anaconda/anaconda_for.rb', line 70

def method_missing(method, *args, &block)
  checking_column = checking_method = nil
  if self.class.anaconda_columns.present? && self.class.anaconda_columns.any? do |column|
      checking_column = column
      Anaconda::MagicMethods.any? do |magic_method|
        checking_method = magic_method
        "#{column.to_s}_#{magic_method.to_s}" == method.to_s
      end
    end
    case checking_method
    when :url
      anaconda_url(checking_column, *args)
    when :download_url
      anaconda_download_url(checking_column, *args)
    when :anaconda_default_base_key
      anaconda_default_base_key_for(checking_column)
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#anaconda_form_data_for(anaconda_column) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/anaconda/anaconda_for.rb', line 102

def anaconda_form_data_for( anaconda_column )
  if self.anaconda_columns.include? anaconda_column.to_sym
    
    options = self.class.anaconda_options[anaconda_column.to_sym].dup
    
    options[:base_key] = self.send(options[:base_key].to_s) if options[:base_key].kind_of? Symbol
    uploader  = Anaconda::S3Uploader.new(options)
    fields = uploader.fields
    fields[:post_url] = uploader.url
    fields
  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_form_data_for_all_columnsObject



94
95
96
97
98
99
100
# File 'lib/anaconda/anaconda_for.rb', line 94

def anaconda_form_data_for_all_columns
  form_data = {}
  self.anaconda_columns.each do |column|
    form_data[column.to_sym] = anaconda_form_data_for column
  end
  form_data
end

#anaconda_options_for(anaconda_column) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/anaconda/anaconda_for.rb', line 117

def anaconda_options_for( anaconda_column )
  if self.class.anaconda_columns.include? anaconda_column.to_sym
    self.anaconda_options[anaconda_column].inject({}) do |hash, (k, v)|
      if v.kind_of? Proc
        hash[k] = self.instance_exec(&v)
      else
        hash[k] = v
      end
      hash
    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

#import_file_to_anaconda_column(file, column_name, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/anaconda/anaconda_for.rb', line 132

def import_file_to_anaconda_column(file, column_name, options = {})
  options = options.reverse_merge(self.anaconda_options_for( column_name ))
  logger.debug "Options:"
  logger.debug(options)
  
  aws = Fog::Storage.new({:provider => 'AWS', :aws_access_key_id => options[:aws_access_key], :aws_secret_access_key => options[:aws_secret_key], :path_style => options[:aws_use_path_style]})
  key = send(options[:base_key])

  bucket = aws.directories.new(key: options[:aws_bucket])
  file_handle = open(file)
  aws_file = bucket.files.create(
    :key    => key,
    :body   => file_handle,
    :public => options[:acl] == "public-read"
  )
  
  self.update_attributes(
    "#{column_name}_filename" => Pathname.new(file).basename,
    "#{column_name}_file_path" => key,
    "#{column_name}_size" => file_handle.size,
    "#{column_name}_original_filename" => Pathname.new(file).basename,
    "#{column_name}_stored_privately" => !(options[:acl] == "public-read"),
    "#{column_name}_type" => ""
  )

end