action_widget 0.9.0 → 0.10.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 +4 -4
- data/README.md +28 -6
- data/action_widget.gemspec +1 -1
- data/lib/action_widget/base.rb +1 -1
- data/lib/action_widget/configuration.rb +15 -24
- data/lib/action_widget/extensions/rails/action_widget_tasks.rake +19 -0
- data/lib/action_widget/extensions/rails/generators.rb +13 -7
- data/lib/action_widget/extensions/rails/railtie.rb +4 -0
- data/lib/action_widget/version.rb +1 -1
- data/lib/action_widget.rb +6 -9
- data/spec/action_widget_spec.rb +26 -0
- data/support/templates/initializer.rb.erb +9 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 936309a17ec24cbcbbabbe95f6a2d1348eb938e7ad96ccb95ad94c81a4c00970
|
4
|
+
data.tar.gz: 25c23abdf0674deab60754b91e57cc44daa68484f2e573bb86bb75a255c616f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22099b7acd486464b24a8cbd18555fe2cff2dd236a885a43dad88a214aa7d47f441285a8d822a934aed64fde7d5490533a70e1722e0cf72bd259d3c2ea25ff7b
|
7
|
+
data.tar.gz: 376888ff223f362a863ef32cdc69d709286fd5439f92fda3dfe7c29698a388f84aeae36431822ce9f0ac7d44b5e869a2b95ecaf8e4a8e763d02129060705011f
|
data/README.md
CHANGED
@@ -21,17 +21,39 @@ change.
|
|
21
21
|
|
22
22
|
## Installation
|
23
23
|
|
24
|
-
Add
|
24
|
+
Add `ActionWidget` to your application by running
|
25
25
|
|
26
|
-
|
26
|
+
$ bundle add action_widget
|
27
27
|
|
28
|
-
|
28
|
+
or install it manually by running:
|
29
29
|
|
30
|
-
$
|
30
|
+
$ gem install action_widget
|
31
31
|
|
32
|
-
|
32
|
+
### Ruby on Rails
|
33
33
|
|
34
|
-
|
34
|
+
If you're working with Ruby on Rails, please run
|
35
|
+
|
36
|
+
$ bin/rails action_widget:install
|
37
|
+
|
38
|
+
in addition to one of the above commands to generate the initializer that allows
|
39
|
+
you to configure `ActionWidget`. The resulting initializer will be located at
|
40
|
+
`config/initializers/action_widget.rb` and should look as follows:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
ActionWidget.configure do |config|
|
44
|
+
# config.class_prefix = ""
|
45
|
+
config.class_suffix = "Widget"
|
46
|
+
|
47
|
+
# config.helper_prefix = ""
|
48
|
+
config.helper_suffix = "widget"
|
49
|
+
|
50
|
+
config.directory = "widgets"
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
As shown in the example above, `ActionWidget` allows you to customize
|
55
|
+
prefixes and suffixes for class names and helper names, respectively, as well as
|
56
|
+
specify the directory name that will house your widgets within the `app/` directory.
|
35
57
|
|
36
58
|
## Usage
|
37
59
|
|
data/action_widget.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.required_ruby_version = '>= 3.0.0'
|
18
18
|
|
19
19
|
gem.add_dependency 'activesupport', '> 6.0'
|
20
|
-
gem.add_dependency 'smart_properties', '~> 1.
|
20
|
+
gem.add_dependency 'smart_properties', '~> 1.12'
|
21
21
|
|
22
22
|
gem.add_development_dependency 'actionview', '>= 6.0'
|
23
23
|
gem.add_development_dependency 'pry'
|
data/lib/action_widget/base.rb
CHANGED
@@ -1,34 +1,25 @@
|
|
1
1
|
module ActionWidget
|
2
2
|
class Configuration
|
3
3
|
include SmartProperties
|
4
|
-
property :prefix
|
5
|
-
property :suffix
|
6
|
-
property :superclass, required: true, default: -> { ActionWidget::Base }
|
7
|
-
property :directory, required: true, converts: :to_s, accepts: ->(string) { !string.empty? }, default: -> { [underscored_prefix, underscored_suffix].compact.join("_").pluralize }
|
8
|
-
property :minitest_superclass
|
9
4
|
|
10
|
-
|
5
|
+
property :class_prefix
|
6
|
+
property :class_suffix, default: "Widget"
|
7
|
+
property :helper_prefix
|
8
|
+
property :helper_suffix, default: "widget"
|
9
|
+
property! :superclass, default: "ActionWidget::Base"
|
10
|
+
property! :directory,
|
11
|
+
converts: :to_s,
|
12
|
+
accepts: ->(string) { !string.empty? },
|
13
|
+
default: "widgets"
|
14
|
+
property! :minitest_superclass,
|
15
|
+
default: "ActionView::TestCase"
|
11
16
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
@pattern = Regexp.new("^%s$" % [
|
16
|
-
underscored_prefix,
|
17
|
-
"(.*)",
|
18
|
-
underscored_suffix
|
19
|
-
].compact.join("_"))
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def underscored_prefix
|
25
|
-
return if prefix.nil?
|
26
|
-
prefix.underscore
|
17
|
+
def class_pattern
|
18
|
+
Regexp.new("^%s$" % [class_prefix, "(.*?)", class_suffix].compact.join(""))
|
27
19
|
end
|
28
20
|
|
29
|
-
def
|
30
|
-
|
31
|
-
suffix.underscore
|
21
|
+
def helper_pattern
|
22
|
+
Regexp.new("^%s$" % [helper_prefix, "(.*?)", helper_suffix].compact.join("_"))
|
32
23
|
end
|
33
24
|
end
|
34
25
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module ActionWidget
|
4
|
+
class InitializerGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('../../../../../support/templates', __FILE__)
|
6
|
+
|
7
|
+
def create_initializer_file
|
8
|
+
template "initializer.rb.erb", Rails.root.join("config", "initializers", "action_widget.rb")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
namespace :action_widget do
|
14
|
+
desc "Install"
|
15
|
+
task :install do
|
16
|
+
require_relative './generators'
|
17
|
+
ActionWidget::InitializerGenerator.start
|
18
|
+
end
|
19
|
+
end
|
@@ -40,7 +40,7 @@ module ActionWidget
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_filename
|
43
|
-
"#{
|
43
|
+
"#{filename}_test.rb"
|
44
44
|
end
|
45
45
|
|
46
46
|
def spec_path
|
@@ -48,7 +48,7 @@ module ActionWidget
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def spec_filename
|
51
|
-
"#{
|
51
|
+
"#{filename}_spec.rb"
|
52
52
|
end
|
53
53
|
|
54
54
|
def implementation_path
|
@@ -56,11 +56,15 @@ module ActionWidget
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def implementation_filename
|
59
|
-
"#{
|
59
|
+
"#{filename}.rb"
|
60
|
+
end
|
61
|
+
|
62
|
+
def filename
|
63
|
+
[configuration.class_prefix&.underscore, basename.underscore, configuration.class_suffix&.underscore].compact.join('_')
|
60
64
|
end
|
61
65
|
|
62
66
|
def class_name
|
63
|
-
[configuration.
|
67
|
+
[configuration.class_prefix, basename.classify, configuration.class_suffix].compact.join('')
|
64
68
|
end
|
65
69
|
|
66
70
|
def superclass_name
|
@@ -68,15 +72,17 @@ module ActionWidget
|
|
68
72
|
end
|
69
73
|
|
70
74
|
def minitest_superclass_name
|
71
|
-
|
75
|
+
configuration.minitest_superclass.to_s
|
72
76
|
end
|
73
77
|
|
74
78
|
def helper_name
|
75
|
-
|
79
|
+
[configuration.helper_prefix, basename.underscore, configuration.helper_suffix].compact.join('_')
|
76
80
|
end
|
77
81
|
|
78
82
|
def basename
|
79
|
-
if match = name.
|
83
|
+
if match = name.match(configuration.class_pattern)
|
84
|
+
match[1]
|
85
|
+
elsif match = name.match(configuration.helper_pattern)
|
80
86
|
match[1]
|
81
87
|
else
|
82
88
|
name
|
@@ -4,6 +4,10 @@ module ActionWidget
|
|
4
4
|
module Extensions
|
5
5
|
module Rails
|
6
6
|
class Railtie < ::Rails::Railtie
|
7
|
+
rake_tasks do
|
8
|
+
load File.expand_path("../action_widget_tasks.rake", __FILE__)
|
9
|
+
end
|
10
|
+
|
7
11
|
initializer "action_widget.helper" do
|
8
12
|
ActiveSupport.on_load(:action_view) do
|
9
13
|
include ActionWidget::ViewHelper
|
data/lib/action_widget.rb
CHANGED
@@ -12,15 +12,11 @@ module ActionWidget
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def configuration
|
15
|
-
@configuration ||= Configuration.new
|
15
|
+
@configuration ||= Configuration.new
|
16
16
|
end
|
17
17
|
|
18
18
|
def configure(&block)
|
19
|
-
@configuration = Configuration.new(&block)
|
20
|
-
end
|
21
|
-
|
22
|
-
def helper?(name)
|
23
|
-
!!configuration.pattern.match(name)
|
19
|
+
@configuration = Configuration.new.tap(&block)
|
24
20
|
end
|
25
21
|
|
26
22
|
protected
|
@@ -38,9 +34,10 @@ module ActionWidget
|
|
38
34
|
private
|
39
35
|
|
40
36
|
def find_action_widget(helper_name)
|
41
|
-
|
42
|
-
|
43
|
-
|
37
|
+
match = configuration.helper_pattern.match(helper_name)
|
38
|
+
return nil if match.nil?
|
39
|
+
basename = match[1]
|
40
|
+
classname = [configuration.class_prefix, basename.classify, configuration.class_suffix].join("")
|
44
41
|
classname.constantize
|
45
42
|
rescue NameError, LoadError
|
46
43
|
nil
|
data/spec/action_widget_spec.rb
CHANGED
@@ -27,6 +27,10 @@ end
|
|
27
27
|
RSpec.describe DummyWidget do
|
28
28
|
let(:view) { ViewContext.empty }
|
29
29
|
|
30
|
+
it "should be registered" do
|
31
|
+
expect(ActionWidget[:dummy_widget]).to be(DummyWidget)
|
32
|
+
end
|
33
|
+
|
30
34
|
it "should delegate unknown method calls to the view context" do
|
31
35
|
expect(described_class.new(view).render).to eq("<p></p>")
|
32
36
|
end
|
@@ -49,4 +53,26 @@ RSpec.describe DummyWidget do
|
|
49
53
|
expect(instance.options).to eq({class: 'wide'})
|
50
54
|
end
|
51
55
|
end
|
56
|
+
|
57
|
+
context "with custom configuration" do
|
58
|
+
let(:expected_html) { '<p>Hello World</p>' }
|
59
|
+
|
60
|
+
before do
|
61
|
+
ActionWidget.configure do |config|
|
62
|
+
config.helper_suffix = nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should have an adjusted helper pattern" do
|
67
|
+
expect(ActionWidget.configuration.helper_pattern).to eq(/^(.*?)$/)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should be possible to override the helper suffix" do
|
71
|
+
expect(view.dummy("Hello World")).to eq(expected_html)
|
72
|
+
end
|
73
|
+
|
74
|
+
after do
|
75
|
+
ActionWidget.configure {}
|
76
|
+
end
|
77
|
+
end
|
52
78
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_widget
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstantin Tennhard
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-03-
|
10
|
+
date: 2025-03-17 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activesupport
|
@@ -29,14 +29,14 @@ dependencies:
|
|
29
29
|
requirements:
|
30
30
|
- - "~>"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '1.
|
32
|
+
version: '1.12'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '1.
|
39
|
+
version: '1.12'
|
40
40
|
- !ruby/object:Gem::Dependency
|
41
41
|
name: actionview
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
@@ -183,12 +183,14 @@ files:
|
|
183
183
|
- lib/action_widget/extensions.rb
|
184
184
|
- lib/action_widget/extensions/middleman.rb
|
185
185
|
- lib/action_widget/extensions/rails.rb
|
186
|
+
- lib/action_widget/extensions/rails/action_widget_tasks.rake
|
186
187
|
- lib/action_widget/extensions/rails/generators.rb
|
187
188
|
- lib/action_widget/extensions/rails/railtie.rb
|
188
189
|
- lib/action_widget/version.rb
|
189
190
|
- lib/action_widget/view_helper.rb
|
190
191
|
- spec/action_widget_spec.rb
|
191
192
|
- spec/spec_helper.rb
|
193
|
+
- support/templates/initializer.rb.erb
|
192
194
|
- support/templates/widget.rb.erb
|
193
195
|
- support/templates/widget_spec.rb.erb
|
194
196
|
- support/templates/widget_test.rb.erb
|