rails_namespace_path_helper 0.1.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 +7 -0
- data/.rspec +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +8 -0
- data/config.ru +9 -0
- data/lib/rails_namespace_path_helper/railtie.rb +12 -0
- data/lib/rails_namespace_path_helper/version.rb +5 -0
- data/lib/rails_namespace_path_helper.rb +68 -0
- data/sig/rails_namespace_path_helper.rbs +4 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2c1f49a54fc2e5c045929ceab9a57c6ba050ee441963a46578e85cb2a9f15e0
|
4
|
+
data.tar.gz: 270b0d5b6dfa0a81f0e41f8ec3435faf040eac5a258e46e2c57e5f64a65e7dc3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3aa9b78e0c183be8777050a82a49c26a0654d63bf6ed317f31b5351c5ba683dab6267353b16b6cb24dcef0d6b636b7e666fcf7effffde11b5e7d4eac97669b3b
|
7
|
+
data.tar.gz: 14a84e8ca20d53061d804c22b744d8a9cc79003bafb37922ae42bb28d3558fcfa9c71013d8c1070197fb507a773b95d1d5a5ba4b9254c075edaa044f2aa91177
|
data/.rspec
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 BenenB
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
## RailsNamespacePathHelper
|
2
|
+
## An extension for namespaced controllers
|
3
|
+
|
4
|
+
Namespacing is a common pattern in rails, however...
|
5
|
+
|
6
|
+
Rails does not account for namespacing when looking for helpers on controllers
|
7
|
+
- Eg. for `Admin::TasksController`
|
8
|
+
- helpers are declared `admin_tasks_url`
|
9
|
+
- however rails bolierplate expects `tasks_url` to be available
|
10
|
+
|
11
|
+
This gem dynamically creates aliases for these methods so that nothing needs to be reconfigured
|
12
|
+
|
13
|
+
### Usage
|
14
|
+
|
15
|
+
To use simply call:
|
16
|
+
- `has_namespaced_paths` at the top of each controller with a namespace
|
17
|
+
- or `has_namespaced_path_plural` / `has_namespaced_path_singular` for only plural/singular paths
|
18
|
+
|
19
|
+
|
20
|
+
Like this:
|
21
|
+
```
|
22
|
+
# in controllers/admin/tasks_controller.rb
|
23
|
+
class Admin::TasksController < ApplicationController
|
24
|
+
has_namespaced_paths
|
25
|
+
|
26
|
+
...
|
27
|
+
|
28
|
+
end
|
29
|
+
```
|
30
|
+
### Installation
|
31
|
+
|
32
|
+
Install the gem and add to the application's Gemfile by executing:
|
33
|
+
|
34
|
+
`$ bundle add rails_namespace_path_helper`
|
35
|
+
|
36
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
37
|
+
|
38
|
+
`$ gem install rails_namespace_path_helper`
|
39
|
+
|
40
|
+
|
41
|
+
### Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/benenb/rails_namespace_path_helper.
|
44
|
+
|
45
|
+
### License
|
46
|
+
|
47
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/config.ru
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rails/railtie"
|
2
|
+
module RailsNamespacePathHelper
|
3
|
+
# with thanks to: 'Gary S. Weaver'
|
4
|
+
# https://stackoverflow.com/questions/11348332/how-can-i-extend-applicationcontroller-in-a-gem
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "rails_namespace_path_helper.action_controller" do
|
7
|
+
ActiveSupport.on_load(:action_controller) do
|
8
|
+
extend RailsNamespacePathHelper
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "rails_namespace_path_helper/version"
|
3
|
+
require_relative "rails_namespace_path_helper/railtie"
|
4
|
+
|
5
|
+
module RailsNamespacePathHelper
|
6
|
+
|
7
|
+
# This helper dynamically creates aliases for helpers generated by rails
|
8
|
+
# eg. in Admin:TasksController, tasks_url becomes an alias for admin_tasks_url
|
9
|
+
|
10
|
+
# These helpers are declared locally on the controller to avoid conflicts
|
11
|
+
|
12
|
+
def has_namespaced_paths
|
13
|
+
|
14
|
+
has_namespaced_path_singular
|
15
|
+
has_namespaced_path_plural
|
16
|
+
end
|
17
|
+
|
18
|
+
def has_namespaced_path_singular
|
19
|
+
# separate controller name from namespace
|
20
|
+
model,namespaces = *parse_class_name
|
21
|
+
|
22
|
+
['url','path'].each do |tag|
|
23
|
+
helper = "#{model.singularize}_#{tag}"
|
24
|
+
|
25
|
+
# short circuit if conflict
|
26
|
+
next unless ensure_methods_available(namespaces, helper)
|
27
|
+
|
28
|
+
# create aliases...
|
29
|
+
define_namespace_helpers(namespaces,helper)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_namespaced_path_plural
|
34
|
+
# separate controller name from namespace
|
35
|
+
model,namespaces = *parse_class_name
|
36
|
+
|
37
|
+
['url','path'].each do |tag|
|
38
|
+
helper = "#{model.pluralize}_#{tag}"
|
39
|
+
|
40
|
+
# short circuit if conflict
|
41
|
+
next unless ensure_methods_available(namespaces, helper)
|
42
|
+
|
43
|
+
# create aliases...
|
44
|
+
define_namespace_helpers(namespaces,helper)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def parse_class_name
|
50
|
+
model = self.name.split("::")[-1].gsub(/(.*)Controller$/,'\1').downcase
|
51
|
+
namespaces = self.name.split("::")[0...-1].join("_").downcase
|
52
|
+
return [model,namespaces]
|
53
|
+
end
|
54
|
+
|
55
|
+
def ensure_methods_available namespaces,helper
|
56
|
+
# false if helper is not defined
|
57
|
+
self.new.respond_to?("#{namespaces}_#{helper}".to_sym) &&
|
58
|
+
# false if alias is already defined AND can resolve properly
|
59
|
+
!(self.new.respond_to?("#{helper}".to_sym) && (self.new.send(helper) rescue false))
|
60
|
+
end
|
61
|
+
|
62
|
+
def define_namespace_helpers namespaces,helper
|
63
|
+
self.define_method(helper.to_sym) do
|
64
|
+
self.send("#{namespaces}_#{helper}".to_sym)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_namespace_path_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- BenenB
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- benenbruen@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- config.ru
|
39
|
+
- lib/rails_namespace_path_helper.rb
|
40
|
+
- lib/rails_namespace_path_helper/railtie.rb
|
41
|
+
- lib/rails_namespace_path_helper/version.rb
|
42
|
+
- sig/rails_namespace_path_helper.rbs
|
43
|
+
homepage: https://github.com/BenenB/rails_namespace_path_helper
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata:
|
47
|
+
homepage_uri: https://github.com/BenenB/rails_namespace_path_helper
|
48
|
+
source_code_uri: https://github.com/BenenB/rails_namespace_path_helper
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.6.0
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.5.3
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: An extension for namespaced controllers in Rails
|
68
|
+
test_files: []
|