Git Product home page Git Product logo

webflow_sync's Introduction

Build Status

WebflowSync

Keep your Ruby on Rails records in sync with WebFlow.*

*Currently only one way Rails => WebFlow synchronization.

For the latest changes, check the CHANGELOG.md.

Installation

Add this line to your application's Gemfile:

gem 'webflow_sync'

And then execute:

$ bundle

Then run the install generator:

bundle exec rails generate webflow_sync:install

Usage

Generate and set WebFlow API token

The easiest way to generate API v2 token is to clone the official webflow-app-starter-v2 repo and follow the instructions in their README.md.

Configuration options

In config/initializers/webflow_sync.rb you can specify configuration options:

  1. api_token

  2. webflow_site_id

  3. skip_webflow_sync - skip synchronization for different environments.

  4. sync_webflow_slug - save slug generated on WebFlow to the Rails model, webflow_slug column.

    This can be useful if you want to link to WebFlow item directly from your Rails app:

    link_to('View on site', "https://#{webflow_domain}/articles/#{record.webflow_slug}", target: :blank)

    To save slug generated on WebFlow in Rails model, webflow_slug column:

    1. add webflow_slug column on the model table, then
    2. set the sync_webflow_slug option to true.

Example:

WebflowSync.configure do |config|
  config.skip_webflow_sync = ActiveModel::Type::Boolean.new.cast(ENV.fetch('SKIP_WEBFLOW_SYNC'))
  config.sync_webflow_slug = ActiveModel::Type::Boolean.new.cast(ENV.fetch('SYNC_WEBFLOW_SLUG'))
end

Add WebflowSync to models

For each model that you want to sync to WebFlow, you need to run the collection generator:

bundle exec rails generate webflow_sync:collection Article

Please note that this does not create a collection in WebFlow. You need to create collections in Webflow manually.

Create WebFlow collections

As mentioned above, you need to create the WebFlow collection yourself.

Make sure that the collection slug matches the Rails model collection name (the output of model_class.model_name.to_s.underscore.dasherize.pluralize).

For example, for Article model:

> Article.model_name.to_s.underscore.dasherize.pluralize
# => "articles"

Your WebFlow collection slug should be "articles".

For Rails models named with multiple words, make a collection that have a space between words in the name. WebFlow will set the slug by replacing space for "-".

For example, for FeaturedArticle model in your Rails app, in Webflow you'll create a collection called "Featured Articles":

> FeaturedArticle.model_name.to_s.underscore.dasherize.pluralize
# => "featured-articles"

Your WebFlow collection slug in this case should be "featured-articles".

Set webflow_site_id

There are couple of ways how you can set the webflow_site_id to be used.

Set webflow_site_id through configuration

In config/initializers/webflow_sync.rb you can specify webflow_site_id:

WebflowSync.configure do |config|
  config.webflow_site_id = ENV.fetch('WEBFLOW_SITE_ID')
end

Set webflow_site_id for each model individually

You can set webflow_site_id per model, or even per record.

To do this, override the #webflow_site_id method provided by WebflowSync::ItemSync in your ActiveRecord model.

For example, you could have Site model in your codebase:

# app/models/webflow_site.rb
class WebflowSite < ApplicationRecord
  has_many :articles
end

# app/models/article.rb
class Article < ApplicationRecord
  include WebflowSync::ItemSync

  belongs_to :webflow_site

  def webflow_site_id
    self.webflow_site.webflow_site_id
  end
end

Customize fields to synchronize

By default, WebflowSync calls #as_webflow_json on a record to get the fields that it needs to push to WebFlow. #as_webflow_json simply calls #as_json in its default implementation. To change this behavior, you can override #as_json in your model:

# app/models/article.rb
class Article < ApplicationRecord
  include WebflowSync::ItemSync

  def as_json
    {
      title: self.title.capitalize,
      slug: self.title.parameterize,
      published_at: self.created_at,
      image: self.image_url
    }
  end
end

Or if you already use #as_json for some other use-case and cannot modify it, you can also override #as_webflow_json method. Here's the default #as_webflow_json implementation (you don't need to add this to your model):

# app/models/article.rb
class Article < ApplicationRecord
  include WebflowSync::ItemSync

  def as_webflow_json
    self.as_json
  end
end

Sync a Rails model with the custom collection

If collection slug in Webflow does not match the Rails model collection name, you can override webflow_collection_slug method in your Rails model. If you want to call WebflowSync::CreateItemJob, WebflowSync::UpdateItemJob, or WebflowSync::DestroyItemJob directly in your code, you can do so by passing the collection_slug as an argument.

Here are method signatures:

WebflowSync::CreateItemJob.perform_later(model_name, id, collection_slug)
WebflowSync::UpdateItemJob.perform_later(model_name, id, collection_slug)
WebflowSync::DestroyItemJob.perform_later(collection_slug:, webflow_site_id:, webflow_item_id:)

