Skip to content

Instantly share code, notes, and snippets.

@firedev
Last active August 29, 2015 14:17
Show Gist options
  • Save firedev/be2cac112f314ab91903 to your computer and use it in GitHub Desktop.
Save firedev/be2cac112f314ab91903 to your computer and use it in GitHub Desktop.
Blocks
class Page
has_many :blocks, -> { ordered }, dependent: :destroy
attr_accessors :title
def visible_blocks
@visible_block ||= blocks.current_locale.map(&:blockable)
end
end
class Block
belongs_to :page
belongs_to :blockable, polymorphic: true, dependent: :destroy
attr_accessor :locale
scope :ordered, -> { order position: :desc }
scope :current_locale, -> { where('(locale = ? OR locale IS NULL)') }
end
module Blockable
extend ActiveSupport::Concern
included do
has_one :block, as: :blockable, dependent: :destroy
end
end
class TextBlock
include Blockable
attr_accessor :body
end
class DownloadBlock
include Blockable
has_many :attachments
end
class ImageBlock
include Blockable
has_many :images
end
class MapBlock
include Blockable
attr_accessor :latitude, :longitude, :zoom
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment