app/controllers/import_controller.rb
def generic_import
    authorize! :import, :generic_import
    if params[:generic_import] ## file upload -> do validation
      if !(file = params[:generic_import][:file])
        flash.now[:alert] = "Bitte wähle eine Datei."
        render :index
        return
      end
      
      begin
        @sheet = open_spreadsheet file
      rescue Exception => e
        flash.now[:alert] = e.message
        render :index
        return
      end
      
      @users = []
      @lists = []
      @subscriptions = []
      
      @sheet.drop(1).each do |line|
        # try to find user
        user = User.find_by_email(line[2].downcase)
        # try to find list
        list = List.find_by_name(line[3])
        
        if (user && list)# if both found
          # create new subscription
          s = Subscription.find_by(user: user, list: list)
          if s
            # nothing  
          else
            @subscriptions << {user: user.email,list: list.name}
          end 
        elsif (user) # if user exists
          # and list not empty
          if !(line[3] == nil || line[3] == '')
            # create new list, then subscription
            @lists << list = List.new(name: line[3])
            @subscriptions << {user: user.email,list: list.name}
          end
        elsif (list) # if list exists
          # create new user, then subscription
          @users << user = User.new(first_name: line[0], name: line[1], email: line[2].downcase)
          @subscriptions << {user: user.email,list: list.name}
        elsif # if nothing exists
          # create user
          @users << user = User.new(first_name: line[0], name: line[1], email: line[2].downcase)
          
          if !(line[3] == nil || line[3] == '') # and if list not empty also list
            # create all
            @lists << list = List.new(name: line[3])
            @subscriptions << {user: user.email,list: list.name}
          end
        end
        
        @users.uniq! {|u| u.email}
        @lists.uniq! {|l| l.name}
       
      end
    elsif params[:confirm]
      
      list_params = JSON.parse params[:lists]
      user_params = JSON.parse params[:users]
      subscription_params = JSON.parse params[:subscriptions]
      
      
      @lists = []
      @users = []
      @subscriptions = []
      
      successful_saved = 0
      
      list_params.each do |params|
        x = List.new params
        @lists << x
        if x.save
          successful_saved += 1
        end
      end
      
      user_params.each do |params|
        x = User.new params
        @users << x
        if x.save
          successful_saved += 1
        end
      end
      
      subscription_params.each do |subscription|
        x = Subscription.new(user: User.find_by_email(subscription['user']), list: List.find_by_name(subscription['list']))
        @subscriptions << x
        if x.save
          successful_saved += 1
        end
      end
      
      n = @subscriptions.count + @users.count + @lists.count
      
      flash.now[:notice] = "Import abgeschlossen. #{successful_saved} von #{n} Datensätzen erfolgreich importiert."
      render :index
    else
      redirect_to import_url
    end
    
  end
