Module: OaiDcHelpersOverrides

Included in:
AudioRecording, Comment, Document, StillImage, Topic, Video, WebLink
Defined in:
lib/kete_translatable_content/extensions/oai_dc_helpers_overrides.rb

Overview

redefine helpers to include translated versions

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/kete_translatable_content/extensions/oai_dc_helpers_overrides.rb', line 4

def self.included(klass)
  klass.class_eval do

    if self.new.respond_to?(:oai_dc_xml_dc_title)
      alias_method(:oai_dc_xml_dc_title_orig, :oai_dc_xml_dc_title) unless self.new.respond_to?(:oai_dc_xml_dc_title_orig)

      def oai_dc_xml_dc_title(xml, options = {})
        is_translatable = Kete.translatables.keys.include?(self.class.name.tableize.singularize)

        # handle original title
        options = options.merge({ "xml:lang" => locale }) if is_translatable
        oai_dc_xml_dc_title_orig(xml, options)

        original_title = title

        return unless is_translatable

        @translations_for_this_version ||= self.class::Translation.all(self.class.as_foreign_key_sym => id,
                                                                       :version => version.to_s)
        @translations_for_this_version.each do |t|
          if t.title.present?
            self.title = t.title
            options = options.merge({ "xml:lang" => t.locale })
            oai_dc_xml_dc_title_orig(xml, options)
          end
        end
        self.title = original_title
      end
    end

    if self.new.respond_to?(:oai_dc_xml_dc_description)
      alias_method(:oai_dc_xml_dc_description_orig, :oai_dc_xml_dc_description) unless self.new.respond_to?(:oai_dc_xml_dc_description_orig)

      def oai_dc_xml_dc_description(xml, passed_description = nil, options = {})
        unless passed_description.blank?
          oai_dc_xml_dc_description_orig(xml, passed_description, options)
        else
          class_name = self.class.name
          is_translatable = Kete.translatables.keys.include?(class_name.tableize.singularize)
          has_short_summary = [Topic, Document].include?(self.class)

          options = options.merge({ "xml:lang" => locale }) if is_translatable

          # topic/document specific
          # order is important, first description will be used as blurb
          # in result list
          if has_short_summary && short_summary.present?
            oai_dc_xml_dc_description(xml, short_summary, options)
          end

          oai_dc_xml_dc_description(xml, description, options) if description.present?

          return unless is_translatable

          @translations_for_this_version ||= self.class::Translation.all(self.class.as_foreign_key_sym => id,
                                                                       :version => version.to_s)
          @translations_for_this_version.each do |t|
            options = options.merge({ "xml:lang" => t.locale })

            if has_short_summary && t.short_summary.present?
              oai_dc_xml_dc_description(xml, t.short_summary, options)
            end

            if t.description.present?
              options = options.merge({ "xml:lang" => t.locale })
              oai_dc_xml_dc_description(xml, t.description, options)
            end
          end
        end
      end
    end

    if self.new.respond_to?(:oai_dc_xml_tags_to_dc_subjects)
      alias_method(:oai_dc_xml_tags_to_dc_subjects_orig, :oai_dc_xml_tags_to_dc_subjects) unless self.new.respond_to?(:oai_dc_xml_tags_to_dc_subjects_orig)

      def oai_dc_xml_tags_to_dc_subjects(xml)
        tags.each do |tag|
          # handle original
          xml.send("dc:subject", "xml:lang" => tag.original_locale) {
            xml.cdata tag.name
          }

          # do each translation of tag
          tag.translations.each do |t|
            xml.send("dc:subject", "xml:lang" => t.locale) {
              xml.cdata t.name
            }
          end
        end
      end
    end

  end
end