deface 1.6.2 → 1.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d70515c1f6f72cef4d6b7d34d936e5fac929ddda6a896cbd9c0c30dddff8c19e
4
- data.tar.gz: e6950eb7f793b4111513744ddb48d7ad7a83a918a1f9b8850619b41c7864f515
3
+ metadata.gz: 7beeaa21dc8761d6d24072002e6c90c5f7e08f283e603b8404e0b4003e68e557
4
+ data.tar.gz: 35b96c7138f0c3c5309e35d030bdd758d8c5d9f856924805d5004e5925134423
5
5
  SHA512:
6
- metadata.gz: fd12b650a9104d57bc0b17c7a88324ff77008516e0fa789724f0622f59b26160050ac1eafa9dc42c73b51a93942ccb9c45902761e629f525fd32685a62afbdc9
7
- data.tar.gz: 4924e7dfd0a97adf3acebe4f4f15c5d3146e64aea5f75b9d64c0ac5746f9320678988d23af84b196ef30a2e8f1e4f1e8e37d546a5bad676fb12c6fecfc63e596
6
+ metadata.gz: 6e21f73d0d80ae1806cfa6e6ec0244f38f281b87c398a0606a12debb566bdd20458627cbfad341df85a8c72b8c7b16d83bdeec3b36cac071112c07612ef60d75
7
+ data.tar.gz: 2dac481cd10a35a8d3916af3fbaff0f155f9a7ca9828be454a39fa1c9a4f6c25bf8946a05b7274f1df7025476314cea113be50ffd8a948184bd4ca13619c5022
data/bin/sandbox-setup CHANGED
@@ -1,24 +1,49 @@
1
- #!/usr/bin/env bash
2
-
3
- bin/rails g scaffold Post title body
4
- bin/rails db:migrate
5
-
6
- mkdir sandbox/app/overrides
7
-
8
- echo <<RUBY >> sandbox/app/overrides/sparkling_title.rb
9
- Deface::Override.new(
10
- virtual_path: "posts/show",
11
- name: "sparkling_title",
12
- replace: 'p:nth-child(2)',
13
- text: "<h1>✨<%= @post.title %>✨</h1>"
14
- )
15
-
16
- Deface::Override.new(
17
- virtual_path: "posts/show",
18
- name: "sparkling_body",
19
- replace: 'p:nth-child(3)',
20
- text: "<p style='border:2px gray solid; padding: 1rem;'><%= @post.body %></p>"
21
- )
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/overrides/improved_posts.rb', <<~RUBY
12
+ Deface::Override.new(
13
+ virtual_path: "posts/show",
14
+ name: "sparkling_post_title",
15
+ replace: 'p:nth-child(2)',
16
+ text: "<h1>✨<%= @post.title %>✨</h1>"
17
+ )
18
+
19
+ Deface::Override.new(
20
+ virtual_path: "posts/show",
21
+ name: "modern_style_post_body",
22
+ replace: 'p:nth-child(3)',
23
+ text: "<p style='border:2px gray solid; padding: 1rem;'><%= @post.body %></p>"
24
+ )
25
+
26
+ Deface::Override.new(
27
+ virtual_path: "posts/index",
28
+ name: "sparkling_posts_title",
29
+ replace: 'tr td:first-child',
30
+ text: "<td>✨<%= post.title %>✨</td>"
31
+ )
32
+
33
+ Deface::Override.new(
34
+ virtual_path: "posts/index",
35
+ name: "modern_style_post_body",
36
+ replace: 'tr td:nth-child(2)',
37
+ text: "<td style='border:2px gray solid; padding: 1rem;'><%= post.body %></d>"
38
+ )
39
+ RUBY
40
+
41
+ File.write 'sandbox/config/routes.rb', <<~RUBY
42
+ Rails.application.routes.draw do
43
+ resources :posts
44
+ root to: "posts#index"
45
+ end
22
46
  RUBY
23
47
 
24
- bin/rails runner "Post.create(title: 'Foo', body: 'Bar '*10)"
48
+ system "bin/rails", "runner", "Post.create(title: 'Foo', body: 'Bar '*10)"
49
+ system "bin/rails", "runner", "Post.create(title: 'Baz', body: 'Boz '*10)"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 6.1.0"
6
+
7
+ group :test do
8
+ gem "test-unit"
9
+ gem "pry"
10
+ gem "pry-byebug"
11
+ end
12
+
13
+ gemspec path: "../"
@@ -14,46 +14,46 @@ module Deface::ActionViewExtensions
14
14
  end
15
15
 
16
16
  module DefacedTemplate
17
- def initialize(source, identifier, handler, details)
18
- syntax = Deface::ActionViewExtensions.determine_syntax(handler)
17
+ def encode!
18
+ return super unless Rails.application.config.deface.enabled
19
19
 
20
- if syntax
21
- processed_source = Deface::Override.apply(source.to_param, details, true, syntax)
20
+ # Before Rails 6 encode! returns nil
21
+ source = Deface.before_rails_6? ? (super; @source) : super
22
22
 
23
- # force change in handler before continuing to original Rails method
24
- # as we've just converted some other template language into ERB!
25
- #
26
- if [:slim, :haml].include?(syntax) && processed_source != source.to_param
27
- handler = ActionView::Template::Handlers::ERB
28
- end
29
- else
30
- processed_source = source.to_param
23
+ if (syntax = Deface::ActionViewExtensions.determine_syntax(@handler))
24
+ # Modify the existing string instead of returning a copy
25
+ source.replace Deface::Override.apply(
26
+ source, {
27
+ locals: @locals,
28
+ format: @format,
29
+ variant: @variant,
30
+ virtual_path: @virtual_path,
31
+ },
32
+ true,
33
+ syntax
34
+ )
35
+ @handler = ActionView::Template::Handlers::ERB
31
36
  end
32
37
 
33
- super(processed_source, identifier, handler, **details)
38
+ source
34
39
  end
35
40
 
36
- # refresh view to get source again if
37
- # view needs to be recompiled
38
- #
39
- def render(view, locals, buffer=nil, &block)
40
- mod = view.is_a?(Deface.template_class) ? Deface.template_class : view.singleton_class
41
+ private
41
42
 
42
- if @compiled && !mod.instance_methods.include?(method_name.to_sym)
43
- @compiled = false
44
- @source = refresh(view).source if respond_to?(:refresh)
45
- end
46
- buffer.nil? ? super(view, locals, buffer, &block) : super(view, locals, **buffer, &block)
47
- end
43
+ def compile!(view)
44
+ return super unless Rails.application.config.deface.enabled
48
45
 
49
- # inject deface hash into compiled view method name
50
- # used to determine if recompilation is needed
51
- #
52
- def method_name
53
- deface_hash = Deface::Override.digest(:virtual_path => @virtual_path)
46
+ @compile_mutex.synchronize do
47
+ current_deface_hash = Deface::Override.digest(virtual_path: @virtual_path)
48
+ @deface_hash = current_deface_hash if @deface_hash.nil?
54
49
 
55
- #we digest the whole method name as if it gets too long there's problems
56
- "_#{Deface::Digest.hexdigest("#{deface_hash}_#{super}")}"
50
+ if @deface_hash != current_deface_hash
51
+ @compiled = nil
52
+ @deface_hash = current_deface_hash
53
+ end
54
+ end
55
+
56
+ super
57
57
  end
58
58
 
59
59
  ActionView::Template.prepend self
@@ -1,32 +1,55 @@
1
1
  module Deface
2
2
  module TemplateHelper
3
+ def self.lookup_context
4
+ @lookup_context ||= ActionView::LookupContext.new(
5
+ ActionController::Base.view_paths, {:formats => [:html]}
6
+ )
7
+ end
3
8
 
4
9
  # used to find source for a partial or template using virtual_path
