editmode 1.2.2 → 1.2.7

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: 2beab37962d65855d3ecfd66b6d9731fd4f1826eac260d97a30d8c1b022b9ff8
4
- data.tar.gz: b05b7f2425a796262ec31e2d8cc447405d9bb2ed339b37a6f71cc2d52cd10ad7
3
+ metadata.gz: aac6adb5e24a89664dc5939ddb97606ac0003f33fb329b7d00b507c98a8ebc98
4
+ data.tar.gz: ae22fe6ff1b9cf07ea3a1d7761c298f856482b051a080111fa9e29a7965633ee
5
5
  SHA512:
6
- metadata.gz: 75944607fde64c8da7a99751a421d43fbc473b7f0c0c7ce34297b16d70869d2d393de8731fed85dfd6387ba22977ef3e92ae892370b07a13a9ed41bad7d6472b
7
- data.tar.gz: c2109b7ca239fce3ddb025348bc534201002d6396a18e4eec9357fdc992f64489efed095e4568db984148e5ff45aafad70b1026cf93111d9c04aa20bed329924
6
+ metadata.gz: 1264161505c4cb1ded693b68231825962f686420817244d427510e76ac71950b78ba5a4da5ab85bb02c8a163f724e012f4232b397bdaa7fcef74bedea9ea8e03
7
+ data.tar.gz: 3a5afb3beac64da0120e296b3bd41747031b6a1d662aa24df1b56f82eb3e29e1f44665df76ce29a6cd87848a4ebd3e9b61df79ad6112feb7747a30bca3816b84
data/README.md CHANGED
@@ -50,6 +50,12 @@ Editmode provides helper methods for use in your rails views and controllers.
50
50
  <%= E('cnk_x4ts...', class: "a-css-class") %> # Render a chunk with inline css class
51
51
  ```
52
52
 
53
+ ### Working with multiple projects in the same codebase
54
+ If you want to include content from a different project to the one you've specified in the initializer, you can pass the project id in to the view helper.
55
+ ```erb
56
+ <%= E("cnk_16e04a02d577afb610ce", project_id: "prj_02d577afb617hdb") %>
57
+ ```
58
+
53
59
  ### Content can also be accessed in Controllers
54
60
  ```ruby
55
61
  @page_title = e("cnk_x4ts............") # Using a chunk identifier
@@ -78,6 +84,8 @@ e("cnk_16e04a02d577afb610ce", "Email Content", variables: variable_values)
78
84
  # Response: "Hi Dexter Morgan"
79
85
  ```
80
86
 
87
+
88
+
81
89
  ### Use collections for repeatable content
82
90
  ```erb
83
91
  <%= c('col_j8fbs...', class: "profiles-container", item_class: "profile-item") do %>
@@ -56,14 +56,14 @@ module Editmode
56
56
  chunks.each do |chunk|
57
57
  @custom_field_chunk = chunk
58
58
  concat(content_tag(:div, class: "chunks-collection-item--wrapper #{item_class}") do
59
- yield
59
+ yield(@custom_field_chunk)
60
60
  end)
61
61
  end
62
62
 
63
63
  # Placeholder element for new collection item
64
64
  @custom_field_chunk = chunks.first.merge!({placeholder: true})
65
65
  concat(content_tag(:div, class: "chunks-hide chunks-col-placeholder-wrapper") do
66
- yield
66
+ yield(@custom_field_chunk)
67
67
  end)
68
68
  end
69
69
  else
@@ -3,22 +3,23 @@ module Editmode
3
3
  include ActionView::Helpers::TagHelper
4
4
  include ActionView::Context
5
5
 
6
- attr_accessor :identifier, :variable_values, :branch_id,
7
- :variable_fallbacks, :chunk_type, :project_id,
8
- :url, :collection_id, :cache_identifier,
9
- :response
6
+ attr_accessor :identifier, :variable_values, :branch_id,
7
+ :variable_fallbacks, :chunk_type, :project_id,
8
+ :url, :collection_id, :cache_identifier,
9
+ :response
10
10
 
11
11
  attr_writer :content
12
12
 
13
- def initialize(identifier, **options)
13
+ def initialize(identifier, project_id: Editmode.project_id, **options)
14
14
  @identifier = identifier
15
15
  @branch_id = options[:branch_id].presence
16
- @project_id = Editmode.project_id
16
+ @project_id = project_id
17
17
  @variable_values = options[:variables].presence || {}
18
18
  @raw = options[:raw].present?
19
+ @skip_sanitize = options[:dangerously_skip_sanitization]
20
+ @skip_cache = options[:skip_cache]
19
21
 
20
- branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
21
- @url = "#{api_root_url}/chunks/#{identifier}?project_id=#{project_id}&#{branch_params}"
22
+ @url = "#{api_root_url}/chunks/#{identifier}"
22
23
  @cache_identifier = set_cache_identifier(identifier)
