wallaby-view 0.1.4 → 0.1.5

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: dbb2b53edfd05292aa8277741b572b703f952befd6048c4592f9f1eea60e6cb0
4
- data.tar.gz: 57fb94ffc44a1b6cd67a9e20939c3535b74d6c5a2283237f69548551d437eb44
3
+ metadata.gz: 7f9bf01cc8876f03b073afdaa8b322f00d781ff50fefd39f2eaac22f8feb535d
4
+ data.tar.gz: 04e7113c6650b3b756d886d859d08369f96beb39c2f2e5ffac4d8ba5c44c16c9
5
5
  SHA512:
6
- metadata.gz: 4fe5edfe3875669399eab479e38cab13b549926348f0d402796f66b5f19c914e95e3f15b088e91292575436cb838cdfd57cf82ed980e493859602ee16d2c3e68
7
- data.tar.gz: 5e3557307cc24027184dc95eca8eb6945815f7b5b99a0182618a4ccee64fcd083b401421d5fd08c5eb71a3c2ecea7594ee53329b318399ac21d7e1cb0f2d8e4c
6
+ metadata.gz: bd9d7a5a6985bfd6e317a44d8adf737a8e5639227e154fed90bdb2ac7230d6094efd0bebf5a60763826571b3cbc574365a1bf22d4dd4a6f326df279fd4f049e4
7
+ data.tar.gz: 7658edb6c169b0e577043856abe82583b3312a24c514cb4355584e15e2c2dd695dca2994731207a51f9b48c6e4575f0ece43c4de06edb133aae387db555fcbfa
data/README.md CHANGED
@@ -7,11 +7,14 @@
7
7
  [![Test Coverage](https://api.codeclimate.com/v1/badges/d3e924dd70cc12562eab/test_coverage)](https://codeclimate.com/github/wallaby-rails/wallaby-view/test_coverage)
8
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
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.
10
+ **Wallaby::View** extends Rails template/partial inheritance chain to be able to:
11
+
12
+ - organize partials in `#{controller_path}/#{action}` fashion on top of Rails' controller fashion by extending Rails' [\_prefixes](https://github.com/rails/rails/blob/master/actionview/lib/action_view/view_paths.rb#L90).
13
+ - configure the theme (a set of layout/templates/partials starting with the theme prefix).
11
14
 
12
15
  ## Install
13
16
 
14
- Add `Wallaby::View` to `Gemfile`.
17
+ Add **Wallaby::View** to `Gemfile`.
15
18
 
16
19
  ```ruby
17
20
  gem 'wallaby-view'
@@ -23,7 +26,7 @@ And re-bundle.
23
26
  bundle install
24
27
  ```
25
28
 
26
- Include `Wallaby::View` in the target controller (e.g. `ApplicationController`):
29
+ Include **Wallaby::View** in the controller (e.g. **ApplicationController**):
27
30
 
28
31
  ```ruby
29
32
  # app/controllers/application_controller
@@ -43,7 +46,7 @@ class ApplicationController < ActionController::Base
43
46
  end
44
47
 
45
48
  # app/controllers/admin/application_controller
46
- class Admin::ApplicationController < ApplicationController
49
+ class Admin::ApplicationController < ::ApplicationController
47
50
  self.theme_name = 'secure'
48
51
  end
49
52
 
@@ -54,7 +57,7 @@ class Admin::UsersController < Admin::ApplicationController
54
57
  end
55
58
  ```
56
59
 
57
- By using `Wallaby::View`, the lookup folder order of `admin/application#edit` action becomes:
60
+ By using **Wallaby::View**, a template/partial for the `admin/application#edit` action will be looked up in the following folder order from top to bottom:
58
61
 
59
62
  - app/views/admin/application/edit
60
63
  - app/views/admin/application
@@ -63,18 +66,22 @@ By using `Wallaby::View`, the lookup folder order of `admin/application#edit` ac
63
66
  - app/views/application/edit
64
67
  - app/views/application
65
68
 
66
- Then it is possible to create a relative partial in one of the above folder for `admin/application#edit` action, for instance:
69
+ Then it depends on how a relative partial should be shared, the partial can be created in one of the above folders.
70
+ For example, if a `form` partial is designed specifically for `admin/application#edit` action, then it can be created in `admin/application/edit` folder as below:
71
+
72
+ ```erb
73
+ <%# app/views/admin/application/edit/_form.html.erb %>
74
+ a form for `admin/application#edit`
75
+ ```
76
+
77
+ Then in the `admin/application#edit` template, rendering the relative `form` partial will result in using the above partial.
67
78
 
68
79
  ```erb
69
80
  <%# app/views/admin/application/edit.html.erb %>
70
81
  <% 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
82
  ```
76
83
 
77
- For `admin/users#edit` action, since `mapping_actions` option is set, `edit` will be mapped to `form`.
84
+ For `admin/users#edit` action, since `mapping_actions` option is set, `edit` will be mapped to `form`, and `form` will be added to the prefixes as well.
78
85
  Therefore, the lookup folder order of `admin/users#edit` becomes:
79
86
 
80
87
  - app/views/admin/users/edit
@@ -95,7 +102,7 @@ Therefore, the lookup folder order of `admin/users#edit` becomes:
95
102
 
96
103
  ## Advanced Usage
97
104
 
98
- It is possible to override the `_prefixes` method to make more changes to the prefixes:
105
+ It is possible to override the `_prefixes` method to make more changes to the prefixes before suffixing them with the action name:
99
106
 
100
107
  ```ruby
101
108
  class ApplicationController < ActionController::Base
@@ -109,14 +116,13 @@ class ApplicationController < ActionController::Base
109
116
  end
110
117
  ```
111
118
 
112
- Then the lookup folder order of `application#edit` becomes:
119
+ Then the lookup folder order of e.g. `application#edit` becomes:
113
120
 
114
121
  - app/views/application/edit
115
122
  - app/views/application
116
123
  - app/views/last_resort/edit
117
124
  - app/views/last_resort
118
125
 
119
-
120
126
  ## Documentation
121
127
 
122
128
  - [API Reference](https://www.rubydoc.info/gems/wallaby-view)
@@ -11,35 +11,18 @@ require 'wallaby/view/custom_prefixes'
11
11
 
12
12
  module Wallaby # :nodoc:
13
13
  # To extend Rails prefixes and improve lookup performance.
14
+ #
15
+ # For example, include {Wallaby::View} module in the controller:
16
+ #
17
+ # ```
18
+ # # app/controllers/application_controller
19
+ # class ApplicationController < ActionController::Base
20
+ # include Wallaby::View
21
+ # end
22
+ # ```
14
23
  module View
15
24
  extend ActiveSupport::Concern
16
25
 
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
26
  COMMA = ',' # :nodoc:
44
27
  EMPTY_STRING = '' # :nodoc:
45
28
  DOT_RB = '.rb' # :nodoc:
@@ -49,5 +32,14 @@ module Wallaby # :nodoc:
49
32
 
50
33
  include ActionViewable
51
34
  include Themeable
35
+
36
+ included do
37
+ # NOTE: Basically, it renames the methods so that it is possible
38
+ # to access the original methods after overriding them:
39
+ alias_method :original_lookup_context, :lookup_context
40
+ alias_method :original_prefixes, :_prefixes
41
+ alias_method :lookup_context, :override_lookup_context
42
+ alias_method :_prefixes, :override_prefixes
43
+ end
52
44
  end
53
45
  end
@@ -7,15 +7,13 @@ module Wallaby
7
7
  module ActionViewable
8
8
  extend ActiveSupport::Concern
9
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
10
+ module ClassMethods # :nodoc:
11
+ # @!attribute[w] prefix_options
17
12
  attr_writer :prefix_options
18
13
 
14
+ # @!attribute[r] prefix_options
15
+ # It stores the options for {#_prefixes}. It can inherit options from superclass.
16
+ # @return [Hash] prefix options
19
17
  def prefix_options
20
18
  @prefix_options ||= superclass.try(:prefix_options)
21
19
  end
@@ -48,7 +46,7 @@ module Wallaby
48
46
  # @param prefixes [Array<String>] the base prefixes
49
47
  # @param action_name [String] the action name to add to the prefixes list
50
48
  # @param themes [String] the theme name to add to the prefixes list
51
- # @param options [Hash] the options for {Wallaby::View::CustomPrefixes}
49
+ # @param options [Hash] the options that {Wallaby::View::CustomPrefixes} accepts
52
50
  # @return [Array<String>]
53
51
 
54
52
  # (see #_prefixes)
@@ -9,15 +9,18 @@ module Wallaby
9
9
  # @return [Array<String>]
10
10
  # @see Wallaby::View::ActionViewable#_prefixes
11
11
  attr_reader :prefixes
12
+
12
13
  # @!attribute [r] action_name
13
14
  # Action name to be added
14
15
  # @return [String]
15
16
  attr_reader :action_name
17
+
16
18
  # @!attribute [r] themes
17
19
  # Themes to be inserted
18
20
  # @return [Array<Hash>]
19
21
  # @see Wallaby::View::Themeable#.themes
20
22
  attr_reader :themes
23
+
21
24
  # @!attribute [r] options
22
25
  # Options for extending the given prefixes
23
26
  # @return [Hash]
@@ -28,6 +31,7 @@ module Wallaby
28
31
  # Wallaby::View::CustomPrefixes.execute(
29
32
  # prefixes: ['users', 'application'], action_name: 'index'
30
33
  # )
34
+ #
31
35
  # # => [
32
36
  # # 'users/index',
33
37
  # # 'users',
@@ -39,6 +43,7 @@ module Wallaby
39
43
  # prefixes: ['users', 'application'], action_name: 'index',
40
44
  # themes: [{ theme_name: 'secure', theme_path: 'users' }]
41
45
  # )
46
+ #
42
47
  # # => [
43
48
  # # 'users/index',
44
49
  # # 'users',
@@ -52,6 +57,7 @@ module Wallaby
52
57
  # prefixes: ['users', 'application'], action_name: 'edit',
53
58
  # options: { mapping_actions: { 'edit' => 'form' } }
54
59
  # )
60
+ #
55
61
  # # => [
56
62
  # # 'users/form',
57
63
  # # 'users',
@@ -94,8 +100,10 @@ module Wallaby
94
100
  end
95
101
  end
96
102
 
97
- private
103
+ protected
98
104
 
105
+ # @yield [array] To allow the array to be further modified
106
+ # @yieldparam [Array<String>] array
99
107
  # @return [Array<String>]
100
108
  def new_prefixes
101
109
  prefixes.dup.try do |array|
@@ -105,20 +113,18 @@ module Wallaby
105
113
  # in {Wallaby::View::ActionViewable#override_prefixes}
106
114
  new_array = yield array if block_given?
107
115
 
108
- # If the above block doesn't return an array,
109
- # it's assumed that `array` is changed
116
+ # If the above block doesn't return a new array, it returns the old `array`.
110
117
  new_array.is_a?(Array) ? new_array : array
111
118
  end
112
119
  end
113
120
 
114
- # Action name actions
115
- # @return [Array<String>]
121
+ # @return [Array<String>] Action names
116
122
  def actions
117
123
  @actions ||= [action_name, mapped_action_name].compact
118
124
  end
119
125
 
120
- # Insert theme name into the prefixes
121
- # @param [Array] array
126
+ # Insert theme names into the prefixes
127
+ # @param [Array<String>] array
122
128
  def insert_themes_into(array)
123
129
  themes.each do |theme|
124
130
  index = array.index theme[:theme_path]
@@ -6,8 +6,7 @@ module Wallaby
6
6
  module Themeable
7
7
  extend ActiveSupport::Concern
8
8
 
9
- class << self
10
- # @!attribute theme_name
9
+ module ClassMethods # :nodoc:
11
10
  # The theme name is used to specify the layout and prefixes
12
11
  # so that a set of theme implementation for the frontend (html/css/javascript)
13
12
  # can be applied
@@ -27,6 +26,7 @@ module Wallaby
27
26
  #
28
27
  # def index
29
28
  # _prefixes
29
+ #
30
30
  # # =>
31
31
  # # [
32
32
  # # 'admin/application/index',
@@ -37,8 +37,22 @@ module Wallaby
37
37
  # # 'application'
38
38
  # # ]
39
39
  # end
40
- # end
40
+ # end
41
+ # @param theme_name [String]
42
+ # @param options [Hash] same options as Rails'
43
+ # {https://api.rubyonrails.org/classes/ActionView/Layouts/ClassMethods.html#method-i-layout
44
+ # layout} method
45
+ def theme_name=(theme_name, **options, &block)
46
+ layout theme_name, options, &block
47
+ @theme_path = theme_name && controller_path || nil
48
+ @theme_name = theme_name || nil
49
+ end
50
+
51
+ # @!attribute [r] theme_name
41
52
  # @return [String, nil] theme name
53
+ def theme_name
54
+ defined?(@theme_name) && @theme_name || superclass.try(:theme_name)
55
+ end
42
56
 
43
57
  # @!attribute [r] theme
44
58
  # @example Once theme is set, the metadata will be set as well:
@@ -46,6 +60,7 @@ module Wallaby
46
60
  # self.theme_name = 'secure'
47
61
  #
48
62
  # self.theme
63
+ #
49
64
  # # =>
50
65
  # # {
51
66
  # # theme_name: 'secure',
@@ -53,25 +68,6 @@ module Wallaby
53
68
  # # }
54
69
  # end
55
70
  # @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
- layout theme_name, options, &block
65
- @theme_path = theme_name && controller_path || nil
66
- @theme_name = theme_name || nil
67
- end
68
-
69
- # (see .theme_name)
70
- def theme_name
71
- defined?(@theme_name) && @theme_name || superclass.try(:theme_name)
72
- end
73
-
74
- # (see .theme)
75
71
  def theme
76
72
  defined?(@theme_name) && @theme_name && {
77
73
  theme_name: @theme_name,
@@ -79,7 +75,8 @@ module Wallaby
79
75
  } || superclass.try(:theme)
80
76
  end
81
77
 
82
- # (see .themes)
78
+ # @!attribute [r] themes
79
+ # @return [Array<Hash>] a list of {#theme} metadata
83
80
  def themes
84
81
  list = superclass.try(:themes) || []
85
82
  list.prepend theme if defined?(@theme_name) && @theme_name
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Wallaby
4
4
  module View
5
- VERSION = '0.1.4' # :nodoc:
5
+ VERSION = '0.1.5' # :nodoc:
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wallaby-view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tian Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-28 00:00:00.000000000 Z
11
+ date: 2020-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  requirements: []
107
- rubygems_version: 3.0.3
107
+ rubygems_version: 3.1.2
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Wallaby View to extend Rails layout/template/partial inheritance chain.