5
- def load_template_source(virtual_path, partial, apply_overrides=true)
10
+ def load_template_source(virtual_path, partial, apply_overrides=true, lookup_context: Deface::TemplateHelper.lookup_context)
6
11
  parts = virtual_path.split("/")
7
- prefix = []
12
+
8
13
  if parts.size == 2
9
- prefix << ""
14
+ prefix = ""
10
15
  name = virtual_path
11
16
  else
12
- prefix << parts.shift
17
+ prefix = parts.shift
13
18
  name = parts.join("/")
14
19
  end
15
20
 
16
- #this needs to be reviewed for production mode, overrides not present
17
- Rails.application.config.deface.enabled = apply_overrides
18
- @lookup_context ||= ActionView::LookupContext.new(ActionController::Base.view_paths, {:formats => [:html]})
19
- view = @lookup_context.disable_cache do
20
- @lookup_context.find(name, prefix, partial)
21
- end
21
+ view = lookup_context.disable_cache { lookup_context.find(name, [prefix], partial) }
22
22
 
23
- if view.handler.to_s == "Haml::Plugin"
24
- Deface::HamlConverter.new(view.source).result
25
- elsif view.handler.class.to_s == "Slim::RailsTemplate"
26
- Deface::SlimConverter.new(view.source).result
27
- else
28
- view.source
23
+ source =
24
+ if view.handler.to_s == "Haml::Plugin"
25
+ Deface::HamlConverter.new(view.source).result
26
+ elsif view.handler.class.to_s == "Slim::RailsTemplate"
27
+ Deface::SlimConverter.new(view.source).result
28
+ else
29
+ view.source
30
+ end
31
+
32
+ if apply_overrides
33
+ begin
34
+ # This needs to be reviewed for production mode, overrides not present
35
+ original_enabled = Rails.application.config.deface.enabled
36
+ Rails.application.config.deface.enabled = apply_overrides
37
+
38
+ if (syntax = Deface::ActionViewExtensions.determine_syntax(view.handler))
39
+ details = {
40
+ locals: view.instance_variable_get(:@locals),
41
+ format: view.instance_variable_get(:@format),
42
+ variant: view.instance_variable_get(:@variant),
43
+ virtual_path: view.instance_variable_get(:@virtual_path),
44
+ }
45
+ source = Deface::Override.apply(source, details, true, syntax)
46
+ end
47
+ ensure
48
+ Rails.application.config.deface.enabled = original_enabled
49
+ end
29
50
  end
51
+
52
+ source
30
53
  end
31
54
 
32
55
  #gets source erb for an element
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Deface
4
- VERSION = '1.6.2'
4
+ VERSION = '1.7.0'
5
5
 
6
6
  def gem_version
7
7
  Gem::Version.new(VERSION)
@@ -3,8 +3,12 @@ require 'spec_helper'
3
3
  describe Deface::ActionViewExtensions do
4
4
  include_context "mock Rails.application"
5
5
 
6
- before supports_updated_at: true do
7
- skip "Current Rails doesn't support the updated_at attribute on ActionView" unless supports_updated_at?
6
+ before after_rails_6: true do
7
+ skip "This spec is targeted at Rails v6+" if Deface.before_rails_6?
8
+ end
9
+
10
+ before before_rails_6: true do
11
+ skip "This spec is targeted at Rails before v6+" unless Deface.before_rails_6?
8
12
  end
9
13
 
10
14
  let(:template) { ActionView::Template.new(
@@ -12,7 +16,7 @@ describe Deface::ActionViewExtensions do
12
16
  path,
13
17
  handler,
14
18
  **options,
15
- **(supports_updated_at? ? {updated_at: updated_at} : {})
19
+ **(Deface.before_rails_6? ? {updated_at: updated_at} : {})
16
20
  ) }
17
21
 
18
22
  let(:source) { "<p>test</p>" }
@@ -26,7 +30,6 @@ describe Deface::ActionViewExtensions do
26
30
  let(:format) { :html }
27
31
  let(:virtual_path) { "posts/index" }
28
32
 
