forked from refinery/refinerycms
/
resource.rb
52 lines (40 loc) · 1.33 KB
/
resource.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Refinery
class Resource < ActiveRecord::Base
include Resources::Validators
attr_accessible :id, :file
resource_accessor :file
validates :file, :presence => true
validates_with FileSizeValidator
# Docs for acts_as_indexed http://github.com/dougal/acts_as_indexed
acts_as_indexed :fields => [:file_name, :title, :type_of_content]
# when listing resources out in the admin area, how many resources should show per page
PAGES_PER_ADMIN_INDEX = 20
delegate :ext, :size, :mime_type, :url, :to => :file
# used for searching
def type_of_content
mime_type.split("/").join(" ")
end
# Returns a titleized version of the filename
# my_file.pdf returns My File
def title
CGI::unescape(file_name.to_s).gsub(/\.\w+$/, '').titleize
end
class << self
# How many resources per page should be displayed?
def per_page(dialog = false)
dialog ? Resources::Options.pages_per_dialog : PAGES_PER_ADMIN_INDEX
end
def create_resources(params)
resources = []
unless params.present? and params[:file].is_a?(Array)
resources << create(params)
else
params[:file].each do |resource|
resources << create(:file => resource)
end
end
resources
end
end
end
end