app/controllers/tours_controller.rb
# Never trust parameters from the scary internet, only allow the white list through.
Never trust parameters from the scary internet, only allow the white list through.
Never trust parameters from the scary internet only allow the white list through .
def tour_params
    split_val = /[,;.]+/
    organizer = params[:tour][:organizer_id] || params[:tour][:organizer]
    if organizer.class == String
      new_organizer = Organizer.find_by_name(organizer)
      unless new_organizer.nil?
        new_user = new_organizer.user
        params[:tour][:user] = new_user
        params[:tour][:organizer] = new_organizer
      end
    end
    if params[:tour][:tags] == "" or params[:tour][:tags].nil?
      params[:tour][:tag_ids] = []
    else
      params[:tour][:tag_ids] = params[:tour][:tags]
        .split(split_val)
        .map{|t| Tag.find_or_create_by(name: t).try(:id) }.compact
    end
    params[:tour].delete(:tags)
    if params[:tour][:languages] == "" or params[:tour][:languages].nil?
      params[:tour][:languages] = []
    else
      params[:tour][:language_ids] = params[:tour][:languages]
        .split(split_val)
        .map{|l| Language.find_or_create_by(name: l).try(:id) }.compact
    end
    params[:tour].delete(:languages)
    pkg_attr = params[:tour][:packages_attributes]
    if !pkg_attr.nil?
      pkg_attr.each do |p|
        included_array = p[1]["included"].split(split_val) rescue nil
        if included_array
          params[:tour][:packages_attributes][p[0]][:included] = included_array
        else
          params[:tour][:packages_attributes][p[0]][:included] = []
        end
      end
    end
    if params[:tour][:included] == "" or params[:tour][:included].nil?
      params[:tour][:included] = []
    else
      included_to_array = params[:tour][:included].split(split_val)
      included = []
      included_to_array.each do |i|
        included.push i
      end
      params[:tour][:included] = included
    end
    if params[:tour][:nonincluded] == "" or params[:tour][:nonincluded].nil?
      params[:tour][:nonincluded] = []
    else
      nonincluded_to_array = params[:tour][:nonincluded].split(split_val)
      nonincluded = []
      nonincluded_to_array.each do |i|
        nonincluded.push i
      end
      params[:tour][:nonincluded] = nonincluded
    end
    if params[:tour][:take] == "" or params[:tour][:take].nil?
      params[:tour][:take] = []
    else
      take_to_array = params[:tour][:take].split(split_val)
      take = []
      take_to_array.each do |t|
        take.push t
      end
      params[:tour][:take] = take
    end
    if params[:tour][:goodtoknow] == "" or params[:tour][:goodtoknow].nil?
      params[:tour][:goodtoknow] = []
    else
      goodtoknow_to_array = params[:tour][:goodtoknow].split(split_val)
      goodtoknow = []
      goodtoknow_to_array.each do |g|
        goodtoknow.push g
      end
      params[:tour][:goodtoknow] = goodtoknow
    end
    current_category = params[:tour][:category]
    if current_category == "" or current_category.nil?
      params[:tour][:category] = Category.find_or_create_by(name: 'Outras')
    else
      begin
        params[:tour][:category] = Category.find(current_category)
      rescue => e
        params[:tour][:category] = Category.find_or_create_by(name: current_category)
      end
    end
    params[:tour][:attractions] = []
    params[:tour][:currency] = "BRL"
    params.fetch(:tour, {}).permit!
  end
def tour_params split_val = / [,;.]+ / organizer = params [ :tour ] [ :organizer_id ] || params [ :tour ] [ :organizer ] if organizer . class == String new_organizer = Organizer . find_by_name ( organizer ) unless new_organizer . nil? new_user = new_organizer . user params [ :tour ] [ :user ] = new_user params [ :tour ] [ :organizer ] = new_organizer end end if params [ :tour ] [ :tags ] == "" or params [ :tour ] [ :tags ] . nil? params [ :tour ] [ :tag_ids ] = [ ] else params [ :tour ] [ :tag_ids ] = params [ :tour ] [ :tags ] . split ( split_val ) . map { | t | Tag . find_or_create_by ( name : t ) . try ( :id ) } . compact end params [ :tour ] . delete ( :tags ) if params [ :tour ] [ :languages ] == "" or params [ :tour ] [ :languages ] . nil? params [ :tour ] [ :languages ] = [ ] else params [ :tour ] [ :language_ids ] = params [ :tour ] [ :languages ] . split ( split_val ) . map { | l | Language . find_or_create_by ( name : l ) . try ( :id ) } . compact end params [ :tour ] . delete ( :languages ) pkg_attr = params [ :tour ] [ :packages_attributes ] if ! pkg_attr . nil? pkg_attr . each do | p | included_array = p [ 1 ] [ "included" ] . split ( split_val ) rescue nil if included_array params [ :tour ] [ :packages_attributes ] [ p [ 0 ] ] [ :included ] = included_array else params [ :tour ] [ :packages_attributes ] [ p [ 0 ] ] [ :included ] = [ ] end end end if params [ :tour ] [ :included ] == "" or params [ :tour ] [ :included ] . nil? params [ :tour ] [ :included ] = [ ] else included_to_array = params [ :tour ] [ :included ] . split ( split_val ) included = [ ] included_to_array . each do | i | included . push i end params [ :tour ] [ :included ] = included end if params [ :tour ] [ :nonincluded ] == "" or params [ :tour ] [ :nonincluded ] . nil? params [ :tour ] [ :nonincluded ] = [ ] else nonincluded_to_array = params [ :tour ] [ :nonincluded ] . split ( split_val ) nonincluded = [ ] nonincluded_to_array . each do | i | nonincluded . push i end params [ :tour ] [ :nonincluded ] = nonincluded end if params [ :tour ] [ :take ] == "" or params [ :tour ] [ :take ] . nil? params [ :tour ] [ :take ] = [ ] else take_to_array = params [ :tour ] [ :take ] . split ( split_val ) take = [ ] take_to_array . each do | t | take . push t end params [ :tour ] [ :take ] = take end if params [ :tour ] [ :goodtoknow ] == "" or params [ :tour ] [ :goodtoknow ] . nil? params [ :tour ] [ :goodtoknow ] = [ ] else goodtoknow_to_array = params [ :tour ] [ :goodtoknow ] . split ( split_val ) goodtoknow = [ ] goodtoknow_to_array . each do | g | goodtoknow . push g end params [ :tour ] [ :goodtoknow ] = goodtoknow end current_category = params [ :tour ] [ :category ] if current_category == "" or current_category . nil? params [ :tour ] [ :category ] = Category . find_or_create_by ( name : 'Outras' ) else begin params [ :tour ] [ :category ] = Category . find ( current_category ) rescue => e params [ :tour ] [ :category ] = Category . find_or_create_by ( name : current_category ) end end params [ :tour ] [ :attractions ] = [ ] params [ :tour ] [ :currency ] = "BRL" params . fetch ( :tour , { } ) . permit! end
Never trust parameters from the scary internet, only allow the white list through.
Never trust parameters from the scary internet only allow the white list through .
#current_where = params[:tour][:wheres_attributes][0] #if current_where #  where_exist = Where.where({place_id: current_where["0"][:place_id] }) #else #  where_exist = {} #end #if where_exist.any? #  params[:tour][:wheres_attributes] = where_exist[0] #end #params.fetch(:tour, {}).permit(:title, :organizer_id, :status, {:packages => [:name, :value, :included]}, {:packages_attributes => [:name, :value, :included]}, {:where_attributes => [:name, :place_id, :background_id, :lat, :long, :city, :state, :country, :postal_code, :address, :google_id, :url]}, :user_id, :picture, :link, :address, :availability, :minimum, :maximum, :difficulty, :start, :end, :value, :description, {:included => []}, {:nonincluded => []}, {:take => []}, {:goodtoknow => []}, :meetingpoint, :category_id, :category, :tags, {:languages => []}, :organizer, :user, {:attractions => []}, :currency).merge(params[:tour])