Where:

  1. model_name - Rails model that has webflow_site_id and webflow_item_id defined
  2. collection_slug - slug of the WebFlow collection (defaults to: model_name.underscore.dasherize.pluralize)

For example:

WebflowSync::CreateItemJob.perform_now('articles', 1, 'stories')

Or, if you want to use the default 'articles' collection_slug:

WebflowSync::CreateItemJob.perform_now('articles', 1)

Run the initial sync

After setting up which models you want to sync to WebFlow, you can run the initial sync for each of the models:

WebflowSync::InitialSyncJob.perform_later('articles')

You can also run this from a Rake task:

bundle exec rails "webflow_sync:initial_sync[articles]"

*Quotes are needed in order for this to work in all shells.

Important note

This gem silently "fails" (does nothing) when webflow_site_id or webflow_item_id is nil! This is not always desired behavior so be aware of that.

Contributing

PRs welcome!

To run RuboCop style check and RSpec tests run:

bundle exec rake

Thanks and Credits

Thank you to @holden and @aedificator-nl for contributing to this gem.

This gem wouldn't be possible without the amazing work of webflow-ruby gem. Thank you, @phoet!

This gem depends on the webflow-rb gem.

License

The gem is available as open source under the terms of the MIT License.

webflow_sync's People

Contributors

aedificator-nl avatar dependabot[bot] avatar stankovicjovana avatar vfonic avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

webflow_sync's Issues

Error performing WebflowSync::CreateItemJob -> NameError (uninitialized constant WebflowSync::Api):

Hi,

I am trying to get webflow_sync working, however I get a NameError for WebflowSync::Api when the CreateItemJob is run from Sidekiq.

If I run the job from the Rails console I get the same error. If I then type WebflowSync::API I get a Zeitwerk::NameError as the api.rb does not contain a class named API (logically). This however does result in Api being loaded and attempting the CreateItemJob again now works.

I am not sure why the Rails autoloader is not picking up the file correctly, so unfortunately don't have a suggested solution.

Kind regards,

Steven

InitialSyncJob always uses `find_collection_id` from API instead of calling `webflow_collection_id` on model

The InitialSyncJob always uses the find_collection_id API method (initial_sync_job.rb:10) instead of calling webflow_collection_id on the appropriate model.

It would be nice if the InitialSyncJob would call the model for the webflow_collection_id, so any overwrite can be used, for example when the collection_id is set directly, without using the slug.

I wanted to create a PR for this, couldn't get the bundle working as I had errors with making nio4r. Will create a PR once fixed, but for now adding an issue as well.

uninitialized constant Webflow::Error

Hi, I've just tried starting to use your gem and I got stuck on an uninitialized error, it appears after the item is push into the job queue, so it shows up in sidekiq and I'm not able to debug the underlying error.

Rails 7.0.0
Ruby: 3.1.2
Sidekiq: 6.5.6

Any ideas?

Thanks

worker | 2022-09-21T10:23:33.103Z pid=65995 tid=h9z class=WebflowSync::UpdateItemJob jid=a89cf313155042c112cdb95b elapsed=0.912 INFO: fail
worker | 2022-09-21T10:23:33.103Z pid=65995 tid=h9z WARN: {"context":"Job raised exception","job":{"retry":true,"queue":"default","class":"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper","wrapped":"WebflowSync::UpdateItemJob","args":[{"job_class":"WebflowSync::UpdateItemJob","job_id":"e7b1059d-94ed-4a4b-b5ea-af9b22b1dc67","provider_job_id":null,"queue_name":"default","priority":null,"arguments":["adventures",32],"executions":0,"exception_executions":{},"locale":"en","timezone":"Europe/Warsaw","enqueued_at":"2022-09-21T10:23:32Z"}],"jid":"a89cf313155042c112cdb95b","created_at":1663755812.190053,"enqueued_at":1663755812.190326}}
worker | 2022-09-21T10:15:49.101Z pid=57343 tid=25pn WARN: NameError: uninitialized constant Webflow::Error
worker | 
worker |       rescue Webflow::Error => e
worker |                     ^^^^^^^
worker | Did you mean?  IOError
worker |                Errno
/Users/holdenthomas/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/webflow_sync-b4b0d071d677/app/services/webflow_sync/api.rb:139:in `rescue in make_request': uninitialized constant Webflow::Error (NameError)

      rescue Webflow::Error => e
                    ^^^^^^^
Did you mean?  IOError
               Errno
/Users/holdenthomas/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/webflow-ruby-0.7.0/lib/webflow/client.rb:87:in `update_item': wrong number of arguments (given 3, expected 2) (ArgumentError)

Are you using WebflowSync?

Write us how you're using WebflowSync and any issues you've encountered so we can keep those in mind as we add new features.

Simply post your project name and let us know how you are using WebflowSync.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.