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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'app/models/concerns/enjoy/gallery/mongoid_paperclip.rb', line 8
def enjoy_cms_mongoid_attached_file(name, opts = {})
name = name.to_sym
is_image = true
styles_method_name = nil
unless opts.blank?
content_type = opts.delete(:content_type)
jcrop_options = opts.delete(:jcrop_options)
is_image = opts.delete(:is_image)
is_image = true if is_image.nil?
end
if is_image
opts[:processors] ||= []
opts[:processors] << :paperclip_optimizer
opts[:processors].flatten!
opts[:processors].uniq!
opts[:convert_options] = {all: "-quality 75 -strip"} if opts[:convert_options].blank?
if opts[:styles].blank?
styles_method_name = "#{name}_styles"
opts[:styles] = lambda { |attachment| attachment.instance.send(styles_method_name) }
end
end
has_mongoid_attached_file name, opts
validates_attachment_content_type name, content_type: /\Aimage\/.*\Z/ if is_image
class_eval <<-EVAL
def #{name}_file_name=(val)
return self[:#{name}_file_name] = "" if val == ""
return self[:#{name}_file_name] = nil if val == nil
val = val.to_s
extension = File.extname(val)[1..-1]
file_name = val[0..val.size-extension.size-1]
self[:#{name}_file_name] = "\#{file_name.filename_to_slug}.\#{extension.filename_to_slug}"
end
before_#{name}_post_process do
p_o = self.#{name}.processors.delete :paperclip_optimizer
self.#{name}.processors << p_o if p_o
true
end
def #{name}_svg?
#{name}_content_type =~ /svg/
end
def #{name}_url(opts)
if #{name}_svg?
#{name}.url
else
#{name}.url(opts)
end
end
EVAL
jcrop_options ||= {}
if jcrop_options
class_eval <<-EVAL
def #{name}_jcrop_options
#{jcrop_options}
end
EVAL
end
if styles_method_name
class_eval <<-EVAL
def #{styles_method_name}
{}
end
EVAL
end
end
|