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, #render_chunk_content, #render_custom_field, #require_field_id, #variable_parse!

Methods included from Helper

#e, #parse_arguments, #render_custom_field_raw

Constructor Details

#initialize(identifier, **options) ⇒ ChunkValue

Returns a new instance of ChunkValue.



11
12
13
14
15
16
# File 'lib/editmode/chunk_value.rb', line 11

def initialize(identifier, **options)
  @identifier = identifier
  @branch_id = options[:branch_id].presence
  @variable_values = options[:variables].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



40
41
42
43
44
45
# File 'lib/editmode/chunk_value.rb', line 40

def content
  raise "undefined method 'content` for chunk_type: collection_item \nDid you mean? field" if chunk_type == 'collection_item'
  
  result = variable_parse!(@content, variable_fallbacks, variable_values, true)
  result.try(:html_safe)
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



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

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, true)
      else
        raise no_response_received(field)
      end
    else
      raise require_field_id
    end
  else
    raise "undefined method 'field` for chunk_type: #{chunk_type} \n"
  end
  result ||= @content
  result.try(:html_safe)
end