wallaby-view 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +102 -0
- data/lib/action_view/partial_renderer.rb +42 -0
- data/lib/wallaby/view/action_viewable.rb +71 -0
- data/lib/wallaby/view/custom_lookup_context.rb +59 -0
- data/lib/wallaby/view/custom_prefixes.rb +134 -0
- data/lib/wallaby/view/themeable.rb +93 -0
- data/lib/wallaby/view/version.rb +7 -0
- data/lib/wallaby/view.rb +66 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8cd23eec0f8295c872243d260290b34f21d43ec2145fc0b365a20eb3a0ff2580
|
4
|
+
data.tar.gz: dfbbefc56fd77282ecd22f516eb00549e91952cf1f0c91b92d7e41aeb518f778
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: db9196eebc15e9e47b34689b8cadc2b77105f6e1f6fbc1fa0779fd16fce3495c0b605c444f5fc67979f0f1280a2812ae099defb311fe13337207b894433d9aee
|
7
|
+
data.tar.gz: 4917269b5e80693821fb5a08d3cbc7cd1c8789c4bf5e2a87e41389994ba5bc449f6e4ae420f8db5e2ef7c4c164d4d2b91401556738e36f15d1457a2caaee77fc
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Wallaby::View
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/wallaby-view.svg)](https://badge.fury.io/rb/wallaby-view)
|
4
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
5
|
+
[![Travis CI](https://travis-ci.com/wallaby-rails/wallaby-view.svg?branch=master)](https://travis-ci.com/wallaby-rails/wallaby-view)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/d3e924dd70cc12562eab/maintainability)](https://codeclimate.com/github/wallaby-rails/wallaby-view/maintainability)
|
7
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/d3e924dd70cc12562eab/test_coverage)](https://codeclimate.com/github/wallaby-rails/wallaby-view/test_coverage)
|
8
|
+
[![Inch CI](https://inch-ci.org/github/wallaby-rails/wallaby-view.svg?branch=master)](https://inch-ci.org/github/wallaby-rails/wallaby-view)
|
9
|
+
|
10
|
+
`Wallaby::View` is a Ruby gem that extends Rails layout/template/partial inheritance chain to allow searching layout/template/partial using theme name and action name.
|
11
|
+
|
12
|
+
## Install
|
13
|
+
|
14
|
+
Add `Wallaby::View` to `Gemfile`.
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'wallaby-view'
|
18
|
+
```
|
19
|
+
|
20
|
+
And re-bundle.
|
21
|
+
|
22
|
+
```shell
|
23
|
+
bundle install
|
24
|
+
```
|
25
|
+
|
26
|
+
Include `Wallaby::View` in the target controller (e.g. `ApplicationController`):
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
# app/controllers/application_controller
|
30
|
+
class ApplicationController < ActionController::Base
|
31
|
+
include Wallaby::View
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## What It Does
|
36
|
+
|
37
|
+
For example, given the following controllers:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
# app/controllers/application_controller
|
41
|
+
class ApplicationController < ActionController::Base
|
42
|
+
include Wallaby::View
|
43
|
+
end
|
44
|
+
|
45
|
+
# app/controllers/admin/application_controller
|
46
|
+
class Admin::ApplicationController < ApplicationController
|
47
|
+
self.theme_name = 'secure'
|
48
|
+
end
|
49
|
+
|
50
|
+
# app/controllers/admin/users_controller
|
51
|
+
class Admin::UsersController < Admin::ApplicationController
|
52
|
+
self.theme_name = 'account'
|
53
|
+
self.options = { mapping_actions: { edit: 'form' } }
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
By using `Wallaby::View`, the lookup folder order of `admin/application#edit` action becomes:
|
58
|
+
|
59
|
+
- app/views/admin/application/edit
|
60
|
+
- app/views/admin/application
|
61
|
+
- app/views/secure/edit
|
62
|
+
- app/views/secure
|
63
|
+
- app/views/application/edit
|
64
|
+
- app/views/application
|
65
|
+
|
66
|
+
Then it is possible to create a relative partial in one of the above folder for `admin/application#edit` action, for instance:
|
67
|
+
|
68
|
+
```erb
|
69
|
+
<%# app/views/admin/application/edit.html.erb %>
|
70
|
+
<% render 'form' %>
|
71
|
+
|
72
|
+
<%# app/views/secure/edit/_form.html.erb %>
|
73
|
+
This form partial is under `secure` theme and `edit` action,
|
74
|
+
but still can be rendered by `admin/application#edit` action
|
75
|
+
```
|
76
|
+
|
77
|
+
For `admin/users#edit` action, since `mapping_actions` option is set, `edit` will be mapped to `form`.
|
78
|
+
Therefore, the lookup folder order of `admin/users#edit` becomes:
|
79
|
+
|
80
|
+
- app/views/admin/users/form
|
81
|
+
- app/views/admin/users
|
82
|
+
- app/views/secure/form
|
83
|
+
- app/views/secure
|
84
|
+
- app/views/admin/application/form
|
85
|
+
- app/views/admin/application
|
86
|
+
- app/views/secure/form
|
87
|
+
- app/views/secure
|
88
|
+
- app/views/application/form
|
89
|
+
- app/views/application
|
90
|
+
|
91
|
+
## Documentation
|
92
|
+
|
93
|
+
- [API Reference](https://www.rubydoc.info/gems/wallaby-view)
|
94
|
+
- [Change Logs](CHANGELOG.md)
|
95
|
+
|
96
|
+
## Want to contribute?
|
97
|
+
|
98
|
+
Raise an issue, discuss and resolve!
|
99
|
+
|
100
|
+
## License
|
101
|
+
|
102
|
+
This project uses [MIT License](LICENSE).
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActionView # :nodoc:
|
4
|
+
# Re-open ActionView::PartialRenderer to disable logging for partials
|
5
|
+
# that have been rendered for more than once.
|
6
|
+
#
|
7
|
+
# Every log requires the IO. Reducing the logs will dramatically improve the performance.
|
8
|
+
# @see #instrument
|
9
|
+
class PartialRenderer
|
10
|
+
protected
|
11
|
+
|
12
|
+
# @!method original_instrument(name, **options, &block)
|
13
|
+
# Original method of {#instrument}
|
14
|
+
# @param name [String]
|
15
|
+
# @param options [Hash]
|
16
|
+
# @yield [payload]
|
17
|
+
# @yieldparam payload [Hash] payload object for instrument
|
18
|
+
# @return [ActionView::OutputBuffer]
|
19
|
+
alias original_instrument instrument
|
20
|
+
|
21
|
+
# Logs for partial rendering. Only one log will be printed
|
22
|
+
# even if a partial has been rendered for more than once.
|
23
|
+
#
|
24
|
+
# To disable this feature and see all logs,
|
25
|
+
# set `ENV['ALL_PARTIAL_LOGS']` to `true`
|
26
|
+
# @param name [String]
|
27
|
+
# @param options [Hash]
|
28
|
+
# @yield [payload]
|
29
|
+
# @yieldparam payload [Hash] payload object for instrument
|
30
|
+
# @return [ActionView::OutputBuffer]
|
31
|
+
def instrument(name, **options, &block)
|
32
|
+
identifier = options[:identifier] || @template.try(:identifier) || @path
|
33
|
+
instrumented = RequestStore.store[:instrumented] ||= {}
|
34
|
+
|
35
|
+
return yield({}) if !ENV['ALL_PARTIAL_LOGS'] && instrumented[identifier]
|
36
|
+
|
37
|
+
original_instrument(name, **options, &block).tap do
|
38
|
+
instrumented[identifier] ||= true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Wallaby
|
4
|
+
module View
|
5
|
+
# This module overrides Rails core methods {#lookup_context} and {#_prefixes}
|
6
|
+
# to provide better performance and more lookup prefixes.
|
7
|
+
module ActionViewable
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# @!attribute prefix_options
|
12
|
+
# It stores the options for {#_prefixes}. It can inherit options from superclass.
|
13
|
+
# @return [Hash] prefix options
|
14
|
+
end
|
15
|
+
|
16
|
+
class_methods do
|
17
|
+
attr_writer :prefix_options
|
18
|
+
|
19
|
+
def prefix_options
|
20
|
+
@prefix_options ||= View.try_to superclass, :prefix_options
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# @!method original_lookup_context
|
25
|
+
# Original method of {#lookup_context}
|
26
|
+
# @return [ActionView::LookupContext]
|
27
|
+
|
28
|
+
# @!method original_prefixes
|
29
|
+
# Original method of {#_prefixes}
|
30
|
+
# @return [Array<String>]
|
31
|
+
|
32
|
+
# @!method lookup_context
|
33
|
+
# Override
|
34
|
+
# {https://github.com/rails/rails/blob/master/actionview/lib/action_view/view_paths.rb#L97 lookup_context}
|
35
|
+
# to provide caching for template/partial lookup.
|
36
|
+
# @return {Wallaby::View::CustomLookupContext}
|
37
|
+
|
38
|
+
# (see #lookup_context)
|
39
|
+
def override_lookup_context
|
40
|
+
@_lookup_context ||= # rubocop:disable Naming/MemoizedInstanceVariableName
|
41
|
+
CustomLookupContext.convert(original_lookup_context, prefixes: _prefixes)
|
42
|
+
end
|
43
|
+
|
44
|
+
# @!method _prefixes(prefixes: nil, controller_path: nil, action_name: nil, themes: nil, options: nil, &block)
|
45
|
+
# Override {https://github.com/rails/rails/blob/master/actionview/lib/action_view/view_paths.rb#L90 _prefixes}
|
46
|
+
# to allow other (e.g. {Wallaby::View::CustomPrefixes#action_name},
|
47
|
+
# {Wallaby::View::CustomPrefixes#themes}) to be added to the prefixes list.
|
48
|
+
# @param prefixes [Array<String>] the base prefixes
|
49
|
+
# @param action_name [String] the action name to add to the prefixes list
|
50
|
+
# @param themes [String] the theme name to add to the prefixes list
|
51
|
+
# @param options [Hash] the options for {Wallaby::View::CustomPrefixes}
|
52
|
+
# @return [Array<String>]
|
53
|
+
|
54
|
+
# (see #_prefixes)
|
55
|
+
def override_prefixes(
|
56
|
+
prefixes: nil,
|
57
|
+
action_name: nil,
|
58
|
+
themes: nil,
|
59
|
+
options: nil, &block
|
60
|
+
)
|
61
|
+
@_prefixes ||= # rubocop:disable Naming/MemoizedInstanceVariableName
|
62
|
+
CustomPrefixes.execute(
|
63
|
+
prefixes: prefixes || original_prefixes,
|
64
|
+
action_name: action_name || params[:action],
|
65
|
+
themes: themes || self.class.themes,
|
66
|
+
options: options || self.class.prefix_options, &block
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Wallaby
|
4
|
+
module View
|
5
|
+
# Custom lookup context to cache lookup results.
|
6
|
+
class CustomLookupContext < ::ActionView::LookupContext
|
7
|
+
# Convert an ActionView::LookupContext instance into {Wallaby::View::CustomLookupContext}
|
8
|
+
# @param lookup_context [ActionView::LookupContext]
|
9
|
+
# @param details [Hash]
|
10
|
+
# @param prefixes [Array<String>]
|
11
|
+
# @return [Wallaby::View::CustomLookupContext]
|
12
|
+
def self.convert(lookup_context, details: nil, prefixes: nil)
|
13
|
+
return lookup_context if lookup_context.is_a? self
|
14
|
+
|
15
|
+
new(
|
16
|
+
lookup_context.view_paths,
|
17
|
+
details || lookup_context.instance_variable_get('@details'),
|
18
|
+
prefixes || lookup_context.prefixes
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
# @!method original_find(path, prefixes, partial, *args)
|
23
|
+
# Original find method.
|
24
|
+
# @param path [String, Symbol]
|
25
|
+
# @param prefixes [Array<String>]
|
26
|
+
# @param partial [true, false]
|
27
|
+
# @param args [Array] the rest of the arguments
|
28
|
+
# @return [ActionView::Template]
|
29
|
+
alias original_find find
|
30
|
+
|
31
|
+
# This is to resolve the performance bottleneck for template/partial lookup.
|
32
|
+
#
|
33
|
+
# {#cached_lookup} is used to cache the lookup result throughout a request.
|
34
|
+
# @param path [String, Symbol]
|
35
|
+
# @param prefixes [Array<String>]
|
36
|
+
# @param partial [true, false]
|
37
|
+
# @param args [Array] the rest of the arguments
|
38
|
+
# @return [ActionView::Template]
|
39
|
+
def find(path, prefixes, partial, *args)
|
40
|
+
key = [path, prefixes, partial].join(EQUAL)
|
41
|
+
cached_lookup[key] ||= original_find(path, prefixes, partial, *args)
|
42
|
+
end
|
43
|
+
|
44
|
+
# @!method find_template(path, prefixes, partial, *args)
|
45
|
+
# This is an alias method of {#find}
|
46
|
+
# (see #find)
|
47
|
+
alias find_template find
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
# @!attribute [r] cached_lookup
|
52
|
+
# This is a lookup cache for method {#find}
|
53
|
+
# @return [Hash] prefix options
|
54
|
+
def cached_lookup
|
55
|
+
@cached_lookup ||= {}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Wallaby
|
4
|
+
module View
|
5
|
+
# Custom prefix builder to add more lookup prefix paths to given {#prefixes}.
|
6
|
+
class CustomPrefixes
|
7
|
+
# @!attribute [r] prefixes
|
8
|
+
# Base prefixes to extend
|
9
|
+
# @return [Array<String>]
|
10
|
+
# @see Wallaby::View::ActionViewable#_prefixes
|
11
|
+
attr_reader :prefixes
|
12
|
+
# @!attribute [r] action_name
|
13
|
+
# Action name to be added
|
14
|
+
# @return [String]
|
15
|
+
attr_reader :action_name
|
16
|
+
# @!attribute [r] themes
|
17
|
+
# Themes to be inserted
|
18
|
+
# @return [Array<Hash>]
|
19
|
+
# @see Wallaby::View::Themeable#.themes
|
20
|
+
attr_reader :themes
|
21
|
+
# @!attribute [r] options
|
22
|
+
# Options for extending the given prefixes
|
23
|
+
# @return [Hash]
|
24
|
+
attr_reader :options
|
25
|
+
|
26
|
+
# Extend given prefixes with action name and theme name
|
27
|
+
# @example To extend given prefixes:
|
28
|
+
# Wallaby::View::CustomPrefixes.execute(
|
29
|
+
# prefixes: ['users', 'application'], action_name: 'index'
|
30
|
+
# )
|
31
|
+
# # => [
|
32
|
+
# # 'users/index',
|
33
|
+
# # 'users',
|
34
|
+
# # 'application/index',
|
35
|
+
# # 'application'
|
36
|
+
# # ]
|
37
|
+
# @example To extend given prefixes with themes:
|
38
|
+
# Wallaby::View::CustomPrefixes.execute(
|
39
|
+
# prefixes: ['users', 'application'], action_name: 'index',
|
40
|
+
# themes: [{ theme_name: 'secure', theme_path: 'users' }]
|
41
|
+
# )
|
42
|
+
# # => [
|
43
|
+
# # 'users/index',
|
44
|
+
# # 'users',
|
45
|
+
# # 'secure/index',
|
46
|
+
# # 'secure',
|
47
|
+
# # 'application/index',
|
48
|
+
# # 'application'
|
49
|
+
# # ]
|
50
|
+
# @example To extend given prefixes with mapped action:
|
51
|
+
# Wallaby::View::CustomPrefixes.execute(
|
52
|
+
# prefixes: ['users', 'application'], action_name: 'edit',
|
53
|
+
# options: { mapping_actions: { 'edit' => 'form' } }
|
54
|
+
# )
|
55
|
+
# # => [
|
56
|
+
# # 'users/form',
|
57
|
+
# # 'users',
|
58
|
+
# # 'application/form',
|
59
|
+
# # 'application'
|
60
|
+
# # ]
|
61
|
+
# @param prefixes [Array<String>]
|
62
|
+
# @param action_name [String]
|
63
|
+
# @param themes [String, nil]
|
64
|
+
# @param options [Hash, nil]
|
65
|
+
# @return [Array<String>]
|
66
|
+
def self.execute(
|
67
|
+
prefixes:, action_name:, themes: nil, options: nil, &block
|
68
|
+
)
|
69
|
+
new(
|
70
|
+
prefixes: prefixes, action_name: action_name,
|
71
|
+
themes: themes, options: options
|
72
|
+
).execute(&block)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Create the instance
|
76
|
+
# @param prefixes [Array<String>]
|
77
|
+
# @param action_name [String]
|
78
|
+
# @param themes [String]
|
79
|
+
# @param options [Hash]
|
80
|
+
def initialize(prefixes:, action_name:, themes:, options:)
|
81
|
+
@prefixes = prefixes
|
82
|
+
@action_name = action_name
|
83
|
+
@themes = themes
|
84
|
+
@options = (options || {}).with_indifferent_access
|
85
|
+
end
|
86
|
+
|
87
|
+
# Extend given prefixes with action name and theme name
|
88
|
+
# @return [Array<String>]
|
89
|
+
def execute(&block)
|
90
|
+
new_prefixes(&block).each_with_object([]) do |prefix, array|
|
91
|
+
# Extend the prefix with action name suffix
|
92
|
+
array << "#{prefix}/#{suffix}" << prefix
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
# @return [Array<String>]
|
99
|
+
def new_prefixes
|
100
|
+
prefixes.dup.try do |array|
|
101
|
+
insert_themes_into array
|
102
|
+
|
103
|
+
# Be able to change the array in overriding methods
|
104
|
+
# in {Wallaby::View::ActionViewable#override_prefixes}
|
105
|
+
new_array = yield array if block_given?
|
106
|
+
|
107
|
+
# If the above block doesn't return an array,
|
108
|
+
# it's assumed that `array` is changed
|
109
|
+
new_array.is_a?(Array) ? new_array : array
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Action name suffix
|
114
|
+
# @return [Hash]
|
115
|
+
def suffix
|
116
|
+
@suffix ||= mapped_action_name || action_name
|
117
|
+
end
|
118
|
+
|
119
|
+
# Insert theme name into the prefixes
|
120
|
+
def insert_themes_into(array)
|
121
|
+
themes.each do |theme|
|
122
|
+
index = array.index theme[:theme_path]
|
123
|
+
array.insert(index + 1, theme[:theme_name]) if index
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Map the {#action_name} using `options[:mapping_actions]`
|
128
|
+
# @return [String, nil] mapped action name
|
129
|
+
def mapped_action_name
|
130
|
+
options[:mapping_actions].try(:[], action_name)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Wallaby
|
4
|
+
module View
|
5
|
+
# Theme module to allow layout and prefixes to be specified.
|
6
|
+
module Themeable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
class << self
|
10
|
+
# @!attribute theme_name
|
11
|
+
# The theme name is used to specify the layout and prefixes
|
12
|
+
# so that a set of theme implementation for the frontend (html/css/javascript)
|
13
|
+
# can be applied
|
14
|
+
#
|
15
|
+
# When theme name is set to e.g. `custom_theme`,
|
16
|
+
# the following changes will be made:
|
17
|
+
#
|
18
|
+
# - layout will be set to the same name `custom_theme`
|
19
|
+
# - theme name will be added to the lookup prefixes
|
20
|
+
# right after the controller path of where it's defined.
|
21
|
+
#
|
22
|
+
# Once theme name is set, all its subclass controllers
|
23
|
+
# will inherit the same theme name
|
24
|
+
# @example To set an theme name:
|
25
|
+
# class Admin::ApplicationController < ApplicationController
|
26
|
+
# self.theme_name = 'secure'
|
27
|
+
#
|
28
|
+
# def index
|
29
|
+
# _prefixes
|
30
|
+
# # =>
|
31
|
+
# # [
|
32
|
+
# # 'admin/application/index',
|
33
|
+
# # 'admin/application',
|
34
|
+
# # 'secure/index',
|
35
|
+
# # 'secure',
|
36
|
+
# # 'application/index',
|
37
|
+
# # 'application'
|
38
|
+
# # ]
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
# @return [String, nil] theme name
|
42
|
+
|
43
|
+
# @!attribute [r] theme
|
44
|
+
# @example Once theme is set, the metadata will be set as well:
|
45
|
+
# class Admin::ApplicationController < ApplicationController
|
46
|
+
# self.theme_name = 'secure'
|
47
|
+
#
|
48
|
+
# self.theme
|
49
|
+
# # =>
|
50
|
+
# # {
|
51
|
+
# # theme_name: 'secure',
|
52
|
+
# # theme_path: 'admin/application'
|
53
|
+
# # }
|
54
|
+
# end
|
55
|
+
# @return [Hash] theme metadata
|
56
|
+
|
57
|
+
# @!attribute [r] themes
|
58
|
+
# @return [Array<Hash>] a list of {.theme} metadata
|
59
|
+
end
|
60
|
+
|
61
|
+
class_methods do
|
62
|
+
# (see .theme_name)
|
63
|
+
def theme_name=(theme_name, **options, &block)
|
64
|
+
return unless theme_name
|
65
|
+
|
66
|
+
layout theme_name, options, &block
|
67
|
+
@theme_path = theme_name && controller_path
|
68
|
+
@theme_name = theme_name
|
69
|
+
end
|
70
|
+
|
71
|
+
# (see .theme_name)
|
72
|
+
def theme_name
|
73
|
+
defined?(@theme_name) && @theme_name || View.try_to(superclass, :theme_name)
|
74
|
+
end
|
75
|
+
|
76
|
+
# (see .theme)
|
77
|
+
def theme
|
78
|
+
defined?(@theme_name) && @theme_name && {
|
79
|
+
theme_name: @theme_name,
|
80
|
+
theme_path: @theme_path
|
81
|
+
} || View.try_to(superclass, :theme)
|
82
|
+
end
|
83
|
+
|
84
|
+
# (see .themes)
|
85
|
+
def themes
|
86
|
+
list = View.try_to(superclass, :themes) || []
|
87
|
+
list.prepend theme if defined?(@theme_name) && @theme_name
|
88
|
+
list.compact
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/wallaby/view.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'request_store'
|
4
|
+
require 'action_view/partial_renderer'
|
5
|
+
|
6
|
+
require 'wallaby/view/version'
|
7
|
+
require 'wallaby/view/action_viewable'
|
8
|
+
require 'wallaby/view/themeable'
|
9
|
+
require 'wallaby/view/custom_lookup_context'
|
10
|
+
require 'wallaby/view/custom_prefixes'
|
11
|
+
|
12
|
+
module Wallaby # :nodoc:
|
13
|
+
# To extend Rails prefixes and improve lookup performance.
|
14
|
+
module View
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
|
17
|
+
# This is the method executed when this module is included by other modules.
|
18
|
+
#
|
19
|
+
# Basically, it renames the methods so that it is possible
|
20
|
+
# to access the original methods after overriding them:
|
21
|
+
#
|
22
|
+
# 1. Rename the following original methods:
|
23
|
+
#
|
24
|
+
# - rename {https://github.com/rails/rails/blob/master/actionview/lib/action_view/view_paths.rb#L97
|
25
|
+
# lookup_context} to {Wallaby::View::ActionViewable#original_lookup_context original_lookup_context}
|
26
|
+
# - rename {https://github.com/rails/rails/blob/master/actionview/lib/action_view/view_paths.rb#L90 _prefixes}
|
27
|
+
# to {Wallaby::View::ActionViewable#original_prefixes original_prefixes}
|
28
|
+
#
|
29
|
+
# 2. Override the original methods:
|
30
|
+
#
|
31
|
+
# - rename {Wallaby::View::ActionViewable#override_lookup_context override_lookup_context}
|
32
|
+
# to {Wallaby::View::ActionViewable#lookup_context lookup_context}
|
33
|
+
# - rename {Wallaby::View::ActionViewable#override_prefixes override_prefixes}
|
34
|
+
# to {Wallaby::View::ActionViewable#\_prefixes \_prefixes}
|
35
|
+
# @param mod [Module]
|
36
|
+
def self.included(mod)
|
37
|
+
mod.send :alias_method, :original_lookup_context, :lookup_context
|
38
|
+
mod.send :alias_method, :original_prefixes, :_prefixes
|
39
|
+
mod.send :alias_method, :lookup_context, :override_lookup_context
|
40
|
+
mod.send :alias_method, :_prefixes, :override_prefixes
|
41
|
+
end
|
42
|
+
|
43
|
+
# Util method to check if the given subject responds to the given method.
|
44
|
+
#
|
45
|
+
# If so, it will send the message and return the result. Otherwise, nil.
|
46
|
+
# @param subject [Object]
|
47
|
+
# @param method_id [Symbol, String]
|
48
|
+
# @param args [Array]
|
49
|
+
# @return [Object, nil]
|
50
|
+
def self.try_to(subject, method_id, *args, &block)
|
51
|
+
return unless subject.respond_to?(method_id)
|
52
|
+
|
53
|
+
subject.public_send(method_id, *args, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
COMMA = ',' # :nodoc:
|
57
|
+
EMPTY_STRING = '' # :nodoc:
|
58
|
+
DOT_RB = '.rb' # :nodoc:
|
59
|
+
SLASH = '/' # :nodoc:
|
60
|
+
EQUAL = '=' # :nodoc:
|
61
|
+
UNDERSCORE = '_' # :nodoc:
|
62
|
+
|
63
|
+
include ActionViewable
|
64
|
+
include Themeable
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wallaby-view
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tian Chen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: railties
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.2.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.2.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: request_store
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: wallaby-cop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Wallaby View to extend Rails layout/template/partial inheritance chain.
|
84
|
+
email:
|
85
|
+
- me@tian.im
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- LICENSE
|
91
|
+
- README.md
|
92
|
+
- lib/action_view/partial_renderer.rb
|
93
|
+
- lib/wallaby/view.rb
|
94
|
+
- lib/wallaby/view/action_viewable.rb
|
95
|
+
- lib/wallaby/view/custom_lookup_context.rb
|
96
|
+
- lib/wallaby/view/custom_prefixes.rb
|
97
|
+
- lib/wallaby/view/themeable.rb
|
98
|
+
- lib/wallaby/view/version.rb
|
99
|
+
homepage: https://github.com/wallaby-rails/wallaby-view
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata:
|
103
|
+
homepage_uri: https://github.com/wallaby-rails/wallaby-view
|
104
|
+
source_code_uri: https://github.com/wallaby-rails/wallaby-view
|
105
|
+
changelog_uri: https://github.com/wallaby-rails/wallaby-view/blob/master/CHANGELOG.md
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubygems_version: 3.0.3
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Wallaby View to extend Rails layout/template/partial inheritance chain.
|
125
|
+
test_files: []
|