OSDN Git Service

b1331c126789c73d551baecbb4c5db5679e9ad05
[tdcgexplorer/nimono.git] / app / models / arc.rb
1 class Arc < ActiveRecord::Base
2   belongs_to :location
3   has_many :pmds
4   has_many :vmds
5   has_many :xes
6   has_many :arc_thumbs
7   has_many :thumbs, :through => :arc_thumbs
8
9   def location_code
10     /([a-z]+)/.match(code).to_a[1]
11   end
12
13   def number
14     /([0-9]+)/.match(code).to_a[1].to_i
15   end
16
17   def site
18     case location_code
19     when "up"
20       "http://bytatsu.net/uploader/mikumikudance"
21     when "file"
22       "http://loda.jp/mmdfile"
23     when "mini"
24       "http://www9.atpages.jp/~mmdaccessory/uploader"
25     end
26   end
27
28   def url
29     case location_code
30     when "up"
31       if locked?
32         "#{site}/upload.cgi?mode=dl&file=#{number}"
33       else
34         "#{site}/src/#{code}.#{extname}"
35       end
36     when "file"
37       if locked?
38         "#{site}/?mode=pass&idd=#{number}"
39       else
40         "#{site}/?id=#{number}"
41       end
42     when "mini"
43       "#{site}/log/#{number}.#{extname}"
44     end
45   end
46
47   class Search
48     attr_accessor :text
49
50     def initialize(attributes)
51       attributes.each do |name, value|
52         send("#{name}=", value)
53       end if attributes
54     end
55
56     def conditions
57       @conditions ||= begin
58         sql = "TRUE"
59         ret = [ sql ]
60         terms = []
61         unless text.blank?
62           sql.concat " and (code like ? or summary like ? or origname like ?)"
63           ret.push "%#{text}%"
64           ret.push "%#{text}%"
65           ret.push "%#{text}%"
66         end
67         ret
68       end
69     end
70
71     def find_options
72       { :conditions => conditions }
73     end
74   end
75 end