showcase-rails 0.2.8 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03270bcd056181e09393db4fb8c701fb082e41d3c30ed2ec00b82004e8c967a6
4
- data.tar.gz: d762aea659bfd35a7b7a14126976d8bd56f9edf0ff8a8399e882b37907cbd4c9
3
+ metadata.gz: 1fcccbcd02f9ffc40e5c1555b816250dd78dace039662c5302ae4149f37a8fed
4
+ data.tar.gz: b4591324e7afa694e34bc412ae5d4442ecc5308d834b6874a29750e1b522a578
5
5
  SHA512:
6
- metadata.gz: c7a642d7d0f7bf5dc52cf290116b566f81206650bdbd3880bbee20aefebb83edead2b4688abbc3ae376a89e6a63913880e8be51c908133c4bb4823ce3c2ddd89
7
- data.tar.gz: 9ce4be151434d49e3a6acc329cf6c8765786844855fd00319b33ac48fe301bdf6609989acd44e16c72174bbc09a3774a4a736c18d79ef9e4a33acf6ebd76aaa2
6
+ metadata.gz: 4719a8283d75170d95c789469824aab301df3d822b0d2da6f89a3ecd60c1e0a660c4af275dfffcf1b4967a546799e7193ab1ebedd2c11715e240ac7f4c1fc28d
7
+ data.tar.gz: b5d74c620b87ca6860d4a7356196ce8f7dd25a0ac2d71caa37b1d9cd440cb6b8f79eea28384dde8cd955766c8d5468b7cc3c6e2f389d81cf4c09914e3089e87a
data/README.md CHANGED
@@ -99,9 +99,10 @@ class ShowcaseTest < Showcase::PreviewsTest
99
99
  assert_text "This is a combobox, for sure."
100
100
  end
101
101
 
102
- test showcase: "button", id: "basic" do
103
- # This test block runs within the #button container element's #basic sample.
104
- assert_button class: ["text-xs"]
102
+ test showcase: "button" do
103
+ assert_selector id: "basic" do
104
+ assert_button class: ["text-xs"]
105
+ end
105
106
  end
106
107
 
107
108
  test "some non-Showcase test" do
@@ -110,6 +111,18 @@ class ShowcaseTest < Showcase::PreviewsTest
110
111
  end
111
112
  ```
112
113
 
114
+ ## Linking to previews
115
+
116
+ Call `showcase.link_to` with the URL path to the other Showcase:
117
+
118
+ ```erb
119
+ <%= showcase.link_to "stimulus_controllers/welcome" %>
120
+ <%= showcase.link_to "components/button", id: "extra-large" %> <%# Pass an id to link to a specific sample %>
121
+
122
+ <%# You can also pass just an id: to target a sample on the current showcase %>
123
+ <%= showcase.link_to id: "extra-large" %>
124
+ ```
125
+
113
126
  ## Full Rails engine support
114
127
 
115
128
  Any Rails engines in your app that ships previews in their `app/views/showcase/previews` directory will automatically be surfaced in your app. Here's an example from the [bullet_train-themes-light Rails engine](https://github.com/bullet-train-co/bullet_train-core/tree/main/bullet_train-themes-light/app/views/showcase/previews).
@@ -916,6 +916,10 @@ select {
916
916
  padding-top: 1.75rem;
917
917
  }
918
918
 
919
+ .sc-font-mono {
920
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
921
+ }
922
+
919
923
  .sc-text-2xl {
920
924
  font-size: 1.5rem;
921
925
  line-height: 2rem;
@@ -931,6 +935,11 @@ select {
931
935
  line-height: 1.5rem;
932
936
  }
933
937
 
938
+ .sc-text-sm {
939
+ font-size: 0.875rem;
940
+ line-height: 1.25rem;
941
+ }
942
+
934
943
  .sc-text-xl {
935
944
  font-size: 1.25rem;
936
945
  line-height: 1.75rem;
@@ -32,6 +32,22 @@ class Showcase::Preview
32
32
  @badges.concat badges
33
33
  end
34
34
 
35
+ # Allows linking out to other Showcases
36
+ #
37
+ # <%= showcase.link_to "components/button", id: "extra-large" %>
38
+ # # => <a href="components/button#extra-large"><showcase components/button#extra-large></a>
39
+ #
40
+ # Can link to other samples on the current showcase too:
41
+ #
42
+ # # If we're within app/views/showcase/previews/components/_button.html.erb
43
+ # <%= showcase.link_to id: "extra-large" %>
44
+ # # => <a href="components/button#extra-large"><showcase components/button#extra-large></a>
45
+ def link_to(preview_id = id, id: nil)
46
+ @view_context.link_to @view_context.preview_path(preview_id, anchor: id), class: "sc-font-mono sc-text-sm" do
47
+ "<showcase #{[preview_id, id].compact.join("#").squish}>"
48
+ end
49
+ end
50
+
35
51
  # Adds a named sample to demonstrate with the Showcase can do.
36
52
  #
37
53
  # By default, sample takes a block that'll automatically have its source extracted, like this:
@@ -66,7 +82,7 @@ class Showcase::Preview
66
82
  # - the `sample.events` what JavaScript `events` to listen for on the element
67
83
  # - any other custom options are available in `sample.details`.
68
84
  def sample(name, **options, &block)
69
- @samples << Showcase::Sample.new(@view_context, name, **options).tap { _1.collect(&block) }
85
+ @samples << Showcase::Sample.new(@view_context, name, **options).tap { _1.evaluate(&block) }
70
86
  end
71
87
 
72
88
  # Yields an Options object to help define the configuration table for a Preview.
@@ -88,7 +104,7 @@ class Showcase::Preview
88
104
  end
89
105
 
90
106
  def render_associated_partial
91
- @view_context.render "#{Showcase.previews_path}/#{id}", showcase: self
107
+ @view_context.render "showcase/previews/#{id}", showcase: self
92
108
  nil
93
109
  end
94
110
  end
@@ -1,6 +1,6 @@
1
1
  class Showcase::Sample
2
2
  attr_reader :name, :id, :events, :details
3
- attr_reader :source, :instrumented
3
+ attr_reader :rendered, :source, :instrumented
4
4
 
5
5
  def initialize(view_context, name, description: nil, id: name.parameterize, syntax: :erb, events: nil, **details)
6
6
  @view_context = view_context
@@ -14,36 +14,39 @@ class Showcase::Sample
14
14
  @description
15
15
  end
16
16
 
17
- def collect(&block)
17
+ def evaluate(&block)
18
18
  if block.arity.zero?
19
- preview(&block)
20
- extract(&block)
19
+ consume(&block)
21
20
  else
22
21
  @view_context.capture(self, &block)
23
22
  end
24
23
  end
25
24
 
26
- def preview(&block)
27
- return @preview unless block_given?
25
+ def consume(&block)
26
+ render(&block)
27
+ extract_source(&block)
28
+ end
28
29
 
30
+ def render(&block)
29
31
  # TODO: Remove `is_a?` check when Rails 6.1 support is dropped.
30
32
  assigns = proc { @instrumented = _1 if _1.is_a?(ActiveSupport::Notifications::Event) }
31
33
  ActiveSupport::Notifications.subscribed(assigns, "render_partial.action_view") do
32
- @preview = @view_context.capture(&block)
34
+ @rendered = @view_context.capture(&block)
33
35
  end
34
36
  end
35
37
 
36
- def extract(&block)
38
+ def extract_source(&block)
37
39
  source = extract_source_block_via_matched_indentation_from(*block.source_location)
38
40
  @source = @view_context.instance_exec(source, @syntax, &Showcase.sample_renderer)
39
41
  end
40
42
 
41
43
  private
42
44
 
43
- def extract_source_block_via_matched_indentation_from(file, starting_index)
44
- first_line, *lines = File.readlines(file).from(starting_index - 1)
45
+ def extract_source_block_via_matched_indentation_from(file, source_location_index)
46
+ # `Array`s are zero-indexed, but `source_location` indexes are not, hence `pred`.
47
+ starting_line, *lines = File.readlines(file).slice(source_location_index.pred..)
45
48
 
46
- indentation = first_line.match(/^\s+(?=\b)/).to_s
49
+ indentation = starting_line.match(/^\s+/).to_s
47
50
  matcher = /^#{indentation}\S/
48
51
 
49
52
  index = lines.index { _1.match?(matcher) }
@@ -19,9 +19,9 @@
19
19
  <% end %>
20
20
  </header>
21
21
 
22
- <% if sample.preview %>
22
+ <% if sample.rendered %>
23
23
  <section class="sc-px-4 sc-py-2">
24
- <%= sample.preview %>
24
+ <%= sample.rendered %>
25
25
  </section>
26
26
  <% end %>
27
27
 
@@ -1,3 +1,3 @@
1
1
  <article class="hover:sc-bg-indigo-50 dark:hover:sc-bg-neutral-700/50 <%= "sc-bg-indigo-50 dark:sc-bg-neutral-700/50" if path.id == params[:id] %>">
2
- <%= link_to path.basename.titleize, showcase.preview_path(path.id), class: "sc-inline-block sc-py-2 sc-px-8 sc-w-full !sc-text-inherit !sc-no-underline" %>
2
+ <%= link_to path.basename.titleize, preview_path(path.id), class: "sc-inline-block sc-py-2 sc-px-8 sc-w-full !sc-text-inherit !sc-no-underline" %>
3
3
  </article>
@@ -4,7 +4,7 @@ module.exports = {
4
4
  prefix: 'sc-',
5
5
  content: [
6
6
  './public/*.html',
7
- './app/helpers/**/*.rb',
7
+ './app/{models,helpers}/**/*.rb',
8
8
  './app/javascript/**/*.js',
9
9
  './app/views/**/*.{erb,haml,html,slim}'
10
10
  ],
@@ -1,12 +1,13 @@
1
1
  class Showcase::PreviewsTest < ActionView::TestCase
2
+ extensions = [ Showcase::EngineController._helpers, Showcase::Engine.routes.url_helpers ]
3
+ setup { view.extend *extensions }
4
+
2
5
  def self.inherited(test_class)
3
6
  super
4
7
  test_class.prepare
5
8
  end
6
9
 
7
10
  def self.prepare
8
- tests Showcase::EngineController._helpers
9
-
10
11
  tree = Showcase::Path.tree
11
12
  tree.flat_map(&:ordered_paths).each do |path|
12
13
  test "Showcase: automatically renders showcase/previews/#{path.id}" do
@@ -20,17 +21,16 @@ class Showcase::PreviewsTest < ActionView::TestCase
20
21
  end
21
22
  end
22
23
 
23
- def self.test(name = nil, showcase: nil, id: nil, &block)
24
- case
25
- when name then super(name, &block)
26
- when id && showcase.nil? then raise ArgumentError, "can't test a sample without a showcase"
24
+ def self.test(name = nil, showcase: nil, &block)
25
+ if name
26
+ super(name, &block)
27
27
  else
28
- super "Showcase: showcase/previews/#{showcase} #{"sample #{id}" if id}".squish do
28
+ super "Showcase: showcase/previews/#{showcase}" do
29
29
  path = Showcase::Path.new(showcase)
30
30
  render "showcase/engine/preview", preview: path.preview_for(view)
31
31
 
32
32
  assert_showcase_preview(path.id)
33
- assert_element(id: id || path.id) { instance_eval(&block) }
33
+ instance_eval(&block)
34
34
  end
35
35
  end
36
36
  end
@@ -1,3 +1,3 @@
1
1
  module Showcase
2
- VERSION = "0.2.8"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/showcase.rb CHANGED
@@ -24,12 +24,9 @@ module Showcase
24
24
  end
25
25
  end
26
26
 
27
- singleton_class.attr_reader :previews_path
28
- @previews_path = "showcase/previews"
29
-
30
27
  def self.previews
31
28
  Showcase::EngineController.view_paths.map(&:path).flat_map do |root|
32
- Dir.glob("**/*.*", base: File.join(root, previews_path))
29
+ Dir.glob("**/*.*", base: File.join(root, "showcase/previews"))
33
30
  end.uniq
34
31
  end
35
32
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: showcase-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pence
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-03-25 00:00:00.000000000 Z
12
+ date: 2023-03-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails