editmode 1.2.0 → 1.2.5

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: c56cee03d73400139003e5a7b643e5ba0cc87827dd8373f973e4e5bbbf41e234
4
- data.tar.gz: 5cc247b0737cf3a34a7b7507895ff5b6b67d14363bd4ef328ab0630155ac8cd2
3
+ metadata.gz: 944adb32f3ec654befa27d7f8f85e95939d56af9fa6892b890702d4d1a6b1ac1
4
+ data.tar.gz: 89c1928fa45b58c2cb7579b38aa0ffcc109c199f3b62c3696f799fb739ddf88b
5
5
  SHA512:
6
- metadata.gz: 909f2860b00987679e572d4c7c3202fe208fc7cc99ae75df7b698e5371b97f90861f65598193a0882fd924ec653978c0547d57bdc66ec2ad76651b6d1358aff1
7
- data.tar.gz: 6ccc29e44f1cd14ad281c182a78904f74a199b1b9fc22fe5cf7757753b24b3ac5eeb9a0db2098e34662b0e1448a49831e9bc99db7f78ff931485ae64cded024b
6
+ metadata.gz: 2aade9c6ba2688cfedcedef29bb4fc58d2c5a57f671458f79925951b79fbf9374f2e94fb2a3d2478856a725e99b1fb9544b67045315121b2b342003c84244b40
7
+ data.tar.gz: 98cd35b091d8993d601443fa7cf4097b0f1d42ea4b93d6c15eafc936b1eb5cc66086b666c752ad7c2cc6ee563dcb08a110f88e50b1e363bb053d23fd0d63118e
@@ -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"
@@ -135,7 +131,7 @@ module Editmode
135
131
  end
136
132
  else
137
133
  content_tag("em-span", :class => css_class, :data => chunk_data.merge!({:chunk_editable => true}) ) do
138
- chunk_content
134
+ chunk_content.html_safe
139
135
  end
140
136
  end
141
137
  when "image"
@@ -150,62 +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
156
 
166
- if !cached_content_present
167
- response = HTTParty.get(url)
168
- response_received = true if response.code == 200
169
- end
170
-
171
- if !cached_content_present && !response_received
172
- 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
173
164
  else
174
- if field.present? && response.present?
175
- field_content = response["content"].detect {|f| f["custom_field_identifier"].downcase == field.downcase || f["custom_field_name"].downcase == field.downcase }
176
- if field_content
177
- content = field_content["content"]
178
- type = field_content["chunk_type"]
179
- identifier = Rails.cache.fetch("#{cache_identifier}_field_identifier") do
180
- field_content["identifier"]
181
- end
182
- end
183
- end
184
-
185
- variable_fallbacks = Rails.cache.fetch("#{cache_identifier}_variables") do
186
- response['variable_fallbacks'].presence || {}
187
- end
188
-
189
- chunk_content = Rails.cache.fetch(cache_identifier) do
190
- content.presence || response["content"]
191
- end
192
-
193
- chunk_type = Rails.cache.fetch("#{cache_identifier}_type") do
194
- type.presence || response['chunk_type']
195
- end
196
-
197
- identifier = Rails.cache.fetch("#{cache_identifier}_field_identifier") do
198
- identifier
199
- end
200
-
201
- options[:variable_fallbacks] = variable_fallbacks
202
- options[:variable_values] = options[:variables].presence || {}
203
-
204
- options[:cache_identifier] = parent_identifier.presence || identifier
205
-
206
- 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_"
207
168
  end
208
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
+
209
174
  rescue => error
210
175
  # Show fallback content by default
211
176
  return content_tag("em-span", &block) if block_given?
@@ -232,22 +197,6 @@ module Editmode
232
197
  end
233
198
  alias_method :E, :render_chunk
234
199
 
