-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move in some rails specific functionality
This makes it so that queries are no longer in their own module but instead have `Query` as a suffix.
- Loading branch information
Showing
9 changed files
with
127 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module DatoCmsGraphql::Rails | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |