Skip to content

Commit

Permalink
Merge pull request #12 from nebulab/vassalloandrea/add-solidus-static…
Browse files Browse the repository at this point in the history
…-content-provider

Add solidus static content content provider
  • Loading branch information
elia committed Apr 3, 2020
2 parents bc507e4 + a8eb104 commit 12319b2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
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 @@ -184,6 +184,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

0 comments on commit 12319b2

Please sign in to comment.