Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solidus static content content provider #12

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ end
# Supported content providers
gem 'contentful'
gem 'prismic.io', require: 'prismic'
gem 'solidus_static_content', github: 'solidusio-contrib/solidus_static_content'

gemspec

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ body: My first post!

_NOTE: Absolute paths are taken as they are and won't be joined to `Rails.root`._

### Solidus static content

To retrieve the page we have to pass the page `slug` to the entry options.
If the page slug is the same of the entry one, we can avoid passing the options.

```rb
posts = SolidusContent::EntryType.create(
name: 'posts',
content_provider_name: 'solidus_static_content'
)

entry = SolidusContent::Entry.create!(
slug: '2020-03-27-hello-world',
entry_type: posts,
options: { slug: 'XXX' } # Can be omitted if the page slug is the same of the entry
)
```

### Contentful

To fetch the data we have to create a connection with Contentful passing the
Expand Down
1 change: 1 addition & 0 deletions lib/solidus_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ module ContentProviders
require 'solidus_content/content_providers/yaml'
require 'solidus_content/content_providers/contentful'
require 'solidus_content/content_providers/prismic'
require 'solidus_content/content_providers/solidus_static_content'
11 changes: 11 additions & 0 deletions lib/solidus_content/content_providers/solidus_static_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module SolidusContent::ContentProviders::SolidusStaticContent
def self.call(input)
slug = input.dig(:options, :slug) || input[:slug]

input.merge(data: Spree::Page.find_by!(slug: slug).attributes.symbolize_keys)
end

SolidusContent.config.content_providers[:solidus_static_content] = self
end
9 changes: 8 additions & 1 deletion spec/models/solidus_content/entry_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@
it_behaves_like :content_provider
end

context 'with the solidus static content provider' do
let(:content_provider_name) { 'solidus_static_content' }
let(:content) { { foo: 'bar' } }

it_behaves_like :content_provider
end

context 'with the Prismic provider' do
let(:content_provider_name) { 'prismic' }
let(:content) { { foo: "bar" } }
let(:content) { { foo: 'bar' } }

it_behaves_like :content_provider
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SolidusContent::ContentProviders::SolidusStaticContent do
let(:page) { create(:page) }

context 'passing the slug in the options' do
it 'returns static content data' do
expect(
described_class.call(slug: page.slug)[:data]
).to eq(page.reload.attributes.symbolize_keys)
end
end

context 'using the entry slug' do
it 'returns static content data' do
expect(
described_class.call(
slug: page.slug,
options: {
slug: page.slug
}
)[:data]
).to eq(page.reload.attributes.symbolize_keys)
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Requires factories defined in lib/solidus_content/factories.rb
require 'solidus_content/factories'
require 'solidus_static_content/factories'

RSpec.configure do |config|
config.infer_spec_type_from_file_location!
Expand Down