2012-04-10 14:06:46 +00:00
|
|
|
require 'digest/md5'
|
|
|
|
|
2013-01-24 08:18:29 +00:00
|
|
|
class Store < ApplicationModel
|
2012-10-14 21:00:33 +00:00
|
|
|
store :preferences
|
|
|
|
belongs_to :store_object, :class_name => 'Store::Object'
|
|
|
|
belongs_to :store_file, :class_name => 'Store::File'
|
|
|
|
validates :filename, :presence => true
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.add(data)
|
|
|
|
data = data.stringify_keys
|
|
|
|
|
|
|
|
# lookup store_object.id
|
2013-01-24 08:18:29 +00:00
|
|
|
store_object = Store::Object.create_if_not_exists( :name => data['object'] )
|
2012-04-10 14:06:46 +00:00
|
|
|
data['store_object_id'] = store_object.id
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
# check if record already exists
|
|
|
|
# store = Store.where( :store_object_id => store_object.id, :o_id => data['o_id'], ).first
|
|
|
|
# if store != nil
|
|
|
|
# return store
|
|
|
|
# end
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
# check real store
|
|
|
|
md5 = Digest::MD5.hexdigest( data['data'] )
|
2013-01-24 08:18:29 +00:00
|
|
|
data['size'] = data['data'].to_s.bytesize
|
2012-04-10 14:06:46 +00:00
|
|
|
|
|
|
|
file = Store::File.where( :md5 => md5 ).first
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
# store attachment
|
|
|
|
if file == nil
|
|
|
|
file = Store::File.create(
|
|
|
|
:data => data['data'],
|
|
|
|
:md5 => md5
|
|
|
|
)
|
|
|
|
end
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
data['store_file_id'] = file.id
|
|
|
|
|
|
|
|
# not needed attributes
|
|
|
|
data.delete('data')
|
|
|
|
data.delete('object')
|
|
|
|
|
|
|
|
# store meta data
|
|
|
|
store = Store.create(data)
|
|
|
|
|
|
|
|
return store
|
|
|
|
end
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.list(data)
|
|
|
|
# search
|
2013-05-10 21:48:29 +00:00
|
|
|
store_object_id = Store::Object.lookup( :name => data[:object] )
|
|
|
|
stores = Store.where( :store_object_id => store_object_id, :o_id => data[:o_id].to_i ).
|
2012-04-10 14:06:46 +00:00
|
|
|
order('created_at ASC, id ASC')
|
|
|
|
return stores
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.remove(data)
|
|
|
|
# search
|
2013-05-10 21:48:29 +00:00
|
|
|
store_object_id = Store::Object.lookup( :name => data[:object] )
|
|
|
|
stores = Store.where( :store_object_id => store_object_id ).
|
2012-04-10 14:06:46 +00:00
|
|
|
where( :o_id => data[:o_id] ).
|
|
|
|
order('created_at ASC, id ASC')
|
|
|
|
stores.each do |store|
|
|
|
|
store.destroy
|
|
|
|
end
|
2012-12-02 10:18:55 +00:00
|
|
|
return true
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-01-24 08:18:29 +00:00
|
|
|
class Object < ApplicationModel
|
2012-10-14 21:00:33 +00:00
|
|
|
validates :name, :presence => true
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2013-01-24 08:18:29 +00:00
|
|
|
class File < ApplicationModel
|
2012-04-10 14:06:46 +00:00
|
|
|
before_validation :add_md5
|
|
|
|
|
|
|
|
private
|
|
|
|
def add_md5
|
|
|
|
self.md5 = Digest::MD5.hexdigest( self.data )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|