editmode 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64a20fe2b3a8ccf1cd1dafc516a2484b6f4ce6701ecdd2d4f2b8a431137959bd
4
- data.tar.gz: 683549e7c9085dda0d1ef74ff7e2b142fa746372c1da288af5e154017b69b957
3
+ metadata.gz: 2beab37962d65855d3ecfd66b6d9731fd4f1826eac260d97a30d8c1b022b9ff8
4
+ data.tar.gz: b05b7f2425a796262ec31e2d8cc447405d9bb2ed339b37a6f71cc2d52cd10ad7
5
5
  SHA512:
6
- metadata.gz: 92953c97a18f6fb3b91389f4beab95ffc75f92bf91e4a603094a016c6673724c49442a46643259b0e9a9c4b48c855f9821eee7e19b7801cf2fca2b1d8c2cce4b
7
- data.tar.gz: 3763c6e47edd0cd6c326a7cc80a0583b20d3a9db66d652b3416c643548a3356e45896846ea321b4a63f3b605e07a6f1df3dc6b8f070b63939d5b6b86fbb3c2a6
6
+ metadata.gz: 75944607fde64c8da7a99751a421d43fbc473b7f0c0c7ce34297b16d70869d2d393de8731fed85dfd6387ba22977ef3e92ae892370b07a13a9ed41bad7d6472b
7
+ data.tar.gz: c2109b7ca239fce3ddb025348bc534201002d6396a18e4eec9357fdc992f64489efed095e4568db984148e5ff45aafad70b1026cf93111d9c04aa20bed329924
@@ -80,9 +80,8 @@ module Editmode
80
80
  def chunk_field_value(parent_chunk_object, custom_field_identifier, options = {})
81
81
  begin
82
82
  chunk_identifier = parent_chunk_object["identifier"]
83
- custom_field_item = parent_chunk_object["content"].detect do |f|
84
- f["custom_field_identifier"].try(:downcase) == custom_field_identifier.try(:downcase) || f["custom_field_name"].try(:downcase) == custom_field_identifier.try(:downcase)
85
- end
83
+ chunk_value = Editmode::ChunkValue.new(parent_chunk_object["identifier"], options.merge({response: parent_chunk_object}))
84
+ custom_field_item = chunk_value.field_chunk(custom_field_identifier)
86
85
 
87
86
  options[:field] = custom_field_identifier
88
87
 
@@ -94,7 +93,7 @@ module Editmode
94
93
  if custom_field_item.present?
95
94
  render_chunk_content(
96
95
  custom_field_item["identifier"],
97
- custom_field_item["content"],
96
+ chunk_value.field(custom_field_identifier),
98
97
  custom_field_item["chunk_type"],
99
98
  { parent_identifier: chunk_identifier, custom_field_identifier: custom_field_identifier}.merge(options)
100
99
  )
@@ -106,12 +105,7 @@ module Editmode
106
105
  end
107
106
 
108
107
  def render_chunk_content(chunk_identifier, chunk_content, chunk_type,options = {})
109
-
110
108
  begin
111
- # Always sanitize the content!!
112
- chunk_content = ActionController::Base.helpers.sanitize(chunk_content) unless chunk_type == 'rich_text'
113
- chunk_content = variable_parse!(chunk_content, options[:variable_fallbacks], options[:variable_values])
114
-
115
109
  css_class = options[:class]
116
110
  cache_id = options[:cache_identifier]
117
111
 
@@ -126,6 +120,8 @@ module Editmode
126
120
  chunk_data.merge!({parent_identifier: options[:parent_identifier]}) if options[:parent_identifier].present?
127
121
  chunk_data.merge!({custom_field_identifier: options[:custom_field_identifier]}) if options[:custom_field_identifier].present?
128
122
  chunk_data.merge!({chunk_cache_id: cache_id}) if cache_id.present?
123
+ chunk_data.merge!({chunk_collection_identifier: options[:collection_id]}) if options[:collection_id].present?
124
+ chunk_data.merge!({chunk_content_key: options[:content_key]}) if options[:content_key].present?
129
125
 
130
126
  case display_type
131
127
  when "span"
@@ -150,61 +146,31 @@ module Editmode
150
146
  end
151
147
 
152
148
  def chunk_display(label, identifier, options = {}, &block)
153
- branch_id = params[:em_branch_id]
149
+ options[:branch_id] = params[:em_branch_id] if params[:em_branch_id].present?
154
150
  # This method should never show an error.
155
151
  # If anything goes wrong fetching content
156
152
  # We should just show blank content, not
157
153
  # prevent the page from loading.
158
154
  begin
159
- branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
160
155
  field = options[:field].presence || ""
161
- cache_identifier = "chunk_#{identifier}#{branch_id}#{field}"
162
- url = "#{api_root_url}/chunks/#{identifier}?project_id=#{Editmode.project_id}&#{branch_params}"
163
- cached_content_present = Rails.cache.exist?(cache_identifier)
164
- parent_identifier = identifier if field.present?
165
- if !cached_content_present
166
- response = HTTParty.get(url)
167
- response_received = true if response.code == 200
168
- end
169
156
 
170
- if !cached_content_present && !response_received
171
- raise "No response received"
157
+ chunk_value = Editmode::ChunkValue.new(identifier, options)
158
+
159
+ if field.present? && chunk_value.chunk_type == 'collection_item'
160
+ chunk_content = chunk_value.field(field)
161
+ identifier = chunk_value.field_chunk(field)["identifier"]
162
+ chunk_type = chunk_value.field_chunk(field)["chunk_type"]
163
+ options[:collection_id] = chunk_value.collection_id
172
164
  else
173
- if field.present? && response.present?
174
- field_content = response["content"].detect {|f| f["custom_field_identifier"].downcase == field.downcase || f["custom_field_name"].downcase == field.downcase }
175
- if field_content
176
- content = field_content["content"]
177
- type = field_content["chunk_type"]
178
- identifier = Rails.cache.fetch("#{cache_identifier}_field_identifier") do
179
- field_content["identifier"]
180
- end
181
- end
182
- end
183
-
184
- variable_fallbacks = Rails.cache.fetch("#{cache_identifier}_variables") do
185
- response['variable_fallbacks'].presence || {}
186
- end
187
-
188
- chunk_content = Rails.cache.fetch(cache_identifier) do
189
- content.presence || response["content"]
190
- end
191
-
192
- chunk_type = Rails.cache.fetch("#{cache_identifier}_type") do
193
- type.presence || response['chunk_type']
194
- end
195
-
196
- identifier = Rails.cache.fetch("#{cache_identifier}_field_identifier") do
197
- identifier
198
- end
199
-
200
- options[:variable_fallbacks] = variable_fallbacks
201
- options[:variable_values] = options[:variables].presence || {}
202
-
203
- options[:cache_identifier] = parent_identifier.presence || identifier
204
-
205
- render_chunk_content(identifier,chunk_content,chunk_type, options)
165
+ chunk_content = chunk_value.content
166
+ chunk_type = chunk_value.chunk_type
167
+ identifier = chunk_value.response["identifier"] unless identifier.include? "cnk_"
206
168
  end
207
169
 
170
+ options[:cache_identifier] = chunk_value.identifier
171
+ options[:content_key] = chunk_value.response.try(:[], "content_key")
172
+ render_chunk_content(identifier, chunk_content, chunk_type, options)
173
+
208
174
  rescue => error
209
175
  # Show fallback content by default
210
176
  return content_tag("em-span", &block) if block_given?
@@ -231,28 +197,6 @@ module Editmode
231
197
  end
232
198
  alias_method :E, :render_chunk
233
199
 
