deface 1.5.0 → 1.9.0
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/.github/dependabot.yml +7 -0
- data/.github/workflows/main.yml +45 -0
- data/.gitignore +2 -0
- data/Appraisals +28 -12
- data/CHANGELOG.markdown +1 -4
- data/CONTRIBUTING.md +12 -0
- data/README.markdown +90 -106
- data/Rakefile +5 -1
- data/bin/rails +8 -0
- data/bin/rails-engine +13 -0
- data/bin/rails-sandbox +18 -0
- data/bin/sandbox +42 -0
- data/bin/sandbox-setup +89 -0
- data/bin/setup +8 -0
- data/deface.gemspec +51 -29
- data/gemfiles/rails_5.2.gemfile +2 -1
- data/gemfiles/rails_6.0.gemfile +2 -1
- data/gemfiles/{rails_4.2.gemfile → rails_6.1.gemfile} +2 -1
- data/gemfiles/{rails_5.0.gemfile → rails_7.0.gemfile} +2 -1
- data/lib/deface/action_view_extensions.rb +67 -83
- data/lib/deface/applicator.rb +49 -37
- data/lib/deface/environment.rb +4 -12
- data/lib/deface/errors.rb +5 -0
- data/lib/deface/haml_converter.rb +1 -0
- data/lib/deface/override.rb +16 -22
- data/lib/deface/railtie.rb +15 -9
- data/lib/deface/search.rb +1 -1
- data/lib/deface/template_helper.rb +42 -16
- data/lib/deface/version.rb +9 -0
- data/lib/deface.rb +12 -8
- data/spec/deface/action_view_template_spec.rb +127 -69
- data/spec/deface/environment_spec.rb +8 -12
- data/spec/deface/haml_converter_spec.rb +10 -0
- data/spec/deface/override_spec.rb +2 -2
- data/spec/deface/search_spec.rb +6 -0
- data/spec/spec_helper.rb +3 -6
- metadata +75 -22
- data/.travis.yml +0 -27
- data/gemfiles/rails_5.1.gemfile +0 -13
- data/init.rb +0 -1
data/bin/sandbox-setup
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
extend FileUtils
|
|
5
|
+
|
|
6
|
+
system 'bin/rails g scaffold Post title body'
|
|
7
|
+
system 'bin/rails db:migrate'
|
|
8
|
+
|
|
9
|
+
mkdir_p 'sandbox/app/overrides'
|
|
10
|
+
|
|
11
|
+
File.write 'sandbox/app/views/posts/_haml_partial.html.haml', <<~HAML
|
|
12
|
+
%h2 hello from haml
|
|
13
|
+
HAML
|
|
14
|
+
|
|
15
|
+
File.write 'sandbox/app/views/posts/_slim_partial.html.slim', <<~SLIM
|
|
16
|
+
h2 hello from slim
|
|
17
|
+
SLIM
|
|
18
|
+
|
|
19
|
+
File.write 'sandbox/app/views/posts/_haml_defaced_partial.html.haml', <<~HAML
|
|
20
|
+
%h3 hello from defaced haml
|
|
21
|
+
HAML
|
|
22
|
+
|
|
23
|
+
File.write 'sandbox/app/views/posts/_slim_defaced_partial.html.slim', <<~SLIM
|
|
24
|
+
h3 hello from defaced slim
|
|
25
|
+
SLIM
|
|
26
|
+
|
|
27
|
+
File.write 'sandbox/app/overrides/improved_posts.rb', <<~RUBY
|
|
28
|
+
Deface::Override.new(
|
|
29
|
+
virtual_path: "posts/show",
|
|
30
|
+
name: "sparkling_post_title",
|
|
31
|
+
replace: 'p:nth-child(2)',
|
|
32
|
+
text: "<h1>✨<%= @post.title %>✨</h1>"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
Deface::Override.new(
|
|
36
|
+
virtual_path: "posts/show",
|
|
37
|
+
name: "modern_style_post_body",
|
|
38
|
+
replace: 'p:nth-child(3)',
|
|
39
|
+
text: "<p style='border:2px gray solid; padding: 1rem;'><%= @post.body %></p>"
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
Deface::Override.new(
|
|
43
|
+
virtual_path: "posts/index",
|
|
44
|
+
name: "sparkling_posts_title",
|
|
45
|
+
replace: 'tr td:first-child',
|
|
46
|
+
text: "<td>✨<%= post.title %>✨</td>"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
Deface::Override.new(
|
|
50
|
+
virtual_path: "posts/index",
|
|
51
|
+
name: "modern_style_post_body",
|
|
52
|
+
replace: 'tr td:nth-child(2)',
|
|
53
|
+
text: "<td style='border:2px gray solid; padding: 1rem;'><%= post.body %></d>"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
Deface::Override.new(
|
|
57
|
+
virtual_path: "posts/index",
|
|
58
|
+
name: "haml_and_slim_partials",
|
|
59
|
+
insert_before: 'table',
|
|
60
|
+
text: "
|
|
61
|
+
<header><%= render 'haml_partial' %><%= render 'slim_partial' %></header>
|
|
62
|
+
<section><%= render 'haml_defaced_partial' %><%= render 'slim_defaced_partial' %></section>
|
|
63
|
+
"
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
Deface::Override.new(
|
|
67
|
+
virtual_path: "posts/_haml_defaced_partial",
|
|
68
|
+
name: "haml_deface",
|
|
69
|
+
insert_before: 'h3',
|
|
70
|
+
text: "<h4>HAML subtitle</h4>"
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
Deface::Override.new(
|
|
74
|
+
virtual_path: "posts/_slim_defaced_partial",
|
|
75
|
+
name: "slim_deface",
|
|
76
|
+
insert_before: 'h3',
|
|
77
|
+
text: "<h4>SLIM subtitle</h4>"
|
|
78
|
+
)
|
|
79
|
+
RUBY
|
|
80
|
+
|
|
81
|
+
File.write 'sandbox/config/routes.rb', <<~RUBY
|
|
82
|
+
Rails.application.routes.draw do
|
|
83
|
+
resources :posts
|
|
84
|
+
root to: "posts#index"
|
|
85
|
+
end
|
|
86
|
+
RUBY
|
|
87
|
+
|
|
88
|
+
system "bin/rails", "runner", "Post.create(title: 'Foo', body: 'Bar '*10)"
|
|
89
|
+
system "bin/rails", "runner", "Post.create(title: 'Baz', body: 'Boz '*10)"
|
data/bin/setup
ADDED
data/deface.gemspec
CHANGED
|
@@ -1,30 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
require_relative 'lib/deface/version'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "deface"
|
|
5
|
+
spec.version = Deface::VERSION
|
|
6
|
+
spec.authors = ["Brian D Quinn"]
|
|
7
|
+
spec.email = "brian@spreecommerce.com"
|
|
8
|
+
|
|
9
|
+
spec.summary = "Deface is a library that allows you to customize ERB, Haml and Slim views in Rails"
|
|
10
|
+
spec.description = "Deface is a library that allows you to customize ERB, Haml and Slim views in a Rails application without editing the underlying view."
|
|
11
|
+
spec.homepage = "https://github.com/spree/deface#readme"
|
|
12
|
+
spec.license = "MIT"
|
|
13
|
+
|
|
14
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
15
|
+
spec.metadata['source_code_uri'] = 'https://github.com/spree/deface'
|
|
16
|
+
spec.metadata['changelog_uri'] = 'https://github.com/spree/deface/releases'
|
|
17
|
+
|
|
18
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
|
19
|
+
|
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
22
|
+
files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0") }
|
|
23
|
+
|
|
24
|
+
spec.files = files.grep_v(%r{^(test|spec|features)/})
|
|
25
|
+
spec.test_files = files.grep(%r{^(test|spec|features)/})
|
|
26
|
+
spec.bindir = "exe"
|
|
27
|
+
spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
spec.rdoc_options = ["--charset=UTF-8"]
|
|
30
|
+
spec.extra_rdoc_files = ["README.markdown"]
|
|
31
|
+
|
|
32
|
+
spec.add_dependency('nokogiri', '>= 1.6')
|
|
33
|
+
|
|
34
|
+
%w[
|
|
35
|
+
actionview
|
|
36
|
+
railties
|
|
37
|
+
].each do |rails_gem|
|
|
38
|
+
spec.add_dependency(rails_gem, '>= 5.2')
|
|
39
|
+
end
|
|
40
|
+
spec.add_dependency('rainbow', '>= 2.1.0')
|
|
41
|
+
spec.add_dependency('polyglot')
|
|
42
|
+
|
|
43
|
+
spec.add_development_dependency('appraisal')
|
|
44
|
+
spec.add_development_dependency('erubis')
|
|
45
|
+
spec.add_development_dependency('gem-release')
|
|
46
|
+
spec.add_development_dependency('rspec', '>= 3.1.0')
|
|
47
|
+
spec.add_development_dependency('haml', ['>= 4.0', '< 6'])
|
|
48
|
+
spec.add_development_dependency('slim', '~> 4.1')
|
|
49
|
+
spec.add_development_dependency('simplecov', '>= 0.6.4')
|
|
50
|
+
spec.add_development_dependency('generator_spec', '~> 0.8')
|
|
51
|
+
spec.add_development_dependency('pry')
|
|
30
52
|
end
|
data/gemfiles/rails_5.2.gemfile
CHANGED
data/gemfiles/rails_6.0.gemfile
CHANGED
|
@@ -1,102 +1,86 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
# force change in handler before continuing to original Rails method
|
|
12
|
-
# as we've just converted some other template language into ERB!
|
|
13
|
-
#
|
|
14
|
-
if [:slim, :haml].include?(syntax) && processed_source != source.to_param
|
|
15
|
-
handler = ActionView::Template::Handlers::ERB
|
|
16
|
-
end
|
|
1
|
+
module Deface::ActionViewExtensions
|
|
2
|
+
def self.determine_syntax(handler)
|
|
3
|
+
return unless Rails.application.config.deface.enabled
|
|
4
|
+
|
|
5
|
+
if handler.to_s == "Haml::Plugin"
|
|
6
|
+
:haml
|
|
7
|
+
elsif handler.class.to_s == "Slim::RailsTemplate"
|
|
8
|
+
:slim
|
|
9
|
+
elsif handler.to_s.demodulize == "ERB" || handler.class.to_s.demodulize == "ERB"
|
|
10
|
+
:erb
|
|
17
11
|
else
|
|
18
|
-
|
|
12
|
+
nil
|
|
19
13
|
end
|
|
20
|
-
|
|
21
|
-
initialize_without_deface(processed_source, identifier, handler, details)
|
|
22
14
|
end
|
|
23
15
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
16
|
+
module DefacedTemplate
|
|
17
|
+
def encode!
|
|
18
|
+
return super unless Rails.application.config.deface.enabled
|
|
19
|
+
|
|
20
|
+
# Before Rails 6 encode! returns nil
|
|
21
|
+
source = Deface.before_rails_6? ? (super; @source) : super
|
|
22
|
+
syntax = Deface::ActionViewExtensions.determine_syntax(@handler)
|
|
23
|
+
overrides = Deface::Override.find(
|
|
24
|
+
locals: @locals,
|
|
25
|
+
format: @format,
|
|
26
|
+
variant: @variant,
|
|
27
|
+
virtual_path: @virtual_path,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
if syntax && overrides.any?
|
|
31
|
+
# Prevents any caching by rails in development mode.
|
|
32
|
+
@updated_at = Time.now if Deface.before_rails_6?
|
|
33
|
+
@handler = ActionView::Template::Handlers::ERB
|
|
34
|
+
|
|
35
|
+
# Modify the existing string instead of returning a copy
|
|
36
|
+
new_source = Deface::Override.apply_overrides(
|
|
37
|
+
Deface::Override.convert_source(source, syntax: syntax),
|
|
38
|
+
overrides: overrides
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if Deface.before_rails_6?
|
|
42
|
+
@source.replace new_source
|
|
43
|
+
else
|
|
44
|
+
source.replace new_source
|
|
45
|
+
end
|
|
46
|
+
end
|
|
38
47
|
|
|
39
|
-
|
|
40
|
-
@compiled = false
|
|
41
|
-
@source = refresh(view).source
|
|
48
|
+
source
|
|
42
49
|
end
|
|
43
|
-
render_without_deface(view, locals, buffer, &block)
|
|
44
|
-
end
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
private
|
|
47
52
|
|
|
48
|
-
|
|
53
|
+
def compile!(view)
|
|
54
|
+
return super unless Rails.application.config.deface.enabled
|
|
49
55
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def method_name
|
|
54
|
-
deface_hash = Deface::Override.digest(:virtual_path => @virtual_path)
|
|
56
|
+
@compile_mutex.synchronize do
|
|
57
|
+
current_deface_hash = Deface::Override.digest(virtual_path: @virtual_path)
|
|
58
|
+
@deface_hash = current_deface_hash if @deface_hash.nil?
|
|
55
59
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
if @deface_hash != current_deface_hash
|
|
61
|
+
@compiled = nil
|
|
62
|
+
@deface_hash = current_deface_hash
|
|
63
|
+
end
|
|
64
|
+
end
|
|
61
65
|
|
|
62
|
-
|
|
63
|
-
syntax != :unknown
|
|
66
|
+
super
|
|
64
67
|
end
|
|
65
68
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
:haml
|
|
69
|
-
elsif handler.class.to_s == "Slim::RailsTemplate"
|
|
70
|
-
:slim
|
|
71
|
-
elsif handler.to_s.demodulize == "ERB" || handler.class.to_s.demodulize == "ERB"
|
|
72
|
-
:erb
|
|
73
|
-
else
|
|
74
|
-
:unknown
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
|
69
|
+
ActionView::Template.prepend self
|
|
70
|
+
end
|
|
78
71
|
|
|
79
|
-
# Rails 6 fix
|
|
80
|
-
#
|
|
81
|
-
# https://github.com/rails/rails/commit/
|
|
82
|
-
|
|
83
|
-
|
|
72
|
+
# Rails 6 fix.
|
|
73
|
+
#
|
|
74
|
+
# https://github.com/rails/rails/commit/ec5c946138f63dc975341d6521587adc74f6b441
|
|
75
|
+
# https://github.com/rails/rails/commit/ccfa01c36e79013881ffdb7ebe397cec733d15b2#diff-dfb6e0314ad9639bab460ea64871aa47R27
|
|
76
|
+
module ErubiHandlerFix
|
|
84
77
|
def initialize(input, properties = {})
|
|
85
|
-
@
|
|
86
|
-
|
|
87
|
-
# Dup properties so that we don't modify argument
|
|
88
|
-
properties = Hash[properties]
|
|
89
|
-
properties[:preamble] = "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
|
|
90
|
-
properties[:postamble] = "@output_buffer.to_s"
|
|
91
|
-
properties[:bufvar] = "@output_buffer"
|
|
92
|
-
properties[:escapefunc] = ""
|
|
93
|
-
|
|
78
|
+
properties[:preamble] = "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
|
|
94
79
|
super
|
|
95
80
|
end
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
81
|
|
|
99
|
-
#
|
|
100
|
-
|
|
101
|
-
|
|
82
|
+
# We use include to place the module between the class' call to super and the
|
|
83
|
+
# actual execution within Erubi::Engine.
|
|
84
|
+
ActionView::Template::Handlers::ERB::Erubi.include self unless Deface.before_rails_6?
|
|
85
|
+
end
|
|
102
86
|
end
|
data/lib/deface/applicator.rb
CHANGED
|
@@ -6,58 +6,70 @@ module Deface
|
|
|
6
6
|
def apply(source, details, log=true, syntax=:erb)
|
|
7
7
|
overrides = find(details)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'"
|
|
11
|
-
end
|
|
9
|
+
return source if overrides.empty?
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
case syntax
|
|
15
|
-
when :haml
|
|
16
|
-
#convert haml to erb before parsing before
|
|
17
|
-
source = Deface::HamlConverter.new(source.to_param).result
|
|
18
|
-
when :slim
|
|
19
|
-
source = Deface::SlimConverter.new(source.to_param).result
|
|
20
|
-
end
|
|
11
|
+
Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'" if log
|
|
21
12
|
|
|
22
|
-
|
|
13
|
+
apply_overrides(
|
|
14
|
+
convert_source(source, syntax: syntax),
|
|
15
|
+
overrides: overrides,
|
|
16
|
+
log: log
|
|
17
|
+
)
|
|
18
|
+
end
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log
|
|
27
|
-
next
|
|
28
|
-
end
|
|
20
|
+
# applies specified overrides to given source
|
|
21
|
+
def apply_overrides(source, overrides:, log: true)
|
|
29
22
|
|
|
30
|
-
|
|
31
|
-
matches = override.matcher.matches(doc, log)
|
|
23
|
+
doc = Deface::Parser.convert(source)
|
|
32
24
|
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
overrides.each do |override|
|
|
26
|
+
if override.disabled?
|
|
27
|
+
Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log
|
|
28
|
+
next
|
|
29
|
+
end
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
end
|
|
31
|
+
override.parsed_document = doc
|
|
32
|
+
matches = override.matcher.matches(doc, log)
|
|
33
|
+
|
|
34
|
+
if log
|
|
35
|
+
Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'")
|
|
42
36
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
override.
|
|
47
|
-
matches.each {|match| override.execute_action match }
|
|
37
|
+
# temporarily check and notify on use of old selector styles.
|
|
38
|
+
#
|
|
39
|
+
if matches.empty? && override.selector.match(/code|erb-loud|erb-silent/)
|
|
40
|
+
Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m Override '#{override.name}' may be using an invalid selector of '#{override.selector}', <code erb-loud|silent> tags are now <erb loud|silent>"
|
|
48
41
|
end
|
|
49
42
|
end
|
|
50
43
|
|
|
51
|
-
|
|
52
|
-
|
|
44
|
+
if matches.empty?
|
|
45
|
+
override.failure = "failed to match :#{override.action} selector '#{override.selector}'"
|
|
46
|
+
else
|
|
47
|
+
override.failure = nil
|
|
48
|
+
matches.each {|match| override.execute_action match }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
53
51
|
|
|
54
|
-
|
|
52
|
+
source = doc.to_s
|
|
55
53
|
|
|
56
|
-
|
|
57
|
-
end
|
|
54
|
+
Deface::Parser.undo_erb_markup!(source)
|
|
58
55
|
|
|
59
56
|
source
|
|
60
57
|
end
|
|
58
|
+
|
|
59
|
+
# converts the source to a supported syntax (ERB)
|
|
60
|
+
def convert_source(source, syntax:)
|
|
61
|
+
# convert haml/slim to erb before parsing before
|
|
62
|
+
case syntax
|
|
63
|
+
when :erb
|
|
64
|
+
source
|
|
65
|
+
when :haml
|
|
66
|
+
Deface::HamlConverter.new(source.to_s).result
|
|
67
|
+
when :slim
|
|
68
|
+
Deface::SlimConverter.new(source.to_s).result
|
|
69
|
+
else
|
|
70
|
+
raise "unsupported syntax: #{syntax}"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
61
73
|
end
|
|
62
74
|
|
|
63
75
|
def execute_action(target_element)
|
data/lib/deface/environment.rb
CHANGED
|
@@ -57,13 +57,7 @@ module Deface
|
|
|
57
57
|
Deface::DSL::Loader.register
|
|
58
58
|
|
|
59
59
|
# check all railties / engines / extensions / application for overrides
|
|
60
|
-
railties
|
|
61
|
-
app.railties._all
|
|
62
|
-
else
|
|
63
|
-
app.railties.all
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
railties.dup.push(app).each do |railtie|
|
|
60
|
+
app.railties._all.dup.push(app).each do |railtie|
|
|
67
61
|
next unless railtie.respond_to? :root
|
|
68
62
|
load_overrides(railtie)
|
|
69
63
|
end
|
|
@@ -89,11 +83,9 @@ module Deface
|
|
|
89
83
|
paths ||= ["app/overrides"]
|
|
90
84
|
|
|
91
85
|
paths.each do |path|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
Rails.application.config.watchable_dirs[root.join(path).to_s] = [:rb, :deface]
|
|
96
|
-
end
|
|
86
|
+
# add path to watchable_dir so Rails will call to_prepare on file changes
|
|
87
|
+
# allowing overrides to be updated / reloaded in development mode.
|
|
88
|
+
Rails.application.config.watchable_dirs[root.join(path).to_s] = [:rb, :deface]
|
|
97
89
|
|
|
98
90
|
Dir.glob(root.join path, "**/*.rb") do |c|
|
|
99
91
|
Rails.application.config.cache_classes ? require(c) : load(c)
|
data/lib/deface/override.rb
CHANGED
|
@@ -70,8 +70,8 @@ module Deface
|
|
|
70
70
|
raise(ArgumentError, ":action is invalid") if self.action.nil?
|
|
71
71
|
end
|
|
72
72
|
|
|
73
|
-
#
|
|
74
|
-
@args[:updated_at] ||= Time.
|
|
73
|
+
# Set loaded time (if not already present) for hash invalidation
|
|
74
|
+
@args[:updated_at] ||= Time.current.to_f
|
|
75
75
|
@args[:railtie_class] = self.class.current_railtie
|
|
76
76
|
|
|
77
77
|
self.class.all[virtual_key][name_key] = self
|
|
@@ -211,28 +211,22 @@ module Deface
|
|
|
211
211
|
|
|
212
212
|
private
|
|
213
213
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
ActionView::CompiledTemplates.send :remove_method, compiled_method_name
|
|
225
|
-
end
|
|
226
|
-
end
|
|
227
|
-
else
|
|
228
|
-
if compiled_method_name = ActionDispatch::DebugView.instance_methods.detect { |name| name =~ /#{args[:virtual_path].gsub(/[^a-z_]/, '_')}/ }
|
|
229
|
-
unless compiled_method_name =~ /\A_#{self.class.digest(:virtual_path => @args[:virtual_path])}_/
|
|
230
|
-
ActionDispatch::DebugView.send :remove_method, compiled_method_name
|
|
231
|
-
end
|
|
232
|
-
end
|
|
233
|
-
end
|
|
214
|
+
# Check if method is compiled for the current virtual path.
|
|
215
|
+
#
|
|
216
|
+
# If the compiled method does not contain the current deface digest
|
|
217
|
+
# then remove the old method - this will allow the template to be
|
|
218
|
+
# recompiled the next time it is rendered (showing the latest changes).
|
|
219
|
+
def expire_compiled_template
|
|
220
|
+
virtual_path = args[:virtual_path]
|
|
221
|
+
|
|
222
|
+
method_name = Deface.template_class.instance_methods.detect do |name|
|
|
223
|
+
name =~ /#{virtual_path.gsub(/[^a-z_]/, '_')}/
|
|
234
224
|
end
|
|
235
225
|
|
|
226
|
+
if method_name && method_name !~ /\A_#{self.class.digest(virtual_path: virtual_path)}_/
|
|
227
|
+
Deface.template_class.send :remove_method, method_name
|
|
228
|
+
end
|
|
229
|
+
end
|
|
236
230
|
end
|
|
237
231
|
|
|
238
232
|
end
|
data/lib/deface/railtie.rb
CHANGED
|
@@ -36,19 +36,15 @@ module Deface
|
|
|
36
36
|
initializer "deface.tweak_eager_loading", :before => :set_load_path do |app|
|
|
37
37
|
|
|
38
38
|
# application
|
|
39
|
-
app
|
|
39
|
+
tweak_eager_loading(app)
|
|
40
40
|
|
|
41
41
|
# railites / engines / extensions
|
|
42
|
-
railties
|
|
43
|
-
app.railties._all
|
|
44
|
-
else
|
|
45
|
-
app.railties.all
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
railties.each do |railtie|
|
|
42
|
+
app.railties._all.each do |railtie|
|
|
49
43
|
next unless railtie.respond_to?(:root) && railtie.config.respond_to?(:eager_load_paths)
|
|
50
|
-
|
|
44
|
+
|
|
45
|
+
tweak_eager_loading(railtie)
|
|
51
46
|
end
|
|
47
|
+
|
|
52
48
|
end
|
|
53
49
|
|
|
54
50
|
# sets up deface environment and requires / loads all
|
|
@@ -87,5 +83,15 @@ module Deface
|
|
|
87
83
|
end
|
|
88
84
|
end
|
|
89
85
|
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def tweak_eager_loading(railtie)
|
|
89
|
+
paths_to_reject = railtie.config.eager_load_paths.select { |path| path.to_s =~ /app\/overrides\z/ }
|
|
90
|
+
railtie.config.eager_load_paths = railtie.config.eager_load_paths.reject { |path| path.in?(paths_to_reject) }
|
|
91
|
+
|
|
92
|
+
if Rails.configuration.respond_to?(:autoloader) && Rails.configuration.autoloader == :zeitwerk
|
|
93
|
+
Rails.autoloaders.each { |autoloader| autoloader.ignore(*paths_to_reject) }
|
|
94
|
+
end
|
|
95
|
+
end
|
|
90
96
|
end
|
|
91
97
|
end
|
data/lib/deface/search.rb
CHANGED
|
@@ -7,7 +7,7 @@ module Deface
|
|
|
7
7
|
def find(details)
|
|
8
8
|
return [] if self.all.empty? || details.empty?
|
|
9
9
|
|
|
10
|
-
virtual_path = details[:virtual_path]
|
|
10
|
+
virtual_path = details[:virtual_path].dup
|
|
11
11
|
return [] if virtual_path.nil?
|
|
12
12
|
|
|
13
13
|
[/^\//, /\.\w+\z/].each { |regex| virtual_path.gsub!(regex, '') }
|