editmode 1.1.8 → 1.2.3

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: 611f68ffcb87c0e259a2c087f322ac8477933621d8253c35e7c59c1a9515d8a4
4
- data.tar.gz: 4427afba193aab085412cc8fb07ad01c4c7628721daef9f7b8183c325cb36e0a
3
+ metadata.gz: 78dda874aecacc9edfdf0e96598019e7dad8cc44693336d94511083238b55a91
4
+ data.tar.gz: d111ab02c60c737e5711139473e7be988f02ffdb3a8ed01a7876174348bf5d83
5
5
  SHA512:
6
- metadata.gz: ee37e1c259186a2592ca9bd6c643999a639d9e01866eeac03f904d1b102145c79949116a680a64bebb1509399164fe837d183a4474801ec555e8ba197d438718
7
- data.tar.gz: b4f7fab610eafa729f0dea3c70d221aba14d593e79998b1253a07d19f09854f1ef11870a05b17b1db7c0af57091fbafb0fd11cabafa364fd117b1468d4418412
6
+ metadata.gz: e896421ca91061b25d28bc0ea217a2891df5f06d109b00301364ad67c3813caf2de47219f0be1b13ac335b6a8b76bcdaa5dc10b5ad4a9308722e90939613cbaf
7
+ data.tar.gz: 9ad90b4008998f64e52cfe34c73c74db62633b468764f0939df16ffb4fbeb1a9acbbdee0acfe053e793ff66b316f9c8ecde7bb64544b81013828e61383995f69
data/README.md CHANGED
@@ -47,6 +47,7 @@ Editmode provides helper methods for use in your rails views and controllers.
47
47
  ```erb
48
48
  <%= E('cnk_x4ts............') %> # Using a chunk identifier
49
49
  <%= E('marketing_page_headline') %> # Using a content key
50
+ <%= E('cnk_x4ts...', class: "a-css-class") %> # Render a chunk with inline css class
50
51
  ```
51
52
 
52
53
  ### Content can also be accessed in Controllers
@@ -79,23 +80,23 @@ e("cnk_16e04a02d577afb610ce", "Email Content", variables: variable_values)
79
80
 
80
81
  ### Use collections for repeatable content
81
82
  ```erb
82
- <%= c('col_j8fbs...') do |chunk| %>
83
- <div class="user-profile">
84
- <h3 class="name">
85
- <%= F("Name") %>
86
- </h3>
87
- <p class="description">
88
- <%= f("Description") %>
89
- </p>
90
- </div>
83
+ <%= c('col_j8fbs...', class: "profiles-container", item_class: "profile-item") do %>
84
+ <h3 class="name">
85
+ <%= F("Name", class: "profile-name") %>
86
+ </h3>
87
+ <p class="description">
88
+ <%= f("Description"), class: "profile-description" %>
89
+ </p>
91
90
  <% end %>
92
91
  ```
93
92
 
94
93
  |Parameter|Type|Description|
95
94
  |---|---|---|
96
95
  | identifier | string | The first argument of `c` takes the id of the collection you want to loop through |
97
- | limit | int |`optional` The number of collection items you want to display |
96
+ | limit | int/string |`optional` The number of collection items you want to display |
98
97
  | tags | array |`optional` Filter collection items based on tags listed in this parameter |
98
+ | class | string | `optional` Class name(s) that will be added along with "chunks-collection-wrapper" to the main collection `<div>` element |
99
+ | item_class | string | `optional` Class name(s) that will be added along with "chunks-collection-item--wrapper" to all collection items |
99
100
 
100
101
 
101
102
  ## Caching
@@ -13,8 +13,7 @@ class EditmodeController < ApplicationController
13
13
  Rails.cache.delete("chunk_#{project_id}_variables")
14
14
  render status: 200, json: {:response => "success"}
15
15
  elsif params[:identifier]
16
- Rails.cache.delete("chunk_#{params[:identifier]}")
17
- Rails.cache.delete("chunk_#{params[:identifier]}_type")
16
+ Rails.cache.delete_matched("#{params[:identifier]}")
18
17
  render status: 200, json: {:response => "success"}
19
18
  else
20
19
  render status: 404, json: {:response => "no identifier specified"}
@@ -36,7 +36,7 @@ module Editmode
36
36
  begin
37
37
  Editmode::ChunkValue.new(identifier, **options )
38
38
  rescue => er
39
- raise er
39
+ puts er
40
40
  end
