OSDN Git Service

t#:
[laoz/laoz.git] / app / models / thema.rb
1 class Thema < ActiveRecord::Base
2   has_many :thema_sections
3   has_one :hateda_thema
4   
5   before_save :modify
6   after_create :created
7   
8   def self.import(data)
9     Thema.transaction do
10       data.to_s.gsub(/\r/, '').split(/\n/).each do |l|
11         columns = l.split(/\t/)
12         thema = Thema.find columns[0]
13         if thema 
14           thema.caption = columns[1] unless columns[1].to_s.empty?
15           thema.detail = columns[2] unless columns[2].to_s.empty?
16           thema.update_at = columns[3] unless columns[3].to_s.empty?
17           thema.save!
18         else
19           thema = Thema.new :caption => columns[1], :detail => columns[2]
20           thema.save!
21         end
22         ht = HatedaThema.find :first, :conditions => ['thema_id = ?', thema.id]
23         ht.name = columns[4] unless columns[4].to_s.empty?
24         ht.upload_at = columns[5] unless columns[5].to_s.empty?
25         unless columns[6].to_s.empty?
26           columns[6].split(/\// ).each do |num|
27             section = Section.find_num num
28             thema.make section.id
29           end
30         end
31       end
32     end
33   end
34   
35   def self.export
36     themas = Thema.find(:all).map { |thema|
37       thema.id.to_s + "\t" + thema.caption.to_s + "\t" + thema.detail.to_s + "\t" + thema.update_at.to_s + "\t" + 
38         thema.hateda_thema.name.to_s + "\t" + thema.hateda_thema.upload_at.to_s + "\t" + 
39         thema.thema_sections.map {|ts| ts.section.num }.join("/")
40     }.join("\n")
41     themas
42   end
43   
44   def self.find_cap cap
45     Thema.find :first, :conditions => ['caption = ?', cap]
46   end
47   
48   def modify?
49     if self.update_at and self.hateda_thema.upload_at
50       self.update_at > self.hateda_thema.upload_at
51     else
52       true
53     end
54   end
55   
56   def update_time
57     if self.update_at
58       self.update_at.strftime('%Y/%m/%d %H:%M:%S')
59     else
60       nil
61     end
62   end
63   
64   def upd(th)
65     res = nil
66     begin
67       Thema.transaction do
68         self.caption = th[:caption]
69         self.detail = th[:detail]
70         self.save!
71         res = true
72       end
73     rescue
74     end
75     res
76   end
77   
78   def make(section_id)
79     res = nil
80     begin
81       Thema.transaction do
82         self.save!
83         ThemaSection.create!(:thema_id => self.id, :section_id => section_id)
84         res = true
85       end
86     rescue
87     end
88     res
89   end
90   
91   def modify
92     self.detail = self.detail.to_s.gsub(/\r/, '').gsub(/\n/, '/')
93     self.update_at = Time.now
94   end
95   
96   def edit_detail
97     self.detail.to_s.gsub('/', "\n")
98   end
99   
100   def disp_detail
101     self.edit_detail.gsub('[', '').gsub(']', '')
102   end
103   
104   def desc
105     self.edit_detail.scan(/\[.+?\]/m).join('...').gsub('[', '').gsub(']', '')
106   end
107   
108   def created
109     unless self.hateda_thema
110       ht = HatedaThema.new(:thema_id => self.id)
111       ht.save!
112     end
113   end
114   
115 end