Skip to content

Admin improvements #603

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 2 commits into from
Mar 16, 2021
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: 5 additions & 10 deletions app/admin/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

form do |f|
f.inputs "Admin Details" do
f.inputs do
f.input :name
end
f.actions
Expand All @@ -18,17 +18,12 @@
row :created_at
row :updated_at
row :name_translations do
cat.disable_fallback
content_tag :div do
I18n.available_locales.map do |loc|
next unless cat.send("name_#{loc}")
content_tag(:strong, "#{loc}: ") +
content_tag(:span, cat.send("name_#{loc}"))
end.compact.sum
end
cat.name_translations.map do |locale, translation|
tag.strong("#{I18n.t("locales.#{locale}", locale: locale)}: ") +
tag.span(translation)
end.join(" | ").html_safe
end
end
active_admin_comments
end

permit_params :name
Expand Down
5 changes: 2 additions & 3 deletions app/admin/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
end

collection_action :import_csv, method: :post do
errors = CsvDb.convert_save(params[:dump][:organization_id],
params[:dump][:file])
errors = UserImporter.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
# selectable_column
column do |user|
link_to image_tag(avatar_url(user, 24)), admin_user_path(user)
end
Expand Down
17 changes: 3 additions & 14 deletions app/assets/stylesheets/active_admin.scss
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
// SASS variable overrides must be declared before loading up Active Admin's styles.
//
// To view the variables that Active Admin provides, take a look at
// `app/assets/stylesheets/active_admin/mixins/_variables.css.scss` in the
// Active Admin source.
//
// For example, to change the sidebar width:
// $sidebar-width: 242px;
$primary-color: #165e6d;
$link-color: $primary-color;
$table-stripe-color: #f5f5f5;

// Active Admin's got SASS!
@import "active_admin/mixins";
@import "active_admin/base";

// Overriding any non-variable SASS must be done after the fact.
// For example, to change the default status-tag color:
//
// .status_tag { background: #6090DB; }
45 changes: 0 additions & 45 deletions app/models/csv_db.rb

This file was deleted.

75 changes: 75 additions & 0 deletions app/services/user_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Used in the Admin section to import users and members
# to a specific organization, from a CSV file.

require "csv"

class UserImporter
Row = Struct.new(
:username,
:member_id,
:entry_date,
:date_of_birth,
:email,
:phone,
:alt_phone,
:gender
) do
def user_from_row
User.new(
username: username,
date_of_birth: date_of_birth,
email: email,
phone: phone,
alt_phone: alt_phone,
gender: gender
)
end
end

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

CSV.parse(data, headers: false) do |data_row|
row = Row.new(
data_row[2..4].join(" "),
data_row[0],
data_row[1],
data_row[6],
data_row[9],
data_row[7],
data_row[8],
User::GENDERS[data_row[5].to_i - 1]
)
process_row(row, organization, errors)
end

organization.ensure_reg_number_seq!
errors
end

def process_row(row, organization, errors)
user = row.user_from_row
user.skip_confirmation! # auto-confirm, not sending confirmation email

if user.save
member_from_row(row, user, organization, errors)
else
errors.push(member_id: row.member_id, email: row.email,
errors: user.errors.full_messages)
end
end

def member_from_row(row, user, organization, errors)
member = organization.members.create(member_uid: row.member_id,
entry_date: row.entry_date,
user_id: user.id)

errors.push(member_id: row.member_id, email: row.email,
errors: member.errors.full_messages
) if member.errors.present?
end
end
end
6 changes: 3 additions & 3 deletions app/views/admin/csv/upload_csv.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<%= form_for :dump, url: { action: "import_csv" }, html: { multipart: true, class: "formtastic user" } do |f| %>
<%= form_for :dump, url: { action: "import_csv" }, html: { multipart: true } do |f| %>
<fieldset class="inputs">
<ol>
<li class="string input optional stringish">
<%= label_tag I18n.t("active_admin.users.upload_csv") %>
<%= f.file_field :file %>
<%= f.file_field :file, required: true %>
</li>
</ol>
<ol>
<li class="string input optional stringish">
<%= label_tag I18n.t("active_admin.users.organization") %>
<%= f.select :organization_id, options_for_select(Organization.all.map{ |org| [org.name, org.id] }), include_blank: true %>
<%= f.select :organization_id, options_for_select(Organization.pluck(:name, :id)), { include_blank: true }, { required: true } %>
</li>
</ol>
</fieldset>
Expand Down
Loading