Skip to content

Commit

Permalink
Use a railtie for configuration
Browse files Browse the repository at this point in the history
Now with defaults
  • Loading branch information
netikular committed Mar 13, 2024
1 parent 15157f2 commit 0b91968
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
11 changes: 8 additions & 3 deletions lib/dato_cms_graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -57,17 +58,21 @@ 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) }
.flatten
end
end

def self.path_to_queries
@path_to_queries
end

def self.path_to_queries=(value)
@path_to_queries = value
end
Expand Down
10 changes: 10 additions & 0 deletions lib/dato_cms_graphql/rails/railtie.rb
Original file line number Diff line number Diff line change
@@ -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
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.2.2"
VERSION = "0.2.3"
end
9 changes: 9 additions & 0 deletions test/dato_cms_graphql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0b91968

Please sign in to comment.