Skip to content

Commit

Permalink
Remove Active Model, AS callbacks, spurious reference to indifferent …
Browse files Browse the repository at this point in the history
…access
  • Loading branch information
jaredcwhite committed Apr 21, 2024
1 parent 311d67c commit 5d22e35
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 37 deletions.
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions bridgetown-builder/lib/bridgetown-builder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions bridgetown-builder/test/test_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion bridgetown-core/bridgetown-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions bridgetown-core/lib/bridgetown-core/commands/esbuild.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
19 changes: 6 additions & 13 deletions bridgetown-core/lib/bridgetown-core/model/base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions bridgetown-core/lib/roda/plugins/bridgetown_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/test/ssr/server/routes/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions bridgetown-website/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d22e35

Please sign in to comment.