Class: Redmine::FieldFormat::AttachmentFormat

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

Overview

Since:

  • 3.4.0

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



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

def after_save_custom_value(custom_field, custom_value)
  if custom_value.value_changed?
    if custom_value.value.present?
      attachment = Attachment.where(:id => custom_value.value.to_s).first
      if attachment
        attachment.container = custom_value
        attachment.save!
      end
    end
    if custom_value.value_was.present?
      attachment = Attachment.where(:id => custom_value.value_was.to_s).first
      if attachment
        attachment.destroy
      end
    end
  end
end

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



960
961
962
# File 'lib/redmine/field_format.rb', line 960

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



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/redmine/field_format.rb', line 1002

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



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'lib/redmine/field_format.rb', line 912

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



964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/redmine/field_format.rb', line 964

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.where(:id => custom_value.value.to_s).first
      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