Skip to content

Commit

Permalink
Merge pull request #3 from Paradem/tweaks
Browse files Browse the repository at this point in the history
Bridgetown Builders
  • Loading branch information
psguazz authored Mar 13, 2024
2 parents e55f3c6 + a5dbcd5 commit fc813bc
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 73 deletions.
31 changes: 20 additions & 11 deletions lib/dato_cms_graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@
require_relative "dato_cms_graphql/version"
require_relative "dato_cms_graphql/fields"
require_relative "dato_cms_graphql/base_query"
require_relative "dato_cms_graphql/rails"
require_relative "dato_cms_graphql/rails/routing"
require_relative "dato_cms_graphql/rails/persistence"
require_relative "dato_cms_graphql/rails/cache_table"

require_relative "test_schema"

if defined?(::Rails)
require_relative "dato_cms_graphql/rails/routing"
require_relative "dato_cms_graphql/rails/persistence"
require_relative "dato_cms_graphql/rails/cache_table"
end

if defined?(::Bridgetown)
require_relative "dato_cms_graphql/bridgetown/query_builder"
require_relative "dato_cms_graphql/bridgetown/initializer"
end

module DatoCmsGraphql
class Error < StandardError; end

Expand Down Expand Up @@ -49,14 +56,16 @@ def self.count(query, variables: {})
end

def self.queries
raise "DatoCmsGraphql.path_to_queries has not been set with the path to your queries" if @path_to_queries.nil?
raise "\"#{@path_to_queries}\" does not exist" unless File.exist?(@path_to_queries)
@queries ||= begin
raise "DatoCmsGraphql.path_to_queries has not been set with the path to your queries" if @path_to_queries.nil?
raise "\"#{@path_to_queries}\" does not exist" unless File.exist?(@path_to_queries)

Dir[File.join(@path_to_queries, "*.rb")].sort.each { require(_1) }
ObjectSpace.each_object(::Class)
.select { |klass| klass < DatoCmsGraphql::BaseQuery }
.group_by(&:name).values.map { |values| values.max_by(&:object_id) }
.flatten
Dir[File.join(@path_to_queries, "*.rb")].sort.each { require(_1) }
ObjectSpace.each_object(::Class)
.select { |klass| klass < DatoCmsGraphql::BaseQuery }
.group_by(&:name).values.map { |values| values.max_by(&:object_id) }
.flatten
end
end

def self.path_to_queries=(value)
Expand Down
3 changes: 3 additions & 0 deletions lib/dato_cms_graphql/bridgetown/initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
::Bridgetown.initializer :dato_cms_graphql do |config|
config.builder DatoCmsGraphql::Bridgetown::Builder
end
30 changes: 30 additions & 0 deletions lib/dato_cms_graphql/bridgetown/query_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module DatoCmsGraphql::Bridgetown
class Builder < ::Bridgetown::Builder
def build
queries = DatoCmsGraphql.queries

hook :site, :post_read do
queries.each do |query|
if query.single_instance?
site.data[query.single_name.underscore] = query.get
else
results = query.all
site.data[query.plural_name.underscore] = results

if query.render?
results.each do |item|
add_resource query.plural_name.underscore, "#{item.id}.erb" do
result item
permalink "#{item.permalink}/"
title item.title
layout query.single_name.underscore
content ""
end
end
end
end
end
end
end
end
end
2 changes: 0 additions & 2 deletions lib/dato_cms_graphql/rails.rb

This file was deleted.

48 changes: 25 additions & 23 deletions lib/dato_cms_graphql/rails/cache_table.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
require "active_support/concern"

module DatoCmsGraphql::Rails::CacheTable
extend ActiveSupport::Concern
module DatoCmsGraphql::Rails
module CacheTable
extend ActiveSupport::Concern

included do
enum :locale, I18n.available_locales
end
included do
enum :locale, I18n.available_locales
end

class_methods do
def allow?(type, request)
permalink = request.params["permalink"]
exists?(type: type, render: true, permalink: permalink)
class_methods do
def allow?(type, request)
permalink = request.params["permalink"]
exists?(type: type, render: true, permalink: permalink)
end
end
end

def cms_record
@parsed ||= begin
data = read_attribute(:cms_record)
JSON.parse(data.to_json, object_class: OpenStruct)
def cms_record
@parsed ||= begin
data = read_attribute(:cms_record)
JSON.parse(data.to_json, object_class: OpenStruct)
end
end
end

def respond_to_missing?(method, *args)
cms_record.respond_to?(method.to_s, *args)
end
def respond_to_missing?(method, *args)
cms_record.respond_to?(method.to_s, *args)
end

def method_missing(method, *a, &block)
if cms_record.respond_to?(method.to_s)
cms_record.send(method, *a, &block)
else
super
def method_missing(method, *a, &block)
if cms_record.respond_to?(method.to_s)
cms_record.send(method, *a, &block)
else
super
end
end
end
end
46 changes: 24 additions & 22 deletions lib/dato_cms_graphql/rails/persistence.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
module DatoCmsGraphql::Rails::Persistence
def self.persist_record(query, record)
Object.const_get(query.query_name)
.find_or_create_by(
locale: I18n.locale,
cms_id: record.id
)
.update(
render: query.render?,
permalink: (record.permalink if record.respond_to?(:permalink)),
cms_record: record.localized_raw_attributes
)
end
module DatoCmsGraphql::Rails
module Persistence
def self.persist_record(query, record)
Object.const_get(query.query_name)
.find_or_create_by(
locale: I18n.locale,
cms_id: record.id
)
.update(
render: query.render?,
permalink: (record.permalink if record.respond_to?(:permalink)),
cms_record: record.localized_raw_attributes
)
end

def self.cache_data
DatoCmsGraphql.queries.each do |query|
I18n.available_locales.each do |locale|
I18n.with_locale(locale) do
if query.single_instance?
record = query.get
persist_record(query, record)
else
query.all.each do |record|
def self.cache_data
DatoCmsGraphql.queries.each do |query|
I18n.available_locales.each do |locale|
I18n.with_locale(locale) do
if query.single_instance?
record = query.get
persist_record(query, record)
else
query.all.each do |record|
persist_record(query, record)
end
end
end
end
Expand Down
32 changes: 17 additions & 15 deletions lib/dato_cms_graphql/rails/routing.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module DatoCmsGraphql::Rails::Routing
def self.draw_routes(base_class)
::Rails.application.routes.draw do
scope "(:locale)", locale: I18n.default_locale do
# Queries that represent collections of pages.
DatoCmsGraphql.renderable.each do |query|
controller = query.plural_name.underscore
module DatoCmsGraphql::Rails
module Routing
def self.draw_routes(base_class)
::Rails.application.routes.draw do
scope "(:locale)", locale: I18n.default_locale do
# Queries that represent collections of pages.
DatoCmsGraphql.renderable.each do |query|
controller = query.plural_name.underscore

if query.route.blank?
root "#{controller}#show"
else
get(
query.route,
to: "#{controller}#show",
constraints: lambda { |request| base_class.allow?(query.query_name, request) }
)
if query.route.blank?
root "#{controller}#show"
else
get(
query.route,
to: "#{controller}#show",
constraints: lambda { |request| base_class.allow?(query.query_name, request) }
)
end
end
end
end
Expand Down

0 comments on commit fc813bc

Please sign in to comment.