Skip to content

Commit

Permalink
chore: extend sitemap builder instead of adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Nov 25, 2024
1 parent cd83de9 commit 05036de
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ SiteMaps.generate(config_file: "config/sitemap.rb")

## Mixins

You can use mixins to extend the sitemap builder with additional methods. The mixins can be used to define common methods that will be used in multiple processes. Make sure they are thread-safe, otherwise I recommend to define them in the process block.
You can use mixins to extend the sitemap builder with additional methods. The mixins can be used to define common methods that will be used in multiple processes.

```ruby
module MyMixin
Expand All @@ -610,10 +610,10 @@ module MyMixin
end

SiteMaps.use(:file_system) do
include_module(MyMixin)
extend_processes_with(MyMixin)
process do |s|
repository.posts.each do |post|
s.add(post_path(post), priority: 0.8)
s.repository.posts.each do |post|
s.add(s.post_path(post), priority: 0.8)
end
end
end
Expand All @@ -622,11 +622,9 @@ end
We already have a built-in mixin for Rails applications that provides the url helpers through the `route` method.

```ruby
SiteMaps.use(:file_system) do
include_module(SiteMaps::Mixins::Rails)
process do |s|
s.add(route.root_path, priority: 1.0)
s.add(route.about_path, priority: 0.9)
s.add(s.route.root_path, priority: 1.0)
s.add(s.route.about_path, priority: 0.9)
end
end
```
Expand Down
7 changes: 4 additions & 3 deletions lib/site_maps/adapters/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ def config_class
end

def_delegators :config, :fetch_sitemap_index_links
attr_reader :sitemap_index, :processes
attr_reader :sitemap_index, :processes, :process_mixins

def initialize(**options, &block)
@config = SiteMaps.config.becomes(self.class.config_class, **options)
@processes = Concurrent::Hash.new
@process_mixins = Concurrent::Array.new
reset!
instance_exec(&block) if block
end
Expand Down Expand Up @@ -68,8 +69,8 @@ def repo
@repo ||= SiteMaps::AtomicRepository.new(config.url)
end

def include_module(mod)
extend(mod)
def extend_processes_with(mod)
@process_mixins << mod
end

def reset!
Expand Down
1 change: 1 addition & 0 deletions lib/site_maps/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def run
location: process.location(**kwargs),
notification_payload: { process: process }
)
adapter.process_mixins.each { |mixin| builder.extend(mixin) }
process.call(builder, **kwargs)
builder.finalize!
end
Expand Down
6 changes: 3 additions & 3 deletions spec/site_maps/adapters/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
end
end

describe "#include_module" do
describe "#extend_processes_with" do
let(:adapter) { described_class.new }
let(:mod) do
Module.new do
Expand All @@ -208,8 +208,8 @@ def foo
end

it "includes the module" do
adapter.include_module(mod)
expect(adapter).to respond_to(:foo)
adapter.extend_processes_with(mod)
expect(adapter.process_mixins).to include(mod)
end
end
end
31 changes: 31 additions & 0 deletions spec/site_maps/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,37 @@
)
end
end

context "when the adapter has a process mixin" do
let(:adapter) do
SiteMaps.use(:noop) do
config.url = "https://example.com/sitemap.xml"
extend_processes_with(Module.new do
def add_post(year:)
add("/posts/#{year}/index.html")
end
end)

process(:posts, "posts/%{year}/sitemap.xml", year: 2024) do |s, year:|
s.add_post(year: year)
end
end
end

it "extends the process with the mixin" do
runner.enqueue(:posts, year: 2024)

process = adapter.processes[:posts]
allow(process).to receive(:call).and_call_original

expect { runner.run }.not_to raise_error

expect(process).to have_received(:call).with(
an_instance_of(SiteMaps::SitemapBuilder),
year: 2024
)
end
end
end

describe "#preload_sitemap_index_links?" do
Expand Down

0 comments on commit 05036de

Please sign in to comment.