41
41
  end
42
42
  end
@@ -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,13 +105,9 @@ 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]
110
+ cache_id = options[:cache_identifier]
116
111
 
117
112
  if chunk_type == "image"
118
113
  display_type = "image"
@@ -124,6 +119,9 @@ module Editmode
124
119
 
125
120
  chunk_data.merge!({parent_identifier: options[:parent_identifier]}) if options[:parent_identifier].present?
126
121
  chunk_data.merge!({custom_field_identifier: options[:custom_field_identifier]}) if options[:custom_field_identifier].present?
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?
127
125
 
128
126
  case display_type
129
127
  when "span"
@@ -133,7 +131,7 @@ module Editmode
133
131
  end
134
132
  else
135
133
  content_tag("em-span", :class => css_class, :data => chunk_data.merge!({:chunk_editable => true}) ) do
136
- chunk_content
134
+ chunk_content.html_safe
137
135
  end
138
136
  end
139
137
  when "image"
@@ -148,54 +146,31 @@ module Editmode
148
146
  end
149
147
 
150
148
  def chunk_display(label, identifier, options = {}, &block)
151
- branch_id = params[:em_branch_id]
149
+ options[:branch_id] = params[:em_branch_id] if params[:em_branch_id].present?
152
150
  # This method should never show an error.
153
151
  # If anything goes wrong fetching content
154
152
  # We should just show blank content, not
155
153
  # prevent the page from loading.
156
154
  begin
157
- branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
158
155
  field = options[:field].presence || ""
159
- cache_identifier = "chunk_#{identifier}#{branch_id}#{field}"
160
- url = "#{api_root_url}/chunks/#{identifier}?project_id=#{Editmode.project_id}&#{branch_params}"
161
- cached_content_present = Rails.cache.exist?(cache_identifier)
162
-
163
- if !cached_content_present
164
- response = HTTParty.get(url)
165
- response_received = true if response.code == 200
166
- end
167
156
 
168
- if !cached_content_present && !response_received
169
- 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
170
164
  else
171
- if field.present? && response.present?
172
- field_content = response["content"].detect {|f| f["custom_field_identifier"].downcase == field.downcase || f["custom_field_name"].downcase == field.downcase }
173
- if field_content
174
- content = field_content["content"]
175
- type = field_content["chunk_type"]
176
- identifier = field_content["identifier"]
177
- end
178
- end
179
-
180
- variable_fallbacks = Rails.cache.fetch("#{cache_identifier}_variables") do
181
- response['variable_fallbacks'].presence || {}
182
- end
183
-
184
- chunk_content = Rails.cache.fetch(cache_identifier) do
185
- content.presence || response["content"]
186
- end
187
-
188
- chunk_type = Rails.cache.fetch("#{cache_identifier}_type") do
189
- type.presence || response['chunk_type']
190
- end
191
-
192
- options[:variable_fallbacks] = variable_fallbacks
193
- options[:variable_values] = options[:variables].presence || {}
194
-
195
- render_chunk_content(identifier,chunk_content,chunk_type, options)
196
-
165
+ chunk_content = chunk_value.content
166
+ chunk_type = chunk_value.chunk_type
167
+ identifier = chunk_value.response["identifier"] unless identifier.include? "cnk_"
197
168
  end
198
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
+
199
174
  rescue => error
200
175
  # Show fallback content by default
201
176
  return content_tag("em-span", &block) if block_given?
@@ -222,22 +197,6 @@ module Editmode
222
197
  end
223
198
  alias_method :E, :render_chunk
224
199
 
225
-
226
- def variable_parse!(content, variables = {}, values = {})
227
- tokens = content.scan(/\{{(.*?)\}}/)
228
- if tokens.any?
229
- tokens.flatten!
230
- tokens.each do |token|
231
- token_value = values[token.to_sym] || variables[token] || ""
232
- sanitized_value = ActionController::Base.helpers.sanitize(token_value)
233
-
234
- content.gsub!("{{#{token}}}", sanitized_value)
235
- end
236
- end
237
-
238
- content
239
- end
240
-
241
200
  def no_response_received(id = "")
242
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."
243
202
  end
@@ -1,29 +1,41 @@
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
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
+ @url = "#{api_root_url}/chunks/#{identifier}"
21
+ @cache_identifier = set_cache_identifier(identifier)
22
+
23
+ if options[:response].present?
24
+ @response = options[:response]
25
+ set_response_attributes!
26
+ else
27
+ get_content
28
+ end
16
29
  end
