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/>.
19 # Collection of most recent SmrBlogItem objects of given User.id.
21 # As of now all items from previous 30 days are collected.
24 # Optional pattern String used to match Smr::BlogItems.
28 # Collects BlogItem objects relevant at given :date. The given User
29 # should be authenticated!
31 # By default all BlogItem objects within the 30 days preceeding :date are
34 # If a :pattern String is given, it will be matched against all models
35 # appearing in the blog. Time constraints are ignord when a :pattern is
38 def initialize(user, date=Time.now,
43 raise ':user must be of User' unless user.is_a?(User)
44 raise ':date must be of Time' unless date.is_a?(Time)
45 @collection = Array.new
46 @pattern = (options[:pattern].blank? ? nil : options[:pattern])
48 start_date = (date - 30.days).to_i
51 # find Comment records
53 Comment.where(:id_user=>user.id).ransack({:title_or_comment_cont=>@pattern})
55 Comment.where(:id_user=>user.id).where( :date_recorded=>(start_date..end_date) ).ransack
57 q.result.to_a.each do |c|
58 @collection << Smr::BlogItem.new(
59 c.time_recorded, c.class, c.title, c.comment,
60 :link=>Smr::Link.new(:comment_edit, c.id)
64 # find Order commentary
66 Order.joins('LEFT JOIN position p ON p.id = order.id_position')
67 .joins('LEFT JOIN portfolio po ON po.id = p.id_portfolio')
68 .where('po.id_user=%i' % user.id).ransack({:ransackcolumns_cont=>@pattern})
70 Order.where.not(:comment=>'')
71 .where( :date_issued=>(start_date..end_date) )
72 .joins('LEFT JOIN position p ON p.id = order.id_position')
73 .joins('LEFT JOIN portfolio po ON po.id = p.id_portfolio')
74 .where('po.id_user=%i' % user.id).ransack
76 q.result.to_a.each do |o|
77 title = if o.Position.is_cash_position? and o.PositionRevision.first
78 o.PositionRevision.first.describe_type
79 else '%s %s' % [o.type.upcase, o.Security.to_s] end
80 @collection << Smr::BlogItem.new(
81 o.time_issued, o.class, title, o.comment,
82 :link=>Smr::Link.new(:order, o.id)
86 # find Quoterecord commentary
88 Quoterecord.where(:id_user=>user.id).ransack({:ransackcolumns_cont=>@pattern})
90 Quoterecord.where(:id_user=>user.id)
91 .where( :date_created=>(start_date..end_date) )
92 .where.not(:comment=>'').ransack
94 q.result.to_a.each do |qr|
95 @collection << Smr::BlogItem.new(qr.time_created, qr.class, qr.to_s, qr.comment)
98 # find Document commentary
100 Document.where(:id_user=>user.id).ransack({:comment_cont=>@pattern})
102 Document.where(:id_user=>user.id)
103 .where( :date_upload=>(start_date..end_date) )
104 .where.not(:comment=>'').ransack
106 q.result.to_a.each do |d|
107 @collection << Smr::BlogItem.new(d.time_upload, d.class, 'Commented Upload', d.comment)
110 # find Workdesk entries
112 Workdesk.where(:id_user=>user.id).ransack({:ransackcolumns_cont=>@pattern})
114 Workdesk.where(:id_user=>user.id)
115 .where( :date_added=>(start_date..end_date) )
116 .where.not(:comment=>'').ransack
118 q.result.to_a.each do |wi|
119 @collection << Smr::BlogItem.new(wi.time_added, wi.class, wi.to_s, wi.comment)
122 @collection.sort_by!{|i| i.date}.reverse!
127 # Tells whether this Blog actually performed a pattern match.
133 # loops over Smr::BlogItem collection
135 @collection.each(&block)
139 # number of Smr::BlogItem objects the blog has available for viewing.
145 # tell whether there is anything in the collection
153 # Represents a single Blog item, ie. for rendering in a view.
156 # reader for data field
164 # - optionally pass a Smr::Link as :link to reference this BlogItem to
166 def initialize(date, type, title, body, options={})
167 options.reverse_merge!({
170 raise ':date must be of type Time' unless date.is_a? Time
171 raise ':link must be of Smr::Link' if options[:link] and not options[:link].is_a? Smr::Link
172 @date=date; @type=type.to_s; @title=title; @body=body;
177 # Identifier of this item. Unique hopefully and a String.
179 ::Digest::MD5.hexdigest title + date.to_i.to_s