This Rails app displays receipes stored in Contentful. You can see a quick demo here
Docker is the only requirement. This command will build the docker image, install the gems and setup the database
echo "<masterkey>" > config/master.key
make init
Alternatively, you can install Ruby 2.7.1, Postgresql, bundler, which should be:
# install rvm
rvm install 2.7.1
rvm use 2.7.1
gem install bundler
bundle install
bundle exec rake db:create db:schema:load db:migrate
This will just start the server with the database using docker-compose
make server
Visit http://localhost:3000
make test
or
make guard
Just some thoughts on why I built it this way.
I wanted to use what was already there, since I have no experience with
Contentful and what they have already built is going to be more robust
and with more features than my ApiClient™
. ActiveResource
is something
else I thought about, but I thought it was going to be too much customization.
I think it could be a good solution though, since objects might behave more like
ActiveRecord
objects.
I don't think having a database where to store the recipes locally would bring a lot. It defeats the purpose of having Contentful, and while it's nice to have a 'local' storage, the complexity grows a lot.
Then the other thing to consider was testing. Not having a database makes
testing a bit more annoying, since either you need to have VCR
and make http calls
(which I tend to avoid when possible) or mock the http calls. (You don't have anything
behind when you do Recipe.all
)
I tried to use ContentfulModel
and do Recipe.new()
to build the test objects,
but that actually throws errors. This turned out to be a quite big inconvenient,
because testing model methods gets very hard. I also tried to use FactoryBot
with OpenStruct
, so build(:recipe)
returns an OpenStruct
. That worked, but
I would still have to mock relatively a lot, and using this technique with complex objects
ends up in tests that are not so good.
So I went for VCR and recorded some of the tests. Most of the tests are the ones generated by rails, since I mostly used other libraries' code.
The whole docker setup was not necessary, but I have a sort of template for that, and I think it's nice and quick.
I used some helpers to have part of the code out of the views and so it's easier to test. I did not want to complicate myself with adding a decorator gem or similar.
I think it's possible in the Contentful gems to enable caching, but I did not dig further.
ContentfulModel
also has support for paging, which could also be added.
I had a few more commits but I had to remove them to filter the organization id from the VCR cassettes.