editmode 1.3.1 → 1.3.6

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: 392f282609e410c8656791b6120245b8b4c790ed8ba102d6dfe1c5d2062acd6f
4
- data.tar.gz: 94246ddabf9aadcdf65ceab231e83a6704a7d89f39ce5039685b7f44ea4e18c9
3
+ metadata.gz: 32d49f4215168d36260f8b3f363c12d7ba6948fd7e9f97e8dae1e1b58ddb7ef8
4
+ data.tar.gz: ed1cf4a9c60ae63973ed9e3f98b5c2a7e66eb4b273e1a392509574aa7ae3f86a
5
5
  SHA512:
6
- metadata.gz: 7a1f7f6442b1109fedbd94be59a08c0658d3aa6d9570a5e21f36a32da45c6befacdab23da75670618034c95ceace95262e440807ccafc0c847544b96600497be
7
- data.tar.gz: b89dc6f25b53ef930ed66c752947a539157cd4cda959a8288366431cb2ea0987ac7d5af6e3b5ab0a5ee49e341046fc47877133ade9c1fa05a8ba0a833add5368
6
+ metadata.gz: b2036241a90a7d9eff95d2da0553918d278f37c5336b181bcf62d076bb7a4184f87b8dac5a286026dc88f5beaf46e54db5020f6228877d88b0a72592bd85ee2c
7
+ data.tar.gz: 6f0354be4a33de404b01f0b1611a87e95c4916b59f0034356cc647192b99f77b113424c5b463d1542c6e405306c959a4477c93949d92392e382c9f7fa98000f1
data/README.md CHANGED
@@ -107,6 +107,20 @@ e("cnk_16e04a02d577afb610ce", "Email Content", variables: variable_values)
107
107
  | item_class | string | `optional` Class name(s) that will be added along with "chunks-collection-item--wrapper" to all collection items |
108
108
 
109
109
 
110
+ ### Working with Image Transformation
111
+ Use `transformation` attribute to perform real-time image transformations to deliver perfect images to the end-users.
112
+
113
+ ```ruby
114
+ # This chunk should render an image with 200 x 200 dimension
115
+ = E('id-of-some-image', transformation: "w-200 h-200")
116
+
117
+ # For image inside a collection
118
+ = c('some-collection-id') do
119
+ = F('Avatar', transformation: "w-200 h-200")
120
+ ```
121
+
122
+ Please see the complete list of [transformation parameters](https://editmode.com/docs#/imagekit_properties).
123
+
110
124
  ## Caching
111
125
  In order to keep your application speedy, Editmode minimizes the amount of network calls it makes by caching content where it can.
112
126
 
@@ -125,6 +139,8 @@ The editmode gem exposes a cache expiration endpoint in your application at `/ed
125
139
 
126
140
  The cache expiration endpoint is currently **not** authenticated.
127
141
 
142
+ ?> We are using a method called `delete_matched` to purge your caches when a content gets updated, and this method isn't supported in `memcached`. We highly recommend using `redis_store` or `file_store`.
143
+
128
144
  ## Disabling editmode.js auto-include
129
145
 
130
146
  To disable automatic insertion for a particular controller or action you can:
@@ -6,14 +6,14 @@ class EditmodeController < ApplicationController
6
6
  render status: 200, json: {:response => "success"}
7
7
  elsif params[:collection]
8
8
  cache_id = "collection_#{params[:identifier]}"
9
- Rails.cache.delete_matched("#{cache_id}*")
9
+ Rails.cache.delete_matched("*#{cache_id}*")
10
10
  render status: 200, json: {:response => "success"}
11
11
  elsif params[:variable_cache_project_id]
12
12
  project_id = params[:variable_cache_project_id]
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_matched("#{params[:identifier]}")
16
+ Rails.cache.delete_matched("*#{params[:identifier]}*")
17
17
  render status: 200, json: {:response => "success"}
18
18
  else
19
19
  render status: 404, json: {:response => "no identifier specified"}
data/lib/editmode.rb CHANGED
@@ -6,12 +6,19 @@ require 'editmode/auto_include_filter'
6
6
  require 'editmode/chunk_value'
7
7
  require 'editmode/railtie' if defined? Rails
8
8
  require 'editmode/engine' if defined?(Rails)
9
- # Todo: Implement RSPEC
9
+ require 'editmode/monkey_patches'
10
+ require 'editmode/logger'
11
+ require 'editmode/chunk'
12
+
10
13
  module Editmode
11
14
  class << self
12
15
  include Editmode::ActionViewExtensions::EditmodeHelper
13
16
  include Editmode::Helper
14
17
 
18
+ def api_root_url
19
+ ENV["EDITMODE_OVERRIDE_API_URL"] || "https://api.editmode.com"
20
+ end
21
+
15
22
  def project_id=(id)
16
23
  config.project_id = id
17
24
  end
@@ -20,6 +27,18 @@ module Editmode
20
27
  config.project_id
21
28
  end
22
29
 
30
+ def logger
31
+ config.logger
32
+ end
33
+
34
+ def log_level
35
+ config.log_level
36
+ end
37
+
38
+ def log_level=(level)
39
+ config.log_level = level
40
+ end
41
+
23
42
  def access_token
24
43
  config.access_token
25
44
  end
@@ -39,15 +58,46 @@ module Editmode
39
58
  puts er
40
59
  end
41
60
  end
61
+
62
+ def cache_all!(chunks)
63
+ chunks.each do |chunk|
64
+ project_id = chunk["project_id"]
65
+ identifier = chunk["identifier"]
66
+ content_key = chunk["content_key"]
67
+ json_data = chunk.to_json
68
+ Rails.cache.write("chunk_#{project_id}#{identifier}", json_data)
69
+ Rails.cache.write("chunk_#{project_id}#{content_key}", json_data) if content_key.present?
70
+ end
71
+ end
42
72
  end
43
73
 
44
74
  class Configuration
45
75
  attr_accessor :access_token, :variable
46
- attr_reader :project_id
76
+ attr_reader :project_id, :log_level, :preload
77
+
78
+ def preload=(bool)
79
+ @preload = bool
80
+ if bool
81
+ chunks = Editmode::Chunk.retrieve
82
+ Editmode.cache_all!(chunks)
83
+ end
84
+ end
85
+
86
+ def logger
87
+ @logger ||= Editmode::Logger.new
88
+ end
47
89
 
48
90
  def project_id=(id)
49
91
  @project_id = id
92
+ if preload
93
+ chunks = Editmode::Chunk.retrieve(id)
94
+ Editmode.cache_all!(chunks)
95
+ end
96
+ end
50
97
 
98
+ def log_level=(level)
99
+ @log_level = level
100
+ logger.log_level = level
51
101
  end
52
102
  end
53
103
  end
@@ -0,0 +1,26 @@
1
+ class Editmode::Chunk
2
+ def initialize
3
+ end
4
+
5
+ class << self
6
+ def retrieve(project_id = Editmode.project_id, options = {})
7
+ begin
8
+ root_url = Editmode.api_root_url
9
+ chunk_id = options[:identifier] || options[:content_key]
10
+
11
+ url = "#{root_url}/chunks/#{chunk_id}?project_id=#{project_id}"
12
+ response = HTTParty.get(url)
13
+
14
+ if chunk_id.present?
15
+ return response
16
+ else
17
+ chunks = response.try(:[], "chunks")
18
+ chunks ||= []
19
+ end
20
+ rescue => er
21
+ Rails.logger.info er
22
+ []
23
+ end
24
+ end
25
+ end
26
+ end
@@ -9,7 +9,7 @@ module Editmode
9
9
  attr_accessor :identifier, :variable_values, :branch_id,
10
10
  :variable_fallbacks, :chunk_type, :project_id,
11
11
  :url, :collection_id, :cache_identifier,
12
- :response
12
+ :response, :transformation
13
13
 
14
14
  attr_writer :content
15
15
 
@@ -22,6 +22,7 @@ module Editmode
22
22
  @raw = options[:raw].present?
23
23
  @skip_sanitize = options[:dangerously_skip_sanitization]
24
24
  @skip_cache = options[:skip_cache]
25
+ @transformation = options[:transformation]
25
26
 
26
27
  @url = "#{api_root_url}/chunks/#{identifier}"
27
28
  @cache_identifier = set_cache_identifier(identifier)
@@ -40,7 +41,7 @@ module Editmode
40
41
  if field.present?
41
42
  field_chunk = field_chunk(field)
42
43
  if field_chunk.present?
43
- result = field_chunk['content']
44
+ result = field_chunk['chunk_type'] == 'image' ? set_transformation_properties!(field_chunk['content']) : field_chunk['content']
44
45
  result = variable_parse!(result, variable_fallbacks, variable_values, @raw, @skip_sanitize)
45
46
  else
46
47
  raise no_response_received(field)
@@ -73,6 +74,23 @@ module Editmode
73
74
  end
74
75
 
75
76
  private
77
+ def set_transformation_properties!(url)
78
+ if transformation.present? && url.present?
79
+ transformation.gsub!(" ", ",")
80
+ transformation.gsub!(/\s/, '')
81
+
82
+ uri = URI(url)
83
+ uri.query = [uri.query, "tr=#{transformation}"].compact.join("&")
84
+
85
+ url = uri.to_s
86
+ end
87
+
88
+ url
89
+ end
90
+
91
+ def allowed_tag_attributes
92
+ %w(style href title src alt width height class target)
93
+ end
76
94
 
77
95
  # Todo: Transfer to helper utils
78
96
  def api_root_url
@@ -108,7 +126,7 @@ module Editmode
108
126
  end
109
127
  end
110
128
 
111
- content = ActionController::Base.helpers.sanitize(content) unless skip_sanitize
129
+ content = ActionController::Base.helpers.sanitize(content, attributes: allowed_tag_attributes) unless skip_sanitize
112
130
  return content
113
131
  end
114
132
 
@@ -139,8 +157,9 @@ module Editmode
139
157
  end
140
158
 
141
159
  def set_response_attributes!
142
- @content = response['content']
143
160
  @chunk_type = response['chunk_type']
161
+
162
+ @content = @chunk_type == 'image' ? set_transformation_properties!(response['content']) : response['content']
144
163
  @variable_fallbacks = response['variable_fallbacks'].presence || {}
145
164
  @collection_id = response["collection"]["identifier"] if chunk_type == 'collection_item'
146
165
  @branch_id = response['branch_id']
@@ -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.merge({response: @custom_field_chunk}))
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
 
@@ -0,0 +1,28 @@
1
+ require 'active_support'
2
+
3
+ module Editmode
4
+ class Logger
5
+ attr_accessor :httparty_subscription
6
+
7
+ def log_level=(level)
8
+ if level == :normal
9
+ # Add more subscription here
10
+ enable_httparty!
11
+ end
12
+ end
13
+
14
+ def enable_httparty!
15
+ @httparty_subscription = ActiveSupport::Notifications.subscribe('request.httparty') do |name, start, ending, transaction_id, payload|
16
+ event = ActiveSupport::Notifications::Event.new(name, start, ending, transaction_id, payload)
17
+ Rails.logger.info " HTTParty -- " + "#{event.payload[:method]} #{event.payload[:url]} (Duration: #{event.duration}ms)"
18
+ Thread.current[:http_runtime] ||= 0
19
+ Thread.current[:http_runtime] += event.duration
20
+ payload[:http_runtime] = event.duration
21
+ end
22
+ end
23
+
24
+ def unsubscribe(subscriber)
25
+ ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber.present?
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ require 'httparty'
2
+
3
+ # Support logging on httparty requests
4
+ module HTTParty
5
+ class Request
6
+ alias_method :_original_perform, :perform
7
+ def perform(&block)
8
+ payload = {
9
+ method: http_method.const_get(:METHOD),
10
+ url: uri
11
+ }
12
+ ActiveSupport::Notifications.instrument 'request.httparty', payload do
13
+ _original_perform(&block)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Editmode
2
- VERSION = "1.3.1"
2
+ VERSION = "1.3.6"
3
3
  end
@@ -1,5 +1,11 @@
1
1
  Editmode.setup do |config|
2
- # Replace TodoProjectId with your Editmode Project ID,
3
- # visit https://editmode.com/projects
4
2
  config.project_id = "<%= @project_id %>"
3
+
4
+ # Enables logging on every API request
5
+ # config.log_level = :silence
6
+
7
+ # Preload contents - this will take all the contents
8
+ # from the project specified in config.project_id
9
+ # and store it to your Application cache.
10
+ # config.preload = true
5
11
  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.3.1
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Ennis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-26 00:00:00.000000000 Z
11
+ date: 2021-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,9 +82,12 @@ files:
82
82
  - lib/editmode.rb
83
83
  - lib/editmode/action_view_extensions/editmode_helper.rb
84
84
  - lib/editmode/auto_include_filter.rb
85
+ - lib/editmode/chunk.rb
85
86
  - lib/editmode/chunk_value.rb
86
87
  - lib/editmode/engine.rb
87
88
  - lib/editmode/helper.rb
89
+ - lib/editmode/logger.rb
90
+ - lib/editmode/monkey_patches.rb
88
91
  - lib/editmode/railtie.rb
89
92
  - lib/editmode/script_tag.rb
90
93
  - lib/editmode/version.rb