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



943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
# File 'lib/redmine/field_format.rb', line 943

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



919
920
921
# File 'lib/redmine/field_format.rb', line 919

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



961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/redmine/field_format.rb', line 961

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



871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'lib/redmine/field_format.rb', line 871

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



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/redmine/field_format.rb', line 923

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