Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
Update template rendering and update_attributes for Rails 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Dec 15, 2020
1 parent 6b3e291 commit eb8abdc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/vanity/adapters/active_record_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/vanity/commands/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def render_erb(path, locals = {})
locals = struct.new(*locals.values_at(*keys))
dir, base = File.split(path)
path = File.join(dir, partialize(base))
erb = ERB.new(File.read("#{path}.erb"), nil, '<>')
erb = ERB.new(File.read(path), nil, '<>')
erb.filename = path
erb.result(locals.instance_eval { binding })
end
Expand All @@ -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
Expand Down
29 changes: 20 additions & 9 deletions lib/vanity/frameworks/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -348,55 +348,66 @@ 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
}
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
Expand Down
6 changes: 3 additions & 3 deletions lib/vanity/templates/_experiment.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
<% if experiment.type == 'ab_test' && experiment.active? && experiment.playground.collecting? %>
<span class='enabled-links'>
<% action = experiment.enabled? ? :disable : :enable %>
<% if experiment.enabled? %> <%= I18n.t( 'vanity.enabled' ) %> | <% end %>
<a title='<%=I18n.t( action, scope: 'vanity.act_on_this_experiment' )%>' href='#'
data-id='<%= experiment.id %>' data-url='<%= url_for(:action=>action, :e => experiment.id) %>'>
<%= action %></a>
<% if !experiment.enabled? %> | <%= I18n.t( 'vanity.disabled' ) %> <% end %>

</span>
<% end %>
</h3>
Expand All @@ -26,7 +26,7 @@
</p>
<% end %>
<a class="button reset" title="<%= I18n.t('vanity.reset') %>" href="#" data-id="<%= experiment.id %>" data-url="<%= url_for(:action=>:reset, :e=>experiment.id) %>"><%= I18n.t 'vanity.reset' %></a>
<%= render :file => Vanity.template("_" + experiment.type), :locals => {:experiment => experiment} %>
<%= render :template => "_" + experiment.type, :locals => {:experiment => experiment} %>
<p class="meta">
<%= 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? %>
Expand Down
2 changes: 1 addition & 1 deletion lib/vanity/templates/_experiments.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ul class="experiments">
<% experiments.sort_by{|id, experiment| experiment.created_at}.sort_by{ |id, experiment| experiment.name }.reverse.each do |id, experiment| %>
<li class="experiment <%= experiment.type %>" id="experiment_<%=vanity_h id.to_s %>">
<%= render :file => Vanity.template("_experiment"), :locals => { :id => id, :experiment => experiment } %>
<%= render :template=>"_experiment", :locals => { :id => id, :experiment => experiment } %>
</li>
<% end %>
</ul>
2 changes: 1 addition & 1 deletion lib/vanity/templates/_metrics.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ul class="metrics">
<% metrics.sort_by { |id, metric| metric.name }.each do |id, metric| %>
<li class="metric" id="metric_<%= id %>">
<%= render :file=>Vanity.template("_metric"), :locals=>{:id=>id, :metric=>metric} %>
<%= render :template=>"_metric", :locals=>{:id=>id, :metric=>metric} %>
</li>
<% end %>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
<% if experiments_persisted %>
<% if experiments.present? %>
<h2><%= I18n.t 'vanity.experiments' %></h2>
<%= render :file=>Vanity.template("_experiments"), :locals=>{:experiments=>experiments} %>
<%= render :template=>"_experiments", :locals=>{:experiments=>experiments} %>
<% end %>
<% unless metrics.empty? %>
<h2><%= I18n.t 'vanity.metrics' %></h2>
<%= render :file=>Vanity.template("_metrics"), :locals=>{:metrics=>metrics, :experiments=>experiments} %>
<%= render :template=>"_metrics", :locals=>{:metrics=>metrics, :experiments=>experiments} %>
<% end %>
<% else %>
<div class="alert persistance">
Expand Down

0 comments on commit eb8abdc

Please sign in to comment.