diff --git a/Gemfile b/Gemfile index 71dea2c..b4fecfd 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem 'rails', '4.2.4' # Assets gem 'sass-rails', '~> 4.0.3' gem 'bootstrap-sass', '3.2.0.0' +gem 'bootstrap-switch-rails' gem 'uglifier', '>= 1.3.0' gem 'jquery-rails' # Haml for templating! diff --git a/Gemfile.lock b/Gemfile.lock index 9a7683f..f52bd57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,7 @@ GEM bcrypt (3.1.10) bootstrap-sass (3.2.0.0) sass (~> 3.2) + bootstrap-switch-rails (3.3.3) bootstrap-will_paginate (0.0.10) will_paginate builder (3.2.2) @@ -307,6 +308,7 @@ DEPENDENCIES airbrake (~> 4) annotate bootstrap-sass (= 3.2.0.0) + bootstrap-switch-rails bootstrap-will_paginate (= 0.0.10) cancancan capistrano (~> 3.1) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index f5b34af..28d7755 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,6 +13,7 @@ //= require jquery //= require jquery_ujs //= require bootstrap +//= require bootstrap-switch //= require turbolinks //= require_tree . diff --git a/app/assets/javascripts/switch.js b/app/assets/javascripts/switch.js new file mode 100644 index 0000000..76e5db7 --- /dev/null +++ b/app/assets/javascripts/switch.js @@ -0,0 +1,13 @@ +ready = function() { + $('[data-switch]').bootstrapSwitch({ onText: "private", offText: "public" }); + $('[data-switch]').on('switchChange.bootstrapSwitch', function(event, state) { + $(this).closest('form').submit(); + }); + + $('#edit_user_1').on("ajax:error", function(xhr, status, error) { + alert("An error occured. Your account has not been updated."); + }); +} + +$(document).ready(ready); +$(document).on('page:load', ready); diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 8042d64..2edc170 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -16,8 +16,10 @@ @import "bootstrap-sprockets"; +@import "bootstrap3-switch"; @import "bootstrap"; + /* mixins, variables, etc. */ $gray-medium-light: #eaeaea; diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9922687..4bc82c8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -9,13 +9,25 @@ class UsersController < ApplicationController if user_params.empty? flash[:notice] = "Nothing happened." redirect_to @user - elsif @user.update_attributes(user_params) - flash[:success] = "Successfully updated!" - redirect_to @user else - flash[:error] = "Update failed!" - @user.reload - render 'show' + if @user.update_attributes(user_params) + respond_to do |format| + format.html do + flash[:success] = "Successfully updated!" + redirect_to @user + end + format.js { head :ok } + end + else + respond_to do |format| + format.html do + flash[:error] = "Update failed!" + @user.reload + render 'show' + end + format.js { head :bad_request } + end + end end end diff --git a/app/views/users/_sidebar.html.haml b/app/views/users/_sidebar.html.haml index 6387df7..000e303 100644 --- a/app/views/users/_sidebar.html.haml +++ b/app/views/users/_sidebar.html.haml @@ -26,11 +26,9 @@ = link_to "Set dagschotel", edit_dagschotel_user_path(@user), class: "btn btn-default btn-block" %li.list-group-item.text-muted %p Orders can be placed on koelkast for every public account. Private accounts can only order products by logging in here. - // = link_to "Go #{if @user.private then "public" else "private" end}", toggle_privacy_user_path(@user), class: "btn btn-default btn-block" - = f_form_for @user do |f| - .hidden - = f.check_box :private, checked: !@user.private - = f.submit "Go #{@user.private ? "public" : "private"}", class: "btn btn-default btn-block" + = f_form_for @user, remote: true do |f| + .center + = f.check_box :private, skip_label: true, data: { switch: true } %li.list-group-item.text-muted = f_form_for @user do |f| = f.file_field :avatar