orbit-4-2/app/models/preview.rb

111 lines
3.6 KiB
Ruby
Raw Normal View History

class Preview
include Mongoid::Document
include Mongoid::Timestamps
# field :object_f, :type => Hash
field :object, :type=> Hash
field :preview_at_link
field :expired_at , :type => DateTime
field :link_args, :type => Array
field :object_class_type
has_many :preview_files, :autosave => true, :dependent => :destroy
has_many :preview_associations, :autosave => true, :dependent => :destroy
# def object=(params)
# save_upload_temp_link(params,"news_bulletin_files_attributes") #unless params[]
# self.object_f = params
# end
# def object
# return object_f
# end
# def save_upload_temp_link(params,field_name = "bulletin_files_attributes")
# image = preview_files.build(:file=>params[:image])
# params[:image] = image.id
# params[field_name].each_with_index do |item,index|
# bfa = preview_files.build(:file=>params[field_name][index.to_s][:file])
# params[field_name][index.to_s] = bfa.id
# end unless params[field_name].nil?
# end
# def dig_in_hash(hash,paths_ary)
# hash.each_pair do |key,in_hash|
# if in_hash.kind_of? Array
# dig_in_array(hash,paths_ary)
# elsif in_hash.kind_of? Hash
# dig_in_hash(hash,paths_ary)
# else
# puts("\n End Node: \t #{paths_ary.join } #{in_hash.class} : #{key}##{in_hash}")
# end
# end
# end
# def dig_in_array(array,paths_ary)
# array.each do |item|
# if item.kind_of? Array
# dig_in_array(hash,paths_ary)
# elsif item.kind_of? Hash
# dig_in_hash(hash,paths_ary)
# else
# puts("\n End Node: \t #{paths_ary.join } #{item.class} : {item}")
# end
# end
# end
# def dig_in_hash_old(hash,paths_ary)
# hash.each_pair do |k,in_hash|
# if (!in_hash.kind_of? Array and !in_hash.kind_of? Hash)
# #p "UploadedFile : #{in_hash.is_a? ActionDispatch::Http::UploadedFile}"
# #in_hash = "no file" if in_hash.is_a? ActionDispatch::Http::UploadedFile
# #in_hash.select{|key,hash| hash.is_a? ActionDispatch::Http::UploadedFile}
# puts("\n End Node:# \n")
# p "#{paths_ary.join } #{in_hash.class} : #{in_hash}"
# #p "UploadedFile(#{in_hash}) : #{in_hash.is_a? ActionDispatch::Http::UploadedFile}"
# else
# if (!in_hash.first.kind_of? Array and !in_hash.first.kind_of? Hash)
# paths_ary << "[#{in_hash.first}]"
# end
# puts("\n Go Down [#{in_hash.first}]\n")
# dig_in_hash(in_hash,paths_ary)
# puts("\n Go Out \n")
# end
# puts "This is last"
# paths_ary.pop
# end
# end
def get_arg_hash
object.slice(*link_args).inject({}){|la,(k,v)| la[k.to_sym] = v; la}
end
def get_preview_link
ap = Rails.application.routes.url_helpers
ap.send preview_at_link,({:id=>id,:preview=>true}.merge get_arg_hash)
#func = eval("Rails.application.routes.url_helpers.#{preview_at_link}").send
end
def get_virtual_object
virtual_object = eval(self.object_class_type).new object
preview_files.each do |file|
if file.file_in_array
eval("virtual_object.#{file.field_name_for_rebuild}.build :file=>file.file")
else
eval("virtual_object.#{file.field_name_for_rebuild} = file.file")
end
end
preview_associations.each do |local_object|
if local_object.object_in_array
eval("virtual_object.#{local_object.field_name_for_rebuild}.build local_object.object")
else
eval("virtual_object.#{local_object.field_name_for_rebuild} = local_object.object")
end
end
virtual_object
end
end