2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR. If not, see <http://www.gnu.org/licenses/>.
20 # Collection of most recent SmrBlogItem objects of given User.id.
22 # As of now all items from previous 30 days are collected.
24 def initialize(id_user, end_date=Time.now)
25 @collection = Array.new
27 start_date = (end_date - 30.days).to_i
28 end_date = end_date.to_i
30 # load personal commentary
31 Comment.where(:id_user=>id_user)
32 .where( :date=>(start_date..end_date) ).each do |c|
33 @collection << BlogItem.new(
34 c.time, c.class, c.title, c.comment,
35 Smr::Link.new(:comment_edit, c.id)
39 # load Order commentary
40 Order.where.not(:comment=>'')
41 .where( :issued=>(start_date..end_date) )
42 .joins('LEFT JOIN position p ON p.id = order.id_position')
43 .joins('LEFT JOIN portfolio po ON po.id = p.id_portfolio')
44 .where('po.id_user=%i' % id_user).each do |o|
46 title = if o.Position.is_cash_position? and o.PositionRevision.first
47 o.PositionRevision.first.describe_type
48 else '%s %s' % [o.type.upcase, o.Security.to_s] end
50 @collection << BlogItem.new(
51 o.time_issued, o.class, title, o.comment,
52 Smr::Link.new(:order, o.id)
56 # load Quoterecord commentary
57 Quoterecord.where(:id_user=>id_user)
58 .where( :created=>(start_date..end_date) )
59 .where.not(:comment=>'').each do |qr|
60 @collection << BlogItem.new(qr.time_created, qr.class, qr.to_s, qr.comment)
63 # load Document commentary
64 Document.where(:id_user=>id_user)
65 .where( :date_upload=>(start_date..end_date) )
66 .where.not(:comment=>'').each do |d|
67 @collection << BlogItem.new(d.time_upload, d.class, 'Commented Upload', d.comment)
70 @collection.sort_by!{|i| i.date}.reverse!
75 # loops over SmrBlogItem collection
77 @collection.each(&block)
81 # number of Smr::BlogItem objects the blog has available for viewing.
87 # tell whether there is anything in the collection
95 # Represents a single SmrBlog item, ie. for rendering in a view.
98 # reader for data field
106 # - pass a Smr::Link as :link option to reference this BlogItem to
108 def initialize(date, type, title, body, link=false)
109 raise ':date must be of type Time' unless date.is_a? Time
110 raise ':link must be of Smr::Link' if link!=false and not link.is_a? Smr::Link
111 @date=date; @type=type.to_s; @title=title; @body=body; @link=link;
115 # Identifier of this item. Unique hopefully and a String.
117 ::Digest::MD5.hexdigest title + date.to_i.to_s