235
-
236
- def variable_parse!(content, variables = {}, values = {})
237
- tokens = content.scan(/\{{(.*?)\}}/)
238
- if tokens.any?
239
- tokens.flatten!
240
- tokens.each do |token|
241
- token_value = values[token.to_sym] || variables[token] || ""
242
- sanitized_value = ActionController::Base.helpers.sanitize(token_value)
243
-
244
- content.gsub!("{{#{token}}}", sanitized_value)
245
- end
246
- end
247
-
248
- content
249
- end
250
-
251
200
  def no_response_received(id = "")
252
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."
253
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
5
+
6
+ attr_accessor :identifier, :variable_values, :branch_id,
7
+ :variable_fallbacks, :chunk_type, :project_id,
8
+ :url, :collection_id, :cache_identifier,
9
+ :response
4
10
 
5
- attr_accessor :identifier, :variable_values, :branch_id,
6
- :variable_fallbacks, :chunk_type, :project_id,
7
- :response
8
-
9
11
  attr_writer :content
10
12
 
11
- def initialize(identifier, **options)
13
+ def initialize(identifier, project_id: Editmode.project_id, **options)
12
14
  @identifier = identifier
13
15
  @branch_id = options[:branch_id].presence
16
+ @project_id = project_id
14
17
  @variable_values = options[:variables].presence || {}
15
- get_content
18
+ @raw = options[:raw].present?
19
+ @skip_sanitize = options[:dangerously_skip_sanitization]
20
+
21
+ @url = "#{api_root_url}/chunks/#{identifier}"
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
- def field(field = nil)
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)
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, @skip_sanitize)
27
40
  else
28
41
  raise no_response_received(field)
29
42
  end
@@ -33,17 +46,33 @@ module Editmode
33
46
  else
34
47
  raise "undefined method 'field` for chunk_type: #{chunk_type} \n"
35
48
  end
36
- result || @content
49
+ result ||= @content
50
+ result.try(:html_safe)
51
+ end
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 }
37
56
  end
38
57
 
39
58
  def content
40
- raise "undefined method 'content` for chunk_type: collection_item \nDid you mean? field" if chunk_type == 'collection_item'
41
-
42
- variable_parse!(@content, variable_fallbacks, variable_values)
59
+ raise "undefined method 'content' for chunk_type: collection_item \nDid you mean? field" if chunk_type == 'collection_item'
60
+
61
+ result = variable_parse!(@content, variable_fallbacks, variable_values, @raw, @skip_sanitize)
62
+ result.try(:html_safe)
43
63
  end
44
64
 
45
65
  private
46
66
 
67
+ # Todo: Transfer to helper utils
68
+ def api_root_url
69
+ ENV["EDITMODE_OVERRIDE_API_URL"] || "https://api.editmode.com"
70
+ end
71
+
72
+ def set_cache_identifier(id)
73
+ "chunk_#{project_id}#{branch_id}#{id}"
74
+ end
75
+
47
76
  def json?(json)
48
77
  JSON.parse(json)
49
78
  return true
@@ -51,19 +80,46 @@ module Editmode
51
80
  return false
52
81
  end
53
82
 
54
- def get_content
55
- branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
56
- url = "#{api_root_url}/chunks/#{identifier}?project_id=#{Editmode.project_id}&#{branch_params}"
83
+ def variable_parse!(content, variables = {}, values = {}, raw = true, skip_sanitize=false)
84
+ content = ActionController::Base.helpers.sanitize(content) unless skip_sanitize
85
+ tokens = content.scan(/\{{(.*?)\}}/)
86
+ if tokens.any?
87
+ tokens.flatten!
88
+ tokens.each do |token|
89
+ token_value = values[token.to_sym] || variables[token] || ""
90
+ sanitized_value = ActionController::Base.helpers.sanitize(token_value)
57
91
 
