Sha256: 8a8542942982260b13eaa53737f229dd522d7c7c33819decb70c1231607e20cf

Contents?: true

Size: 896 Bytes

Versions: 5

Compression:

Stored size: 896 Bytes

Contents

module Rooftop
  module Coercions
    module TitleCoercion
      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

      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rooftop-0.1.4.1 lib/rooftop/coercions/title_coercion.rb
rooftop-0.1.4 lib/rooftop/coercions/title_coercion.rb
rooftop-0.1.3 lib/rooftop/coercions/title_coercion.rb
rooftop-0.1.2 lib/rooftop/coercions/title_coercion.rb
rooftop-0.1.1 lib/rooftop/coercions/title_coercion.rb