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_link_safe_scheme?, #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



1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/redmine/field_format.rb', line 1042

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



1016
1017
1018
# File 'lib/redmine/field_format.rb', line 1016

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



1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/redmine/field_format.rb', line 1060

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 => true,
        :attachment_format_custom_field => true
      })
end

#set_custom_field_value(custom_field, custom_field_value, value) ⇒ Object



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
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 967

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



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

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