2012-11-20 18:39:19 +00:00
|
|
|
require 'cache'
|
2012-11-18 11:26:24 +00:00
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
class ApplicationModel < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
after_create :cache_delete
|
|
|
|
after_update :cache_delete
|
|
|
|
after_destroy :cache_delete
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
def self.param_cleanup(params)
|
|
|
|
data = {}
|
|
|
|
self.new.attributes.each {|item|
|
|
|
|
if params.has_key?(item[0])
|
|
|
|
# puts 'use ' + item[0].to_s + '-' + params[item[0]].to_s
|
|
|
|
data[item[0].to_sym] = params[item[0]]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# we do want to set this via database
|
|
|
|
data.delete( :updated_at )
|
|
|
|
data.delete( :created_at )
|
|
|
|
data.delete( :updated_by_id )
|
|
|
|
data.delete( :created_by_id )
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
def cache_update(o)
|
|
|
|
# puts 'u ' + self.class.to_s
|
|
|
|
if self.respond_to?('cache_delete') then self.cache_delete end
|
|
|
|
# puts 'g ' + group.class.to_s
|
|
|
|
if o.respond_to?('cache_delete') then o.cache_delete end
|
|
|
|
end
|
|
|
|
def cache_delete
|
2012-04-16 09:45:02 +00:00
|
|
|
key = self.class.to_s + '::' + self.id.to_s
|
2012-07-29 15:27:01 +00:00
|
|
|
Cache.delete( key.to_s )
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
|
|
|
def self.cache_set(data_id, data)
|
2012-04-16 09:45:02 +00:00
|
|
|
key = self.to_s + '::' + data_id.to_s
|
2012-07-29 15:27:01 +00:00
|
|
|
Cache.write( key.to_s, data )
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
|
|
|
def self.cache_get(data_id)
|
2012-04-16 09:45:02 +00:00
|
|
|
key = self.to_s + '::' + data_id.to_s
|
2012-07-29 15:27:01 +00:00
|
|
|
Cache.get( key.to_s )
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2012-12-27 20:17:33 +00:00
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
def self.lookup(data)
|
|
|
|
if data[:id]
|
|
|
|
# puts "GET- + #{self.to_s}.#{data[:id].to_s}"
|
|
|
|
cache = self.cache_get( data[:id] )
|
|
|
|
return cache if cache
|
|
|
|
|
|
|
|
# puts "Fillup- + #{self.to_s}.#{data[:id].to_s}"
|
|
|
|
record = self.where( :id => data[:id] ).first
|
|
|
|
self.cache_set( data[:id], record )
|
|
|
|
return record
|
|
|
|
elsif data[:name]
|
|
|
|
cache = self.cache_get( data[:name] )
|
|
|
|
return cache if cache
|
|
|
|
|
|
|
|
record = self.where( :name => data[:name] ).first
|
|
|
|
self.cache_set( data[:name], record )
|
|
|
|
return record
|
|
|
|
else
|
|
|
|
raise "Need name or id for lookup()"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-27 20:17:33 +00:00
|
|
|
def self.create_if_not_exists(data)
|
|
|
|
if data[:name]
|
|
|
|
record = self.where( :name => data[:name] ).first
|
|
|
|
return record if record
|
|
|
|
elsif data[:locale] && data[:source]
|
|
|
|
record = self.where( :locale => data[:locale], :source => data[:source] ).first
|
|
|
|
return record if record
|
|
|
|
end
|
|
|
|
self.create(data)
|
|
|
|
end
|
2012-12-30 09:45:01 +00:00
|
|
|
|
|
|
|
def self.create_or_update(data)
|
|
|
|
if data[:name]
|
|
|
|
record = self.where( :name => data[:name] ).first
|
|
|
|
if record
|
|
|
|
record.update_attributes( :data => data[:data] )
|
|
|
|
else
|
|
|
|
record = self.new( data )
|
|
|
|
record.save
|
|
|
|
end
|
|
|
|
return record
|
|
|
|
else
|
|
|
|
raise "Need name for create_or_update()"
|
|
|
|
end
|
|
|
|
end
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|