From f3e5b467dd979dad67df257dbe289d08ebd34e96 Mon Sep 17 00:00:00 2001 From: psguazz Date: Wed, 13 Mar 2024 11:20:40 +0100 Subject: [PATCH 1/3] Explicit module definition Nesting modules lets us get rid of the extra files. --- lib/dato_cms_graphql.rb | 1 - lib/dato_cms_graphql/rails.rb | 2 - lib/dato_cms_graphql/rails/cache_table.rb | 48 ++++++++++++----------- lib/dato_cms_graphql/rails/persistence.rb | 46 +++++++++++----------- lib/dato_cms_graphql/rails/routing.rb | 32 ++++++++------- 5 files changed, 66 insertions(+), 63 deletions(-) delete mode 100644 lib/dato_cms_graphql/rails.rb diff --git a/lib/dato_cms_graphql.rb b/lib/dato_cms_graphql.rb index c39e8ac..0df963d 100644 --- a/lib/dato_cms_graphql.rb +++ b/lib/dato_cms_graphql.rb @@ -10,7 +10,6 @@ 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" diff --git a/lib/dato_cms_graphql/rails.rb b/lib/dato_cms_graphql/rails.rb deleted file mode 100644 index e842d18..0000000 --- a/lib/dato_cms_graphql/rails.rb +++ /dev/null @@ -1,2 +0,0 @@ -module DatoCmsGraphql::Rails -end diff --git a/lib/dato_cms_graphql/rails/cache_table.rb b/lib/dato_cms_graphql/rails/cache_table.rb index 887cb0c..2a4f398 100644 --- a/lib/dato_cms_graphql/rails/cache_table.rb +++ b/lib/dato_cms_graphql/rails/cache_table.rb @@ -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 diff --git a/lib/dato_cms_graphql/rails/persistence.rb b/lib/dato_cms_graphql/rails/persistence.rb index 228071f..ed78cf0 100644 --- a/lib/dato_cms_graphql/rails/persistence.rb +++ b/lib/dato_cms_graphql/rails/persistence.rb @@ -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 diff --git a/lib/dato_cms_graphql/rails/routing.rb b/lib/dato_cms_graphql/rails/routing.rb index fb20830..5f7eda1 100644 --- a/lib/dato_cms_graphql/rails/routing.rb +++ b/lib/dato_cms_graphql/rails/routing.rb @@ -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 From fed47bcd3043b169baf2109733347a70883b7dad Mon Sep 17 00:00:00 2001 From: psguazz Date: Wed, 13 Mar 2024 11:21:49 +0100 Subject: [PATCH 2/3] Memoizing queries Shouldn't make much of a difference, but makes me feel better. --- lib/dato_cms_graphql.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/dato_cms_graphql.rb b/lib/dato_cms_graphql.rb index 0df963d..6692aea 100644 --- a/lib/dato_cms_graphql.rb +++ b/lib/dato_cms_graphql.rb @@ -48,14 +48,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) From a5dbcd5dbb35c4d7aa2e8996e57c5717d49e9c66 Mon Sep 17 00:00:00 2001 From: psguazz Date: Wed, 13 Mar 2024 11:40:49 +0100 Subject: [PATCH 3/3] Including Bridgetown builder --- lib/dato_cms_graphql.rb | 14 +++++++-- .../bridgetown/initializer.rb | 3 ++ .../bridgetown/query_builder.rb | 30 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 lib/dato_cms_graphql/bridgetown/initializer.rb create mode 100644 lib/dato_cms_graphql/bridgetown/query_builder.rb diff --git a/lib/dato_cms_graphql.rb b/lib/dato_cms_graphql.rb index 6692aea..1b53f35 100644 --- a/lib/dato_cms_graphql.rb +++ b/lib/dato_cms_graphql.rb @@ -10,12 +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/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 diff --git a/lib/dato_cms_graphql/bridgetown/initializer.rb b/lib/dato_cms_graphql/bridgetown/initializer.rb new file mode 100644 index 0000000..8277616 --- /dev/null +++ b/lib/dato_cms_graphql/bridgetown/initializer.rb @@ -0,0 +1,3 @@ +::Bridgetown.initializer :dato_cms_graphql do |config| + config.builder DatoCmsGraphql::Bridgetown::Builder +end diff --git a/lib/dato_cms_graphql/bridgetown/query_builder.rb b/lib/dato_cms_graphql/bridgetown/query_builder.rb new file mode 100644 index 0000000..4adaba8 --- /dev/null +++ b/lib/dato_cms_graphql/bridgetown/query_builder.rb @@ -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