From 5d22e35cdb09c3e535860e9ee3d732d2f633e901 Mon Sep 17 00:00:00 2001 From: Jared White Date: Sat, 20 Apr 2024 23:19:33 -0700 Subject: [PATCH] Remove Active Model, AS callbacks, spurious reference to indifferent access --- Gemfile.lock | 3 --- .../lib/bridgetown-builder/builder.rb | 22 ++++++++++--------- bridgetown-builder/test/test_hooks.rb | 16 ++++++++++++++ bridgetown-core/bridgetown-core.gemspec | 1 - .../lib/bridgetown-core/commands/esbuild.rb | 6 ++--- .../lib/bridgetown-core/model/base.rb | 19 +++++----------- .../lib/roda/plugins/bridgetown_server.rb | 5 ++--- .../test/ssr/server/routes/cookies.rb | 2 +- bridgetown-website/Gemfile.lock | 3 --- 9 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 02acabf50..f310ffe50 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,7 +8,6 @@ PATH remote: bridgetown-core specs: bridgetown-core (1.3.4) - activemodel (>= 6.0, < 8.0) activesupport (>= 6.0, < 8.0) addressable (~> 2.4) amazing_print (~> 1.2) @@ -58,8 +57,6 @@ PATH GEM remote: https://rubygems.org/ specs: - activemodel (7.1.3.2) - activesupport (= 7.1.3.2) activesupport (7.1.3.2) base64 bigdecimal diff --git a/bridgetown-builder/lib/bridgetown-builder/builder.rb b/bridgetown-builder/lib/bridgetown-builder/builder.rb index 7ee1a38d7..6519a9172 100644 --- a/bridgetown-builder/lib/bridgetown-builder/builder.rb +++ b/bridgetown-builder/lib/bridgetown-builder/builder.rb @@ -3,31 +3,33 @@ module Bridgetown # Superclass for a website's SiteBuilder abstract class class Builder < Bridgetown::Builders::PluginBuilder - extend ActiveSupport::DescendantsTracker - include ActiveSupport::Callbacks - - define_callbacks :build - class << self def register Bridgetown::Builders::PluginBuilder.plugin_registrations << self end def before_build(...) - set_callback(:build, :before, ...) + add_callback(:before, ...) end def after_build(...) - set_callback(:build, :after, ...) + add_callback(:after, ...) + end + + def callbacks + @callbacks ||= {} end - def around_build(...) - set_callback(:build, :around, ...) + def add_callback(name, method_name = nil, &block) + callbacks[name] ||= [] + callbacks[name] << (block || proc { send(method_name) }) end end def build_with_callbacks - run_callbacks(:build) { build } + self.class.callbacks[:before]&.each { instance_exec(&_1) } + build + self.class.callbacks[:after]&.each { instance_exec(&_1) } self end diff --git a/bridgetown-builder/test/test_hooks.rb b/bridgetown-builder/test/test_hooks.rb index 253086e21..299b4507a 100644 --- a/bridgetown-builder/test/test_hooks.rb +++ b/bridgetown-builder/test/test_hooks.rb @@ -18,9 +18,23 @@ class SiteBuilder < Builder end class SubclassOfSiteBuilder < SiteBuilder + class << self + attr_accessor :final_value + end + + after_build :run_after + def build site.config[:site_builder_subclass_loaded] = true end + + def run_after + self.class.final_value = [@initial_value, :goodbye] + end +end + +SubclassOfSiteBuilder.before_build do + @initial_value = :hello end class TestHooks < BridgetownUnitTest @@ -50,9 +64,11 @@ class TestHooks < BridgetownUnitTest @site.reset @site.loaders_manager.unload_loaders @site.setup + SubclassOfSiteBuilder.final_value = nil Bridgetown::Hooks.trigger :site, :pre_read, @site assert @site.config[:site_builder_subclass_loaded] + assert_equal [:hello, :goodbye], SiteBuilder.subclasses.first.final_value end end end diff --git a/bridgetown-core/bridgetown-core.gemspec b/bridgetown-core/bridgetown-core.gemspec index b7c4158cd..5a565c3b9 100644 --- a/bridgetown-core/bridgetown-core.gemspec +++ b/bridgetown-core/bridgetown-core.gemspec @@ -31,7 +31,6 @@ Gem::Specification.new do |s| s.required_ruby_version = ">= 3.1.0" - s.add_runtime_dependency("activemodel", [">= 6.0", "< 8.0"]) s.add_runtime_dependency("activesupport", [">= 6.0", "< 8.0"]) s.add_runtime_dependency("addressable", "~> 2.4") s.add_runtime_dependency("amazing_print", "~> 1.2") diff --git a/bridgetown-core/lib/bridgetown-core/commands/esbuild.rb b/bridgetown-core/lib/bridgetown-core/commands/esbuild.rb index 579bacf29..f47b01e57 100644 --- a/bridgetown-core/lib/bridgetown-core/commands/esbuild.rb +++ b/bridgetown-core/lib/bridgetown-core/commands/esbuild.rb @@ -24,7 +24,7 @@ def esbuild return show_actions if args.empty? action = args.first - if supported_actions.include?(action) + if supported_actions.include?(action.to_sym) perform action else @logger.error "Error:".red, "🚨 Please enter a valid action." @@ -66,7 +66,7 @@ def show_actions longest_action = supported_actions.keys.max_by(&:size).size supported_actions.each do |action, description| - say "#{action.ljust(longest_action).to_s.bold.blue}\t# #{description}" + say "#{action.to_s.ljust(longest_action).bold.blue}\t# #{description}" end end @@ -76,7 +76,7 @@ def supported_actions update: "Updates the Bridgetown esbuild defaults to the latest available version", "migrate-from-webpack": "Removes Webpack from your project and installs/configures esbuild", - }.with_indifferent_access + } end end end diff --git a/bridgetown-core/lib/bridgetown-core/model/base.rb b/bridgetown-core/lib/bridgetown-core/model/base.rb index fa3a164d0..ad7edd728 100644 --- a/bridgetown-core/lib/bridgetown-core/model/base.rb +++ b/bridgetown-core/lib/bridgetown-core/model/base.rb @@ -1,14 +1,8 @@ # frozen_string_literal: true -require "active_model" - module Bridgetown module Model class Base - include ActiveModel::Model - extend ActiveModel::Callbacks - define_model_callbacks :load, :save, :destroy - class << self def find(id, site: Bridgetown::Current.site) raise "A Bridgetown site must be initialized and added to Current" unless site @@ -54,9 +48,7 @@ def build(builder, collection_name, path, data) end def initialize(attributes = {}) - run_callbacks :load do - super - end + self.attributes = attributes end def id @@ -82,9 +74,7 @@ def save raise "`#{origin.class}' doesn't allow writing of model objects" end - run_callbacks :save do - origin.write(self) - end + origin.write(self) end # @return [Bridgetown::Resource::Base] @@ -130,6 +120,10 @@ def attributes @attributes ||= HashWithDotAccess::Hash.new end + def attributes=(new_attributes) + attributes.update new_attributes + end + # Strip out keys like _origin_, _collection_, etc. # @return [HashWithDotAccess::Hash] def data_attributes @@ -146,7 +140,6 @@ def method_missing(method_name, *args) key = method_name.to_s if key.end_with?("=") key.chop! - # attribute_will_change!(key) attributes[key] = args.first return attributes[key] end diff --git a/bridgetown-core/lib/roda/plugins/bridgetown_server.rb b/bridgetown-core/lib/roda/plugins/bridgetown_server.rb index 8abc2e7df..222c30898 100644 --- a/bridgetown-core/lib/roda/plugins/bridgetown_server.rb +++ b/bridgetown-core/lib/roda/plugins/bridgetown_server.rb @@ -132,10 +132,9 @@ def self.css module RequestMethods # Monkeypatch Roda/Rack's Request object so it returns a hash which allows for - # indifferent access + # symbol or dot access def cookies - # TODO: maybe replace with a simpler hash that offers an overloaded `[]` method - _previous_roda_cookies.with_indifferent_access + HashWithDotAccess::Hash.new(_previous_roda_cookies) end # Starts up the Bridgetown routing system diff --git a/bridgetown-core/test/ssr/server/routes/cookies.rb b/bridgetown-core/test/ssr/server/routes/cookies.rb index 9da5d964c..fbac73356 100644 --- a/bridgetown-core/test/ssr/server/routes/cookies.rb +++ b/bridgetown-core/test/ssr/server/routes/cookies.rb @@ -4,7 +4,7 @@ class Routes::Cookies < Bridgetown::Rack::Routes route do |r| # route: GET /cookies r.get "cookies" do - { value: r.cookies[:test_key] } + { value: r.cookies.test_key } end # route: POST /cookies diff --git a/bridgetown-website/Gemfile.lock b/bridgetown-website/Gemfile.lock index 9a6d9de52..0abda2ee2 100644 --- a/bridgetown-website/Gemfile.lock +++ b/bridgetown-website/Gemfile.lock @@ -8,7 +8,6 @@ PATH remote: ../bridgetown-core specs: bridgetown-core (1.3.4) - activemodel (>= 6.0, < 8.0) activesupport (>= 6.0, < 8.0) addressable (~> 2.4) amazing_print (~> 1.2) @@ -58,8 +57,6 @@ PATH GEM remote: https://rubygems.org/ specs: - activemodel (7.1.3.2) - activesupport (= 7.1.3.2) activesupport (7.1.3.2) base64 bigdecimal