minor features, layout improvements, many fixes
[smr.git] / gui / lib / smr / blog.rb
blob6060837bca9be2f4c483bb335b12ff93415f1598
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
7 # version.
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/>.
16 require 'link'
18 module Smr  #:nodoc:
19   ##
20   # Collection of most recent SmrBlogItem objects of given User.id.
21   #
22   # As of now all items from previous 30 days are collected.
23   class Blog
24       def initialize(id_user, end_date=Time.now)
25           @collection = Array.new
26    
27           start_date = (end_date - 30.days).to_i
28           end_date = end_date.to_i
29   
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)
36               )
37           end
38   
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)
53               )
54           end
55   
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)
61           end
62   
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)
68           end
70           @collection.sort_by!{|i| i.date}.reverse!
71           true
72       end
73   
74       ##
75       # loops over SmrBlogItem collection
76       def each(&block)
77           @collection.each(&block)
78       end
80       ##
81       # number of Smr::BlogItem objects the blog has available for viewing.
82       def count
83           @collection.count
84       end
86       ##
87       # tell whether there is anything in the collection
88       def empty?
89           @collection.empty?
90       end
91   end
92   
93   
94   ##
95   # Represents a single SmrBlog item, ie. for rendering in a view.
96   class BlogItem
98       # reader for data field
99       attr_reader :date
100       attr_reader :type
101       attr_reader :title
102       attr_reader :body
103       attr_reader :link
105       ##
106       # - pass a Smr::Link as :link option to reference this BlogItem to
107       #   something else
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;
112       end
114       ##
115       # Identifier of this item. Unique hopefully and a String.
116       def id
117           ::Digest::MD5.hexdigest title + date.to_i.to_s
118       end
119   end
121 end # module