Giter Club home page Giter Club logo

Comments (7)

Fivell avatar Fivell commented on July 30, 2024

@sunyatis , pls add you database table structure and AR model
Also add ActiveRecord queries which are performed during import, you can see them in development.log

from active_admin_import.

sunyatis avatar sunyatis commented on July 30, 2024

CREATE TABLE "courses" (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
catalog_id integer,
title varchar,
code varchar,
description text,
credit integer,
start_date date,
end_date date,
local_course_id varchar,
prefix varchar,
section varchar,
prerequisites text,
corequisites text,
books_url varchar,
registration_url varchar,
active boolean,
level varchar,
school_id integer,
subjectarea_id varchar,
course_area varchar,
instructor varchar,
course_method varchar,
seats_available integer,
class_full boolean,
created_at datetime NOT NULL,
updated_at datetime NOT NULL,
generaleducation_id integer,
slug text,
cat_id integer
)

from active_admin_import.

sunyatis avatar sunyatis commented on July 30, 2024
class Course < ActiveRecord::Base
  extend FriendlyId

  belongs_to :subject_area, :foreign_key => 'subjectarea_id' , :class_name => "SubjectArea"
  belongs_to :school, :foreign_key => 'school_id', :class_name => "School"
  belongs_to :catalog, :foreign_key => 'catalog_id', :class_name => "Catalog"
  belongs_to :general_education, :foreign_key => 'generaleducation_id' , :class_name => "GeneralEducation"
  belongs_to :category, :foreign_key => 'cat_id', :class_name => "Category"

  has_paper_trail


  filterrific default_filter_params: { :sorted_by => 'title_asc' },
               available_filters: [
                 :sorted_by,
                 :search_query,
                 :with_school_id,
                 :with_catalog_id,
                 :with_generaleducation_id,
                 :with_start_date_gte,
                 :with_start_date,
                 :with_subject_area_id,
                 :with_course_area,
                 :with_level,
                 :with_credit,
                 :with_category
               ]

   # default for will_paginate
   #self.per_page = 10

   scope :search_query, ->(query){ 
     return nil  if query.blank?
     puts query.to_s
     # condition query, parse into individual keywords
     terms = query.downcase.split(/\s+/)
     puts terms
     # replace "*" with "%" for wildcard searches,
     # append '%', remove duplicate '%'s
     terms = terms.map { |e|
       (e.gsub('*', '%') + '%').gsub(/%+/, '%')
     }
     # configure number of OR conditions for provision
     # of interpolation arguments. Adjust this if you
     # change the number of OR conditions.
     num_or_conditions = 2

     where(
       terms.map {
         or_clauses = [
           "LOWER(courses.title) LIKE ?",
           "LOWER(courses.description) LIKE ?"
         ].join(' OR ')
         "(#{ or_clauses })"
       }.join(' AND '),
       *terms.map { |e| [e] * num_or_conditions }.flatten
     )
   }

   scope :sorted_by, lambda { |sort_option|
     # extract the sort direction from the param value.
     direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
     case sort_option.to_s
     when /^title_/
       order("courses.title #{ direction }")
     when /^start_date_/
      order("courses.start_date #{ direction }")
     when /^credit_/
      order("courses.credit #{ direction }")
     when /^level_/
       order("LOWER(programs.level) #{ direction }")
     else
       raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
     end
   }
   scope :with_school_id, lambda { |school_ids|
     where(:school_id => [*school_ids])
   }
   scope :with_start_date, lambda { |start_date|
    }
   scope :with_start_date_gte, lambda { |start_date|
     where('courses.start_date >= ?', start_date)
   }
   scope :with_catalog_id, lambda { |catalog_ids|
     where(:catalog_id => [*catalog_ids])
   }
  scope :with_generaleducation_id , ->(generaleducation_ids){ 
      return nil  if generaleducation_ids.blank?

      # condition query, parse into individual keywords
      terms = generaleducation_ids.join(" ").split(/\s+/)

 #    # configure number of OR conditions for provision
 #    # of interpolation arguments. Adjust this if you
 #    # change the number of OR conditions.
     num_or_conditions = 1
 #
     where(
      terms.map { |e|
        or_clauses = [
           "courses.generaleducation_id LIKE ?", 
           "courses.generaleducation_id LIKE \'%, #{e}\'", 
           "courses.generaleducation_id LIKE \'#{e},%\'", 
           "courses.generaleducation_id LIKE \'%, #{e},%\'"
         ].join(' OR ')
         "(#{ or_clauses })"
       }.join(' AND '),
       *terms.map { |e| [e] * num_or_conditions }.flatten
     )
   }

  scope :with_subject_area_id, lambda { |subjectarea_ids|
     where(:subjectarea_id => [*subjectarea_ids])
   }
   scope :with_course_area, lambda { |course_areas|
       where(:course_area => [*course_areas])
     }
  scope :with_level, lambda { |levels|
    where(:level => [*levels])
  }
  scope :with_credit, lambda { |credits|
    where(:credit => [*credits])
  }
  scope :with_category, ->(categories){ 
     return nil  if categories.blank?

     # condition query, parse into individual keywords
     terms = categories.join(" ").split(/\s+/)

#    # configure number of OR conditions for provision
#    # of interpolation arguments. Adjust this if you
#    # change the number of OR conditions.
    num_or_conditions = 1
  #  Course.where("courses.cat_id IN ('2')")

   where(
    terms.map { |e|
      or_clauses = [
         "courses.cat_id IN ('#{e}')"#, 
        # "courses.cat_id LIKE \'%, #{e}\'", 
        # "courses.cat_id LIKE \'#{e},%\'", 
        # "courses.cat_id LIKE \'%, #{e},%\'"
       ].join(' OR ')
       "(#{ or_clauses })"
     }.join(' AND '),
     *terms.map { |e| [e] * num_or_conditions }.flatten
   )
  }

   def self.options_for_sorted_by
     [
       ['Start Date DESC', 'start_date_desc'],
       ['Start Date ASC', 'start_date_asc'],
       ['Title ASC', 'title_asc'],
       ['Credits', 'credit_asc'],
     ]
   end

   def self.options_for_level_select
     Course.pluck(:level).uniq
   end
   def self.options_for_credit_select
      Course.pluck(:credit).uniq
   end
   def self.options_for_course_area_select
      Course.order(:course_area).pluck(:course_area).uniq
   end



 #  scope :search_query, lambda { |query|
 #    return nil  if query.blank?
 #    # condition query, parse into individual keywords
 #    terms = query.downcase.split(/\s+/)
 #    # replace "*" with "%" for wildcard searches,
 #    # append '%', remove duplicate '%'s
 #    terms = terms.map { |e|
 #      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
 #    }
 #    # configure number of OR conditions for provision
 #    # of interpolation arguments. Adjust this if you
 #    # change the number of OR conditions.
 #    num_or_conditions = 3
 #    where(
 #      terms.map {
 #        or_clauses = [
 #          "LOWER(students.first_name) LIKE ?",
 #          "LOWER(students.last_name) LIKE ?",
 #          "LOWER(students.email) LIKE ?"
 #        ].join(' OR ')
 #        "(#{ or_clauses })"
 #      }.join(' AND '),
 #      *terms.map { |e| [e] * num_or_conditions }.flatten
 #    )
 #  }

 #Convert time on import
 def self.strptime(str)
   return nil unless str.present?
   #puts str
   #puts "here"
   if str == "start_date"
     return "start_date"
   end
   month, day, year = str.to_s.strip.split('/')
   if year.to_s.length == 2
     #puts "here2"
      Date.strptime(str.to_s.strip, '%m/%d/%y')
   elsif year.to_s.length == 4
     #puts "here4"
     Date.strptime(str.to_s.strip, '%m/%d/%y')
   end
 end


 def admin_permalink
    admin_post_path(self)
