Skip to content

Commit

Permalink
Rewrite Current and "class attributes" to remove AS dep
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Apr 21, 2024
1 parent af956e9 commit 311d67c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 37 deletions.
4 changes: 1 addition & 3 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ def require_all(path)
require "bridgetown-foundation"

# 3rd party
require "active_support"
require "active_support/core_ext/class/attribute"
require "active_support" # TODO: remove by the end of 2024
require "active_support/core_ext/object/blank"
require "active_support/core_ext/string/inflections"
require "active_support/core_ext/string/output_safety"
require "active_support/current_attributes"
require "addressable/uri"
require "liquid"
require "listen"
Expand Down
3 changes: 3 additions & 0 deletions bridgetown-core/lib/bridgetown-core/commands/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def console # rubocop:disable Metrics
workspace = IRB::WorkSpace.new
workspace.main.define_singleton_method(:site) { Bridgetown::Current.site }
workspace.main.define_singleton_method(:collections) { site.collections }
workspace.main.define_singleton_method(:helpers) do
Bridgetown::RubyTemplateView::Helpers.new
end
irb = IRB::Irb.new(workspace)
IRB.conf[:IRB_RC]&.call(irb.context)
IRB.conf[:MAIN_CONTEXT] = irb.context
Expand Down
4 changes: 1 addition & 3 deletions bridgetown-core/lib/bridgetown-core/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ def _renderer
end

def helpers
@helpers ||= Bridgetown::RubyTemplateView::Helpers.new(
self, view_context&.site || Bridgetown::Current.site
)
@helpers ||= Bridgetown::RubyTemplateView::Helpers.new(self, view_context&.site)
end

def method_missing(method, ...)
Expand Down
12 changes: 11 additions & 1 deletion bridgetown-core/lib/bridgetown-core/concerns/prioritizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ def priority(priority = nil)
def <=>(other)
priorities[other.priority] <=> priorities[priority]
end

# Return either this class' priorities or the superclass which has them (if any)
def priorities
return @priorities if @priorities

superclass.priorities if superclass.respond_to?(:priorities)
end

def priorities=(val)
@priorities = val
end
end

def self.included(klass)
klass.extend ClassMethods
klass.class_attribute :priorities, instance_accessor: false
end

# Spaceship is priority [higher -> lower]
Expand Down
36 changes: 19 additions & 17 deletions bridgetown-core/lib/bridgetown-core/current.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# frozen_string_literal: true

Thread.attr_accessor :bridgetown_state

module Bridgetown
class Current < ActiveSupport::CurrentAttributes
# @!method self.preloaded_configuration
# @return [Bridgetown::Configuration]
attribute :preloaded_configuration
class Current
class << self
def thread_state = Thread.current.bridgetown_state ||= {}

# @return [Bridgetown::Site, nil]
def self.site
sites[:main]
end
# @return [Bridgetown::Site, nil]
def site = sites[:main]

def self.site=(new_site)
sites[:main] = new_site
end
def site=(new_site)
sites[:main] = new_site
end

# @!method self.sites
# @return [Hash<Symbol, Bridgetown::Site>]
# @return [Hash<Symbol, Bridgetown::Site>]
def sites
thread_state[:sites] ||= {}
end

attribute :sites
# @return [Bridgetown::Configuration]
def preloaded_configuration = thread_state[:preloaded_configuration]

def initialize
super
@attributes[:sites] = {}
def preloaded_configuration=(value)
thread_state[:preloaded_configuration] = value
end
end
end
end
16 changes: 6 additions & 10 deletions bridgetown-core/lib/bridgetown-core/filters/from_liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ module Filters
module FromLiquid
extend Liquid::StandardFilters

def strip_html(input)
FromLiquid.strip_html(input)
end
def strip_html(...) = FromLiquid.strip_html(...)

def strip_newlines(input)
FromLiquid.strip_newlines(input)
end
def strip_newlines(...) = FromLiquid.strip_newlines(...)

def newline_to_br(input)
FromLiquid.newline_to_br(input)
end
def newline_to_br(...) = FromLiquid.newline_to_br(...)

# FYI, truncate and truncate words are already provided by ActiveSupport! =)
def truncate(...) = FromLiquid.truncate(...)

def truncate_words(...) = FromLiquid.truncatewords(...)
end
end
end
4 changes: 2 additions & 2 deletions bridgetown-core/lib/bridgetown-core/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Helpers

# @param view [Bridgetown::RubyTemplateView]
# @param site [Bridgetown::Site]
def initialize(view, site)
def initialize(view = nil, site = nil)
@view = view
@site = site
@site = site || Bridgetown::Current.site

# duck typing for Liquid context
@context = Context.new({ site: })
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-website/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ PATH
erubi (~> 1.9)
faraday (~> 2.0)
faraday-follow_redirects (~> 0.3)
hash_with_dot_access (~> 2.0)
i18n (~> 1.0)
kramdown (~> 2.1)
kramdown-parser-gfm (~> 1.0)
Expand All @@ -39,6 +38,7 @@ PATH
remote: ../bridgetown-foundation
specs:
bridgetown-foundation (1.3.4)
hash_with_dot_access (~> 2.0)
zeitwerk (~> 2.5)

PATH
Expand Down

0 comments on commit 311d67c

Please sign in to comment.