Class: Workarea::Content::BlockName

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/content/block_name.rb

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ BlockName

Returns a new instance of BlockName.



4
5
6
# File 'app/models/workarea/content/block_name.rb', line 4

def initialize(block)
  @block = block
end

Instance Method Details

#fields_with_displayable_valuesObject



8
9
10
11
12
13
14
15
# File 'app/models/workarea/content/block_name.rb', line 8

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

#find_field_value(field) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/workarea/content/block_name.rb', line 17

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

#first_displayable_valueObject



29
30
31
32
33
34
# File 'app/models/workarea/content/block_name.rb', line 29

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

#to_sObject



36
37
38
39
40
41
42
# File 'app/models/workarea/content/block_name.rb', line 36

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