From 3ffa0588d1d91144d5c504137d511482c60453b6 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Tue, 28 Mar 2017 12:45:18 -0400 Subject: [PATCH] Move sinatra integration to separate gem Sinatra has been slow to release a version compatible with AR 5, so having a dev. dependency on sinatra has been painful. This is the main reason for the move, but there are the other, usual reasons: - Better organization of github issues - Independent development. The sinatra integration can target older versions of PT, for example. - Rails users don't want sinatra integration in their apps, even if it's basically inactive. And, of course, there is the inevitable downside that sinatra integration is now a second-class citizen. That's the trade-off with separate gems. --- Appraisals | 6 --- CHANGELOG.md | 2 + README.md | 52 +------------------------ gemfiles/ar_4.0.gemfile | 1 - gemfiles/ar_4.2.gemfile | 1 - lib/paper_trail.rb | 1 - lib/paper_trail/frameworks/sinatra.rb | 40 ------------------- test/functional/modular_sinatra_test.rb | 46 ---------------------- test/functional/sinatra_test.rb | 51 ------------------------ 9 files changed, 4 insertions(+), 196 deletions(-) delete mode 100644 lib/paper_trail/frameworks/sinatra.rb delete mode 100644 test/functional/modular_sinatra_test.rb delete mode 100644 test/functional/sinatra_test.rb diff --git a/Appraisals b/Appraisals index e2006042..12009e91 100644 --- a/Appraisals +++ b/Appraisals @@ -9,22 +9,16 @@ appraise "ar-4.0" do gem "activerecord", "~> 4.0" - gem "sinatra", "~> 1.4.6" end appraise "ar-4.2" do gem "activerecord", "~> 4.2" - gem "sinatra", "~> 1.4.6" end appraise "ar-5.0" do gem "activerecord", "~> 5.0.0" gem "rspec-rails", "~> 3.5.1" gem 'rails-controller-testing' - - # The AR5 version of PaperTrail is not compatible with sinatra 2 yet. - # Contributions welcome. - # gem "sinatra", "2.0.0.beta2" end appraise "ar_master" do diff --git a/CHANGELOG.md b/CHANGELOG.md index 24b2870e..30dca1f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/). - PaperTrail.serialized_attributes? - PaperTrail.config.serialized_attributes - PaperTrail.config.serialized_attributes= +- Sinatra integration moved to + [paper_trail-sinatra](https://github.com/jaredbeck/paper_trail-sinatra) gem ### Added diff --git a/README.md b/README.md index 57849a6e..73d717ef 100644 --- a/README.md +++ b/README.md @@ -1499,53 +1499,7 @@ require 'paper_trail/frameworks/rspec' ## 8. Sinatra -To configure PaperTrail for usage with [Sinatra][12], your `Sinatra` -app must be using `ActiveRecord` 3 or 4. There is no released version of sinatra yet -that is compatible with AR 5. We expect sinatra 2.0 to support AR 5, but it will have -breaking changes that will require changes to PaperTrail. - -It is also recommended to use the -[Sinatra ActiveRecord Extension][13] or something similar for managing your -applications `ActiveRecord` connection in a manner similar to the way `Rails` -does. If using the aforementioned `Sinatra ActiveRecord Extension`, steps for -setting up your app with PaperTrail will look something like this: - -1. Add PaperTrail to your `Gemfile`. - - `gem 'paper_trail'` - -2. Generate a migration to add a `versions` table to your database. - - `bundle exec rake db:create_migration NAME=create_versions` - -3. Copy contents of [create_versions.rb][14] -into the `create_versions` migration that was generated into your `db/migrate` directory. - -4. Run the migration. - - `bundle exec rake db:migrate` - -5. Add `has_paper_trail` to the models you want to track. - - -PaperTrail provides a helper extension that acts similar to the controller mixin -it provides for `Rails` applications. - -It will set `PaperTrail.whodunnit` to whatever is returned by a method named -`user_for_paper_trail` which you can define inside your Sinatra Application. (by -default it attempts to invoke a method named `current_user`) - -If you're using the modular [`Sinatra::Base`][15] style of application, you will -need to register the extension: - -```ruby -# bleh_app.rb -require 'sinatra/base' - -class BlehApp < Sinatra::Base - register PaperTrail::Sinatra -end -``` +See [paper_trail-sinatra][41]. ## Articles @@ -1642,10 +1596,7 @@ Released under the MIT licence. [9]: https://github.com/airblade/paper_trail/tree/3.0-stable [10]: https://github.com/airblade/paper_trail/tree/2.7-stable [11]: https://github.com/airblade/paper_trail/tree/rails2 -[12]: http://www.sinatrarb.com -[13]: https://github.com/janko-m/sinatra-activerecord [14]: https://raw.github.com/airblade/paper_trail/master/lib/generators/paper_trail/templates/create_versions.rb -[15]: http://www.sinatrarb.com/intro.html#Modular%20vs.%20Classic%20Style [16]: https://github.com/airblade/paper_trail/issues/113 [17]: https://github.com/rails/protected_attributes [18]: https://github.com/rails/strong_parameters @@ -1671,3 +1622,4 @@ Released under the MIT licence. [38]: https://github.com/sferik/rails_admin [39]: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#class-ActiveRecord::Base-label-Single+table+inheritance [40]: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Polymorphic+Associations +[41]: https://github.com/jaredbeck/paper_trail-sinatra diff --git a/gemfiles/ar_4.0.gemfile b/gemfiles/ar_4.0.gemfile index e5294d0c..99ecb105 100644 --- a/gemfiles/ar_4.0.gemfile +++ b/gemfiles/ar_4.0.gemfile @@ -3,6 +3,5 @@ source "https://rubygems.org" gem "activerecord", "~> 4.0" -gem "sinatra", "~> 1.4.6" gemspec :path => "../" diff --git a/gemfiles/ar_4.2.gemfile b/gemfiles/ar_4.2.gemfile index 7fc0168d..c03c7907 100644 --- a/gemfiles/ar_4.2.gemfile +++ b/gemfiles/ar_4.2.gemfile @@ -3,6 +3,5 @@ source "https://rubygems.org" gem "activerecord", "~> 4.2" -gem "sinatra", "~> 1.4.6" gemspec :path => "../" diff --git a/lib/paper_trail.rb b/lib/paper_trail.rb index 8d75e172..c5d1c0d3 100644 --- a/lib/paper_trail.rb +++ b/lib/paper_trail.rb @@ -155,7 +155,6 @@ ActiveSupport.on_load(:active_record) do end # Require frameworks -require "paper_trail/frameworks/sinatra" if defined?(::Rails) && ActiveRecord::VERSION::STRING >= "3.2" require "paper_trail/frameworks/rails" else diff --git a/lib/paper_trail/frameworks/sinatra.rb b/lib/paper_trail/frameworks/sinatra.rb deleted file mode 100644 index 1c59b680..00000000 --- a/lib/paper_trail/frameworks/sinatra.rb +++ /dev/null @@ -1,40 +0,0 @@ -require "active_support/core_ext/object" # provides the `try` method - -module PaperTrail - # Extensions to `Sinatra`. - module Sinatra - # Register this module inside your Sinatra application to gain access to - # controller-level methods used by PaperTrail. - def self.registered(app) - app.use RequestStore::Middleware - app.helpers self - app.before { set_paper_trail_whodunnit } - end - - protected - - # Returns the user who is responsible for any changes that occur. - # By default this calls `current_user` and returns the result. - # - # Override this method in your controller to call a different - # method, e.g. `current_person`, or anything you like. - def user_for_paper_trail - return unless defined?(current_user) - ActiveSupport::VERSION::MAJOR >= 4 ? current_user.try!(:id) : current_user.try(:id) - rescue NoMethodError - current_user - end - - private - - # Tells PaperTrail who is responsible for any changes that occur. - def set_paper_trail_whodunnit - @set_paper_trail_whodunnit_called = true - ::PaperTrail.whodunnit = user_for_paper_trail if ::PaperTrail.enabled? - end - end -end - -if defined?(::Sinatra) - ::Sinatra.register(::PaperTrail::Sinatra) -end diff --git a/test/functional/modular_sinatra_test.rb b/test/functional/modular_sinatra_test.rb deleted file mode 100644 index 3693b47f..00000000 --- a/test/functional/modular_sinatra_test.rb +++ /dev/null @@ -1,46 +0,0 @@ -# PaperTrail is not compatible with sinatra 2 yet. Contributions welcome. -if Gem::Version.new(Rack.release) < Gem::Version.new("2.0.0.alpha") - require "test_helper" - require "sinatra/base" - - # --- Tests for modular `Sinatra::Base` style ---- - class BaseApp < Sinatra::Base - configs = YAML.load_file(File.expand_path("../../dummy/config/database.yml", __FILE__)) - ActiveRecord::Base.configurations = configs - ActiveRecord::Base.establish_connection(:test) - register PaperTrail::Sinatra - - get "/test" do - Widget.create!(name: "foo") - "Hello" - end - - def current_user - @current_user ||= OpenStruct.new(id: "foobar") - end - end - - class ModularSinatraTest < ActionDispatch::IntegrationTest - include Rack::Test::Methods - - def app - @app ||= BaseApp - end - - test "baseline" do - assert_nil Widget.create.versions.first.whodunnit - end - - context "`PaperTrail::Sinatra` in a `Sinatra::Base` application" do - should "sets the `user_for_paper_trail` from the `current_user` method" do - get "/test" - assert_equal "Hello", last_response.body - widget = Widget.last - assert_not_nil widget - assert_equal "foo", widget.name - assert_equal 1, widget.versions.size - assert_equal "foobar", widget.versions.first.whodunnit - end - end - end -end diff --git a/test/functional/sinatra_test.rb b/test/functional/sinatra_test.rb deleted file mode 100644 index 23d10a10..00000000 --- a/test/functional/sinatra_test.rb +++ /dev/null @@ -1,51 +0,0 @@ -# PaperTrail is not compatible with sinatra 2 yet. Contributions welcome. -if Gem::Version.new(Rack.release) < Gem::Version.new("2.0.0.alpha") - require "test_helper" - # require 'sinatra/main' - - # --- Tests for non-modular `Sinatra::Application` style ---- - module Sinatra - class Application - configs = YAML.load_file(File.expand_path("../../dummy/config/database.yml", __FILE__)) - ActiveRecord::Base.configurations = configs - ActiveRecord::Base.establish_connection(:test) - - # We shouldn't actually need this line if I'm not mistaken but the tests - # seem to fail without it ATM - register PaperTrail::Sinatra - - get "/test" do - Widget.create!(name: "bar") - "Hai" - end - - def current_user - @current_user ||= OpenStruct.new(id: "raboof") - end - end - end - - class SinatraTest < ActionDispatch::IntegrationTest - include Rack::Test::Methods - - def app - @app ||= Sinatra::Application - end - - test "baseline" do - assert_nil Widget.create.versions.first.whodunnit - end - - context "`PaperTrail::Sinatra` in a `Sinatra::Application` application" do - should "sets the `user_for_paper_trail` from the `current_user` method" do - get "/test" - assert_equal "Hai", last_response.body - widget = Widget.last - assert_not_nil widget - assert_equal "bar", widget.name - assert_equal 1, widget.versions.size - assert_equal "raboof", widget.versions.first.whodunnit - end - end - end -end