23
24
 
24
25
  if options[:response].present?
@@ -29,14 +30,14 @@ module Editmode
29
30
  end
30
31
  end
31
32
 
32
- def field(field = nil)
33
+ def field(field = nil)
33
34
  # Field ID can be a slug or field_name
34
35
  if chunk_type == 'collection_item'
35
36
  if field.present?
36
37
  field_chunk = field_chunk(field)
37
38
  if field_chunk.present?
38
39
  result = field_chunk['content']
39
- result = variable_parse!(result, variable_fallbacks, variable_values, @raw)
40
+ result = variable_parse!(result, variable_fallbacks, variable_values, @raw, @skip_sanitize)
40
41
  else
41
42
  raise no_response_received(field)
42
43
  end
@@ -56,13 +57,14 @@ module Editmode
56
57
  end
57
58
 
58
59
  def content
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)
60
+ raise "undefined method 'content' for chunk_type: collection_item \nDid you mean? field" if chunk_type == 'collection_item'
61
+
62
+ result = variable_parse!(@content, variable_fallbacks, variable_values, @raw, @skip_sanitize)
62
63
  result.try(:html_safe)
63
64
  end
64
65
 
65
66
  private
67
+
66
68
  # Todo: Transfer to helper utils
67
69
  def api_root_url
68
70
  ENV["EDITMODE_OVERRIDE_API_URL"] || "https://api.editmode.com"
@@ -79,11 +81,10 @@ module Editmode
79
81
  return false
80
82
  end
81
83
 
82
- def variable_parse!(content, variables = {}, values = {}, raw = true)
83
- content = ActionController::Base.helpers.sanitize(content)
84
+ def variable_parse!(content, variables = {}, values = {}, raw = true, skip_sanitize=false)
84
85
  tokens = content.scan(/\{{(.*?)\}}/)
85
86
  if tokens.any?
86
- tokens.flatten!
87
+ tokens.flatten!
87
88
  tokens.each do |token|
88
89
  token_value = values[token.to_sym] || variables[token] || ""
89
90
  sanitized_value = ActionController::Base.helpers.sanitize(token_value)
@@ -93,30 +94,39 @@ module Editmode
93
94
  sanitized_value
94
95
  end
95
96
  end
96
-
97
+
97
98
  content.gsub!("{{#{token}}}", sanitized_value)
98
99
  end
99
100
  end
100
101
 
101
- content
102
+ content = ActionController::Base.helpers.sanitize(content) unless skip_sanitize
103
+ return content
102
104
  end
103
105
 
104
106
  def cached?
107
+ return false if @skip_cache
105
108
  Rails.cache.exist?(cache_identifier)
106
109
  end
107
110
 
111
+ def query_params
112
+ the_params = { 'project_id' => project_id }
113
+ the_params['branch_id'] = branch_id if branch_id.present?
114
+
115
+ the_params
116
+ end
117
+
108
118
  def get_content
119
+
109
120
  if !cached?
110
- http_response = HTTParty.get(url)
121
+ http_response = HTTParty.get(url, query: query_params)
111
122
  response_received = true if http_response.code == 200
112
123
  end
113
124
 
114
125
  if !cached? && !response_received
115
126
  raise no_response_received(identifier)
116
127
  else
117
- cached_response = Rails.cache.fetch(cache_identifier) do
118
- http_response.to_json
119
- end
128
+ Rails.cache.write(cache_identifier, http_response.to_json) if http_response.present?
129
+ cached_response = Rails.cache.fetch(cache_identifier)
120
130
 
121
131
  @response = json?(cached_response) ? JSON.parse(cached_response) : cached_response
122
132
  set_response_attributes!
@@ -128,7 +138,8 @@ module Editmode
128
138
  @chunk_type = response['chunk_type']
129
139
  @variable_fallbacks = response['variable_fallbacks'].presence || {}
130
140
  @collection_id = response["collection"]["identifier"] if chunk_type == 'collection_item'
141
+ @branch_id = response['branch_id']
131
142
  end
132
143
 
133
144
  end
134
- end
145
+ end
@@ -5,17 +5,34 @@ module Editmode
5
5
  field, options = parse_arguments(args)
6
6
  begin
7
7
  chunk = Editmode::ChunkValue.new(identifier, options.merge({raw: true}))
8
-
9
- if chunk.chunk_type == 'collection_item'
10
- chunk.field(field)
11
- else
12
- chunk.content
13
- end
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
37
  e(@custom_field_chunk["identifier"], label, options.merge({response: @custom_field_chunk}))
21
38
  end
@@ -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.2"
2
+ VERSION = "1.2.7"
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.2
4
+ version: 1.2.7
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-11-09 00:00:00.000000000 Z
11
+ date: 2020-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler