forked from Terrastories/terrastories
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Terrastories#138 from rubyforgood/csv_import
CSV Import
- Loading branch information
Showing
9 changed files
with
253 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
class Place < ApplicationRecord | ||
has_many :points | ||
require 'csv' | ||
has_many :points | ||
|
||
def self.import_csv(filename) | ||
CSV.parse(filename, headers: true) do |row| | ||
loc = Place.where(name: row[0], type_of_place: row[1]).first_or_create | ||
loc.points.create(title:row[0], lat: row[5].to_f, lng: row[4].to_f, region: row[3] ) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<%# | ||
# Collection | ||
This partial is used on the `index` and `show` pages | ||
to display a collection of resources in an HTML table. | ||
## Local variables: | ||
- `collection_presenter`: | ||
An instance of [Administrate::Page::Collection][1]. | ||
The table presenter uses `ResourceDashboard::COLLECTION_ATTRIBUTES` to determine | ||
the columns displayed in the table | ||
- `resources`: | ||
An ActiveModel::Relation collection of resources to be displayed in the table. | ||
By default, the number of resources is limited by pagination | ||
or by a hard limit to prevent excessive page load times | ||
[1]: https://www.rubydoc.info/gems/administrate/Administrate/Page/Collection | ||
%> | ||
|
||
<table aria-labelledby="<%= table_title %>"> | ||
<thead> | ||
<tr> | ||
<% collection_presenter.attribute_types.each do |attr_name, attr_type| %> | ||
<th class="cell-label | ||
cell-label--<%= attr_type.html_class %> | ||
cell-label--<%= collection_presenter.ordered_html_class(attr_name) %>" | ||
scope="col" | ||
role="columnheader" | ||
aria-sort="<%= sort_order(collection_presenter.ordered_html_class(attr_name)) %>"> | ||
<%= link_to(sanitized_order_params(page, collection_field_name).merge( | ||
collection_presenter.order_params_for(attr_name, key: collection_field_name) | ||
)) do %> | ||
<%= t( | ||
"helpers.label.#{collection_presenter.resource_name}.#{attr_name}", | ||
default: attr_name.to_s, | ||
).titleize %> | ||
<% if collection_presenter.ordered_by?(attr_name) %> | ||
<span class="cell-label__sort-indicator cell-label__sort-indicator--<%= collection_presenter.ordered_html_class(attr_name) %>"> | ||
<svg aria-hidden="true"> | ||
<use xlink:href="#icon-up-caret" /> | ||
</svg> | ||
</span> | ||
<% end %> | ||
<% end %> | ||
</th> | ||
<% end %> | ||
<% [valid_action?(:edit, collection_presenter.resource_name), | ||
valid_action?(:destroy, collection_presenter.resource_name)].count(true).times do %> | ||
<th scope="col"></th> | ||
<% end %> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% resources.each do |resource| %> | ||
<tr class="js-table-row" | ||
tabindex="0" | ||
<% if valid_action? :show, collection_presenter.resource_name %> | ||
<%= %(role=link data-url=#{polymorphic_path([namespace, resource])}) %> | ||
<% end %> | ||
> | ||
<% collection_presenter.attributes_for(resource).each do |attribute| %> | ||
<td class="cell-data cell-data--<%= attribute.html_class %>"> | ||
<% if show_action? :show, resource -%> | ||
<a href="<%= polymorphic_path([namespace, resource]) -%>" | ||
class="action-show" | ||
> | ||
<%= render_field attribute %> | ||
</a> | ||
<% end -%> | ||
</td> | ||
<% end %> | ||
<% if valid_action? :edit, collection_presenter.resource_name %> | ||
<td><%= link_to( | ||
t("administrate.actions.edit"), | ||
[:edit, namespace, resource], | ||
class: "action-edit", | ||
) if show_action? :edit, resource%></td> | ||
<% end %> | ||
<% if valid_action? :destroy, collection_presenter.resource_name %> | ||
<td><%= link_to( | ||
t("administrate.actions.destroy"), | ||
[namespace, resource], | ||
class: "text-color-red", | ||
method: :delete, | ||
data: { confirm: t("administrate.actions.confirm") } | ||
) if show_action? :destroy, resource %></td> | ||
<% end %> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<%# | ||
# Index | ||
This view is the template for the index page. | ||
It is responsible for rendering the search bar, header and pagination. | ||
It renders the `_table` partial to display details about the resources. | ||
## Local variables: | ||
- `page`: | ||
An instance of [Administrate::Page::Collection][1]. | ||
Contains helper methods to help display a table, | ||
and knows which attributes should be displayed in the resource's table. | ||
- `resources`: | ||
An instance of `ActiveRecord::Relation` containing the resources | ||
that match the user's search criteria. | ||
By default, these resources are passed to the table partial to be displayed. | ||
- `search_term`: | ||
A string containing the term the user has searched for, if any. | ||
- `show_search_bar`: | ||
A boolean that determines if the search bar should be shown. | ||
[1]: https://www.rubydoc.info/gems/administrate/Administrate/Page/Collection | ||
%> | ||
<% content_for(:title) do %> | ||
<%= display_resource_name(page.resource_name) %> | ||
<% end %> | ||
|
||
<header class="main-content__header" role="banner"> | ||
<h1 class="main-content__page-title" id="page-title"> | ||
<%= content_for(:title) %> | ||
</h1> | ||
|
||
<% if show_search_bar %> | ||
<%= render( | ||
"search", | ||
search_term: search_term, | ||
resource_name: display_resource_name(page.resource_name) | ||
) %> | ||
<% end %> | ||
|
||
<div> | ||
<%= link_to( | ||
t( | ||
"administrate.actions.new_resource", | ||
name: page.resource_name.titleize.downcase | ||
), | ||
[:new, namespace, page.resource_path], | ||
class: "button", | ||
) if valid_action?(:new) && show_action?(:new, new_resource) %> | ||
</div> | ||
</header> | ||
|
||
<section class="main-content__body main-content__body--flush"> | ||
<%= render( | ||
"collection", | ||
collection_presenter: page, | ||
collection_field_name: resource_name, | ||
page: page, | ||
resources: resources, | ||
table_title: "page-title" | ||
) %> | ||
<%= paginate resources %> | ||
</section> | ||
|
||
<% if ["place", "story", "speaker"].include?(page.resource_name) %> | ||
<%= form_tag send("import_csv_#{page.resource_name.pluralize}_path"), multipart: true do %> | ||
<div class="form-group"> | ||
<label for="file">File to Upload</label><%= file_field_tag :file, :accept => 'text/csv', class: "form-control-file" %> | ||
</div> | ||
<br> | ||
<div class="form-group"> | ||
<%= button_tag :class => "btn btn-lg btn-primary" do %> | ||
<i class="fa fa-upload"></i> Import <%= page.resource_name.pluralize.capitalize %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters