Skip to content

Commit

Permalink
Support previous yaml config structure for Analytics, allowing time f…
Browse files Browse the repository at this point in the history
…or folks to transition their applications to the new format
  • Loading branch information
orangewolf committed Dec 31, 2021
1 parent e1c0c4f commit 4f08a74
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion app/services/hyrax/analytics/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ def self.load_from_yaml
Rails.logger.error("Unable to fetch any keys from #{filename}.")
return new({})
end
new yaml.fetch('analytics')&.fetch('google')
config = yaml.fetch('analytics')&.fetch('google', nil)
unless config
Rails.logger.info("Deprecated analytics format found. Please update your yaml file.")
config = yaml.fetch('analytics')
# this has to exist here with a placeholder so it can be set in the Hyrax initializer
# it is only for backward compatibility
config['analytics_id'] = '-'
end
new config
end

REQUIRED_KEYS = %w[analytics_id app_name app_version privkey_path privkey_secret client_email].freeze
Expand All @@ -48,6 +56,12 @@ def valid?
REQUIRED_KEYS.each do |key|
class_eval %{ def #{key}; @config.fetch('#{key}'); end }
end

# This method allows setting the analytics id in the initializer
# which is deprecated
def analytics_id=(value)
@config['analytics_id'] = value
end
end

# Generate an OAuth2 token for Google Analytics
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
args:
- EXTRA_APK_PACKAGES=git less
image: ghcr.io/samvera/dassie
command: sh -l -c 'bundle && bundle exec puma -v -b tcp://0.0.0.0:3000'
stdin_open: true
tty: true
user: root
Expand Down Expand Up @@ -109,6 +110,7 @@ services:
context: .
target: hyrax-engine-dev-worker
image: ghcr.io/samvera/dassie-worker
command: sh -l -c 'bundle && bundle exec sidekiq'
user: root
env_file:
- .env
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/hyrax/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@
end
end

context "When the yaml file has no values" do
before do
allow(File).to receive(:read).and_return <<-FILE
analytics:
app_name: My App Name
app_version: 0.0.1
privkey_path: /tmp/privkey.p12
privkey_secret: s00pers3kr1t
client_email: oauth@example.org
FILE
end

it 'reads its config from a yaml file' do
expect(Rails.logger).to receive(:info)
.with(starting_with("Deprecated analytics format found. Please update your yaml file."))
Hyrax.config.google_analytics_id = "UA-XXXXXXXX"
expect(config.app_name).to eql 'My App Name'
expect(config.app_version).to eql '0.0.1'
expect(config.privkey_path).to eql '/tmp/privkey.p12'
expect(config.privkey_secret).to eql 's00pers3kr1t'
expect(config.client_email).to eql 'oauth@example.org'
# In the previous version analytics id was set ny Hyrax.config
expect(config.analytics_id).to eql 'UA-XXXXXXXX'
end
end

context "When the yaml file has no values" do
before do
allow(File).to receive(:read).and_return("# Just comments\n# and comments\n")
Expand Down

0 comments on commit 4f08a74

Please sign in to comment.