From c749872b666777c99e0e64fd1079f792f54cb544 Mon Sep 17 00:00:00 2001
From: Ben Sheldon
Date: Sun, 13 Dec 2020 16:57:46 -0800
Subject: [PATCH] Update template rendering and update_attributes for Rails 6.1
---
lib/vanity/adapters/active_record_adapter.rb | 2 +-
lib/vanity/commands/report.rb | 2 +-
lib/vanity/frameworks/rails.rb | 29 +++++++++++++------
lib/vanity/templates/_experiment.erb | 6 ++--
lib/vanity/templates/_experiments.erb | 2 +-
lib/vanity/templates/_metrics.erb | 2 +-
.../{_report.erb => _report.html.erb} | 4 +--
7 files changed, 29 insertions(+), 18 deletions(-)
rename lib/vanity/templates/{_report.erb => _report.html.erb} (89%)
diff --git a/lib/vanity/adapters/active_record_adapter.rb b/lib/vanity/adapters/active_record_adapter.rb
index 34b0089c..6cd802d3 100644
--- a/lib/vanity/adapters/active_record_adapter.rb
+++ b/lib/vanity/adapters/active_record_adapter.rb
@@ -105,7 +105,7 @@ def self.retrieve(experiment, identity, create = true, update_with = nil)
retried = false
begin
if record = VanityParticipant.where(:experiment_id => experiment.to_s, :identity => identity.to_s).first
- record.update_attributes(update_with) if update_with
+ record.update(update_with) if update_with
elsif create
record = VanityParticipant.create({ :experiment_id => experiment.to_s, :identity => identity.to_s }.merge(update_with || {}))
end
diff --git a/lib/vanity/commands/report.rb b/lib/vanity/commands/report.rb
index 64187829..925f4d33 100644
--- a/lib/vanity/commands/report.rb
+++ b/lib/vanity/commands/report.rb
@@ -78,7 +78,7 @@ class << self
# Generate an HTML report. Outputs to the named file, or stdout with no
# arguments.
def report(output = nil)
- html = render(Vanity.template("report"),
+ html = render(Vanity.template("_report.erb"),
:experiments=>Vanity.playground.experiments,
:experiments_persisted=>Vanity.playground.experiments_persisted?,
:metrics=>Vanity.playground.metrics
diff --git a/lib/vanity/frameworks/rails.rb b/lib/vanity/frameworks/rails.rb
index 33bd03ad..a088099d 100644
--- a/lib/vanity/frameworks/rails.rb
+++ b/lib/vanity/frameworks/rails.rb
@@ -251,7 +251,7 @@ def vanity_tracking_image(identity, metric, options = {})
def vanity_js
return if Vanity.context.vanity_active_experiments.nil? || Vanity.context.vanity_active_experiments.empty?
javascript_tag do
- render :file => Vanity.template("_vanity"), :formats => [:js]
+ render :file => Vanity.template("_vanity.js.erb"), :formats => [:js]
end
end
@@ -348,8 +348,13 @@ def add_participant
#
# Step 3: Open your browser to http://localhost:3000/vanity
module Dashboard
+ def set_vanity_view_path
+ prepend_view_path Vanity.template('')
+ end
+
def index
- render :file=>Vanity.template("_report"),:content_type=>Mime[:html], :locals=>{
+ set_vanity_view_path
+ render :template=>"_report", :content_type=>Mime[:html], :locals=>{
:experiments=>Vanity.playground.experiments,
:experiments_persisted=>Vanity.playground.experiments_persisted?,
:metrics=>Vanity.playground.metrics
@@ -357,46 +362,52 @@ def index
end
def participant
- render :file=>Vanity.template("_participant"), :locals=>{:participant_id => params[:id], :participant_info => Vanity.playground.participant_info(params[:id])}, :content_type=>Mime[:html]
+ set_vanity_view_path
+ render :template=>"_participant", :locals=>{:participant_id => params[:id], :participant_info => Vanity.playground.participant_info(params[:id])}, :content_type=>Mime[:html]
end
def complete
+ set_vanity_view_path
exp = Vanity.playground.experiment(params[:e].to_sym)
alt = exp.alternatives[params[:a].to_i]
confirmed = params[:confirmed]
# make the user confirm before completing an experiment
if confirmed && confirmed.to_i==alt.id && exp.active?
exp.complete!(alt.id)
- render :file=>Vanity.template("_experiment"), :locals=>{:experiment=>exp}
+ render :template=>"_experiment", :locals=>{:experiment=>exp}
else
@to_confirm = alt.id
- render :file=>Vanity.template("_experiment"), :locals=>{:experiment=>exp}
+ render :template=>"_experiment", :locals=>{:experiment=>exp}
end
end
def disable
+ set_vanity_view_path
exp = Vanity.playground.experiment(params[:e].to_sym)
exp.enabled = false
- render :file=>Vanity.template("_experiment"), :locals=>{:experiment=>exp}
+ render :template=>"_experiment", :locals=>{:experiment=>exp}
end
def enable
+ set_vanity_view_path
exp = Vanity.playground.experiment(params[:e].to_sym)
exp.enabled = true
- render :file=>Vanity.template("_experiment"), :locals=>{:experiment=>exp}
+ render :template=>"_experiment", :locals=>{:experiment=>exp}
end
def chooses
+ set_vanity_view_path
exp = Vanity.playground.experiment(params[:e].to_sym)
exp.chooses(exp.alternatives[params[:a].to_i].value)
- render :file=>Vanity.template("_experiment"), :locals=>{:experiment=>exp}
+ render :template=>"_experiment", :locals=>{:experiment=>exp}
end
def reset
+ set_vanity_view_path
exp = Vanity.playground.experiment(params[:e].to_sym)
exp.reset
flash[:notice] = I18n.t 'vanity.experiment_has_been_reset', name: exp.name
- render :file=>Vanity.template("_experiment"), :locals=>{:experiment=>exp}
+ render :template=>"_experiment", :locals=>{:experiment=>exp}
end
include AddParticipant
diff --git a/lib/vanity/templates/_experiment.erb b/lib/vanity/templates/_experiment.erb
index 58293a66..03959b82 100644
--- a/lib/vanity/templates/_experiment.erb
+++ b/lib/vanity/templates/_experiment.erb
@@ -9,13 +9,13 @@
<% if experiment.type == 'ab_test' && experiment.active? && experiment.playground.collecting? %>
<% action = experiment.enabled? ? :disable : :enable %>
-
+
<% if experiment.enabled? %> <%= I18n.t( 'vanity.enabled' ) %> | <% end %>
' href='#'
data-id='<%= experiment.id %>' data-url='<%= url_for(:action=>action, :e => experiment.id) %>'>
<%= action %>
<% if !experiment.enabled? %> | <%= I18n.t( 'vanity.disabled' ) %> <% end %>
-
+
<% end %>
@@ -26,7 +26,7 @@
<% end %>
<%= I18n.t 'vanity.reset' %>
-<%= render :file => Vanity.template("_" + experiment.type), :locals => {:experiment => experiment} %>
+<%= render :template => "_" + experiment.type, :locals => {:experiment => experiment} %>
<%= I18n.t('vanity.started_at', :timestamp=>I18n.l(experiment.created_at, :format=>'%a, %b %d')) %>
<%= ' | '+I18n.t('vanity.completed_at', :timestamp=>I18n.l(experiment.completed_at, :format=>'%a, %b %d')) unless experiment.active? %>
diff --git a/lib/vanity/templates/_experiments.erb b/lib/vanity/templates/_experiments.erb
index 8a573e13..fb6d9a20 100644
--- a/lib/vanity/templates/_experiments.erb
+++ b/lib/vanity/templates/_experiments.erb
@@ -1,7 +1,7 @@
<% experiments.sort_by{|id, experiment| experiment.created_at}.sort_by{ |id, experiment| experiment.name }.reverse.each do |id, experiment| %>
-
- <%= render :file => Vanity.template("_experiment"), :locals => { :id => id, :experiment => experiment } %>
+ <%= render :template=>"_experiment", :locals => { :id => id, :experiment => experiment } %>
<% end %>
diff --git a/lib/vanity/templates/_metrics.erb b/lib/vanity/templates/_metrics.erb
index 0d98e37e..ebcde12d 100644
--- a/lib/vanity/templates/_metrics.erb
+++ b/lib/vanity/templates/_metrics.erb
@@ -1,7 +1,7 @@
<% metrics.sort_by { |id, metric| metric.name }.each do |id, metric| %>
-
- <%= render :file=>Vanity.template("_metric"), :locals=>{:id=>id, :metric=>metric} %>
+ <%= render :template=>"_metric", :locals=>{:id=>id, :metric=>metric} %>
<% end %>
diff --git a/lib/vanity/templates/_report.erb b/lib/vanity/templates/_report.html.erb
similarity index 89%
rename from lib/vanity/templates/_report.erb
rename to lib/vanity/templates/_report.html.erb
index 337dd0d8..e6e8d825 100644
--- a/lib/vanity/templates/_report.erb
+++ b/lib/vanity/templates/_report.html.erb
@@ -22,12 +22,12 @@
<% if experiments_persisted %>
<% if experiments.present? %>
<%= I18n.t 'vanity.experiments' %>
- <%= render :file=>Vanity.template("_experiments"), :locals=>{:experiments=>experiments} %>
+ <%= render :template=>"_experiments", :locals=>{:experiments=>experiments} %>
<% end %>
<% unless metrics.empty? %>
<%= I18n.t 'vanity.metrics' %>
- <%= render :file=>Vanity.template("_metrics"), :locals=>{:metrics=>metrics, :experiments=>experiments} %>
+ <%= render :template=>"_metrics", :locals=>{:metrics=>metrics, :experiments=>experiments} %>
<% end %>
<% else %>