# Generic import, creates on behalf users
Generic import, creates on behalf users
Generic import creates on behalf users
def generic_import
    authorize! :import, :generic_import
    if params[:generic_import] 
      if !(file = params[:generic_import][:file])
        flash.now[:alert] = "Bitte wähle eine Datei."
        render :index
        return
      end
      begin
        @sheet = open_spreadsheet file
      rescue Exception => e
        flash.now[:alert] = e.message
        render :index
        return
      end
      @users = []
      @lists = []
      @subscriptions = []
      @sheet.drop(1).each do |line|
        user = User.find_by_email(line[2].downcase)
        list = List.find_by_name(line[3])
        if (user && list)
          s = Subscription.find_by(user: user, list: list)
          if s
          else
            @subscriptions << {user: user.email,list: list.name}
          end 
        elsif (user) 
          if !(line[3] == nil || line[3] == '')
            @lists << list = List.new(name: line[3])
            @subscriptions << {user: user.email,list: list.name}
          end
        elsif (list) 
          @users << user = User.new(first_name: line[0], name: line[1], email: line[2].downcase)
          @subscriptions << {user: user.email,list: list.name}
        elsif 
          @users << user = User.new(first_name: line[0], name: line[1], email: line[2].downcase)
          if !(line[3] == nil || line[3] == '') 
            @lists << list = List.new(name: line[3])
            @subscriptions << {user: user.email,list: list.name}
          end
        end
        @users.uniq! {|u| u.email}
        @lists.uniq! {|l| l.name}
      end
    elsif params[:confirm]
      list_params = JSON.parse params[:lists]
      user_params = JSON.parse params[:users]
      subscription_params = JSON.parse params[:subscriptions]
      @lists = []
      @users = []
      @subscriptions = []
      successful_saved = 0
      list_params.each do |params|
        x = List.new params
        @lists << x
        if x.save
          successful_saved += 1
        end
      end
      user_params.each do |params|
        x = User.new params
        @users << x
        if x.save
          successful_saved += 1
        end
      end
      subscription_params.each do |subscription|
        x = Subscription.new(user: User.find_by_email(subscription['user']), list: List.find_by_name(subscription['list']))
        @subscriptions << x
        if x.save
          successful_saved += 1
        end
      end
      n = @subscriptions.count + @users.count + @lists.count
      flash.now[:notice] = "Import abgeschlossen. #{successful_saved} von #{n} Datensätzen erfolgreich importiert."
      render :index
    else
      redirect_to import_url
    end
  end
def generic_import authorize! :import , :generic_import if params [ :generic_import ] if ! ( file = params [ :generic_import ] [ :file ] ) flash . now [ :alert ] = "Bitte wähle eine Datei." render :index return end begin @sheet = open_spreadsheet file rescue Exception => e flash . now [ :alert ] = e . message render :index return end @users = [ ] @lists = [ ] @subscriptions = [ ] @sheet . drop ( 1 ) . each do | line | user = User . find_by_email ( line [ 2 ] . downcase ) list = List . find_by_name ( line [ 3 ] ) if ( user && list ) s = Subscription . find_by ( user : user , list : list ) if s else @subscriptions << { user : user . email , list : list . name } end elsif ( user ) if ! ( line [ 3 ] == nil || line [ 3 ] == '' ) @lists << list = List . new ( name : line [ 3 ] ) @subscriptions << { user : user . email , list : list . name } end elsif ( list ) @users << user = User . new ( first_name : line [ 0 ] , name : line [ 1 ] , email : line [ 2 ] . downcase ) @subscriptions << { user : user . email , list : list . name } elsif @users << user = User . new ( first_name : line [ 0 ] , name : line [ 1 ] , email : line [ 2 ] . downcase ) if ! ( line [ 3 ] == nil || line [ 3 ] == '' ) @lists << list = List . new ( name : line [ 3 ] ) @subscriptions << { user : user . email , list : list . name } end end @users . uniq! { | u | u . email } @lists . uniq! { | l | l . name } end elsif params [ :confirm ] list_params = JSON . parse params [ :lists ] user_params = JSON . parse params [ :users ] subscription_params = JSON . parse params [ :subscriptions ] @lists = [ ] @users = [ ] @subscriptions = [ ] successful_saved = 0 list_params . each do | params | x = List . new params @lists << x if x . save successful_saved += 1 end end user_params . each do | params | x = User . new params @users << x if x . save successful_saved += 1 end end subscription_params . each do | subscription | x = Subscription . new ( user : User . find_by_email ( subscription [ 'user' ] ) , list : List . find_by_name ( subscription [ 'list' ] ) ) @subscriptions << x if x . save successful_saved += 1 end end n = @subscriptions . count + @users . count + @lists . count flash . now [ :notice ] = "Import abgeschlossen. #{successful_saved} von #{n} Datensätzen erfolgreich importiert." render :index else redirect_to import_url end end
Generic import, creates on behalf users
Generic import creates on behalf users
## file upload -> do validation # try to find user # try to find list # if both found # create new subscription # nothing   # if user exists # and list not empty # create new list, then subscription # if list exists # create new user, then subscription # if nothing exists # create user # and if list not empty also list # create all