end

# Get Catalog name
def get_catalog_name(id)
  Catalog.find(id).name
end
# Get School name
def get_school_name(id)
  School.find(id).name
end


#friendly ids
friendly_id :generate_custom_slug, use:  [:slugged, :finders]

def generate_custom_slug
    "#{get_school_name(school_id)}-#{title}-#{code}-#{section}-#{get_catalog_name(catalog_id)}"
end

end

from active_admin_import.

sunyatis avatar sunyatis commented on July 30, 2024

I'm getting this error: Error: undefined method `has_key?' for "2,3":String

from active_admin_import.

Fivell avatar Fivell commented on July 30, 2024

@sunyatis , can you post backtrace of error, message can be not enough

from active_admin_import.

sunyatis avatar sunyatis commented on July 30, 2024

This error is showing directly inside of active admin. It's not producing an error page. Is there another way to find that?

This is what's showing in the console:

tarted POST "/admin/courses/do_import" for ::1 at 2015-12-10 11:17:05 -0500
Processing by Admin::CoursesController#do_import as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jMOH9Ym968/is48FgubZApdZmoMKVROFLShpEKcMgTqQpsTSM7M/C3dlLcBlDILSWjdvVE8suAFP4bPIl55+kg==", "active_admin_import_model"=>{"catalog_id"=>"1", "school_id"=>"2", "file"=>#<ActionDispatch::Http::UploadedFile:0x007feec4caa778 @tempfile=#Tempfile:/var/folders/n1/hdpryrgx4vg2wp7tb35vr0vr0000gn/T/RackMultipart20151210-8174-uv0gmz.csv, @original_filename="courses.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name="active_admin_import_model[file]"; filename="courses.csv"\r\nContent-Type: text/csv\r\n">}, "commit"=>"Import"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 ["id", 2] begin transaction
SQL (2.1ms) DELETE FROM "courses" WHERE "courses"."local_course_id" IN ('local_course_id', '14FA_ADI_ANT101_2', '14FA_ADI_ANT101_1')
DEPRECATION WARNING: Passing a nested array to Active Record finder methods is deprecated and will be removed. Flatten your array before using it for 'IN' conditions. (called from block (2 levels) in <top (required)> at /Users/ameliamanders/Documents/Code_and_Graphics/Code/Sites/open_suny_navigator_2_0/app/admin/course.rb:12)
(0.1ms) SELECT "subject_areas"."id" FROM "subject_areas" WHERE "subject_areas"."name" IN ('subjectarea_id', 'subjectarea_id', 'Communications, Business', 'Communications', 'Business', 'Business, The Arts', 'Business', 'The Arts')
(0.3ms) rollback transaction
Redirected to http://localhost:3000/admin/courses/import
Completed 302 Found in 9ms (ActiveRecord: 2.8ms)

