Skip to content

Commit

Permalink
Added a find by slug method to FinderLoader
Browse files Browse the repository at this point in the history
Filename and slug are wildly different in specialist document schemas. In order to make running rake tasks and maintanence as easy as we can, it's better to use slug since we can see it directly on the website and user queries.
  • Loading branch information
minhngocd committed Jun 6, 2023
1 parent 174785c commit b9f84c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 13 additions & 0 deletions app/lib/finder_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def finder(name)
end
end

def finder_by_slug(slug)
schema_file = files.find do |file|
File.foreach(file).grep(/"base_path": "\/#{slug}"/).any?
end

raise "Could not find any schema with slug: #{slug}" if schema_file.nil?

{
file: MultiJson.load(File.read(schema_file)),
timestamp: File.mtime(schema_file),
}
end

private

def files
Expand Down
31 changes: 28 additions & 3 deletions spec/lib/finder_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
require "finder_loader"

RSpec.describe FinderLoader do
before do
it "returns matching finder data objects" do
expect(File).to receive(:read).with("lib/documents/schemas/format-1.json")
.and_return('{"name":"format-1"}')
expect(File).to receive(:mtime).with("lib/documents/schemas/format-1.json")
.and_return("yesterday")
end
it "returns matching finder data objects" do

expect(Dir).to receive(:glob).with("lib/documents/schemas/*.json").and_return(%w[
lib/documents/schemas/format-1.json
lib/documents/schemas/format-2.json
Expand All @@ -33,6 +32,11 @@
end

it "returns one matching finder data object" do
expect(File).to receive(:read).with("lib/documents/schemas/format-1.json")
.and_return('{"name":"format-1"}')
expect(File).to receive(:mtime).with("lib/documents/schemas/format-1.json")
.and_return("yesterday")

expect(File).to receive(:exist?).with("lib/documents/schemas/format-1.json")
.and_return(true)

Expand All @@ -44,4 +48,25 @@
},
])
end

it "returns one matching finder data object by slug" do
expect(Dir).to receive(:glob).with("lib/documents/schemas/*.json").and_return(%w[
spec/fixtures/documents/schemas/licence_transactions.json
])
expect(File).to receive(:mtime).with("spec/fixtures/documents/schemas/licence_transactions.json")
.and_return("today")

loaded_finder = FinderLoader.new.finder_by_slug("find-licences")
expect(loaded_finder[:file]).to include({"base_path" => "/find-licences"})
expect(loaded_finder[:timestamp]).to eq("today")
end


it "errors when no finder is found with slug" do
expect(Dir).to receive(:glob).with("lib/documents/schemas/*.json").and_return(%w[
spec/fixtures/documents/schemas/licence_transactions.json
])

expect { FinderLoader.new.finder_by_slug("test-slug") }.to raise_error("Could not find any schema with slug: test-slug")
end
end

0 comments on commit b9f84c9

Please sign in to comment.