Class: Redmine::FieldFormat::AttachmentFormat

Inherits:
Base
  • Object
show all
Defined in:
lib/redmine/field_format.rb

Instance Method Summary collapse

Methods inherited from Base

#before_custom_field_save, #bulk_edit_tag, #cast_custom_value, #cast_value, field_attributes, #formatted_custom_value, #formatted_value, #group_statement, #join_for_order_statement, #label, #name, #order_statement, #possible_custom_value_options, #possible_values_options, #query_filter_options, #target_class, #validate_custom_field, #validate_single_value, #value_from_keyword

Methods included from Helpers::URL

#uri_with_safe_scheme?

Methods included from I18n

#current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, included, #l, #l_hours, #l_hours_short, #l_or_humanize, #languages_options, #ll, #lu, #month_name, #set_language_if_valid, #valid_languages

Instance Method Details

#after_save_custom_value(custom_field, custom_value) ⇒ Object


1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/redmine/field_format.rb', line 1005

def after_save_custom_value(custom_field, custom_value)
  if custom_value.saved_change_to_value?
    if custom_value.value.present?
      attachment = Attachment.find_by(:id => custom_value.value.to_s)
      if attachment
        attachment.container = custom_value
        attachment.save!
      end
    end
    if custom_value.value_before_last_save.present?
      attachment = Attachment.find_by(:id => custom_value.value_before_last_save.to_s)
      if attachment
        attachment.destroy
      end
    end
  end
end

#cast_single_value(custom_field, value, customized = nil) ⇒ Object


981
982
983
# File 'lib/redmine/field_format.rb', line 981

def cast_single_value(custom_field, value, customized=nil)
  Attachment.find_by_id(value.to_i) if value.present? && value.respond_to?(:to_i)
end

#edit_tag(view, tag_id, tag_name, custom_value, options = {}) ⇒ Object


1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/redmine/field_format.rb', line 1023

def edit_tag(view, tag_id, tag_name, custom_value, options={})
  attachment = nil
  if custom_value.value.present?
    attachment = Attachment.find_by_id(custom_value.value)
  end

  view.hidden_field_tag("#{tag_name}[blank]", "") +
    view.render(:partial => 'attachments/form',
      :locals => {
        :attachment_param => tag_name,
        :multiple => false,
        :description => false,
        :saved_attachments => [attachment].compact,
        :filedrop => false
      })
end

#set_custom_field_value(custom_field, custom_field_value, value) ⇒ Object


933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'lib/redmine/field_format.rb', line 933

def set_custom_field_value(custom_field, custom_field_value, value)
  attachment_present = false

  if value.is_a?(Hash)
    attachment_present = true
    value = value.except(:blank)

    if value.values.any? && value.values.all? {|v| v.is_a?(Hash)}
      value = value.values.first
    end

    if value.key?(:id)
      value = set_custom_field_value_by_id(custom_field, custom_field_value, value[:id])
    elsif value[:token].present?
      if attachment = Attachment.find_by_token(value[:token])
        value = attachment.id.to_s
      else
        value = ''
      end
    elsif value.key?(:file)
      attachment = Attachment.new(:file => value[:file], :author => User.current)
      if attachment.save
        value = attachment.id.to_s
      else
        value = ''
      end
    else
      attachment_present = false
      value = ''
    end
  elsif value.is_a?(String)
    value = set_custom_field_value_by_id(custom_field, custom_field_value, value)
  end
  custom_field_value.instance_variable_set "@attachment_present", attachment_present

  value
end

#validate_custom_value(custom_value) ⇒ Object


985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/redmine/field_format.rb', line 985

def validate_custom_value(custom_value)
  errors = []

  if custom_value.value.blank?
    if custom_value.instance_variable_get("@attachment_present")
      errors << ::I18n.t('activerecord.errors.messages.invalid')
    end
  else
    if custom_value.value.present?
      attachment = Attachment.find_by(:id => custom_value.value.to_s)
      extensions = custom_value.custom_field.extensions_allowed
      if attachment && extensions.present? && !attachment.extension_in?(extensions)
        errors << "#{::I18n.t('activerecord.errors.messages.invalid')} (#{l(:setting_attachment_extensions_allowed)}: #{extensions})"
      end
    end
  end

  errors.uniq
end