Started GET "/admin/courses/import" for ::1 at 2015-12-10 11:17:05 -0500
Processing by Admin::CoursesController#import as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 ["id", 2] SELECT "catalogs"."name", "catalogs"."id" FROM "catalogs"
DEPRECATION WARNING: input_class is deprecated and will be removed from Formtastic 4.0 (configure Formtastic::FormBuilder.input_class_finder instead (upgrade guide on wiki: http://bit.ly/1F9QtKc )). (called from block (2 levels) in _app_views_admin_course_import_html_erb__3824103305150416633_70331696095300 at /Users/ameliamanders/Documents/Code_and_Graphics/Code/Sites/open_suny_navigator_2_0/app/views/admin/course_import.html.erb:6)
(0.3ms) SELECT "schools"."name", "schools"."id" FROM "schools"
DEPRECATION WARNING: action_class is deprecated and will be removed from Formtastic 4.0 (configure Formtastic::FormBuilder.action_class_finder instead (upgrade guide on wiki: http://bit.ly/1F9QtKc )). (called from block (2 levels) in _app_views_admin_course_import_html_erb__3824103305150416633_70331696095300 at /Users/ameliamanders/Documents/Code_and_Graphics/Code/Sites/open_suny_navigator_2_0/app/views/admin/course_import.html.erb:11)
Rendered admin/course_import.html.erb within layouts/active_admin (11.3ms)
Completed 200 OK in 209ms (Views: 207.3ms | ActiveRecord: 0.6ms)

from active_admin_import.

Fivell avatar Fivell commented on July 30, 2024

@sunyatis , problem that you're passing string isntead of hash calling importer.batch_replace(:subjectarea_id, subjects_join)
instead of subjects_join string you should create hash where it's keys which will be replace by values .
For instance

if your csv has 2 strings like
Business, Communications
The Arts, History

in subjectarea_id column.

You have to pass

{
'Business, Communications' => '1,2' #ids that you should read from database
'The Arts, History' => '3,4'
}

etc

Unfortunately this gem has no nice DSL to do such complicated tasks.

from active_admin_import.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.