editmode-rails 0.0.9.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 003ddbea8995e89ecad468f7ca40d1d795f456e37673dbcb1c91262c8a2f8864
4
+ data.tar.gz: fcf3aa1bca202716586bff3b279df8f3af40e63ebba481ee183731c7a665264e
5
+ SHA512:
6
+ metadata.gz: abd85d25bc598544d1d78a4fda801ca69912c183c38e6762359198bb736975a0e2a2f4575b5df6d9eac06bf8d214ef2065103625e35cd0b349f8aebfdbe038ad
7
+ data.tar.gz: 33035b524294d03bd5befc0e0aaafb46d6486ecf062c2a12d75ec3cc018ab27ad00009583c15f4b670a77c740e299463f8160af169f48117ba0e102fff80803f
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chunks.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+
2
+ # Editmode Rails Gem
3
+
4
+ To be completed
5
+
6
+ ## Installation
7
+
8
+ ## How It Works
9
+ - Autoload editmode.js
10
+ - Expose view helper
11
+
12
+ ## Helper methods
13
+ - chunk_display
14
+ - raw_chunk
15
+ - chunk_list (coming soon)
16
+
17
+ ## Caching
18
+ - All chunks cached by default using Rails.cache
19
+ - GET /editmode/clear_cache?identifier={} to clear cache
20
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,15 @@
1
+ class EditModeController < ApplicationController
2
+
3
+ def clear_cache
4
+
5
+ Rails.cache.delete("bit_#{params[:identifier]}")
6
+
7
+ if params[:identifier]
8
+ render status: 200, json: {:response => "success"}
9
+ else
10
+ render status: 404, json: {:response => "no identifier specified"}
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ # Use a namespaced url because /:generic can easily be overwritten by app default routes
3
+ get "/editmode/clear_cache" => "editmode#clear_cache" , :as => :editmode_clear_cache
4
+ end
@@ -0,0 +1,14 @@
1
+ require "active_support/dependencies"
2
+
3
+ require "editmode-rails/version"
4
+
5
+ require 'editmode-rails/script_tag'
6
+ require 'editmode-rails/action_view_extensions/editmode_helper'
7
+ require 'editmode-rails/auto_include_filter'
8
+ require 'editmode-rails/railtie' if defined? Rails
9
+
10
+ module EditModeRails
11
+ # Your code goes here...
12
+ end
13
+
14
+ require 'editmode-rails/engine' if defined?(Rails)
@@ -0,0 +1,75 @@
1
+ module EditModeRails
2
+
3
+ module ActionViewExtensions
4
+ module EditModeHelper
5
+
6
+ require 'httparty'
7
+
8
+ def api_version
9
+ "v1"
10
+ end
11
+
12
+ def api_root_url
13
+ ENV["EDITMODE_OVERRIDE_API_URL"] || "https://www.editmode.app/api"
14
+ end
15
+
16
+ def versioned_api_url
17
+ "#{api_root_url}/#{api_version}"
18
+ end
19
+
20
+ def chunk_display(label,identifier,options={})
21
+
22
+ chunk_content = Rails.cache.fetch("bit_#{identifier}") do
23
+ url = "#{versioned_api_url}/bits/#{identifier}"
24
+ response = HTTParty.get(url)
25
+ chunk_content = response['content']
26
+ end
27
+
28
+ display_type = options[:display_type] || "span"
29
+ css_class = options[:css_class]
30
+ content_type = "plain"
31
+
32
+ # Simple check to see if returned chunk contains html. Regex will need to be improved
33
+ if /<[a-z][\s\S]*>/i.match(chunk_content)
34
+ content_type = "rich"
35
+ chunk_content = chunk_content.html_safe
36
+ elsif chunk_content.include? "\n"
37
+ content_type = "rich"
38
+ renderer = Redcarpet::Render::HTML.new(no_links: true, hard_wrap: true)
39
+ markdown = Redcarpet::Markdown.new(renderer, extensions = {})
40
+ chunk_content = markdown.render(chunk_content).html_safe
41
+ end
42
+
43
+ case display_type
44
+ when "span"
45
+ if content_type == "rich"
46
+ content_tag(:span, :class => css_class, :data => {:chunk => identifier, :chunk_editable => false }) do
47
+ chunk_content
48
+ end
49
+ else
50
+ content_tag(:span, :class => css_class, :data => {:chunk => identifier, :chunk_editable => true }) do
51
+ chunk_content
52
+ end
53
+ end
54
+ when "raw"
55
+ chunk_content
56
+ end
57
+
58
+ end
59
+
60
+ def bit(label,identifier,options={})
61
+ chunk_display(label,identifier)
62
+ end
63
+
64
+ def chunk(label,identifier,options={})
65
+ chunk_display(label,identifier)
66
+ end
67
+
68
+ def raw_chunk(label,identifier,options={})
69
+ chunk_display(label,identifier,options.merge(:display_type => "raw"))
70
+ end
71
+
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,65 @@
1
+ module EditModeRails
2
+
3
+ module AutoInclude
4
+
5
+ module Method
6
+ def editmode_auto_include
7
+ EditModeRails::AutoInclude::Filter.filter(self)
8
+ end
9
+ end
10
+
11
+ class Filter
12
+
13
+ CLOSING_BODY_TAG = %r{</body>}
14
+ def self.filter(controller)
15
+ auto_include_filter = new(controller)
16
+ return unless auto_include_filter.include_javascript?
17
+
18
+ auto_include_filter.include_javascript!
19
+ end
20
+
21
+ attr_reader :controller
22
+
23
+ def initialize(kontroller)
24
+ @controller = kontroller
25
+ end
26
+
27
+ def include_javascript!
28
+ response.body = response.body.gsub(CLOSING_BODY_TAG, editmode_script_tag.output + '\\0')
29
+ end
30
+
31
+ def include_javascript?
32
+ enabled_for_environment? &&
33
+ html_content_type? &&
34
+ response_has_closing_body_tag? &&
35
+ editmode_script_tag.valid?
36
+ end
37
+
38
+ private
39
+ def response
40
+ controller.response
41
+ end
42
+
43
+ def html_content_type?
44
+ response.content_type == 'text/html'
45
+ end
46
+
47
+ def response_has_closing_body_tag?
48
+ !!(response.body[CLOSING_BODY_TAG])
49
+ end
50
+
51
+ def editmode_script_tag
52
+ @script_tag ||= EditModeRails::ScriptTag.new()
53
+ end
54
+
55
+ def enabled_for_environment?
56
+ enabled_environments = ["production","development","staging"]
57
+ return true if enabled_environments.nil?
58
+ enabled_environments.map(&:to_s).include?(Rails.env)
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,8 @@
1
+ module EditMode
2
+
3
+ class Engine < Rails::Engine
4
+
5
+
6
+ end
7
+
8
+ end
@@ -0,0 +1,19 @@
1
+ module EditModeRails
2
+ class Railtie < Rails::Railtie
3
+ initializer "editmode-rails" do |app|
4
+
5
+ ActiveSupport.on_load :action_view do
6
+ include EditModeRails::ActionViewExtensions::EditModeHelper
7
+ end
8
+ ActiveSupport.on_load :action_controller do
9
+ include AutoInclude::Method
10
+
11
+ if respond_to? :after_action
12
+ after_action :editmode_auto_include
13
+ else
14
+ after_filter :editmode_auto_include
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ module EditModeRails
2
+
3
+ class ScriptTag
4
+
5
+ def self.generate(*args)
6
+ new(*args).output
7
+ end
8
+
9
+ def initialize(options = {})
10
+
11
+ end
12
+
13
+ def valid?
14
+ true
15
+ end
16
+
17
+ def output
18
+
19
+ str = <<-EDITMODE_SCRIPT
20
+ <script src="#{script_url}" async ></script>
21
+ EDITMODE_SCRIPT
22
+
23
+ str.respond_to?(:html_safe) ? str.html_safe : str
24
+
25
+ end
26
+
27
+ def script_url
28
+ ENV["EDITMODE_OVERRIDE_SCRIPT_URL"] || "https://www.editmode.app/assets/chunks.js"
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module EditModeRails
2
+ VERSION = "0.0.9.8"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: editmode-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9.8
5
+ platform: ruby
6
+ authors:
7
+ - Tony Ennis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 10.4.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 10.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.2
69
+ description: The Editmode gem allows you to include chunks from the Editmode platform
70
+ in your rails views
71
+ email:
72
+ - ennis.tony@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - Gemfile
78
+ - README.md
79
+ - Rakefile
80
+ - app/controllers/editmode_controller.rb
81
+ - config/routes.rb
82
+ - lib/editmode-rails.rb
83
+ - lib/editmode-rails/action_view_extensions/editmode_helper.rb
84
+ - lib/editmode-rails/auto_include_filter.rb
85
+ - lib/editmode-rails/engine.rb
86
+ - lib/editmode-rails/railtie.rb
87
+ - lib/editmode-rails/script_tag.rb
88
+ - lib/editmode-rails/version.rb
89
+ homepage: https://github.com/tonyennis145/editmode-rails
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.6
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Editmode allows you to turn plain text in your rails app into easily inline-editable
112
+ bits of content that can be managed by anyone with no technical knowledge
113
+ test_files: []