editmode 0.0.9.111 → 0.0.10.14
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 +4 -4
- data/app/controllers/editmode_controller.rb +4 -0
- data/lib/editmode.rb +28 -38
- data/lib/{editmode-rails → editmode}/action_view_extensions/editmode_helper.rb +39 -4
- data/lib/{editmode-rails → editmode}/auto_include_filter.rb +3 -3
- data/lib/editmode/chunk_value.rb +71 -0
- data/lib/{editmode-rails → editmode}/engine.rb +1 -1
- data/lib/{editmode-rails → editmode}/railtie.rb +3 -3
- data/lib/{editmode-rails → editmode}/script_tag.rb +1 -1
- data/lib/editmode/version.rb +3 -0
- data/lib/generators/editmode/config_generator.rb +26 -0
- data/lib/generators/editmode/templates/editmode.rb.erb +5 -0
- metadata +11 -8
- data/lib/editmode-rails/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a65751ad76225c8f430e7ff2a34b956ed7f656ba601d160f59878b4b53f655e
|
4
|
+
data.tar.gz: 5f443b35f70f917bd0616de2a59202abb78b3c16c73302611a98a794dbf7e74b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57028a84200543b6dba3d732af81c36be495ad55f1d6f85e34f2f4a082d995634cd2edf7e533882d0616f68fbba5ca17e153a31a602ede1b5978a01e7fee696d
|
7
|
+
data.tar.gz: 2c5da69cd042ccc113591d6cba2c5c05cf8ddfd835bc80d226d7c323082129fa3740c2b22fe1ff30e379b068aa591b3a5020f5328e5e1e14c2d82c9d0a38ae83
|
@@ -4,6 +4,10 @@ class EditmodeController < ApplicationController
|
|
4
4
|
if params[:full]
|
5
5
|
Rails.cache.clear
|
6
6
|
render status: 200, json: {:response => "success"}
|
7
|
+
elsif params[:variable_cache_project_id]
|
8
|
+
project_id = params[:variable_cache_project_id]
|
9
|
+
Rails.cache.delete("chunk_#{project_id}_variables")
|
10
|
+
render status: 200, json: {:response => "success"}
|
7
11
|
elsif params[:identifier]
|
8
12
|
Rails.cache.delete("chunk_#{params[:identifier]}")
|
9
13
|
Rails.cache.delete("chunk_#{params[:identifier]}_type")
|
data/lib/editmode.rb
CHANGED
@@ -1,25 +1,29 @@
|
|
1
1
|
require "active_support/dependencies"
|
2
|
-
require "editmode
|
3
|
-
require 'editmode
|
4
|
-
require 'editmode
|
5
|
-
require 'editmode
|
6
|
-
require 'editmode
|
7
|
-
require 'editmode
|
8
|
-
|
9
|
-
|
2
|
+
require "editmode/version"
|
3
|
+
require 'editmode/script_tag'
|
4
|
+
require 'editmode/action_view_extensions/editmode_helper'
|
5
|
+
require 'editmode/auto_include_filter'
|
6
|
+
require 'editmode/chunk_value'
|
7
|
+
require 'editmode/railtie' if defined? Rails
|
8
|
+
require 'editmode/engine' if defined?(Rails)
|
9
|
+
# Todo: Implement RSPEC
|
10
|
+
module Editmode
|
10
11
|
class << self
|
11
|
-
include ::
|
12
|
+
include Editmode::ActionViewExtensions::EditmodeHelper
|
13
|
+
|
14
|
+
def project_id=(id)
|
15
|
+
config.project_id = id
|
16
|
+
end
|
17
|
+
|
12
18
|
def project_id
|
13
|
-
|
19
|
+
config.project_id
|
14
20
|
end
|
15
21
|
|
16
22
|
def access_token
|
17
|
-
|
23
|
+
config.access_token
|
18
24
|
end
|
19
25
|
|
20
26
|
def config
|
21
|
-
# Todo: Instantiate in editmode initializer or base controllers
|
22
|
-
# Todo: Add a generator to create initializer an file?
|
23
27
|
@config ||= Configuration.new
|
24
28
|
end
|
25
29
|
|
@@ -28,36 +32,22 @@ class Editmode
|
|
28
32
|
end
|
29
33
|
|
30
34
|
def chunk_value(identifier, **options)
|
31
|
-
body = options[:values].presence || {}
|
32
|
-
field_id = options[:field_id].presence
|
33
|
-
branch_id = options[:branch_id].presence
|
34
|
-
|
35
|
-
branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
|
36
|
-
cache_identifier = "chunk_#{identifier}#{branch_id}"
|
37
|
-
url = "#{api_root_url}/chunks/#{identifier}?#{branch_params}"
|
38
|
-
|
39
35
|
begin
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
raise "No response received"
|
45
|
-
else
|
46
|
-
if field_id.present?
|
47
|
-
chunk = response["content"].detect {|f| f["custom_field_identifier"] == field_id }
|
48
|
-
chunk['content']
|
49
|
-
else
|
50
|
-
response['content']
|
51
|
-
end
|
52
|
-
end
|
53
|
-
rescue => error
|
54
|
-
# Todo: Send a log to editmode prob like sentry
|
55
|
-
return "No response received"
|
36
|
+
options.merge!(project_id: project_id)
|
37
|
+
Editmode::ChunkValue.new(identifier, **options )
|
38
|
+
rescue => er
|
39
|
+
raise er
|
56
40
|
end
|
57
41
|
end
|
58
42
|
end
|
59
43
|
|
60
44
|
class Configuration
|
61
|
-
attr_accessor :
|
45
|
+
attr_accessor :access_token, :variable
|
46
|
+
attr_reader :project_id
|
47
|
+
|
48
|
+
def project_id=(id)
|
49
|
+
@project_id = id
|
50
|
+
|
51
|
+
end
|
62
52
|
end
|
63
53
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
module
|
1
|
+
module Editmode
|
2
2
|
module ActionViewExtensions
|
3
|
-
module
|
3
|
+
module EditmodeHelper
|
4
4
|
|
5
5
|
require 'httparty'
|
6
6
|
|
@@ -12,13 +12,26 @@ module EditModeRails
|
|
12
12
|
ENV["EDITMODE_OVERRIDE_API_URL"] || "https://api.editmode.com"
|
13
13
|
end
|
14
14
|
|
15
|
-
def chunk_collection(collection_identifier,
|
15
|
+
def chunk_collection(collection_identifier, **options)
|
16
16
|
branch_params = params[:em_branch_id].present? ? "branch_id=#{params[:em_branch_id]}" : ""
|
17
|
+
tags = options[:tags] || []
|
17
18
|
begin
|
18
19
|
url = "#{api_root_url}/chunks?collection_identifier=#{collection_identifier}&#{branch_params}"
|
19
20
|
response = HTTParty.get(url)
|
20
21
|
raise "No response received" unless response.code == 200
|
21
22
|
chunks = response["chunks"]
|
23
|
+
if chunks.any?
|
24
|
+
# Collection item limit
|
25
|
+
limit = options[:limit] || chunks.size
|
26
|
+
|
27
|
+
chunks = chunks.take(limit)
|
28
|
+
chunks.shuffle! if options[:shuffle]
|
29
|
+
end
|
30
|
+
|
31
|
+
if tags.any?
|
32
|
+
chunks.select!{|c| (tags - c['tags']).size < tags.size }
|
33
|
+
end
|
34
|
+
|
22
35
|
return chunks
|
23
36
|
rescue => error
|
24
37
|
puts error
|
@@ -53,7 +66,7 @@ module EditModeRails
|
|
53
66
|
# Always sanitize the content!!
|
54
67
|
chunk_content = ActionController::Base.helpers.sanitize(chunk_content)
|
55
68
|
|
56
|
-
css_class = options[:
|
69
|
+
css_class = options[:class]
|
57
70
|
|
58
71
|
if chunk_type == "image"
|
59
72
|
display_type = "image"
|
@@ -133,6 +146,28 @@ module EditModeRails
|
|
133
146
|
|
134
147
|
alias_method :chunk, :chunk_display
|
135
148
|
|
149
|
+
def variable_parse!(content, variables, values)
|
150
|
+
tokens = content.scan(/\{{(.*?)\}}/)
|
151
|
+
if tokens.any?
|
152
|
+
tokens.flatten!
|
153
|
+
tokens.each do |token|
|
154
|
+
token_value = values[token.to_sym] || variables[token] || ""
|
155
|
+
sanitized_value = ActionController::Base.helpers.sanitize(token_value)
|
156
|
+
|
157
|
+
content.gsub!("{{#{token}}}", sanitized_value)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
content
|
162
|
+
end
|
163
|
+
|
164
|
+
def no_response_received(id = "")
|
165
|
+
"Sorry, we can't find a content using this identifier: \"#{id}\""
|
166
|
+
end
|
167
|
+
|
168
|
+
def require_field_id
|
169
|
+
"Field ID or Field Name is required to retrieve a collection item"
|
170
|
+
end
|
136
171
|
end
|
137
172
|
end
|
138
173
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
module
|
1
|
+
module Editmode
|
2
2
|
|
3
3
|
module AutoInclude
|
4
4
|
|
5
5
|
module Method
|
6
6
|
def editmode_auto_include
|
7
|
-
|
7
|
+
Editmode::AutoInclude::Filter.filter(self)
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -49,7 +49,7 @@ module EditModeRails
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def editmode_script_tag
|
52
|
-
@script_tag ||=
|
52
|
+
@script_tag ||= Editmode::ScriptTag.new()
|
53
53
|
end
|
54
54
|
|
55
55
|
def enabled_for_environment?
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Editmode
|
2
|
+
class ChunkValue
|
3
|
+
include Editmode::ActionViewExtensions::EditmodeHelper
|
4
|
+
|
5
|
+
attr_accessor :identifier, :variable_values, :branch_id,
|
6
|
+
:variable_fallbacks, :chunk_type, :project_id,
|
7
|
+
:content
|
8
|
+
|
9
|
+
def initialize(identifier, **options)
|
10
|
+
@identifier = identifier
|
11
|
+
@branch_id = options[:branch_id].presence
|
12
|
+
@project_id = options[:project_id].presence
|
13
|
+
@variable_values = options[:values].presence || {}
|
14
|
+
get_content
|
15
|
+
end
|
16
|
+
|
17
|
+
def field(field = nil)
|
18
|
+
# Field ID can be a slug or field_name
|
19
|
+
if chunk_type == 'collection_item'
|
20
|
+
if field.present?
|
21
|
+
field_content = content.detect {|f| f["custom_field_identifier"] == field || f["custom_field_name"] == field }
|
22
|
+
if field_content.present?
|
23
|
+
result = field_content['content']
|
24
|
+
result = variable_parse!(result, variable_fallbacks, variable_values)
|
25
|
+
else
|
26
|
+
raise no_response_received(field)
|
27
|
+
end
|
28
|
+
else
|
29
|
+
raise require_field_id
|
30
|
+
end
|
31
|
+
else
|
32
|
+
raise "undefined method field for chunk_type: #{chunk_type}"
|
33
|
+
end
|
34
|
+
result ||= content
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def get_content
|
39
|
+
branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
|
40
|
+
url = "#{api_root_url}/chunks/#{identifier}?#{branch_params}"
|
41
|
+
|
42
|
+
cache_identifier = "chunk_#{identifier}#{branch_id}"
|
43
|
+
cached_content_present = Rails.cache.exist?(cache_identifier)
|
44
|
+
cached_content_present = Rails.cache.exist?("chunk_#{project_id}_variables") if cached_content_present
|
45
|
+
|
46
|
+
if !cached_content_present
|
47
|
+
response = HTTParty.get(url)
|
48
|
+
response_received = true if response.code == 200
|
49
|
+
end
|
50
|
+
|
51
|
+
if !cached_content_present && !response_received
|
52
|
+
raise no_response_received(identifier)
|
53
|
+
else
|
54
|
+
@content = Rails.cache.fetch(cache_identifier) do
|
55
|
+
response['content']
|
56
|
+
end
|
57
|
+
|
58
|
+
@chunk_type = Rails.cache.fetch("#{cache_identifier}_type") do
|
59
|
+
response['chunk_type']
|
60
|
+
end
|
61
|
+
|
62
|
+
# Since variables are defined in the project level,
|
63
|
+
# We use project_id as cache identifier
|
64
|
+
@variable_fallbacks = Rails.cache.fetch("chunk_#{project_id}_variables") do
|
65
|
+
response['variable_fallbacks']
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
module
|
1
|
+
module Editmode
|
2
2
|
class Railtie < Rails::Railtie
|
3
|
-
initializer "editmode
|
3
|
+
initializer "editmode" do |app|
|
4
4
|
|
5
5
|
ActiveSupport.on_load :action_view do
|
6
|
-
include
|
6
|
+
include Editmode::ActionViewExtensions::EditmodeHelper
|
7
7
|
end
|
8
8
|
ActiveSupport.on_load :action_controller do
|
9
9
|
include AutoInclude::Method
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
require 'rails/generators/base'
|
3
|
+
|
4
|
+
module Editmode
|
5
|
+
module Generators
|
6
|
+
class ConfigGenerator < Rails::Generators::Base
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
|
9
|
+
argument :project_id, :desc => "Your Editmode project_id, which can be found here: https://www.editmode.com/projects"
|
10
|
+
|
11
|
+
def create_config_file
|
12
|
+
@project_id = project_id
|
13
|
+
|
14
|
+
introduction = <<-intro
|
15
|
+
Editmode will automatically insert its javascript before the closing '</body>'
|
16
|
+
tag on every page.
|
17
|
+
|
18
|
+
intro
|
19
|
+
|
20
|
+
print "#{introduction} "
|
21
|
+
|
22
|
+
template "editmode.rb.erb", "config/initializers/editmode.rb"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
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: 0.0.
|
4
|
+
version: 0.0.10.14
|
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-06-
|
11
|
+
date: 2020-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -79,13 +79,16 @@ files:
|
|
79
79
|
- Rakefile
|
80
80
|
- app/controllers/editmode_controller.rb
|
81
81
|
- config/routes.rb
|
82
|
-
- lib/editmode-rails/action_view_extensions/editmode_helper.rb
|
83
|
-
- lib/editmode-rails/auto_include_filter.rb
|
84
|
-
- lib/editmode-rails/engine.rb
|
85
|
-
- lib/editmode-rails/railtie.rb
|
86
|
-
- lib/editmode-rails/script_tag.rb
|
87
|
-
- lib/editmode-rails/version.rb
|
88
82
|
- lib/editmode.rb
|
83
|
+
- lib/editmode/action_view_extensions/editmode_helper.rb
|
84
|
+
- lib/editmode/auto_include_filter.rb
|
85
|
+
- lib/editmode/chunk_value.rb
|
86
|
+
- lib/editmode/engine.rb
|
87
|
+
- lib/editmode/railtie.rb
|
88
|
+
- lib/editmode/script_tag.rb
|
89
|
+
- lib/editmode/version.rb
|
90
|
+
- lib/generators/editmode/config_generator.rb
|
91
|
+
- lib/generators/editmode/templates/editmode.rb.erb
|
89
92
|
homepage: https://github.com/tonyennis145/editmode-rails
|
90
93
|
licenses:
|
91
94
|
- MIT
|