17
30
 
18
31
  def field(field = nil)
19
32
  # Field ID can be a slug or field_name
20
33
  if chunk_type == 'collection_item'
21
34
  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)
35
+ field_chunk = field_chunk(field)
36
+ if field_chunk.present?
37
+ result = field_chunk['content']
38
+ result = variable_parse!(result, variable_fallbacks, variable_values, @raw)
27
39
  else
28
40
  raise no_response_received(field)
29
41
  end
@@ -31,19 +43,35 @@ module Editmode
31
43
  raise require_field_id
32
44
  end
33
45
  else
34
- raise NoMethodError.new "undefined method 'field` for chunk_type: #{chunk_type} \n"
46
+ raise "undefined method 'field` for chunk_type: #{chunk_type} \n"
35
47
  end
36
- result || @content
48
+ result ||= @content
49
+ result.try(:html_safe)
50
+ end
51
+
52
+ def field_chunk(field)
53
+ field.downcase!
54
+ @content.detect {|f| f["custom_field_identifier"].downcase == field || f["custom_field_name"].downcase == field }
37
55
  end
38
56
 
39
57
  def content
40
- raise NoMethodError.new "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)
58
+ raise "undefined method 'content' for chunk_type: collection_item \nDid you mean? field" if chunk_type == 'collection_item'
59
+
60
+ result = variable_parse!(@content, variable_fallbacks, variable_values, @raw)
61
+ result.try(:html_safe)
43
62
  end
44
63
 
45
64
  private
46
65
 
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
74
+
47
75
  def json?(json)
48
76
  JSON.parse(json)
49
77
  return true
@@ -51,19 +79,46 @@ module Editmode
51
79
  return false
52
80
  end
53
81
 
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}"
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)
57
90
 
58
- cache_identifier = "chunk_value_#{identifier}#{branch_id}"
59
- cached_content_present = Rails.cache.exist?(cache_identifier)
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
60
96
 
61
- if !cached_content_present
62
- http_response = HTTParty.get(url)
97
+ content.gsub!("{{#{token}}}", sanitized_value)
98
+ end
99
+ end
100
+
101
+ content
102
+ end
103
+
104
+ def cached?
105
+ Rails.cache.exist?(cache_identifier)
106
+ end
107
+
108
+ def query_params
109
+ the_params = { 'project_id' => project_id }
110
+ the_params['branch_id'] = branch_id if branch_id.present?
111
+
112
+ the_params
113
+ end
114
+
115
+ def get_content
116
+ if !cached?
117
+ http_response = HTTParty.get(url, query: query_params)
63
118
  response_received = true if http_response.code == 200
64
119
  end
65
120
 
66
- if !cached_content_present && !response_received
121
+ if !cached? && !response_received
67
122
  raise no_response_received(identifier)
68
123
  else
69
124
  cached_response = Rails.cache.fetch(cache_identifier) do
@@ -71,13 +126,17 @@ module Editmode
71
126
  end
72
127
 
73
128
  @response = json?(cached_response) ? JSON.parse(cached_response) : cached_response
129
+ set_response_attributes!
130
+ end
131
+ end
74
132
 
75
- @content = response['content']
76
- @chunk_type = response['chunk_type']
77
- @project_id = response['project_id']
78
- @variable_fallbacks = response['variable_fallbacks']
79
- end
133
+ def set_response_attributes!
134
+ @content = response['content']
135
+ @chunk_type = response['chunk_type']
136
+ @variable_fallbacks = response['variable_fallbacks'].presence || {}
137
+ @collection_id = response["collection"]["identifier"] if chunk_type == 'collection_item'
138
+ @branch_id = response['branch_id']
80
139
  end
81
140
 
82
141
  end
83
- end
142
+ end
@@ -4,20 +4,20 @@ 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
-
7
+ chunk = Editmode::ChunkValue.new(identifier, options.merge({raw: true}))
8
+
9
9
  if chunk.chunk_type == 'collection_item'
10
10
  chunk.field(field)
11
11
  else
12
12
  chunk.content
13
13
  end
14
14
  rescue => er
15
- raise er
15
+ puts er
16
16
  end
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.1.8"
3
- end
2
+ VERSION = "1.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.1.8
4
+ version: 1.2.3
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-06 00:00:00.000000000 Z
11
+ date: 2020-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler