Class: Editmode::ChunkValue
- Inherits:
-
Object
- Object
- Editmode::ChunkValue
- Includes:
- ActionView::Context, ActionView::Helpers::TagHelper
- Defined in:
- lib/editmode/chunk_value.rb
Instance Attribute Summary collapse
-
#branch_id ⇒ Object
Returns the value of attribute branch_id.
-
#cache_identifier ⇒ Object
Returns the value of attribute cache_identifier.
-
#chunk_type ⇒ Object
Returns the value of attribute chunk_type.
-
#collection_id ⇒ Object
Returns the value of attribute collection_id.
- #content ⇒ Object
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#project_id ⇒ Object
Returns the value of attribute project_id.
-
#response ⇒ Object
Returns the value of attribute response.
-
#transformation ⇒ Object
Returns the value of attribute transformation.
-
#url ⇒ Object
Returns the value of attribute url.
-
#variable_fallbacks ⇒ Object
Returns the value of attribute variable_fallbacks.
-
#variable_values ⇒ Object
Returns the value of attribute variable_values.
Instance Method Summary collapse
- #cached? ⇒ Boolean
- #field(field = nil) ⇒ Object
- #field_chunk(field) ⇒ Object
-
#initialize(identifier, project_id: Editmode.project_id, **options) ⇒ ChunkValue
constructor
A new instance of ChunkValue.
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, **) @identifier = identifier @branch_id = [:branch_id].presence @project_id = project_id @referrer = [:referrer].presence || "" @variable_values = [:variables].presence || {} @raw = [:raw].present? @skip_sanitize = [:dangerously_skip_sanitization] @skip_cache = [:skip_cache] @transformation = [:transformation] @url = "#{api_root_url}/chunks/#{identifier}" @cache_identifier = set_cache_identifier(identifier) if [:response].present? @response = [:response] set_response_attributes! else get_content end end |
Instance Attribute Details
#branch_id ⇒ Object
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_identifier ⇒ Object
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_type ⇒ Object
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_id ⇒ Object
Returns the value of attribute collection_id.
9 10 11 |
# File 'lib/editmode/chunk_value.rb', line 9 def collection_id @collection_id end |
#content ⇒ Object
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 |
#identifier ⇒ Object
Returns the value of attribute identifier.
9 10 11 |
# File 'lib/editmode/chunk_value.rb', line 9 def identifier @identifier end |
#project_id ⇒ Object
Returns the value of attribute project_id.
9 10 11 |
# File 'lib/editmode/chunk_value.rb', line 9 def project_id @project_id end |
#response ⇒ Object
Returns the value of attribute response.
9 10 11 |
# File 'lib/editmode/chunk_value.rb', line 9 def response @response end |
#transformation ⇒ Object
Returns the value of attribute transformation.
9 10 11 |
# File 'lib/editmode/chunk_value.rb', line 9 def transformation @transformation end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/editmode/chunk_value.rb', line 9 def url @url end |
#variable_fallbacks ⇒ Object
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_values ⇒ Object
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
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 |