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.
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/deface.gemspec CHANGED
@@ -1,30 +1,52 @@
1
- Gem::Specification.new do |s|
2
- s.name = "deface"
3
- s.version = "1.5.0"
4
-
5
- s.authors = ["Brian D Quinn"]
6
- s.description = "Deface is a library that allows you to customize ERB, Haml and Slim views in a Rails application without editing the underlying view."
7
- s.email = "brian@spreecommerce.com"
8
- s.extra_rdoc_files = [
9
- "README.markdown", "CHANGELOG.markdown"
10
- ]
11
- s.files = `git ls-files`.split("\n")
12
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
- s.homepage = "https://github.com/spree/deface"
14
- s.rdoc_options = ["--charset=UTF-8"]
15
- s.require_paths = ["lib"]
16
- s.summary = "Deface is a library that allows you to customize ERB, Haml and Slim views in Rails"
17
-
18
- s.add_dependency('nokogiri', '>= 1.6')
19
- s.add_dependency('rails', '>= 4.1')
20
- s.add_dependency('rainbow', '>= 2.1.0')
21
- s.add_dependency('polyglot')
22
-
23
- s.add_development_dependency('appraisal')
24
- s.add_development_dependency('erubis')
25
- s.add_development_dependency('rspec', '>= 3.1.0')
26
- s.add_development_dependency('haml', ['>= 4.0', '< 6'])
27
- s.add_development_dependency('slim', '~> 3.0')
28
- s.add_development_dependency('simplecov', '>= 0.6.4')
29
- s.add_development_dependency('generator_spec', '~> 0.8')
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
@@ -2,7 +2,8 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 5.2.0"
5
+ gem "actionview", "~> 5.2.0"
6
+ gem "railties", "~> 5.2.0"
6
7
 
7
8
  group :test do
8
9
  gem "test-unit"
@@ -2,7 +2,8 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 6.0.0.beta3"
5
+ gem "actionview", "~> 6.0.0"
6
+ gem "railties", "~> 6.0.0"
6
7
 
7
8
  group :test do
8
9
  gem "test-unit"
@@ -2,7 +2,8 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 4.2.0"
5
+ gem "actionview", "~> 6.1.0"
6
+ gem "railties", "~> 6.1.0"
6
7
 
7
8
  group :test do
8
9
  gem "test-unit"
@@ -2,7 +2,8 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 5.0.0"
5
+ gem "actionview", "~> 7.0.0"
6
+ gem "railties", "~> 7.0.0"
6
7
 
7
8
  group :test do
8
9
  gem "test-unit"
@@ -1,102 +1,86 @@
1
- ActionView::Template.class_eval do
2
- alias_method :initialize_without_deface, :initialize
3
-
4
- def initialize(source, identifier, handler, details)
5
- syntax = determine_syntax(handler)
6
-
7
- if Rails.application.config.deface.enabled && should_be_defaced?(syntax)
8
-
9
- processed_source = Deface::Override.apply(source.to_param, details, true, syntax)
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
- processed_source = source.to_param
12
+ nil
19
13
  end
20
-
21
- initialize_without_deface(processed_source, identifier, handler, details)
22
14
  end
23
15
 
24
- alias_method :render_without_deface, :render
25
-
26
- # refresh view to get source again if
27
- # view needs to be recompiled
28
- #
29
- def render(view, locals, buffer=nil, &block)
30
-
31
- if Rails.version < "6.0.0.beta1" && view.is_a?(ActionView::CompiledTemplates)
32
- mod = ActionView::CompiledTemplates
33
- elsif Rails.version >= "6.0.0.beta1" && view.is_a?(ActionDispatch::DebugView)
34
- mod = ActionDispatch::DebugView
35
- else
36
- mod = view.singleton_class
37
- end
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
- if @compiled && !mod.instance_methods.map(&:to_s).include?(method_name)
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
- protected
51
+ private
47
52
 
48
- alias_method :method_name_without_deface, :method_name
53
+ def compile!(view)
54
+ return super unless Rails.application.config.deface.enabled
49
55
 
50
- # inject deface hash into compiled view method name
51
- # used to determine if recompilation is needed
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
- #we digest the whole method name as if it gets too long there's problems
57
- "_#{Deface::Digest.hexdigest("#{deface_hash}_#{method_name_without_deface}")}"
58
- end
59
-
60
- private
60
+ if @deface_hash != current_deface_hash
61
+ @compiled = nil
62
+ @deface_hash = current_deface_hash
63
+ end
64
+ end
61
65
 
62
- def should_be_defaced?(syntax)
63
- syntax != :unknown
66
+ super
64
67
  end
65
68
 
66
- def determine_syntax(handler)
67
- if handler.to_s == "Haml::Plugin"
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
- # https://github.com/rails/rails/commit/ec5c946138f63dc975341d6521587adc74f6b441
81
- # https://github.com/rails/rails/commit/ccfa01c36e79013881ffdb7ebe397cec733d15b2#diff-dfb6e0314ad9639bab460ea64871aa47R27
82
- if defined?( ActionView::Template::Handlers::ERB::Erubi)
83
- ActionView::Template::Handlers::ERB::Erubi.class_eval do
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
- @newline_pending = 0
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
- #fix for Rails 3.1 not setting virutal_path anymore (BOO!)
100
- if defined?(ActionView::Resolver::Path)
101
- ActionView::Resolver::Path.class_eval { alias_method :virtual, :to_s }
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
@@ -6,58 +6,70 @@ module Deface
6
6
  def apply(source, details, log=true, syntax=:erb)
7
7
  overrides = find(details)
8
8
 
9
- if log && overrides.size > 0
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
- unless overrides.empty?
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
- doc = Deface::Parser.convert(source)
13
+ apply_overrides(
14
+ convert_source(source, syntax: syntax),
15
+ overrides: overrides,
16
+ log: log
17
+ )
18
+ end
23
19
 
24
- overrides.each do |override|
25
- if override.disabled?
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
- override.parsed_document = doc
31
- matches = override.matcher.matches(doc, log)
23
+ doc = Deface::Parser.convert(source)
32
24
 
33
- if log
34
- Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'")
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
- # temporarily check and notify on use of old selector styles.
37
- #
38
- if matches.empty? && override.selector.match(/code|erb-loud|erb-silent/)
39
- 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>"
40
- end
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
- if matches.empty?
44
- override.failure = "failed to match :#{override.action} selector '#{override.selector}'"
45
- else
46
- override.failure = nil
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
- #prevents any caching by rails in development mode
52
- details[:updated_at] = Time.now
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
- source = doc.to_s
52
+ source = doc.to_s
55
53
 
56
- Deface::Parser.undo_erb_markup!(source)
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)
@@ -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 = if Rails.version >= "4.0"
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
- if Rails.version[0..2] >= "3.2"
93
- # add path to watchable_dir so Rails will call to_prepare on file changes
94
- # allowing overrides to be updated / reloaded in development mode.
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)
@@ -0,0 +1,5 @@
1
+ module Deface
2
+ class DefaceError < StandardError; end
3
+
4
+ class NotSupportedError < DefaceError; end
5
+ end
@@ -37,6 +37,7 @@ module Deface
37
37
  # coverts { attributes into deface compatibily attributes
38
38
  def deface_attributes(attrs)
39
39
  return if attrs.nil?
40
+ return attrs if attrs.scan(/\{/).count > 1
40
41
 
41
42
  attrs.gsub! /\{|\}/, ''
42
43
 
@@ -70,8 +70,8 @@ module Deface
70
70
  raise(ArgumentError, ":action is invalid") if self.action.nil?
71
71
  end
72
72
 
73
- #set loaded time (if not already present) for hash invalidation
74
- @args[:updated_at] ||= Time.zone.now.to_f
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
- # check if method is compiled for the current virtual path
215
- #
216
- def expire_compiled_template
217
- if Rails.version < "6.0.0.beta1"
218
- if compiled_method_name = ActionView::CompiledTemplates.instance_methods.detect { |name| name =~ /#{args[:virtual_path].gsub(/[^a-z_]/, '_')}/ }
219
- #if the compiled method does not contain the current deface digest
220
- #then remove the old method - this will allow the template to be
221
- #recompiled the next time it is rendered (showing the latest changes)
222
-
223
- unless compiled_method_name =~ /\A_#{self.class.digest(:virtual_path => @args[:virtual_path])}_/
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
@@ -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.config.eager_load_paths = app.config.eager_load_paths.reject { |path| path.to_s =~ /app\/overrides\z/ }
39
+ tweak_eager_loading(app)
40
40
 
41
41
  # railites / engines / extensions
42
- railties = if Rails.version >= "4.0"
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
- railtie.config.eager_load_paths = railtie.config.eager_load_paths.reject { |path| path.to_s =~ /app\/overrides\z/ }
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, '') }