diff --git a/README.md b/README.md index 5377fd5..3b7bd33 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,24 @@ # 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 @@ -20,6 +26,18 @@ get '/foo' do 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 diff --git a/lib/phlex-sinatra.rb b/lib/phlex-sinatra.rb index 08db3bc..128451a 100644 --- a/lib/phlex-sinatra.rb +++ b/lib/phlex-sinatra.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'phlex' +require 'sinatra/base' require_relative 'phlex/sinatra/version' module Phlex @@ -18,26 +19,16 @@ 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, @@ -45,13 +36,13 @@ def phlex( 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 @@ -73,4 +64,8 @@ def phlex( end end end + + SGML.include Sinatra::SGMLOverrides end + +Sinatra.helpers Phlex::Sinatra diff --git a/spec/general_spec.rb b/spec/general_spec.rb index 96710b6..aced156 100644 --- a/spec/general_spec.rb +++ b/spec/general_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2c23c2d..415d9cd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/streaming_spec.rb b/spec/streaming_spec.rb index 3d4070c..392b074 100644 --- a/spec/streaming_spec.rb +++ b/spec/streaming_spec.rb @@ -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