Skip to content

Commit

Permalink
Expose data file's basename as attribute name (#512)
Browse files Browse the repository at this point in the history
This provides `JekyllAdmin::DataFile` instances parity with primitives from Jekyll.

`Jekyll::Page`, `Jekyll::Document`, `Jekyll::StaticFile` instances exposes their *basename with extension* via Liquid and therefore the API here as either `:name` or `:basename`
  • Loading branch information
ashmaroli committed Oct 11, 2019
1 parent 7e95c80 commit 7b6fc14
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/jekyll-admin/data_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module JekyllAdmin
class DataFile
METHODS_FOR_LIQUID = %w(path relative_path slug ext title).freeze
METHODS_FOR_LIQUID = %w(name path relative_path slug ext title).freeze
EXTENSIONS = %w(yaml yml json csv).freeze

include APIable
Expand Down Expand Up @@ -94,9 +94,10 @@ def basename
end

def basename_with_extension
[basename, extension].join
"#{basename}#{extension}"
end
alias_method :filename, :basename_with_extension
alias_method :name, :basename_with_extension
public :name

def namespace
"data"
Expand Down
6 changes: 6 additions & 0 deletions spec/jekyll-admin/data_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@
expect(subject.slug).to eql("data_file")
end

it "exposes the basename_with_extension as 'name'" do
expect(subject.name).to eql("data_file.yml")
end

it "exposes the title" do
expect(subject.title).to eql("Data File")
end

it "returns the hash" do
expect(subject.to_api).to eql(
"name" => "data_file.yml",
"path" => "/_data/data_file.yml",
"relative_path" => "data_file.yml",
"slug" => "data_file",
Expand All @@ -46,6 +51,7 @@

it "returns the hash with content" do
expect(subject.to_api(:include_content => true)).to eql(
"name" => "data_file.yml",
"path" => "/_data/data_file.yml",
"relative_path" => "data_file.yml",
"slug" => "data_file",
Expand Down
10 changes: 10 additions & 0 deletions spec/jekyll-admin/server/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

let(:base_response) do
{
"name" => "data_file.yml",
"path" => "/_data/data_file.yml",
"relative_path" => "data_file.yml",
"slug" => "data_file",
Expand Down Expand Up @@ -44,6 +45,7 @@ def app
get "/data/movies"

expected_response = {
"name" => "actors.yml",
"path" => "/_data/movies/actors.yml",
"relative_path" => "movies/actors.yml",
"slug" => "actors",
Expand All @@ -61,6 +63,7 @@ def app
get "/data/movies/actors.yml"

expected_response = {
"name" => "actors.yml",
"path" => "/_data/movies/actors.yml",
"relative_path" => "movies/actors.yml",
"slug" => "actors",
Expand All @@ -82,6 +85,7 @@ def app
delete_file "_data/data-file-new.yml"

expected_response = {
"name" => "data-file-new.yml",
"path" => "/_data/data-file-new.yml",
"relative_path" => "data-file-new.yml",
"slug" => "data-file-new",
Expand Down Expand Up @@ -109,6 +113,7 @@ def app
delete_file "_data/test-dir/data-file-new.yml"

expected_response = {
"name" => "data-file-new.yml",
"path" => "/_data/test-dir/data-file-new.yml",
"relative_path" => "test-dir/data-file-new.yml",
"slug" => "data-file-new",
Expand Down Expand Up @@ -136,6 +141,7 @@ def app
delete_file "_data/data-file-new.yml"

expected_response = {
"name" => "data-file-new.yml",
"path" => "/_data/data-file-new.yml",
"relative_path" => "data-file-new.yml",
"slug" => "data-file-new",
Expand Down Expand Up @@ -163,6 +169,7 @@ def app
delete_file "_data/test-dir/data-file-new.yml"

expected_response = {
"name" => "data-file-new.yml",
"path" => "/_data/test-dir/data-file-new.yml",
"relative_path" => "test-dir/data-file-new.yml",
"slug" => "data-file-new",
Expand Down Expand Up @@ -190,6 +197,7 @@ def app
write_file "_data/data-file-update.yml", "foo2: bar2"

expected_response = {
"name" => "data-file-update.yml",
"path" => "/_data/data-file-update.yml",
"relative_path" => "data-file-update.yml",
"slug" => "data-file-update",
Expand Down Expand Up @@ -217,6 +225,7 @@ def app
write_file "_data/test-dir/data-file-update.yml", "foo2: bar2"

expected_response = {
"name" => "data-file-update.yml",
"path" => "/_data/test-dir/data-file-update.yml",
"relative_path" => "test-dir/data-file-update.yml",
"slug" => "data-file-update",
Expand Down Expand Up @@ -245,6 +254,7 @@ def app
delete_file "_data/data-file-renamed.yml"

expected_response = {
"name" => "data-file-renamed.yml",
"path" => "/_data/data-file-renamed.yml",
"relative_path" => "data-file-renamed.yml",
"slug" => "data-file-renamed",
Expand Down

0 comments on commit 7b6fc14

Please sign in to comment.