organized code into Smr module
[smr.git] / gui / lib / smr_blog.rb
blob1114af756fc3035df84c49ec37c05fef6c8f3d0c
3 # This file is part of SMR.
5 # SMR is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
10 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # SMR.  If not, see <http://www.gnu.org/licenses/>.
19 # Collection of most recent SmrBlogItem objects of given User.id.
21 # As of now all items from previous 30 days are collected.
22 class SmrBlog
23     def initialize(id_user, end_date=Time.now)
24         @collection = Array.new
26         start_date = (end_date - 30.days).to_i
27         end_date = end_date.to_i
29         # load personal commentary
30         Comment.where(:id_user=>id_user)
31             .where({ :date=>(start_date..end_date) }).each do |c|
32             @collection << SmrBlogItem.new(c.time, c.class, c.title, c.comment, false, c.id)
33         end
35         # load order commentary
36         Order.where.not(:comment=>'')
37             .where({ :issued=>(start_date..end_date) })
38             .joins('LEFT JOIN position p ON p.id = order.id_position')
39             .joins('LEFT JOIN portfolio po ON po.id = p.id_portfolio')
40             .where('po.id_user=%i' % id_user).each do |o|
41             @collection << SmrBlogItem.new(o.time_issued, o.class, o.to_s ,o.comment)
42         end
44         # load quoterecord commentary
45         Quoterecord.where(:id_user=>id_user)
46             .where({ :created=>(start_date..end_date) })
47             .where.not(:comment=>'').each do |qr|
48             @collection << SmrBlogItem.new(qr.time_created, qr.class, qr.to_s, qr.comment)
49         end
51         @collection.sort_by!{|i| i.date}.reverse!
52         true
53     end
55     ##
56     # loops over SmrBlogItem collection
57     def each(&block)
58         @collection.each(&block)
59     end
61     ##
62     # tell whether there is anything in the collection
63     def empty?
64         @collection.empty?
65     end
66 end
70 # Represents a single SmrBlog item for a template to render.
72 # - +url+ is to link to somewhere in the WWW
73 # - +refid+ is some :id of the object that created this SmrBlogItem, its used
74 #   to provide a smr_link_back() to that record.
75 class SmrBlogItem
76     def initialize(date, type, title, body, url=false, refid=false)
77         raise 'date must be of type Time' unless date.is_a?(Time)
79         @date=date; @type=type.to_s; @title=title; @body=body; @url=url;
80         @refid=refid;
81     end
83     def date
84         @date
85     end
87     def type
88         @type
89     end
91     def title
92         @title
93     end
95     def body
96         @body
97     end
99     def url
100         @url
101     end
102     
103     def refid
104         @refid
105     end