Module: JSONModelPublishing

Defined in:
common/jsonmodel_publishing.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._clean_mixed_content_notes(hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'common/jsonmodel_publishing.rb', line 43

def self._clean_mixed_content_notes(hash)
  if hash.has_key?('notes')
    hash['notes'] = hash['notes'].map {|note|
      if note.has_key?('content')
        content = note['content'].is_a?(Array) ? note['content'].join("\n\n") : note['content']
        note['content'] = MixedContentParser.parse(content, "/", {:wrap_blocks => true})
      end

      if note.has_key?('subnotes')
        note['subnotes'] = note['subnotes'].map {|subnote|
          if subnote.has_key?('content')
            subnote['content'] = MixedContentParser.parse(subnote['content'], "/", {:wrap_blocks => true})
          end

          subnote
        }
      end

      note
    }
  end
end

._drop_unpublished(obj) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'common/jsonmodel_publishing.rb', line 27

def self._drop_unpublished(obj)
  if obj.is_a?(Array)
    obj.reject! {|item| self._drop_unpublished(item) }
    obj.each do |value|
      self._drop_unpublished(value)
    end
  elsif obj.is_a?(Hash)
    return true if obj.has_key?('publish') && !obj['publish']
    obj.reject! {|k, v| self._drop_unpublished(v) }
    self._drop_unpublished(obj.values)
  end

  false
end

Instance Method Details

#to_hash(mode = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'common/jsonmodel_publishing.rb', line 8

def to_hash(mode = nil)
  strip = false

  if mode == :publishing
    mode = nil
    strip = true
  end

  hash = super(mode)

  if strip
    JSONModelPublishing._drop_unpublished(hash)
    JSONModelPublishing._clean_mixed_content_notes(hash)
  end

  hash
end