234
-
235
- def variable_parse!(content, variables = {}, values = {}, raw = false)
236
- tokens = content.scan(/\{{(.*?)\}}/)
237
- if tokens.any?
238
- tokens.flatten!
239
- tokens.each do |token|
240
- token_value = values[token.to_sym] || variables[token] || ""
241
- sanitized_value = ActionController::Base.helpers.sanitize(token_value)
242
-
243
- unless raw
244
- sanitized_value = content_tag("em-var", :data => {chunk_variable: token, chunk_variable_value: sanitized_value}) do
245
- sanitized_value
246
- end
247
- end
248
-
249
- content.gsub!("{{#{token}}}", sanitized_value)
250
- end
251
- end
252
-
253
- content
254
- end
255
-
256
200
  def no_response_received(id = "")
257
201
  "Sorry, we can't find a chunk using this identifier: \"#{id}\". This can happen if you've deleted a chunk on editmode.com or if your local cache is out of date. If it persists, try running Rails.cache clear."
258
202
  end
@@ -1,29 +1,42 @@
1
1
  module Editmode
2
2
  class ChunkValue
3
- include Editmode::ActionViewExtensions::EditmodeHelper
3
+ include ActionView::Helpers::TagHelper
4
+ include ActionView::Context
4
5
 
5
6
  attr_accessor :identifier, :variable_values, :branch_id,
6
7
  :variable_fallbacks, :chunk_type, :project_id,
8
+ :url, :collection_id, :cache_identifier,
7
9
  :response
8
-
10
+
9
11
  attr_writer :content
10
12
 
11
13
  def initialize(identifier, **options)
12
14
  @identifier = identifier
13
15
  @branch_id = options[:branch_id].presence
16
+ @project_id = Editmode.project_id
14
17
  @variable_values = options[:variables].presence || {}
15
- get_content
18
+ @raw = options[:raw].present?
19
+
20
+ branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
21
+ @url = "#{api_root_url}/chunks/#{identifier}?project_id=#{project_id}&#{branch_params}"
22
+ @cache_identifier = set_cache_identifier(identifier)
23
+
24
+ if options[:response].present?
25
+ @response = options[:response]
26
+ set_response_attributes!
27
+ else
28
+ get_content
29
+ end
16
30
  end
17
31
 
18
32
  def field(field = nil)
19
33
  # Field ID can be a slug or field_name
20
34
  if chunk_type == 'collection_item'
21
35
  if field.present?
22
- field.downcase!
23
- field_content = @content.detect {|f| f["custom_field_identifier"].downcase == field || f["custom_field_name"].downcase == field }
24
- if field_content.present?
25
- result = field_content['content']
26
- result = variable_parse!(result, variable_fallbacks, variable_values, true)
36
+ field_chunk = field_chunk(field)
37
+ if field_chunk.present?
38
+ result = field_chunk['content']
39
+ result = variable_parse!(result, variable_fallbacks, variable_values, @raw)
27
40
  else
28
41
  raise no_response_received(field)
29
42
  end
@@ -37,14 +50,27 @@ module Editmode
37
50
  result.try(:html_safe)
38
51
  end
39
52
 
53
+ def field_chunk(field)
54
+ field.downcase!
55
+ @content.detect {|f| f["custom_field_identifier"].downcase == field || f["custom_field_name"].downcase == field }
56
+ end
57
+
40
58
  def content
41
59
  raise "undefined method 'content` for chunk_type: collection_item \nDid you mean? field" if chunk_type == 'collection_item'
42
60
 
43
- result = variable_parse!(@content, variable_fallbacks, variable_values, true)
61
+ result = variable_parse!(@content, variable_fallbacks, variable_values, @raw)
44
62
  result.try(:html_safe)
45
63
  end
46
64
 
47
65
  private
