editmode 1.1.9 → 1.2.4

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: fbdfaaca69c2832debd4fb728c6684b48abe8e9337cbaa8f4323a20cf175cf77
4
- data.tar.gz: 51841cadd3a89aea58a0089b9e7c541a286f9da46fa2d7db0f32e9d7a07797ed
3
+ metadata.gz: bd467da504ef11a5ba029cc7dcf3b4957dbbc758fa37fe8cf38f2aed358fb17b
4
+ data.tar.gz: 577f1989ff49f7c624fd2d1864cc78c6bd0f3249e7c67951bf6b94e375794744
5
5
  SHA512:
6
- metadata.gz: 9966ac6b514c9c237db8e2e1f77613431a365f2c4259565422892b9abdd749a2c294575bfd0e1242d9ac682a5340b940bacde295560fa1845e93ddd03ef500f1
7
- data.tar.gz: fee7262f68522bb8e4f76d0de9f9da8c267c2a311105e89c72d7989408d46f124f6bbbf4170d712c0544aa4453b17f2837525c93f47da9afae2a729a0a2c47a5
6
+ metadata.gz: 5466ebc92b03dde2acc135dd0e90ff54c2b18242a0eb5011889ff429fd5c168f9ec09b7181be3df8d8a864f78911a49b620b0ed239a3aec9330e4d3fb9b3a3f7
7
+ data.tar.gz: bd302d21b044b4927e787f5aaacc530bae2a811f5e72138519b6b2b5cc594dc6322ec9fa026a1fa256d5d65a26ee79877d0256e547a11ad6589197482d0e5fb1
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,15 +80,13 @@ 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
 
@@ -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"}
@@ -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,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
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
+ @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
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']
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,8 +4,8 @@ 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
@@ -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.1.9"
2
+ VERSION = "1.2.4"
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.9
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Ennis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-07 00:00:00.000000000 Z
11
+ date: 2020-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,7 +94,7 @@ homepage: https://github.com/tonyennis145/editmode-rails
94
94
  licenses:
95
95
  - MIT
96
96
  metadata: {}
97
- post_install_message:
97
+ post_install_message:
98
98
  rdoc_options: []
99
99
  require_paths:
100
100
  - lib
@@ -109,8 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  requirements: []
112
- rubygems_version: 3.0.8
113
- signing_key:
112
+ rubygems_version: 3.0.3
113
+ signing_key:
114
114
  specification_version: 4
115
115
  summary: Editmode allows you to turn plain text in your rails app into easily inline-editable
116
116
  bits of content that can be managed by anyone with no technical knowledge