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 # A UploadedFile with relations to Portfolio, Position, Order and/or Stock.
22 # For the sake of security its required to supply id of User that is used to
23 # make sure the requested document does belong to that user. The +id_user+
24 # parameter should <b>not</b> come from user input, but rather result from a
25 # successful authentication process.
29 # - right now this is only for viewing perposes
30 # - to store new data records use the model classes directly. These are
31 # Document, DocumentData and DocumentAssign.
35 # initialize with Document id and User id
36 def initialize(id_document, id_user)
40 @document = Document.where(:id=>@id, :id_user=>@id_user).limit(1).first
50 # returns Document.time_upload
72 if DocumentAssign.where(:id_document=>@id, :is_assigned=>1).count > 0 then return true
77 # tell whether a Document record was found
79 not @document.is_a?(Document)
83 # return human readable string of relations of this document
85 # TODO: make link to each relation
87 rel = DocumentAssign.where(:id_document=>@id, :is_assigned=>1).first
92 t << 'security %s' % rel.Stock.name if rel.id_stock
93 t << 'order #%i' % rel.id_order if rel.id_order
94 t << 'portfolio %s' % rel.Portfolio.name if rel.id_portfolio
95 t << 'position %s held at %s' % [rel.Position.Stock.name, rel.Position.Portfolio.name] if rel.id_position
97 return 'related to ' + t.join(', ')
101 # return Portfolio object(s) if any were assigned
102 # FIXME: not yet implemented
108 # return Position object(s) if any were assigned
109 # FIXME: not yet implemented
115 # return Order object(s) if any were assigned
116 # FIXME: not yet implemented
122 # return Stock object(s) if any were assigned
123 # FIXME: not yet implemented
129 # return BLOB (binary large object) data of that document
131 DocumentData.where(:id_document=>@id).first.content
135 # assigns this document to an Order, a Position, a Stock or a Portfolio.
137 # Give a symbol and the id that should be assigned. Like
138 # +assign(:order=>5)+ or more than on with +assign(:order=>6,
141 # Unassign by specifying zero, ie +assign(:order=>0)+.
142 def assign(assignments = {})
143 da = DocumentAssign.find_by_id_document(@id) || DocumentAssign.new(:id_document=>@id, :is_assigned=>1)
145 return false if assignments.empty?
147 da.id_order = assignments[:order] if assignments[:order]; da.id_order=nil if assignments[:order] == 0
148 da.id_position = assignments[:position] if assignments[:position]; da.id_position=nil if assignments[:position] == 0
149 da.id_portfolio = assignments[:portfolio] if assignments[:portfolio]; da.id_portfolio=nil if assignments[:portfolio] == 0
150 da.id_stock = assignments[:stock] if assignments[:stock]; da.id_stock=nil if assignments[:stock] == 0
156 # Make new Smr::UploadedFile from POSTed data.
158 # This creates the records which can later be viewed as Smr::UploadedFile. On
159 # success it will return a new Smr::UploadedFile based on the new records.
160 # - +upload_time+ should be of class Time
161 # - +file+ is expected to be of class ActionDispatch::Http::UploadedFile.
163 # NOTE: this is a class method, not an instance method!
164 def self.store(id_user, file, comment='', upload_time=Time.now)
165 raise 'file must be of ActionDispatch::Http::UploadedFile' unless file.is_a?(ActionDispatch::Http::UploadedFile)
168 :date_upload=>upload_time.to_i,
170 :mimetype=>file.content_type,
171 :filename=>file.original_filename,
175 DocumentData.new(:id_document=>d.id, :content=>file.read).save!
176 return UploadedFile.new(d.id, id_user)
181 # Collection of UploadedFile objects owned by a given user.
183 # Optionally only those related to :id_position.
186 # The +id_user+ parameter should <b>not</b> come from user input, but rather
187 # result from a successful authentication process.
188 class UploadedFiles < Array
190 # provide +Time+ date and SQL statement as string.
191 def initialize(id_user, id_position=false)
192 @docids = Document.select(:id).where(:id_user=>id_user).order(date_upload: :desc)
195 @docids = @docids.joins(:DocumentAssign).where(document_assign: { id_position: id_position })
198 @docids.each { |id| push(UploadedFile.new(id, id_user)) }