Class: Editmode::ChunkValue

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::TagHelper
Defined in:
lib/editmode/chunk_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, project_id: Editmode.project_id, **options) ⇒ ChunkValue

Returns a new instance of ChunkValue.



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

def initialize(identifier, project_id: Editmode.project_id, **options)
  @identifier = identifier
  @branch_id = options[:branch_id].presence
  @project_id = project_id
  @referrer = options[:referrer].presence || ""
  @variable_values = options[:variables].presence || {}
  @raw = options[:raw].present?
  @skip_sanitize = options[:dangerously_skip_sanitization]
  @skip_cache = options[:skip_cache]
  @transformation = options[:transformation]

  @url = "#{api_root_url}/chunks/#{identifier}"
  @cache_identifier = set_cache_identifier(identifier)

  if options[:response].present?
    @response = options[:response]
    set_response_attributes!
  else
    get_content
  end
end

Instance Attribute Details

#branch_idObject

Returns the value of attribute branch_id.



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

def branch_id
  @branch_id
end

#cache_identifierObject

Returns the value of attribute cache_identifier.



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

def cache_identifier
  @cache_identifier
end

#chunk_typeObject

Returns the value of attribute chunk_type.



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

def chunk_type
  @chunk_type
end

#collection_idObject

Returns the value of attribute collection_id.



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

def collection_id
  @collection_id
end

#contentObject



64
65
66
67
68
69
# File 'lib/editmode/chunk_value.rb', line 64

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, @raw, @skip_sanitize)
  result.try(:html_safe)
end

#identifierObject

Returns the value of attribute identifier.



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

def identifier
  @identifier
end

#project_idObject

Returns the value of attribute project_id.



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

def project_id
  @project_id
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#transformationObject

Returns the value of attribute transformation.



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

def transformation
  @transformation
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#variable_fallbacksObject

Returns the value of attribute variable_fallbacks.



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

def variable_fallbacks
  @variable_fallbacks
end

#variable_valuesObject

Returns the value of attribute variable_values.



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

def variable_values
  @variable_values
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/editmode/chunk_value.rb', line 71

def cached?
  return false if @skip_cache
  Rails.cache.exist?(cache_identifier)
end

#field(field = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/editmode/chunk_value.rb', line 38

def field(field = nil)
  # Field ID can be a slug or field_name
  if chunk_type == 'collection_item'
    if field.present?
      field_chunk = field_chunk(field)
      if field_chunk.present?
        result = field_chunk['chunk_type'] == 'image' ? set_transformation_properties!(field_chunk['content']) : field_chunk['content']
        result = variable_parse!(result, variable_fallbacks, variable_values, @raw, @skip_sanitize)
      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

#field_chunk(field) ⇒ Object



59
60
61
62
# File 'lib/editmode/chunk_value.rb', line 59

def field_chunk(field)
  field.downcase!
  @content.detect {|f| f["custom_field_identifier"].downcase == field || f["custom_field_name"].downcase == field }
end