Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.05 KB | None | 0 0
  1. class UsersController < ApplicationController
  2.     skip_before_action :authenticate_user!, only:[:become_merchant, :invite, :fbshare, :fbshare_accept, :about, :blog, :career, :contact, :press, :terms, :policy, :help]
  3.     before_action :set_user_profile, only: [:profile, :profile_avatar, :profile_accounts, :profile_document, :profile_security]
  4.     before_action :set_accounts, only: [:accounts, :accounts_photo, :account_document]
  5.  
  6.     def layout_by_resource
  7.     "product"
  8.   end
  9.  
  10.   # click become merchant
  11.     def become_merchant
  12.  
  13.         if user_signed_in?
  14.             if current_user.status != 'merchant'
  15.                
  16.                 @store_setting = StoreSetting.new({:user_id => current_user.id})
  17.                 gon.store_usernames = StoreSetting.select(:store_username).pluck(:store_username)
  18.             else
  19.                 redirect_to root_path
  20.             end
  21.         else
  22.             redirect_to new_user_registration_path
  23.         end
  24.     end
  25.  
  26.     def profile
  27.         if request.get?
  28.             @profile = Profile.new({:user_id => current_user.id}) if @profile.blank?
  29.             if @profile.birthday.present?
  30.                 @year = @profile.birthday.year
  31.                 @month = @profile.birthday.month
  32.                 @day = @profile.birthday.day
  33.             end
  34.         else
  35.             birthday = nil
  36.             begin
  37.                 birthday = DateTime.new(params["profile-year"].to_i, params["profile-month"].to_i, params["profile-day"].to_i)
  38.             rescue Exception => e
  39.                
  40.             end
  41.            
  42.             if @profile.blank?
  43.                 @profile = Profile.new(profile_params)
  44.                 @profile.user_id = current_user.id
  45.    
  46.                 flash[:success] = "Created the profile successfully."
  47.             else
  48.                 @profile.update(profile_params)
  49.                 flash[:success] = "Updated the profile successfully."
  50.             end
  51.             @profile.birthday = birthday
  52.             @profile.save
  53.             redirect_to root_path
  54.  
  55.         end
  56.     end
  57.  
  58.     def profile_avatar
  59.         if request.get?
  60.             @profile = Profile.new({:user_id => current_user.id}) if @profile.blank?
  61.         else
  62.             if params[:user_avatar].present? && params[:user_avatar][:id].present?
  63.                 user_avatar = UserAvatar.find(params[:user_avatar][:id])
  64.                 if @profile.save
  65.                     user_avatar.profile = @profile
  66.                     user_avatar.save
  67.                     redirect_to root_path
  68.                 end
  69.             end
  70.         end
  71.     end
  72.  
  73.     def profile_security
  74.         if request.get?
  75.             @profile = Profile.new({:user_id => current_user.id}) if @profile.blank?
  76.         else
  77.            
  78.             @form_type = params[:form_type]
  79.             old_password = params[:user][:old_password]
  80.             new_password = params[:user][:new_password]
  81.             new_email = params[:user][:email]
  82.             if @form_type == 'password'
  83.                 if not current_user.valid_password?(old_password)
  84.                     current_user.errors.add(:password, "is not correct.")
  85.                     @error_type = 'password'
  86.                     return render 'profile_security'
  87.                 end
  88.  
  89.                 current_user.password = new_password
  90.                 if current_user.save
  91.                     flash[:success] = 'Successfully updated the password.'
  92.                     redirect_to root_path
  93.                 end
  94.             elsif @form_type == 'email'
  95.                 current_user.email = new_email
  96.                 if current_user.save
  97.                     flash[:success] = 'Successfully updated the email.'
  98.                     redirect_to root_path
  99.                 end
  100.             end
  101.            
  102.         end
  103.     end
  104.  
  105.     def profile_document
  106.         if request.get?
  107.             @profile = Profile.new({:user_id => current_user.id}) if @profile.blank?
  108.         else
  109.             if params[:document_id].present? && params[:document_id].count > 0
  110.                 params[:document_id].each do |document_id|
  111.                     profile_document = ProfileDocument.find(document_id)
  112.                     if profile_document.present?
  113.                         profile_document.profile = @profile
  114.                         profile_document.save
  115.                     end
  116.                 end
  117.             end
  118.  
  119.             redirect_to root_path
  120.         end
  121.     end
  122.  
  123.     def account_document
  124.         if current_user.status != 'merchant'
  125.             return redirect_to root_path
  126.         end
  127.         if request.get?
  128.             if @store_setting.blank?
  129.                 redirect_to root_path
  130.             end
  131.             @account_document = @store_setting.account_document
  132.             @account_document = AccountDocument.new({:store_setting_id => current_user.id}) if @account_document.blank?
  133.         else
  134.             # if params[:document_id].present? && params[:document_id].count > 0
  135.             #   params[:document_id].each do |document_id|
  136.             #       profile_document = ProfileDocument.find(document_id)
  137.             #       if profile_document.present?
  138.             #           profile_document.profile = @profile
  139.             #           profile_document.save
  140.             #       end
  141.             #   end
  142.             # end
  143.  
  144.             return redirect_to root_path
  145.         end
  146.     end
  147.  
  148.     def accounts
  149.         if current_user.status != 'merchant'
  150.             redirect_to root_path
  151.         end
  152.         if request.get?
  153.             @store_setting = StoreSetting.new({:user_id => current_user.id}) if @store_setting.blank?
  154.         else
  155.             if @store_setting.update(store_setting_params)
  156.                 flash[:notice] = "Updated the store setting successfully."
  157.                 redirect_to root_path
  158.             end
  159.         end
  160.     end
  161.  
  162.     def accounts_photo
  163.         if current_user.status != 'merchant'
  164.             redirect_to root_path
  165.         end
  166.         if request.get?
  167.         else
  168.             if params[:store_image].present? && params[:store_image][:id].present?
  169.                 store_image = StoreImage.find(params[:store_image][:id])
  170.                 if store_image.present?
  171.                     store_image.store_setting = @store_setting
  172.                     store_image.save
  173.                     redirect_to root_path
  174.                 end
  175.             end
  176.         end
  177.     end
  178.  
  179.     # complete become merchant
  180.     def complete_merchant
  181.         if user_signed_in?
  182.             @store_setting = StoreSetting.new(store_setting_params)
  183.             @store_setting.user_id = current_user.id
  184.             if params[:store_image].present? && params[:store_image][:id].present?
  185.                 store_image = StoreImage.find(params[:store_image][:id])
  186.                 if store_image.present?
  187.                     if @store_setting.save
  188.                         store_image.store_setting = @store_setting
  189.                         store_image.save
  190.                         # change status of the current user
  191.                         current_user.status = 'merchant'
  192.                         #mixpanel.track(current_user.email, 'New Merchant Signup', { campaign: 'New Merchant Signup' })
  193.                         current_user.save
  194.  
  195. # <<<<<<< HEAD
  196. #                       logger.info "{event=REGISTRATION_STORE_V2 status=successful store=#{current_user.full_name}}"
  197. #                       UserMailer.welcome_merchant(current_user,@store_setting).deliver_later
  198. #                       flash[:notice] = "Merchant signup is successfully. Please check your email"
  199. #                       redirect_to root_path
  200. # =======
  201.                         UserMailer.welcome_merchant(current_user,@store_setting)
  202.                         flash[:notice] = "Become merchant successfully. Please verify for your account."
  203.                         # redirect_to root_path
  204.                         # render :layout => 'product'
  205.                     end
  206.                 end
  207.             else
  208.                 @store_setting.errors.add(:store_image, "can't be blank.")
  209.                 respond_to do |format|
  210.             format.html {render :become_merchant}
  211.             format.json {render json: { :errors => @store_setting.errors, :store_setting => @store_setting }, status: 422}
  212.           end
  213.             end
  214.         end
  215.     end
  216.  
  217.     def verify_document
  218.         if user_signed_in? && current_user.status == 'merchant' && current_user.merchant_status == 'pending'
  219.  
  220.         else
  221.             redirect_to root_path
  222.         end
  223.     end
  224.  
  225.     def complete_merchant1
  226.     end
  227.  
  228.     # verify the store username for uniquness
  229.     def verify_store_username
  230.         store_setting = StoreSetting.where(:store_username => params[:store_username])
  231.         if store_setting.blank?
  232.             respond_to do |format|
  233.                 format.json { render json: {:result => true}}
  234.             end
  235.         else
  236.             respond_to do |format|
  237.                 format.json { render json: {:result => false}}
  238.             end
  239.         end
  240.     end
  241.  
  242.     # GET /resource/invitation/invite?invitation_token=abcdef
  243.     # for invitation by email: the page for invitee
  244.   def invite
  245.     if current_user
  246.         return redirect_to root_path
  247.     end
  248.     @invitation_token = params[:invitation_token]
  249.     @user = User.find_by_id(params[:user_id])
  250.     render :invite
  251.   end
  252.  
  253.   # for fb share page for inviter
  254.   def fbshare
  255.     @fb_share_token = params[:token]
  256.     @original_url = request.original_url
  257.  
  258.     @user = User.find_by_fb_share_token(@fb_share_token)
  259.     if @user.blank?
  260.         return redirect_to root_path
  261.     end
  262.     render 'fbshare'
  263.   end
  264.  
  265.   # fb invite page for invitee
  266.   def fbshare_accept
  267.     @fb_share_token = params[:token]
  268.     if current_user
  269.         return redirect_to root_path
  270.     end
  271.     @inviter = User.find_by_fb_share_token(@fb_share_token)
  272.     if @inviter.blank?
  273.         return redirect_to root_path
  274.     end
  275.     @user = User.new
  276.     render :template => 'users/fbshare_accept', :layout => 'users'
  277.   end
  278.  
  279.  
  280.   # dashboard page
  281.   def dashboard
  282.  
  283.   end
  284.  
  285.   def about
  286.     @nav_obj = 'about'
  287.     render :layout => 'static'
  288.   end
  289.  
  290.   def blog
  291.     @nav_obj = 'blog'
  292.     render :layout => 'static'
  293.   end
  294.  
  295.   def career
  296.     @nav_obj = 'career'
  297.     render :layout => 'static'
  298.   end
  299.  
  300.   def contact
  301.     @nav_obj = 'contact'
  302.     render :layout => 'static'
  303.   end
  304.  
  305.   def press
  306.     @nav_obj = 'press'
  307.     render :layout => 'static'
  308.   end
  309.  
  310.   def terms
  311.     @nav_obj = 'terms'
  312.     render :layout => 'static'
  313.   end
  314.  
  315.   def policy
  316.     @nav_obj = 'policy'
  317.     render :layout => 'static'
  318.   end
  319.  
  320.   def help
  321.     @nav_obj = 'help'
  322.     render :layout => 'static'
  323.   end
  324.  
  325.     private
  326.         def store_setting_params
  327.             params.require(:store_setting).permit(:store_username, :store_name, :phone_hp, :mobile_number, :website, :merchant_type, :know_us_text, :country, :city, :paypal_email)
  328.         end
  329.  
  330.         def profile_params
  331.             params.require(:profile).permit(:first_name, :last_name, :gender, :phone_number)
  332.         end
  333.  
  334.         def set_user_profile
  335.             @profile = current_user.profile
  336.         end
  337.  
  338.         def set_accounts
  339.             @store_setting = current_user.store_setting
  340.         end
  341. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement