Wednesday, 5 December 2018

switch user -- users list click any user for session create

Please visit the above doc :

https://github.com/flyerhzm/switch_user
======================================

Add Gem:

gem "switch_user"
=======================================
If you need to override the default configuration, run rails g switch_user:install and a copy of the configuration file will be copied to config/initializers/switch_user.rb in your project.

rails g switch_user:install

===========================================

Add file for controller:

app/controllers/switch_user_controller.rb

class SwitchUserController < ApplicationController
  def set_current_user
  user = User.find(params[:scope_identifier])
                # without devise
  if user.present?
  session[:user_id] = user.id
  redirect_to '/'
  end
                # with devise 
  # sign_in(user)
  end

  def remember_user
  end

end

==============================================

config/routes.rb

get 'switch_user', to: 'switch_user#set_current_user'

  get 'switch_user/remember_user', to: 'switch_user#remember_user'


=============================================

Add Link for Button:

1. Activeadmin Users index page
   
     column "Switch User" do |show|
        links = link_to 'Login As', "/switch_user?scope_identifier=#{show.id}"
     end

2. simple rails users index
   
    <%= link_to 'Login As', "/switch_user?scope_identifier=#{user.id}"%>
==============================================








Saturday, 1 December 2018

cookie set and delete on click

Require jquery cookie js:

//= require jquery.cookie.js

$(".module_close").on('click', function() {
    $(".module_hide").addClass('hidden')
    $.cookie('the_cookie', 'hide information');
    console.log($.cookie('the_cookie'))
  });
  $(".fa_info_click").on('click', function() {
    $(".module_hide").removeClass('hidden')
    $.cookie('the_cookie', '');
    console.log($.cookie('the_cookie'))
  });

$(window).on('load',function() {
  if ($.cookie('the_cookie') != ''){
    $(".module_hide").addClass('hidden');
  }
});


html :
.row.wrapper.border-bottom.white-bg.page-heading.module_hide
    a.close.module_close id="hide" data-toggle="tooltip" title="Close" href="#"  ×