Module: Rooftop::Coercions::TitleCoercion

Defined in:
lib/rooftop/coercions/title_coercion.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rooftop/coercions/title_coercion.rb', line 4

def self.included(base)
  base.send(:after_find, :coerce_title_to_string)
  base.send(:after_save, :coerce_title_to_string)

  base.send(:before_save, ->(record) {
    if record.respond_to?(:title) && record.respond_to?(:title_object)
      record.title_object ||= {}
      if record.title.nil?
        record.restore_title!
        record.restore_title_object!
      else
        record.title_object[:rendered] = record.title
      end
    end
  })

end

Instance Method Details

#coerce_title_to_stringObject



22
23
24
25
26
27
28
# File 'lib/rooftop/coercions/title_coercion.rb', line 22

def coerce_title_to_string
  record = self
  if record.respond_to?(:title) && record.title.is_a?(ActiveSupport::HashWithIndifferentAccess)
    record.title_object = record.title
    record.title = record.title[:rendered]
  end
end