2012-04-10 14:06:46 +00:00
|
|
|
class History < ActiveRecord::Base
|
|
|
|
self.table_name = 'histories'
|
|
|
|
belongs_to :history_type, :class_name => 'History::Type'
|
|
|
|
belongs_to :history_object, :class_name => 'History::Object'
|
|
|
|
belongs_to :history_attribute, :class_name => 'History::Attribute'
|
|
|
|
# before_validation :check_type, :check_object
|
|
|
|
# attr_writer :history_type, :history_object
|
|
|
|
|
|
|
|
# def history_type(a)
|
|
|
|
# end
|
|
|
|
|
|
|
|
def self.history_list(requested_object, requested_object_id)
|
|
|
|
history = History.where( :history_object_id => History::Object.where( :name => requested_object ) ).
|
|
|
|
where( :o_id => requested_object_id ).
|
|
|
|
where( :history_type_id => History::Type.where( :name => ['created', 'updated']) ).
|
|
|
|
order('created_at ASC, id ASC')
|
|
|
|
return history
|
|
|
|
end
|
|
|
|
|
2012-04-29 21:37:49 +00:00
|
|
|
def self.activity_stream(user, limit = 10)
|
2012-04-10 14:06:46 +00:00
|
|
|
# g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
|
|
|
|
# stream = History.select("distinct(histories.o_id), created_by_id, history_attribute_id, history_type_id, history_object_id, value_from, value_to").
|
2012-04-29 21:37:49 +00:00
|
|
|
# where( :history_type_id => History::Type.where( :name => ['created', 'updated']) ).
|
2012-04-10 14:06:46 +00:00
|
|
|
stream = History.select("distinct(histories.o_id), created_by_id, history_type_id, history_object_id").
|
|
|
|
where( :history_object_id => History::Object.where( :name => 'Ticket').first.id ).
|
2012-04-29 21:37:49 +00:00
|
|
|
where( :history_type_id => History::Type.where( :name => ['updated']) ).
|
2012-04-10 14:06:46 +00:00
|
|
|
order('created_at DESC, id DESC').
|
2012-04-29 21:37:49 +00:00
|
|
|
limit(limit)
|
2012-04-20 12:25:13 +00:00
|
|
|
datas = []
|
2012-04-10 14:06:46 +00:00
|
|
|
stream.each do |item|
|
2012-04-20 12:25:13 +00:00
|
|
|
data = item.attributes
|
|
|
|
data['history_object'] = item.history_object
|
|
|
|
data['history_type'] = item.history_type
|
|
|
|
datas.push data
|
2012-04-10 14:06:46 +00:00
|
|
|
# item['history_attribute'] = item.history_attribute
|
|
|
|
end
|
2012-04-20 12:25:13 +00:00
|
|
|
return datas
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.recent_viewed(user)
|
|
|
|
# g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
|
|
|
|
stream = History.select("distinct(histories.o_id), created_by_id, history_attribute_id, history_type_id, history_object_id, value_from, value_to").
|
|
|
|
where( :history_object_id => History::Object.where( :name => 'Ticket').first.id ).
|
|
|
|
where( :history_type_id => History::Type.where( :name => ['viewed']) ).
|
2012-05-02 11:57:23 +00:00
|
|
|
where( :created_by_id => user.id ).
|
2012-04-10 14:06:46 +00:00
|
|
|
order('created_at DESC, id DESC').
|
|
|
|
limit(10)
|
2012-04-20 12:25:13 +00:00
|
|
|
datas = []
|
2012-04-10 14:06:46 +00:00
|
|
|
stream.each do |item|
|
2012-04-20 12:25:13 +00:00
|
|
|
data = item.attributes
|
|
|
|
data['history_object'] = item.history_object
|
|
|
|
data['history_type'] = item.history_type
|
|
|
|
datas.push data
|
2012-04-10 14:06:46 +00:00
|
|
|
# item['history_attribute'] = item.history_attribute
|
|
|
|
end
|
2012-04-20 12:25:13 +00:00
|
|
|
return datas
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def check_type
|
|
|
|
puts '--------------'
|
|
|
|
puts self.inspect
|
|
|
|
history_type = History::Type.where( :name => self.history_type ).first
|
|
|
|
if !history_type || !history_type.id
|
|
|
|
history_type = History::Type.create(
|
|
|
|
:name => self.history_type,
|
|
|
|
:active => true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
self.history_type_id = history_type.id
|
|
|
|
end
|
|
|
|
def check_object
|
|
|
|
history_object = History::Object.where( :name => self.history_object ).first
|
|
|
|
if !history_object || !history_object.id
|
|
|
|
history_object = History::Object.create(
|
|
|
|
:name => self.history_object,
|
|
|
|
:active => true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
self.history_object_id = history_object.id
|
|
|
|
end
|
|
|
|
|
|
|
|
class Object < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class Type < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class Attribute < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|