Skip to content
Browse files

Test update

  • Loading branch information...
1 parent 2f4fb6a commit 959e9bd9a66f4bdaa9fa31481e0a12e21a6b3340 @winton committed
View
16 lib/a_b_front_end/controller/api.rb
@@ -61,6 +61,22 @@
).to_json
end
+ put '/tests.json' do
+ ABPlugin::API.update_test(
+ :include => params[:include] || {
+ :variants => {
+ :methods => :for_dashboard
+ }
+ },
+ :id => params[:id],
+ :name => params[:name],
+ :old_variants => params[:old_variants],
+ :only => params[:only],
+ :token => current_user.single_access_token,
+ :variants => params[:variants]
+ ).to_json
+ end
+
delete '/tests.json' do
ABPlugin::API.delete_test(
:id => params[:id],
View
7 lib/a_b_front_end/view/dashboard.haml
@@ -31,7 +31,10 @@
.span-2
%label{ :for => 'test_name' } Name
.span-5
- <input class='variants' name='variants[]' type='text' value='<%= (typeof(name) != "undefined") ? name : "" %>' />
+ <% if (typeof(test) != "undefined") { %>
+ <input name='id' type='hidden' value='<%= test.id %>' />
+ <% } %>
+ <input id='test_name' name='name' type='text' value='<%= (typeof(test) != "undefined") ? test.name : "" %>' />
.clear
.submit
%input{ :type => 'submit', :value => 'Submit' }
@@ -46,7 +49,7 @@
Variant
<% } %>
.span-5
- <input class='variants' name='variants[]' type='text' value='<%= (typeof(name) != "undefined") ? name : "" %>' />
+ <input class='variants' name='<%= (typeof(id) != "undefined") ? "old_variants[" + id + "]" : "variants[]" %>' type='text' value='<%= (typeof(name) != "undefined") ? name : "" %>' />
%textarea.hide#test_template
%table{ :id => 'test_<%= test.id %>' }
View
17 lib/a_b_front_end/view/layout.haml
@@ -17,20 +17,23 @@
- if current_user
.span-12.last
.span-4.nav.align-left
- %a{ :href => '/get_started' } Get Started
+ /%a{ :href => '/get_started' } Get Started
+ &nbsp;
.span-4.nav
- %a{ :href => '/account' } Account
+ /%a{ :href => '/account' } Account
+ &nbsp;
.span-4.nav.last
%a{ :href => '/sessions/destroy' } Log out
- else
.span-12.last
- .span-3.nav
+ .span-4.nav
%a.sign_up Sign up
- .span-3.nav
+ .span-4.nav
%a{ :href => 'http://github.com/winton/a_b_plugin' } Github
- .span-3.nav
- %a{ :href => '/help' } Help
- .span-3.nav.last
+ /
+ .span-3.nav
+ %a{ :href => '/help' } Help
+ .span-4.nav.last
%a.log_in Log in
.clear
.dialog.sign_up.hide
View
57 public/js/dashboard.js
@@ -243,19 +243,67 @@ window.Dashboard = function(sites) {
var table = $(this).closest('table');
var id = table.attr('id').match(/\d+/)[0];
var test = byId(currentState().category.tests, id);
- var dialog = $('#tests_form_template').tmpl({ name: test.name });
+ var dialog = $('#tests_form_template').tmpl({ test: test });
+ var submit = $('.submit', dialog);
+ var variant_template = $('#tests_form_variant_template');
table.replaceWith(dialog);
$('input:first', dialog)[0].select();
$.each(test.variants, function(i, item) {
- $('.submit', dialog).before(
- $('#tests_form_variant_template').tmpl({
+ submit.before(
+ variant_template.tmpl({
control: (i == 0),
+ id: item.id,
name: item.name
})
);
});
+
+ submit.before(
+ variant_template.tmpl({
+ control: (test.variants.length == 0)
+ })
+ );
+
+ $('.cancel', dialog).click(function() {
+ dialog.remove();
+ $('#categories .selected').click().click();
+ return false;
+ });
+
+ $('form', dialog).submit(function() {
+ var form = $(this);
+ var submits = $('.submit input', form);
+
+ $(submits[0]).attr({
+ 'disabled': true,
+ 'value': 'One moment...'
+ });
+ $(submits[1]).remove();
+
+ var data = currentState();
+ var category = data.category;
+ var site = data.site;
+
+ queue.queue(function() {
+ $.post(
+ '/tests.json',
+ form.serialize() + '&_method=PUT',
+ function(response) {
+ category.tests = category.tests || [];
+ category.tests = $.map(category.tests, function(item) {
+ return (item.id == id) ? response : item;
+ });
+ queue.dequeue();
+ $('#categories .selected').click().click();
+ },
+ 'json'
+ );
+ });
+
+ return false;
+ });
}
function testRemove() {
@@ -264,9 +312,10 @@ window.Dashboard = function(sites) {
var test = $(this).closest('table');
var id = test.attr('id').match(/\d+/)[0];
+ var category = currentState().category;
test.remove();
- category.tests = $.grep(currentState().category.tests, function(item) {
+ category.tests = $.grep(category.tests, function(item) {
return (item.id != id);
});
View
11 vendor/a_b_plugin/lib/a_b_plugin/api.rb
@@ -105,6 +105,17 @@ def self.sites(attributes={})
))
end
+ def self.update_test(attributes={})
+ return unless Config.token && Config.url
+ base_uri Config.url
+ put('/tests.json', :query => compress(
+ :include => attributes.delete(:include),
+ :only => attributes.delete(:only),
+ :token => attributes.delete(:token) || Config.token,
+ :test => attributes
+ ))
+ end
+
private
def self.compress(hash)

0 comments on commit 959e9bd

Please sign in to comment.
Something went wrong with that request. Please try again.