Sha256: e547f5da7a319807de8d80e0f808ac6485f1d289ea1ab5b836001ee3ee88cc0d

Contents?: true

Size: 1.21 KB

Versions: 62

Compression:

Stored size: 1.21 KB

Contents

module Workarea
  class Content
    class BlockName
      def initialize(block)
        @block = block
      end

      def fields_with_displayable_values
        @block
          .type
          .fields
          .reject { |f| f.type == :string && f.options[:multi_line] }
          .select { |f| [:text, :options].exclude?(f.type) }
          .select { |f| @block.data[f.slug].present? }
      end

      def find_field_value(field)
        value = @block.data[field.slug]
        return value unless BSON::ObjectId.legal?(value)

        Workarea.config.content_block_name_search_classes.each do |class_name|
          model = class_name.constantize.where(id: value).first
          return model.name if model.present?
        end

        value
      end

      def first_displayable_value
        fields_with_displayable_values.each do |field|
          value = find_field_value(field)
          return value if value.present?
        end
      end

      def to_s
        if fields_with_displayable_values.blank? || first_displayable_value.blank? || !first_displayable_value.is_a?(String)
          @block.type.name
        else
          "#{@block.type.name} - #{first_displayable_value}"
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/content/block_name.rb
workarea-core-3.5.26 app/models/workarea/content/block_name.rb
workarea-core-3.4.45 app/models/workarea/content/block_name.rb
workarea-core-3.5.25 app/models/workarea/content/block_name.rb
workarea-core-3.5.23 app/models/workarea/content/block_name.rb
workarea-core-3.4.44 app/models/workarea/content/block_name.rb
workarea-core-3.5.22 app/models/workarea/content/block_name.rb
workarea-core-3.4.43 app/models/workarea/content/block_name.rb
workarea-core-3.5.21 app/models/workarea/content/block_name.rb
workarea-core-3.4.42 app/models/workarea/content/block_name.rb
workarea-core-3.5.20 app/models/workarea/content/block_name.rb
workarea-core-3.4.41 app/models/workarea/content/block_name.rb
workarea-core-3.5.19 app/models/workarea/content/block_name.rb
workarea-core-3.4.40 app/models/workarea/content/block_name.rb
workarea-core-3.5.18 app/models/workarea/content/block_name.rb
workarea-core-3.4.39 app/models/workarea/content/block_name.rb
workarea-core-3.5.17 app/models/workarea/content/block_name.rb
workarea-core-3.4.38 app/models/workarea/content/block_name.rb
workarea-core-3.5.16 app/models/workarea/content/block_name.rb
workarea-core-3.4.37 app/models/workarea/content/block_name.rb