66
+ # Todo: Transfer to helper utils
67
+ def api_root_url
68
+ ENV["EDITMODE_OVERRIDE_API_URL"] || "https://api.editmode.com"
69
+ end
70
+
71
+ def set_cache_identifier(id)
72
+ "chunk_#{project_id}#{branch_id}#{id}"
73
+ end
48
74
 
49
75
  def json?(json)
50
76
  JSON.parse(json)
@@ -53,19 +79,39 @@ module Editmode
53
79
  return false
54
80
  end
55
81
 
56
- def get_content
57
- branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
58
- url = "#{api_root_url}/chunks/#{identifier}?project_id=#{Editmode.project_id}&#{branch_params}"
82
+ def variable_parse!(content, variables = {}, values = {}, raw = true)
83
+ content = ActionController::Base.helpers.sanitize(content)
84
+ tokens = content.scan(/\{{(.*?)\}}/)
85
+ if tokens.any?
86
+ tokens.flatten!
87
+ tokens.each do |token|
88
+ token_value = values[token.to_sym] || variables[token] || ""
89
+ sanitized_value = ActionController::Base.helpers.sanitize(token_value)
90
+
91
+ unless raw
92
+ sanitized_value = content_tag("em-var", :data => {chunk_variable: token, chunk_variable_value: sanitized_value}) do
93
+ sanitized_value
94
+ end
95
+ end
96
+
97
+ content.gsub!("{{#{token}}}", sanitized_value)
98
+ end
99
+ end
59
100
 
60
- cache_identifier = "chunk_value_#{identifier}#{branch_id}"
61
- cached_content_present = Rails.cache.exist?(cache_identifier)
101
+ content
102
+ end
62
103
 
63
- if !cached_content_present
104
+ def cached?
105
+ Rails.cache.exist?(cache_identifier)
106
+ end
107
+
108
+ def get_content
109
+ if !cached?
64
110
  http_response = HTTParty.get(url)
65
111
  response_received = true if http_response.code == 200
66
112
  end
67
113
 
68
- if !cached_content_present && !response_received
114
+ if !cached? && !response_received
69
115
  raise no_response_received(identifier)
70
116
  else
71
117
  cached_response = Rails.cache.fetch(cache_identifier) do
@@ -73,12 +119,15 @@ module Editmode
73
119
  end
74
120
 
75
121
  @response = json?(cached_response) ? JSON.parse(cached_response) : cached_response
122
+ set_response_attributes!
123
+ end
124
+ end
76
125
 
77
- @content = response['content']
78
- @chunk_type = response['chunk_type']
79
- @project_id = response['project_id']
80
- @variable_fallbacks = response['variable_fallbacks'].presence || {}
81
- end
126
+ def set_response_attributes!
127
+ @content = response['content']
128
+ @chunk_type = response['chunk_type']
129
+ @variable_fallbacks = response['variable_fallbacks'].presence || {}
130
+ @collection_id = response["collection"]["identifier"] if chunk_type == 'collection_item'
82
131
  end
83
132
 
84
133
  end
@@ -4,7 +4,7 @@ module Editmode
4
4
  def e(identifier, *args)
5
5
  field, options = parse_arguments(args)
6
6
  begin
7
- chunk = Editmode::ChunkValue.new(identifier, options)
7
+ chunk = Editmode::ChunkValue.new(identifier, options.merge({raw: true}))
8
8
 
9
9
  if chunk.chunk_type == 'collection_item'
10
10
  chunk.field(field)
@@ -17,7 +17,7 @@ module Editmode
17
17
  end
18
18
 
19
19
  def render_custom_field_raw(label, options={})
20
- e(@custom_field_chunk["identifier"], label, options)
20
+ e(@custom_field_chunk["identifier"], label, options.merge({response: @custom_field_chunk}))
21
21
  end
22
22
  alias_method :f, :render_custom_field_raw
23
23
 
@@ -1,3 +1,3 @@
1
1
  module Editmode
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: editmode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Ennis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-23 00:00:00.000000000 Z
11
+ date: 2020-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler