editmode 1.2.8 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68357d9af1673886733d966a8c6bad378f2f0a9242b810b9e61560ab8b793b75
4
- data.tar.gz: d7f45153fe466bb03b68a7b1428cc245883fbd033a4a0e9e449133a14c4480d5
3
+ metadata.gz: 3c2cf39ab030f4bc6d04678170c164d950a4bf947659906c6da343537f447bd4
4
+ data.tar.gz: 89a6a0f4a16d51541404f1f1c0fd8ab90e0ad799b6bc176c28c48e8f653c0064
5
5
  SHA512:
6
- metadata.gz: cf0d706724b4d3fd92b7e3926af60e491adc248b3ac1b36b1913f3bf22eac78e82e8967182b29c8ba86984a0c5ce8b3124fecce2b8695e79fca0b098c72d9d77
7
- data.tar.gz: a2af19294f5625257a5d94239a16bbb68035863404c951f9aa1d2728d0e16e57071f00ac1cecc1c560950505e2b8c9552d802fd16dbaaf6312403cc8db8302aa
6
+ metadata.gz: d3815cd743139b4fd6a354c072d9bd2c8d010c8fa4aa2a6a6e04165f363430a5689d91a1228e5e3789b8d53a9f0434c4175a525bb3d7f85c4105bcecf81d1e96
7
+ data.tar.gz: fcb8a9260a0cd706ad049c2f2dfd9406c3c3bc2ac8addcb6fa74f662709d483a00e338613136959762c5f317798cde9ad9f8d48ce0077fe63cdac323ccd063d4
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in chunks.gemspec
4
+ gem 'pry-rails'
5
+ gem 'rails', '~> 5.2.0'
4
6
  gemspec
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
2
3
 
4
+ task :test do
5
+ Dir['test/*_test.rb'].each do |testfile|
6
+ load testfile
7
+ end
8
+ end
9
+
10
+ task :default => :test
data/lib/editmode.rb CHANGED
@@ -6,7 +6,8 @@ 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'
10
11
  module Editmode
11
12
  class << self
12
13
  include Editmode::ActionViewExtensions::EditmodeHelper
@@ -20,6 +21,18 @@ module Editmode
20
21
  config.project_id
21
22
  end
22
23
 
24
+ def logger
25
+ config.logger
26
+ end
27
+
28
+ def log_level
29
+ config.log_level
30
+ end
31
+
32
+ def log_level=(level)
33
+ config.log_level = level
34
+ end
35
+
23
36
  def access_token
24
37
  config.access_token
25
38
  end
@@ -43,11 +56,19 @@ module Editmode
43
56
 
44
57
  class Configuration
45
58
  attr_accessor :access_token, :variable
46
- attr_reader :project_id
59
+ attr_reader :project_id, :log_level
60
+
61
+ def logger
62
+ @logger ||= Editmode::Logger.new
63
+ end
47
64
 
48
65
  def project_id=(id)
49
66
  @project_id = id
67
+ end
50
68
 
69
+ def log_level=(level)
70
+ @log_level = level
71
+ logger.log_level = level
51
72
  end
52
73
  end
53
74
  end
@@ -1,15 +1,25 @@
1
1
  require 'editmode/helper'
2
+ require 'action_view'
3
+ require 'httparty'
2
4
 
3
5
  module Editmode
4
6
  module ActionViewExtensions
5
7
  module EditmodeHelper
6
- require 'httparty'
8
+ include ::ActionView::Helpers::TagHelper
9
+ include ::ActionView::Helpers::TextHelper
10
+ include ::ActionView::Helpers::AssetTagHelper
11
+ include ::ActionView::Context
7
12
  include Editmode::Helper
8
13
 
14
+
9
15
  def api_version
10
16
  # Todo Add Header Version
11
17
  end
12
18
 
19
+ def allowed_tag_attributes
20
+ [:style, :href, :title, :src, :alt, :width, :height]
21
+ end
22
+
13
23
  def api_root_url
14
24
  ENV["EDITMODE_OVERRIDE_API_URL"] || "https://api.editmode.com"
15
25
  end
@@ -104,7 +114,7 @@ module Editmode
104
114
  end
105
115
  end
106
116
 
107
- def render_chunk_content(chunk_identifier, chunk_content, chunk_type,options = {})
117
+ def render_chunk_content(chunk_identifier, chunk_content, chunk_type, options = {})
108
118
  begin
109
119
  css_class = options[:class]
110
120
  cache_id = options[:cache_identifier]
@@ -126,17 +136,17 @@ module Editmode
126
136
  case display_type
127
137
  when "span"
128
138
  if chunk_type == "rich_text"
129
- content = content_tag("em-span", :class => "editmode-richtext-editor #{css_class}", :data => chunk_data.merge!({:chunk_editable => true}) ) do
139
+ content = content_tag("em-span", :class => "editmode-richtext-editor #{css_class}", :data => chunk_data.merge!({:chunk_editable => true}), **options.slice(*allowed_tag_attributes) ) do
130
140
  chunk_content.html_safe
131
141
  end
132
142
  else
133
- content_tag("em-span", :class => css_class, :data => chunk_data.merge!({:chunk_editable => true}) ) do
143
+ content_tag("em-span", :class => css_class, :data => chunk_data.merge!({:chunk_editable => true}), **options.slice(*allowed_tag_attributes) ) do
134
144
  chunk_content.html_safe
135
145
  end
136
146
  end
137
147
  when "image"
138
148
  chunk_content = chunk_content.blank? || chunk_content == "/images/original/missing.png" ? 'https://www.editmode.com/upload.png' : chunk_content
139
- image_tag(chunk_content, :data => chunk_data, :class => css_class)
149
+ image_tag(chunk_content, :data => chunk_data, :class => css_class, **options.slice(*allowed_tag_attributes))
140
150
  end
141
151
  rescue => errors
142
152
  puts errors
@@ -153,7 +163,7 @@ module Editmode
153
163
  # prevent the page from loading.
154
164
  begin
155
165
  field = options[:field].presence || ""
156
-
166
+ options[:referrer] = request.url
157
167
  chunk_value = Editmode::ChunkValue.new(identifier, options)
158
168
 
159
169
  if field.present? && chunk_value.chunk_type == 'collection_item'
@@ -1,3 +1,6 @@
1
+ require 'action_view'
2
+ require 'active_support'
3
+
1
4
  module Editmode
2
5
  class ChunkValue
3
6
  include ActionView::Helpers::TagHelper
@@ -14,6 +17,7 @@ module Editmode
14
17
  @identifier = identifier
15
18
  @branch_id = options[:branch_id].presence
16
19
  @project_id = project_id
20
+ @referrer = options[:referrer].presence || ""
17
21
  @variable_values = options[:variables].presence || {}
18
22
  @raw = options[:raw].present?
19
23
  @skip_sanitize = options[:dangerously_skip_sanitization]
@@ -63,7 +67,15 @@ module Editmode
63
67
  result.try(:html_safe)
64
68
  end
65
69
 
70
+ def cached?
71
+ return false if @skip_cache
72
+ Rails.cache.exist?(cache_identifier)
73
+ end
74
+
66
75
  private
76
+ def allowed_tag_attributes
77
+ %w(style href title src alt width height class target)
78
+ end
67
79
 
68
80
  # Todo: Transfer to helper utils
69
81
  def api_root_url
@@ -99,15 +111,10 @@ module Editmode
99
111
  end
100
112
  end
101
113
 
102
- content = ActionController::Base.helpers.sanitize(content) unless skip_sanitize
114
+ content = ActionController::Base.helpers.sanitize(content, attributes: allowed_tag_attributes) unless skip_sanitize
103
115
  return content
104
116
  end
105
117
 
106
- def cached?
107
- return false if @skip_cache
108
- Rails.cache.exist?(cache_identifier)
109
- end
110
-
111
118
  def query_params
112
119
  the_params = { 'project_id' => project_id }
113
120
  the_params['branch_id'] = branch_id if branch_id.present?
@@ -116,14 +123,15 @@ module Editmode
116
123
  end
117
124
 
118
125
  def get_content
119
-
120
126
  if !cached?
121
- http_response = HTTParty.get(url, query: query_params)
127
+ http_response = HTTParty.get(url, query: query_params, headers: {referrer: @referrer})
122
128
  response_received = true if http_response.code == 200
123
129
  end
124
130
 
125
131
  if !cached? && !response_received
126
- raise no_response_received(identifier)
132
+ message = http_response.try(:[], 'message') || no_response_received(identifier)
133
+
134
+ raise message
127
135
  else
128
136
  Rails.cache.write(cache_identifier, http_response.to_json) if http_response.present?
129
137
  cached_response = Rails.cache.fetch(cache_identifier)
@@ -141,5 +149,8 @@ module Editmode
141
149
  @branch_id = response['branch_id']
142
150
  end
143
151
 
152
+ def no_response_received(id = "")
153
+ "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."
154
+ end
144
155
  end
145
156
  end
@@ -1,4 +1,5 @@
1
- require 'editmode/chunk_value'
1
+ require 'rails/engine'
2
+ require 'action_controller'
2
3
  require 'editmode/helper'
3
4
 
4
5
  module Editmode
@@ -12,7 +12,7 @@ module Editmode
12
12
  chunk.content
13
13
  end
14
14
  rescue => er
15
- puts er
15
+ Rails.logger.info "#{er}: We can't render content for #{identifier}"
16
16
  end
17
17
  end
18
18
 
@@ -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,4 @@
1
+ require 'rails/railtie'
1
2
  module Editmode
2
3
  class Railtie < Rails::Railtie
3
4
  initializer "editmode" do |app|
@@ -1,3 +1,3 @@
1
1
  module Editmode
2
- VERSION = "1.2.8"
2
+ VERSION = "1.3.3"
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.8
4
+ version: 1.3.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-12-29 00:00:00.000000000 Z
11
+ date: 2021-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,8 @@ files:
85
85
  - lib/editmode/chunk_value.rb
86
86
  - lib/editmode/engine.rb
87
87
  - lib/editmode/helper.rb
88
+ - lib/editmode/logger.rb
89
+ - lib/editmode/monkey_patches.rb
88
90
  - lib/editmode/railtie.rb
89
91
  - lib/editmode/script_tag.rb
90
92
  - lib/editmode/version.rb
@@ -109,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
111
  - !ruby/object:Gem::Version
110
112
  version: '0'
111
113
  requirements: []
112
- rubygems_version: 3.0.8
114
+ rubygems_version: 3.2.4
113
115
  signing_key:
114
116
  specification_version: 4
115
117
  summary: Editmode allows you to turn plain text in your rails app into easily inline-editable