Skip to content

[Feature] Import posts from CSV #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/admin/post.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
ActiveAdmin.register Post do
action_item :upload_csv, only: :index do
link_to I18n.t("active_admin.users.upload_from_csv"), action: "upload_csv"
end

collection_action :upload_csv do
render "admin/csv/upload_csv"
end

collection_action :import_csv, method: :post do
errors = PostImporter.call(params[:dump][:organization_id], params[:dump][:file])
flash[:error] = errors.join("<br/>").html_safe if errors.present?

redirect_to action: :index
end

index do
id_column
column :class
Expand Down
60 changes: 60 additions & 0 deletions app/services/post_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Used in the Admin section to import posts
# to a specific organization, from a CSV file.

require "csv"

class PostImporter
Row = Struct.new(
:user_id,
:type,
:title,
:description,
:category_id,
:created_at,
:start_on,
:end_on
) do
def post_from_row
Object.const_get(type).new(
user_id: user_id,
title: title,
description: description,
category_id: category_id,
created_at: created_at,
start_on: start_on,
end_on: end_on
)
end
end

class << self
def call(organization_id, csv_data)
data = csv_data.read
errors = []

CSV.parse(data, headers: false) do |data_row|
row = Row.new(
data_row[0],
data_row[1],
data_row[2],
data_row[3],
data_row[4],
data_row[5],
data_row[6],
data_row[7]
)
process_row(row, organization_id, errors)
end

errors
end

def process_row(row, organization_id, errors)
post = row.post_from_row
post.organization_id = organization_id
return if post.save

errors.push(user_id: row.user_id, title: row.title, errors: post.errors.full_messages)
end
end
end