Skip to content

Commit 5fa9d44

Browse files
committed
Adding support for smart paperclip field names to admin form field
rendering
1 parent c7aee5c commit 5fa9d44

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

app/helpers/admin/form_helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ def attribute_disabled?(attribute)
383383
def typus_preview(item, attribute)
384384

385385
# typus_preview(attribute).html_safe! unless @item.send(attribute).blank?
386+
#
387+
return unless item.attachment_present?(attribute)
386388

387389
attachment = attribute.split("_file_name").first
388390
file_preview = Typus::Configuration.options[:file_preview]
@@ -430,4 +432,4 @@ def expand_tree_into_select_field(items, attribute)
430432
end
431433
end
432434

433-
end
435+
end

app/views/admin/templates/_file.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
attachment = attribute.split('_file_name').first
44

5-
if !@item.send(attribute).blank?
5+
if @item.attachment_present?(attribute)
66
label_text << <<-HTML
77
<small>#{link_to _("Remove file"), { :action => 'detach',
88
:id => @item,

lib/typus/active_record.rb

+12
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,18 @@ def owned_by?(user)
349349
send(Typus.user_fk) == user.id
350350
end
351351

352+
# Determine if file attachment is blank, taking into account that
353+
# user may be using a Paperclip attachment without a _file_name
354+
# suffix
355+
def attachment_present?(attribute)
356+
attribute = attribute.to_s
357+
if attribute.index('_file_name')
358+
!send(attribute).blank?
359+
elsif respond_to?("#{attribute}_file_name")
360+
!send("#{attribute}_file_name").blank?
361+
end
362+
end
363+
352364
end
353365

354366
end

0 commit comments

Comments
 (0)