58
- cache_identifier = "chunk_value_#{identifier}#{branch_id}"
59
- cached_content_present = Rails.cache.exist?(cache_identifier)
92
+ unless raw
93
+ sanitized_value = content_tag("em-var", :data => {chunk_variable: token, chunk_variable_value: sanitized_value}) do
94
+ sanitized_value
95
+ end
96
+ end
60
97
 
61
- if !cached_content_present
62
- http_response = HTTParty.get(url)
98
+ content.gsub!("{{#{token}}}", sanitized_value)
99
+ end
100
+ end
101
+
102
+ content
103
+ end
104
+
105
+ def cached?
106
+ Rails.cache.exist?(cache_identifier)
107
+ end
108
+
109
+ def query_params
110
+ the_params = { 'project_id' => project_id }
111
+ the_params['branch_id'] = branch_id if branch_id.present?
112
+
113
+ the_params
114
+ end
115
+
116
+ def get_content
117
+ if !cached?
118
+ http_response = HTTParty.get(url, query: query_params)
63
119
  response_received = true if http_response.code == 200
64
120
  end
65
121
 
66
- if !cached_content_present && !response_received
122
+ if !cached? && !response_received
67
123
  raise no_response_received(identifier)
68
124
  else
69
125
  cached_response = Rails.cache.fetch(cache_identifier) do
@@ -71,13 +127,17 @@ module Editmode
71
127
  end
72
128
 
73
129
  @response = json?(cached_response) ? JSON.parse(cached_response) : cached_response
130
+ set_response_attributes!
131
+ end
132
+ end
74
133
 
75
- @content = response['content']
76
- @chunk_type = response['chunk_type']
77
- @project_id = response['project_id']
78
- @variable_fallbacks = response['variable_fallbacks'].presence || {}
79
- end
134
+ def set_response_attributes!
135
+ @content = response['content']
136
+ @chunk_type = response['chunk_type']
137
+ @variable_fallbacks = response['variable_fallbacks'].presence || {}
138
+ @collection_id = response["collection"]["identifier"] if chunk_type == 'collection_item'
139
+ @branch_id = response['branch_id']
80
140
  end
81
141
 
82
142
  end
83
- end
143
+ end
@@ -4,20 +4,37 @@ 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)
8
-
9
- if chunk.chunk_type == 'collection_item'
10
- chunk.field(field)
11
- else
12
- chunk.content
13
- end
7
+ chunk = Editmode::ChunkValue.new(identifier, options.merge({raw: true}))
8
+ render_noneditable_chunk(chunk, field, options)
14
9
  rescue => er
15
10
  puts er
16
11
  end
17
12
  end
18
13
 
14
+ def render_noneditable_chunk(chunk, field=nil, options=nil)
15
+ return render_collection_item(chunk, field, options) if chunk.chunk_type == 'collection_item'
16
+
17
+ render_content(chunk, options)
18
+ end
19
+
20
+ def render_collection_item(chunk, field=nil, options=nil)
21
+ return render_image(chunk.field(field), options[:class]) if chunk.field_chunk(field)['chunk_type'] == 'image'
22
+
23
+ chunk.field(field)
24
+ end
25
+
26
+ def render_content(chunk, options=nil)
27
+ return render_image(chunk.content, options[:class]) if chunk.chunk_type == 'image'
28
+
29
+ chunk.content
30
+ end
31
+
32
+ def render_image(content, css_class=nil)
33
+ image_tag(content, class: css_class)
34
+ end
35
+
19
36
  def render_custom_field_raw(label, options={})
20
- e(@custom_field_chunk["identifier"], label, options)
37
+ e(@custom_field_chunk["identifier"], label, options.merge({response: @custom_field_chunk}))
21
38
  end
22
39
  alias_method :f, :render_custom_field_raw
23
40
 
@@ -33,4 +50,4 @@ module Editmode
33
50
  return field, options
34
51
  end
35
52
  end
36
- end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module Editmode
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.5"
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.0
4
+ version: 1.2.5
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-21 00:00:00.000000000 Z
11
+ date: 2020-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler