action_view_preview 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 143b01a8f2f804443f8b5a6894acab8f0a077bd6465a2bfb2a0e13440af553b9
4
+ data.tar.gz: 902199c1a8f455d4cdb10655f3e812f315d4861f60b9c14a8efd8d697b5382d9
5
+ SHA512:
6
+ metadata.gz: d527e086a7835cd02fe3d1fb0f97f96b340f3f392be4271cd6ddddd83854729a43f605f7f8a1999f79f99a118f0095e3e43391abf9208547f7298913d4b428ee
7
+ data.tar.gz: a105ad9734d535800ea73e028ff1eb23b44708cff75e5624b06c442bd5430b3d4d0e0f19dc60997f48663211e77421a79dbe7a419c6d8d1821f38a250074d31c
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Lucas Campanari
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # ActionViewPreview
2
+ Sometimes being able to preview a view might come in handy, like when a view is going to be used only inside a PDF, for example.
3
+
4
+ This gem allows a setup similar `ActionMailer::Base`, and provides a development tool for rendering and debugging views that wouldn't otherwise be rendered in your application.
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'action_view_preview'
11
+ ```
12
+
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ Or install it yourself as:
19
+ ```bash
20
+ $ gem install action_view_preview
21
+ ```
22
+
23
+ ## Usage
24
+ 1. Mount the engine in your `routes.rb`:
25
+
26
+ ```ruby
27
+ Rails.application.routes.draw do
28
+ # Add the line below
29
+ mount ActionViewPreview::Engine => "/action_view_preview"
30
+ end
31
+ ```
32
+
33
+ 2. Create files `*_preview.rb` on `test/views/previews/`, e.g. `hello_preview.rb`:
34
+
35
+ ```ruby
36
+ class HelloViewPreview < ActionViewPreview::Base
37
+ def hello
38
+ '<b>Hello world</b>'
39
+ end
40
+ end
41
+ ```
42
+
43
+ 3. Access the route `/action_view_preview` to see the available previews
44
+
45
+ ## Configuration
46
+
47
+ Add a `ActionViewPreview.setup` block to an initializer. For example, if you want to place the preview files in `lib/view_previews`:
48
+
49
+ ```ruby
50
+ ActionViewPreview.setup do |config|
51
+ config.preview_path = "#{Rails.root}/lib/view_previews"
52
+ end
53
+ ```
54
+
55
+ Possible config values:
56
+
57
+ | Config | Default | Description |
58
+ | - | - | - |
59
+ | `preview_path` | `test/views/previews` | Path to the preview files |
60
+
61
+ ## License
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
9
+
10
+ require "rake/testtask"
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
17
+
18
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/action_view_preview .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ module ActionViewPreview
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,50 @@
1
+ require_dependency "action_view_preview/application_controller"
2
+
3
+ module ActionViewPreview
4
+ class PreviewController < ApplicationController
5
+ before_action :find_preview, only: :show
6
+
7
+ def index
8
+ @previews = ActionViewPreview::Base.all
9
+ @page_title = "View Previews"
10
+ end
11
+
12
+ def show
13
+ preview
14
+ end
15
+
16
+
17
+ private
18
+
19
+ def preview
20
+ if params[:path] == @preview.preview_name
21
+ @page_title = "View Previews for #{@preview.preview_name}"
22
+ render action: "view"
23
+ else
24
+ @view_action = File.basename(params[:path])
25
+
26
+ if @preview.view_exists?(@view_action)
27
+ @page_title = "View Preview for #{@preview.preview_name}##{@view_action}"
28
+ @view = @preview.call(@view_action, params)
29
+ render action: "show", layout: false, formats: [:html]
30
+ else
31
+ # raise AbstractController::ActionNotFound, "View '#{@view_action}' not found in #{@preview.name}"
32
+ raise "View '#{@view_action}' not found in #{@preview.name}"
33
+ end
34
+ end
35
+ end
36
+
37
+ def find_preview
38
+ candidates = []
39
+ params[:path].to_s.scan(%r{/|$}) { candidates << $` }
40
+ preview = candidates.detect { |candidate| ActionViewPreview::Base.exists?(candidate) }
41
+
42
+ if preview
43
+ @preview = ActionViewPreview::Base.find(preview)
44
+ else
45
+ # raise AbstractController::ActionNotFound, "View preview '#{params[:path]}' not found"
46
+ raise "View preview '#{params[:path]}' not found"
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ module ActionViewPreview
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActionViewPreview
2
+ module PreviewHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActionViewPreview
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module ActionViewPreview
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module ActionViewPreview
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ <% @previews.each do |preview| %>
2
+ <h3><%= link_to preview.preview_name.titleize, url_for(controller: "preview", action: "show", path: preview.preview_name) %></h3>
3
+ <ul>
4
+ <% preview.views.each do |view| %>
5
+ <li><%= link_to view, url_for(controller: "preview", action: "show", path: "#{preview.preview_name}/#{view}") %></li>
6
+ <% end %>
7
+ </ul>
8
+ <% end %>
@@ -0,0 +1 @@
1
+ <%= @view.html_safe %>
@@ -0,0 +1,6 @@
1
+ <h3><%= @preview.preview_name.titleize %></h3>
2
+ <ul>
3
+ <% @preview.views.each do |view| %>
4
+ <li><%= link_to view, url_for(controller: "preview", action: "show", path: "#{@preview.preview_name}/#{view}") %></li>
5
+ <% end %>
6
+ </ul>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Action view preview</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+ </head>
8
+ <body>
9
+ <%= yield %>
10
+ </body>
11
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ ActionViewPreview::Engine.routes.draw do
2
+ get '/' => 'preview#index', as: :preview_index, internal: true
3
+ get '/*path' => 'preview#show', as: :preview_show, internal: true
4
+ end
@@ -0,0 +1,62 @@
1
+ module ActionViewPreview
2
+ class Base
3
+ extend ActiveSupport::DescendantsTracker
4
+
5
+ attr_reader :params
6
+
7
+ def initialize(params = {})
8
+ @params = params
9
+ end
10
+
11
+ class << self
12
+
13
+ def call(view_action, params = {})
14
+ preview = new(params)
15
+ preview.public_send(view_action)
16
+ end
17
+
18
+ # Returns all preview classes
19
+ def all
20
+ load_previews if descendants.empty?
21
+ descendants
22
+ end
23
+
24
+ # Returns all of the available views
25
+ def views
26
+ public_instance_methods(false).map(&:to_s).sort
27
+ end
28
+
29
+ # Returns +true+ if the view exists.
30
+ def view_exists?(view)
31
+ views.include?(view)
32
+ end
33
+
34
+ # Returns the underscored name of the preview without the suffix
35
+ def preview_name
36
+ name.delete_suffix("Preview").underscore
37
+ end
38
+
39
+ # Find a preview by its underscored class name
40
+ def find(preview)
41
+ all.find { |p| p.preview_name == preview }
42
+ end
43
+
44
+ # Returns +true+ if the preview exists
45
+ def exists?(preview)
46
+ all.any? { |p| p.preview_name == preview }
47
+ end
48
+
49
+ private
50
+
51
+ def load_previews
52
+ if preview_path
53
+ Dir["#{preview_path}/**/*_preview.rb"].sort.each { |file| require_dependency file }
54
+ end
55
+ end
56
+
57
+ def preview_path
58
+ ActionViewPreview.preview_path
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ module ActionViewPreview
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ActionViewPreview
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module ActionViewPreview
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,19 @@
1
+ require "action_view_preview/version"
2
+ require "action_view_preview/engine"
3
+
4
+ module ActionViewPreview
5
+ extend ::ActiveSupport::Autoload
6
+
7
+ autoload :Base
8
+
9
+ mattr_accessor :preview_path
10
+
11
+ def self.setup
12
+ yield self
13
+ end
14
+
15
+ def self.preview_path
16
+ @@preview_path || "#{Rails.root}/test/views/previews"
17
+ end
18
+
19
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :action_view_preview do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_view_preview
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Campanari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-29 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: '6.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ description: A development tool for rendering and debugging views that wouldn't otherwise
28
+ be rendered in your application
29
+ email:
30
+ - campanari.ls@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/assets/config/action_view_preview_manifest.js
39
+ - app/assets/stylesheets/action_view_preview/application.css
40
+ - app/assets/stylesheets/action_view_preview/preview.css
41
+ - app/controllers/action_view_preview/application_controller.rb
42
+ - app/controllers/action_view_preview/preview_controller.rb
43
+ - app/helpers/action_view_preview/application_helper.rb
44
+ - app/helpers/action_view_preview/preview_helper.rb
45
+ - app/jobs/action_view_preview/application_job.rb
46
+ - app/mailers/action_view_preview/application_mailer.rb
47
+ - app/models/action_view_preview/application_record.rb
48
+ - app/views/action_view_preview/preview/index.html.erb
49
+ - app/views/action_view_preview/preview/show.html.erb
50
+ - app/views/action_view_preview/preview/view.html.erb
51
+ - app/views/layouts/action_view_preview/application.html.erb
52
+ - config/routes.rb
53
+ - lib/action_view_preview.rb
54
+ - lib/action_view_preview/base.rb
55
+ - lib/action_view_preview/engine.rb
56
+ - lib/action_view_preview/version.rb
57
+ - lib/tasks/action_view_preview_tasks.rake
58
+ homepage: https://github.com/lcampanari/action_view_preview
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/lcampanari/action_view_preview
63
+ source_code_uri: https://github.com/lcampanari/action_view_preview
64
+ changelog_uri: https://github.com/lcampanari/action_view_preview
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.0.9
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Views preview for Rails
84
+ test_files: []