Sha256: 55fdecf6795e8f9562445654f17670da99a8401de55af11ae70756db7fc09f48

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module SirTrevorRails
  module Blocks
    ##
    # Multi-up featured page block
    class FeaturedPagesBlock < SirTrevorRails::Block
      include Displayable

      def page_options(id)
        (items.detect { |x| x[:id] == id }) || {}
      end

      def pages
        @pages ||= parent.exhibit.pages.published.where(slug: item_ids).sort do |a, b|
          ordered_items.index(a.slug) <=> ordered_items.index(b.slug)
        end
      end

      def pages?
        !pages.empty?
      end

      # rubocop:disable Metrics/MethodLength
      def as_json(*)
        result = super
        result[:data][:item] ||= {}

        result[:data][:item].transform_values! do |v|
          begin
            v['thumbnail_image_url'] = parent.exhibit.pages.find(v['id']).thumbnail_image_url
          rescue ActiveRecord::RecordNotFound
            v = nil
          end
          v
        end

        result[:data][:item].compact!
        result
      end
      # rubocop:enable Metrics/MethodLength
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blacklight-spotlight-3.1.0 app/models/sir_trevor_rails/blocks/featured_pages_block.rb
blacklight-spotlight-3.0.3 app/models/sir_trevor_rails/blocks/featured_pages_block.rb
blacklight-spotlight-3.0.2 app/models/sir_trevor_rails/blocks/featured_pages_block.rb