diff --git a/Gemfile.lock b/Gemfile.lock index 863ca4a..19a5502 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - dato_cms_graphql (0.2.2) + dato_cms_graphql (0.2.3) activesupport (~> 7.1.3) graphql-client (~> 0.19.0) diff --git a/lib/dato_cms_graphql.rb b/lib/dato_cms_graphql.rb index 1b53f35..ee3b717 100644 --- a/lib/dato_cms_graphql.rb +++ b/lib/dato_cms_graphql.rb @@ -17,6 +17,7 @@ require_relative "dato_cms_graphql/rails/routing" require_relative "dato_cms_graphql/rails/persistence" require_relative "dato_cms_graphql/rails/cache_table" + require_relative "dato_cms_graphql/rails/railtie" end if defined?(::Bridgetown) @@ -57,10 +58,10 @@ def self.count(query, variables: {}) def self.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) + 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) } + 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) } @@ -68,6 +69,10 @@ def self.queries end end + def self.path_to_queries + @path_to_queries + end + def self.path_to_queries=(value) @path_to_queries = value end diff --git a/lib/dato_cms_graphql/rails/railtie.rb b/lib/dato_cms_graphql/rails/railtie.rb new file mode 100644 index 0000000..24bd496 --- /dev/null +++ b/lib/dato_cms_graphql/rails/railtie.rb @@ -0,0 +1,10 @@ +module DatoCmsGraphql + module Rails + class Railtie < ::Rails::Railtie + initializer "dato_cms_graphql_railtie.configure_rails_initialization" do |app| + DatoCmsGraphql.path_to_queries = app.root.join("app", "queries") + puts DatoCmsGraphql.path_to_queries + end + end + end +end diff --git a/lib/dato_cms_graphql/version.rb b/lib/dato_cms_graphql/version.rb index ba2cee3..95c9285 100644 --- a/lib/dato_cms_graphql/version.rb +++ b/lib/dato_cms_graphql/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DatoCmsGraphql - VERSION = "0.2.2" + VERSION = "0.2.3" end diff --git a/test/dato_cms_graphql_test.rb b/test/dato_cms_graphql_test.rb index f9228b6..5c1bff9 100644 --- a/test/dato_cms_graphql_test.rb +++ b/test/dato_cms_graphql_test.rb @@ -8,4 +8,13 @@ def test_that_it_has_a_version_number def test_it_does_something_useful assert true end + + def test_queries_path_starts_as_nil + assert DatoCmsGraphql.path_to_queries.nil? + end + + def test_queries_path_can_be_set + DatoCmsGraphql.path_to_queries = "/app/queries" + assert_equal DatoCmsGraphql.path_to_queries, "/app/queries" + end end