Skip to content
Snippets Groups Projects
block.rb 1.46 KiB
Newer Older
Arnaud Levy's avatar
Arnaud Levy committed
# == Schema Information
#
# Table name: communication_blocks
#
#  id            :uuid             not null, primary key
#  about_type    :string           indexed => [about_id]
#  data          :jsonb
#  position      :integer          default(0), not null
#  template      :integer          default(NULL), not null
#  title         :string
Arnaud Levy's avatar
Arnaud Levy committed
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#  about_id      :uuid             indexed => [about_type]
#  university_id :uuid             not null, indexed
#
# Indexes
#
#  index_communication_blocks_on_university_id  (university_id)
#  index_communication_website_blocks_on_about  (about_type,about_id)
#
# Foreign Keys
#
#  fk_rails_18291ef65f  (university_id => universities.id)
#
class Communication::Block < ApplicationRecord
Arnaud Levy's avatar
Arnaud Levy committed
  include WithUniversity
Arnaud Levy's avatar
Arnaud Levy committed
  include WithPosition

  belongs_to :about, polymorphic: true

  enum template: {
Arnaud Levy's avatar
Arnaud Levy committed
    chapter: 50,
Arnaud Levy's avatar
Arnaud Levy committed
    organization_chart: 100,
Arnaud Levy's avatar
Arnaud Levy committed
    partners: 200,
Arnaud Levy's avatar
Arnaud Levy committed
    gallery: 300,
alexisben's avatar
alexisben committed
    testimonials: 400,
    posts: 500
Arnaud Levy's avatar
Arnaud Levy committed
  }

  def data=(value)
    value = JSON.parse value if value.is_a? String
    super(value)
  end

  def git_dependencies
Arnaud Levy's avatar
Arnaud Levy committed
    template_class.git_dependencies
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def last_ordered_element
    about.blocks.ordered.last
  end

  def to_s
    title.blank?  ? "Block #{position}"
                  : "#{title}"
Arnaud Levy's avatar
Arnaud Levy committed
  end

  protected

Arnaud Levy's avatar
Arnaud Levy committed
  def template_class
    @template_class ||= "Communication::Block::#{template.classify}".constantize.new self
Arnaud Levy's avatar
Arnaud Levy committed
  end
Arnaud Levy's avatar
Arnaud Levy committed
end