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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1664be8c46748dec0e0b84edf45896f04450cdb71682dca02b403e8208995cdf
4
- data.tar.gz: 25f3d481ff1e29b665572cfdcea8a834724c602235aac9344bffda55e616332f
3
+ metadata.gz: 936309a17ec24cbcbbabbe95f6a2d1348eb938e7ad96ccb95ad94c81a4c00970
4
+ data.tar.gz: 25c23abdf0674deab60754b91e57cc44daa68484f2e573bb86bb75a255c616f5
5
5
  SHA512:
6
- metadata.gz: a39e59538ecdfdc4d956713ed5dba9cebeaf61c7556793cf1705c6325e02bb374940e0be1b663e6e43841a0d6a5af66c48e6d7fc94378c62e98b3c11b0530c5a
7
- data.tar.gz: cbb6e5e01d7b13fbce76c04cdc38bf26e4454707655b43272ff42f74c15cc652ea91c68b98deed4c5bee309de7c0705a53a8e3b699bb2c048783f971c75e5a61
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 this line to your application's Gemfile:
24
+ Add `ActionWidget` to your application by running
25
25
 
26
- gem 'action_widget'
26
+ $ bundle add action_widget
27
27
 
28
- And then execute:
28
+ or install it manually by running:
29
29
 
30
- $ bundle
30
+ $ gem install action_widget
31
31
 
32
- Or install it yourself as:
32
+ ### Ruby on Rails
33
33
 
34
- $ gem install action_widget
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
 
@@ -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.10'
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'
@@ -18,7 +18,7 @@ module ActionWidget
18
18
  super(view, Hash[attributes])
19
19
  end
20
20
 
21
- def render
21
+ def render(*)
22
22
  raise NotImplementedError, "#{self.class} must implement #render"
23
23
  end
24
24
  end
@@ -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
- attr_reader :pattern
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 initialize(*)
13
- super
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 underscored_suffix
30
- return if suffix.nil?
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
- "#{helper_name}_test.rb"
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
- "#{helper_name}_spec.rb"
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
- "#{helper_name}.rb"
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.prefix, basename.classify, configuration.suffix].join('')
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
- (configuration.minitest_superclass || "ActionView::TestCase").to_s
75
+ configuration.minitest_superclass.to_s
72
76
  end
73
77
 
74
78
  def helper_name
75
- class_name.underscore
79
+ [configuration.helper_prefix, basename.underscore, configuration.helper_suffix].compact.join('_')
76
80
  end
77
81
 
78
82
  def basename
79
- if match = name.underscore.match(configuration.pattern)
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
@@ -1,3 +1,3 @@
1
1
  module ActionWidget
2
- VERSION = '0.9.0'
2
+ VERSION = '0.10.0'
3
3
  end
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(suffix: "Widget")
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
- return nil unless helper?(helper_name)
42
- basename = configuration.pattern.match(helper_name)[1]
43
- classname = [configuration.prefix, basename.camelcase, configuration.suffix].join("")
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
@@ -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
@@ -0,0 +1,9 @@
1
+ ActionWidget.configure do |config|
2
+ # config.class_prefix = nil
3
+ config.class_suffix = "Widget"
4
+
5
+ # config.helper_prefix = nil
6
+ config.helper_suffix = "widget"
7
+
8
+ # config.directory = "widgets"
9
+ 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.9.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 00:00:00.000000000 Z
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.10'
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.10'
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