Skip to content

Commit

Permalink
Use the official Sinatra extensions API
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Feb 1, 2025
1 parent 5b3bf8e commit 1965af8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
# phlex-sinatra

[Phlex](https://github.com/phlex-ruby/phlex) already works with Sinatra (and everything else) but its normal usage leaves you without access to Sinatra's standard helper methods. This integration lets you use the `url()` helper method from within a Phlex view (along with the rest of the helper methods available in a Sinatra action).
[Phlex](https://github.com/phlex-ruby/phlex) already works with Sinatra (and everything else) but its normal usage leaves you without access to Sinatra's standard helper methods. This extension lets you use Sinatra's `url()` helper method from within a Phlex view (along with the rest of the helper methods available in a Sinatra action).

## Installation

Add phlex-sinatra to your application's Gemfile and run `bundle install`.
Add phlex-sinatra to your application's `Gemfile` and run `bundle install`.

```ruby
gem 'phlex-sinatra'
```

Then require it in your app:

```ruby
require 'phlex-sinatra'
```

## Usage

To enable the integration use the `phlex` method in your Sinatra action and pass an _instance_ of the Phlex view (instead of using `.call` to get its output):
Use the `phlex` helper method to render a Phlex view and pass an _instance_ of the Phlex view (instead of using `.call` to get its output). For a classic-style Sinatra app it'll Just Work™:

```ruby
get '/foo' do
phlex MyView.new
end
```

For a modular app the extension must be explicitly registered:

```ruby
class MyApp < Sinatra::Base
helpers Phlex::Sinatra

get '/foo' do
phlex MyView.new
end
end
```

You can now use Sinatra's `url()` helper method directly and its other methods (`params`, `request`, etc) via the `helpers` proxy:

```ruby
Expand Down
33 changes: 14 additions & 19 deletions lib/phlex-sinatra.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'phlex'
require 'sinatra/base'
require_relative 'phlex/sinatra/version'

module Phlex
Expand All @@ -18,40 +19,30 @@ def initialize(obj)
end
end

module SGML
module Overrides
def helpers
@_view_context
end
module SGMLOverrides
def helpers
@_view_context
end

def url(...)
helpers.url(...)
end
def url(...)
helpers.url(...)
end
end
end

class SGML
include Sinatra::SGML::Overrides
end
end

module Sinatra
module Templates
def phlex(
obj,
content_type: nil,
layout: false,
layout_engine: :erb,
stream: false
)
raise Phlex::Sinatra::TypeError.new(obj) unless obj.is_a?(Phlex::SGML)
raise TypeError.new(obj) unless obj.is_a?(SGML)

if layout && stream
raise Phlex::Sinatra::ArgumentError.new('streaming is not compatible with layout')
raise ArgumentError.new('streaming is not compatible with layout')
end

content_type ||= :svg if obj.is_a?(Phlex::SVG) && !layout
content_type ||= :svg if obj.is_a?(SVG) && !layout
self.content_type(content_type) if content_type

# Copy Sinatra's behaviour and interpret layout=true as meaning "use the
Expand All @@ -73,4 +64,8 @@ def phlex(
end
end
end

SGML.include Sinatra::SGMLOverrides
end

Sinatra.helpers Phlex::Sinatra
4 changes: 3 additions & 1 deletion spec/general_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def view_template
end
end

class TestApp < Sinatra::Application
class TestApp < Sinatra::Base
helpers Phlex::Sinatra

set :environment, :test

get '/error' do
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require 'phlex-sinatra'
require 'rack/test'
require 'sinatra/base'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down
4 changes: 3 additions & 1 deletion spec/streaming_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def view_template
end
end

class StreamingApp < Sinatra::Application
class StreamingApp < Sinatra::Base
helpers Phlex::Sinatra

set :environment, :test

get '/stream' do
Expand Down

0 comments on commit 1965af8

Please sign in to comment.