Skip to content
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

Pager for places, loading spinner for map (refs #333 #332) #334

Merged
merged 10 commits into from
Aug 3, 2024
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
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Metrics/CyclomaticComplexity:
- app/helpers/application_helper.rb
- app/controllers/layers_controller.rb
- app/controllers/maps_controller.rb
- lib/imports/csv_importer.rb


Metrics/MethodLength:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ GEM
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.3.2)
rexml (3.3.4)
strscan
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand Down
5 changes: 3 additions & 2 deletions app/assets/javascripts/layers.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function ShowPlacesForLayer(map,text_layers,image_layers,marker_meta_layers,curv

var request = $.getJSON(layer_json_url, function(data) {

$('.loader_wrapper').hide(300);
console.log("-----------------------------");
console.log("Data from layer_json_url",data);

Expand Down Expand Up @@ -365,8 +366,8 @@ function ShowPlacesForLayer(map,text_layers,image_layers,marker_meta_layers,curv
}
}).fail(function( jqxhr, textStatus, error ) {
// display error only if != 'Not Found' (which is normal behaviour for edit mode)
if ( error !== 'Not Found') {
console.log( "Error: Could not load layer data (w/JSON) "+ error );
if ( error && ( error !== 'Not Found') ) {
console.log( "Error: Could not load layer data (w/JSON) "+ error );
$('#flash_wrapper').append('<div id="flash" class="callout alert">Layer data could not be loaded. ('+error+')</div>');
};
});
Expand Down
52 changes: 52 additions & 0 deletions app/assets/stylesheets/1_basics.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,58 @@ tbody th, tbody td {
text-align: left;
}

nav.pagination {
text-align: right;
}

.loader_wrapper {
// cover the whole page, content in the middle
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(30,30,30,0.5);
z-index: 2;
}
.loader {
// place in the moddle of loader_wrapper div
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

width: 48px;
height: 48px;
}
.loader::after,
.loader::before {
content: '';
box-sizing: border-box;
width: 48px;
height: 48px;
border-radius: 50%;
border: 5px solid rgba(255,255,255,0.7);
position: absolute;
left: 0;
top: 0;
animation: animloader 2s linear infinite;
}
.loader::after {
animation-delay: 1s;
}

@keyframes animloader {
0% {
transform: scale(0);
opacity: 1;
}
100% {
transform: scale(1);
opacity: 0;
}
}

/* -------------------- */
/* foundation adapations */
.callout {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/places_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PlacesController < ApplicationController
def index
@layer = Layer.friendly.find(params[:layer_id])
@map = @layer.map
@places = @layer.places
@places = @layer.places.page params[:page]
end

# GET /places/1
Expand Down
10 changes: 10 additions & 0 deletions app/models/place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class Place < ApplicationRecord
elsif enddate_date.nil? || startdate_date.blank?
self.enddate = nil
end

clean_text_fields
end

def title_and_location
Expand Down Expand Up @@ -220,4 +222,12 @@ def self.to_csv
def check_audio_format
errors.add(:audio, 'format must be MP3.') if audio.attached? && !audio.content_type.in?(%w[audio/mpeg])
end

def clean_text_fields
self.text = remove_4byte_characters(text) if text
end

def remove_4byte_characters(string)
string.each_char.select { |char| char.bytesize < 4 }.join
end
end
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
%body{:id=>"#{id}", :class=>"#{klass}", "data-app-shortname"=>"#{Rails.application.config_for(:settings).app_shortname}"}
%header
= render 'layouts/navigation'

%div.loader_wrapper
%span.loader
#map
&nbsp;
%main{role: "main"}
Expand Down
7 changes: 6 additions & 1 deletion app/views/places/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
= link_to map_layer_path(@layer.map,@layer) do
= @layer.title
%span.shy (with #{@places.count} Places)
%span.shy (with #{@layer.places.count} Places)

.grid-x.grid-padding-x
.medium-12.cell.card
Expand Down Expand Up @@ -135,6 +135,10 @@
%input{"type":"hidden"}



%p.text-right.small
Showing #{@places.count} of #{@layer.places.count}
= paginate @places
%table
%thead
%tr
Expand Down Expand Up @@ -255,3 +259,4 @@
= link_to map_layer_place_path(place.layer.map,place.layer,place), method: :delete, data: { confirm:warning }, :class => "button alert small" do
%i.fi-trash.fi-21
Destroy
%p.text-right= paginate @places
7 changes: 4 additions & 3 deletions app/views/places/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@
- if @place.subtitle.present?
%p
= @place.subtitle
%div
%strong
=h @place.teaser.html_safe
- if @place.teaser.present?
%div
%strong
=h @place.teaser.html_safe
- if @place.text.present?
%div
=h @place.text.html_safe
Expand Down
1 change: 1 addition & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ development:
password: orte00
encoding: utf8


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/kaminari_config.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Kaminari.configure do |config|
config.default_per_page = Rails.env.development? ? 5 : 40
config.default_per_page = Rails.env.development? ? 5 : 50
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ConvertSessionDataToLongText < ActiveRecord::Migration[6.1]
def up
change_column :sessions, :data, :longtext
end

def down
change_column :sessions, :data, :text
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_15_143133) do
ActiveRecord::Schema.define(version: 2024_07_30_200352) do

create_table "active_storage_attachments", charset: "utf8mb3", collation: "utf8mb3_general_ci", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -287,7 +287,7 @@

create_table "sessions", charset: "utf8mb3", collation: "utf8mb3_general_ci", force: :cascade do |t|
t.string "session_id", null: false
t.text "data"
t.text "data", size: :long
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["session_id"], name: "index_sessions_on_session_id", unique: true
Expand Down
4 changes: 3 additions & 1 deletion lib/imports/csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Imports::CsvImporter

REQUIRED_FIELDS = %w[title lat lon].freeze

ALLOWED_FIELDS = %w[title subtitle teaser text link startdate startdate_date startdate_time enddate enddate_date enddate_time lat lon location address zip city country published featured sensitive sensitive_radius shy imagelink layer_id icon_id relations_tos relations_froms].freeze
ALLOWED_FIELDS = %w[title subtitle teaser text link startdate startdate_date startdate_time enddate enddate_date enddate_time lat lon location address zip city country published featured sensitive sensitive_radius shy imagelink layer_id icon_id relations_tos relations_froms tag_list].freeze

PREVIEW_FIELDS = %w[title lat lon location address zip city country].freeze

Expand Down Expand Up @@ -38,6 +38,8 @@ def import
next
end

processed_row['tag_list'] = processed_row['tag_list'].split(',').map(&:strip) if processed_row['tag_list'].present?

# dupe handling
title = do_sanitize(processed_row['title'])

Expand Down
25 changes: 19 additions & 6 deletions lib/tasks/geocode_addresses_in_csv.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ require 'csv'
namespace :geocode do
desc 'Geocode addresses,add lat/lon columns and separate address components'
task addresses_in_csv_and_separate: :environment do
input_file = Rails.root.join('tmp', 'input_tryout.csv')
input_file = Rails.root.join('tmp', 'input.csv')
output_file = Rails.root.join('tmp', 'output.csv')
error_counter = 0
read_counter = 0

CSV.open(output_file, 'w') do |csv_out|
csv_out << %w[title link full_address category published text date_published address city state zip lat lon]
CSV.foreach(input_file, headers: true) do |row|
# wait 1 sec, to avoid hitting rate limit
sleep(1.2)
# wait 2 sec, to avoid hitting rate limit
sleep(2)
# sleep even longer every 99 iterations
sleep(240) if (read_counter % 99).zero?

full_address = row['address']
lookup_address = full_address.gsub(/,[^,]*$/, '')
address_parts = full_address.split(',').map(&:strip)

address = address_parts[0]
city = address_parts[1]
if address_parts[2].nil?
puts "Error: #{lookup_address}"
error_counter += 1
next
end
state_zip = address_parts[2].split(' ')
state = state_zip[0]
zip = state_zip[1]
Expand All @@ -26,9 +37,11 @@ namespace :geocode do
begin
puts "Looking up: #{lookup_address}"
results = Geocoder.search(lookup_address)
read_counter += 1
rescue StandardError => e
puts "Error geocoding address: #{e.message}"
results = []
error_counter += 1
# skip to next row
next
end
Expand All @@ -42,10 +55,10 @@ namespace :geocode do
end

csv_out << row.fields + [address, city, state, zip, lat, lon]
puts "Processed: #{lookup_address}"
puts "#{read_counter} // Processed: #{lookup_address}"
end
end

puts "Geocoding complete. Output saved to #{output_file}"
puts "Errors (w/wrong or incomplete address): #{error_counter}" if error_counter.positive?
puts "Geocoding complete w/#{read_counter} entries. Output saved to #{output_file}"
end
end
2 changes: 1 addition & 1 deletion spec/views/places/index.html.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@other_layer = FactoryBot.create(:layer, map_id: @map.id)
place1 = FactoryBot.create(:place, layer_id: @layer.id, title: 'Title1')
place2 = FactoryBot.create(:place, layer_id: @layer.id, title: 'Title2')
@places = [place1, place2]
@places = Kaminari.paginate_array([place1, place2]).page(1)
end

it 'renders a list of places' do
Expand Down
Loading