29
- let(:supports_updated_at?) { Deface.before_rails_6? }
30
33
  let(:updated_at) { Time.now - 600 }
31
34
 
32
35
  describe "with no overrides defined" do
@@ -38,45 +41,11 @@ describe Deface::ActionViewExtensions do
38
41
  expect(template.source).to eq("<p>test</p>")
39
42
  end
40
43
 
41
- it "should not change updated_at", :supports_updated_at do
44
+ it "should not change updated_at", :before_rails_6 do
42
45
  expect(template.updated_at).to eq(updated_at)
43
46
  end
44
47
  end
45
48
 
46
- describe "with a single remove override defined" do
47
- let(:updated_at) { Time.now - 300 }
48
- let(:source) { "<p>test</p><%= raw(text) %>" }
49
-
50
- before do
51
- Deface::Override.new(virtual_path: "posts/index", name: "Posts#index", remove: "p", text: "<h1>Argh!</h1>")
52
- end
53
-
54
- it "should return modified source" do
55
- expect(template.source).to eq("<%= raw(text) %>")
56
- end
57
-
58
- it "should change updated_at", :supports_updated_at do
59
- expect(template.updated_at).to be > updated_at
60
- end
61
- end
62
-
63
- describe "#method_name" do
64
- before do
65
- ActionView::Template.define_method(
66
- :method_name_without_deface,
67
- ActionView::Template.instance_method(:method_name)
68
- )
69
- end
70
-
71
- it "returns hash of overrides plus original method_name " do
72
- deface_hash = Deface::Override.digest(virtual_path: 'posts/index')
73
- super_method = template.method(:method_name).super_method
74
- method_name = "_#{Digest::MD5.new.update("#{deface_hash}_#{super_method.call}").hexdigest}"
75
-
76
- expect(template.send(:method_name)).to eq(method_name)
77
- end
78
- end
79
-
80
49
  describe "non erb or haml template" do
81
50
  let(:source) { "xml.post => :blah" }
82
51
  let(:path) { "/some/path/to/file.erb" }
@@ -130,17 +99,25 @@ describe Deface::ActionViewExtensions do
130
99
  locals: local_assigns.keys
131
100
  } }
132
101
 
133
- let!(:deface) {
102
+ it 'renders the template modified by deface using :replace' do
134
103
  Deface::Override.new(
135
104
  virtual_path: virtual_path,
136
105
  name: "Posts#index",
137
106
  replace: "p",
138
107
  text: "<h1>Argh!</h1>"
139
108
  )
140
- }
141
109
 
142
- it 'renders the template modified by deface' do
143
110
  expect(template.render(view, local_assigns)).to eq(%{"<h1>Argh!</h1>some <br> text"})
144
111
  end
112
+
113
+ it 'renders the template modified by deface using :remove' do
114
+ Deface::Override.new(
115
+ virtual_path: virtual_path,
116
+ name: "Posts#index",
117
+ remove: "p",
118
+ )
119
+
120
+ expect(template.render(view, local_assigns)).to eq(%{"some <br> text"})
121
+ end
145
122
  end
146
123
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deface
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian D Quinn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-10 00:00:00.000000000 Z
11
+ date: 2021-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -223,6 +223,7 @@ files:
223
223
  - deface.gemspec
224
224
  - gemfiles/rails_5.2.gemfile
225
225
  - gemfiles/rails_6.0.gemfile
226
+ - gemfiles/rails_6.1.gemfile
226
227
  - gemfiles/rails_6_1.gemfile
227
228
  - lib/deface.rb
228
229
  - lib/deface/action_view_extensions.rb
@@ -340,7 +341,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
340
341
  - !ruby/object:Gem::Version
341
342
  version: '0'
342
343
  requirements: []
343
- rubygems_version: 3.2.3
344
+ rubygems_version: 3.1.4
344
345
  signing_key:
345
346
  specification_version: 4
346
347
  summary: Deface is a library that allows you to customize ERB, Haml and Slim views