Skip to content

Commit

Permalink
Move in some rails specific functionality
Browse files Browse the repository at this point in the history
This makes it so that queries are no longer in their own module but
instead have `Query` as a suffix.
  • Loading branch information
netikular committed Mar 11, 2024
1 parent c9e91c3 commit 51a13cf
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
dato_cms_graphql (0.1.4)
dato_cms_graphql (0.1.5)
activesupport (~> 7.1.3)
graphql-client (~> 0.19.0)

GEM
Expand Down
1 change: 1 addition & 0 deletions dato_cms_graphql.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
# spec.add_dependency "example-gem", "~> 1.0"
#
spec.add_dependency "graphql-client", "~> 0.19.0"
spec.add_dependency "activesupport", "~> 7.1.3"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
12 changes: 12 additions & 0 deletions lib/dato_cms_graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
require_relative "dato_cms_graphql/version"
require_relative "dato_cms_graphql/fields"
require_relative "dato_cms_graphql/graphql_base"
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"

Expand Down Expand Up @@ -44,4 +48,12 @@ def self.query(query, variables: {})
def self.count(query, variables: {})
Client.query(query, variables: variables).data.meta_data.count
end

def self.queries
ObjectSpace.each_object(::Class).select { |klass| klass < DatoCmsGraphql::GraphqlBase }
end

def self.renderable
queries.select(&:render?)
end
end
31 changes: 21 additions & 10 deletions lib/dato_cms_graphql/graphql_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def render(value)
self.bridgetown_render = value
end

def query_name
to_s.humanize.pluralize
end

def graphql_fields(*args)
self.fields = Fields.new(args).to_query
initialize_queries
Expand All @@ -37,18 +33,16 @@ def initialize_queries
end
end

def name
to_s.split("::").last
def query_name
name.gsub("Query", "")
end

def plural_name
name.pluralize
query_name.pluralize
end

def single_name
rv = name
rv[0] = rv[0].downcase
rv
query_name.camelize(:lower)
end

def parse(query)
Expand Down Expand Up @@ -125,14 +119,23 @@ def single_instance?
def render?
bridgetown_render || false
end

def route
":permalink"
end
end

attr_reader :attributes
page_size(100) # Set the maximum page size as default.
single_instance(false)
render(true)

def route
self.class.route.gsub(":permalink", permalink)
end

def initialize(attributes)
@raw_attributes = attributes
@attributes = JSON.parse(attributes.to_json, object_class: OpenStruct)
end

Expand All @@ -144,6 +147,14 @@ def localized_attributes
end
end

def localized_raw_attributes
if @raw_attributes.has_key?("#{I18n.locale}_item")
@raw_attributes.dig("#{I18n.locale}_item")
else
@raw_attributes
end
end

def respond_to_missing?(method, *args)
localized_attributes.respond_to?(method)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/dato_cms_graphql/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module DatoCmsGraphql::Rails
end
35 changes: 35 additions & 0 deletions lib/dato_cms_graphql/rails/cache_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "active_support/concern"

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

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)
end
end

def cms_record
@parsed ||= begin
data = read_attribute(:cms_record)
JSON.parse(data.to_json, object_class: OpenStruct)
end
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
end
end
end
31 changes: 31 additions & 0 deletions lib/dato_cms_graphql/rails/persistence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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,
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|
persist_record(query, record)
end
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions lib/dato_cms_graphql/rails/routing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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

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
end
end
2 changes: 1 addition & 1 deletion lib/dato_cms_graphql/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DatoCmsGraphql
VERSION = "0.1.5"
VERSION = "0.2.0"
end

0 comments on commit 51a13cf

Please sign in to comment.