Class: Editmode::ChunkValue

Inherits:
Object
  • Object
show all
Includes:
ActionViewExtensions::EditmodeHelper
Defined in:
lib/editmode/chunk_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActionViewExtensions::EditmodeHelper

#api_root_url, #api_version, #chunk_collection, #chunk_display, #chunk_field_value, #no_response_received, #render_chunk_content, #require_field_id, #variable_parse!

Constructor Details

#initialize(identifier, **options) ⇒ ChunkValue



9
10
11
12
13
14
# File 'lib/editmode/chunk_value.rb', line 9

def initialize(identifier, **options)
  @identifier = identifier
  @branch_id = options[:branch_id].presence
  @variable_values = options[:values].presence || {}
  get_content
end

Instance Attribute Details

#branch_idObject

Returns the value of attribute branch_id.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def branch_id
  @branch_id
end

#chunk_typeObject

Returns the value of attribute chunk_type.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def chunk_type
  @chunk_type
end

#contentObject

Returns the value of attribute content.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def content
  @content
end

#identifierObject

Returns the value of attribute identifier.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def identifier
  @identifier
end

#project_idObject

Returns the value of attribute project_id.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def project_id
  @project_id
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def response
  @response
end

#variable_fallbacksObject

Returns the value of attribute variable_fallbacks.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def variable_fallbacks
  @variable_fallbacks
end

#variable_valuesObject

Returns the value of attribute variable_values.



5
6
7
# File 'lib/editmode/chunk_value.rb', line 5

def variable_values
  @variable_values
end

Instance Method Details

#field(field = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/editmode/chunk_value.rb', line 16

def field(field = nil)
  # Field ID can be a slug or field_name
  if chunk_type == 'collection_item'
    if field.present?
      field.downcase!
      field_content = content.detect {|f| f["custom_field_identifier"].downcase == field || f["custom_field_name"].downcase == field }
      if field_content.present?
        result = field_content['content']
        result = variable_parse!(result, variable_fallbacks, variable_values)
      else
        raise no_response_received(field)
      end
    else
      raise require_field_id
    end
  else
    raise "undefined method field for chunk_type: #{chunk_type}"
  end
  result ||= content
end