Skip to content

Commit

Permalink
introduced a general 'data type' filter for the map
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinHinz committed Dec 18, 2024
1 parent 18c94fb commit 6148d0b
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 46 deletions.
97 changes: 66 additions & 31 deletions app/controllers/xronos_data_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,56 @@ def index
private

def set_data
# Initialize raw filter parameters for C14 and Dendro
@c14_filter_params = filter_params
@dendro_filter_params = filter_params.except(:c14s, :cals)

# Initialize data for C14 and Dendro
@c14_data = XronosData.new(@c14_filter_params, select_params, :c14)
@dendro_data = XronosData.new(@dendro_filter_params, select_params, :dendro)

# Assign the default data view to @data (e.g., C14 by default) temporarily
@data = @c14_data

@n_data = @c14_data.xrons.count + @dendro_data.xrons.count
@total_data = @c14_data.everything.count + @dendro_data.everything.count

# Fetch site data from both C14 and Dendro queries
c14_sites = @c14_data.xrons
.joins("LEFT JOIN sites ON sites.id = contexts.site_id")
.select("sites.id AS site_id, sites.lng, sites.lat, sites.name AS site_name")
.distinct

dendro_sites = @dendro_data.xrons
.joins("LEFT JOIN sites ON sites.id = contexts.site_id")
.select("sites.id AS site_id, sites.lng, sites.lat, sites.name AS site_name")
.distinct

# Combine site queries and remove duplicates
combined_sites_query = "((#{c14_sites.to_sql}) UNION (#{dendro_sites.to_sql})) AS combined_sites"
@sites = Site.from(combined_sites_query).select("site_id, lng, lat, site_name").distinct
# Determine the requested type (C14 or Dendro) and adjust filters
type_filter = filter_params[:type]&.map(&:downcase) || %w[c14 dendro]

# Separate filters based on type
if type_filter.include?("c14")
@c14_filter_params = filter_params.except(:type)
@c14_data = XronosData.new(@c14_filter_params, select_params, :c14)
else
@c14_data = nil
end

if type_filter.include?("dendro")
@dendro_filter_params = filter_params.except(:type, :c14s, :cals)
@dendro_data = XronosData.new(@dendro_filter_params, select_params, :dendro)
else
@dendro_data = nil
end

# Set @data to the primary dataset, or a placeholder with filters
if @c14_data
@data = @c14_data
elsif @dendro_data
@data = @dendro_data
else
@data = XronosData.new(filter_params, select_params)
end

# Calculate total records and initialize sites
@n_data = (@c14_data&.xrons&.count || 0) + (@dendro_data&.xrons&.count || 0)
@total_data = (@c14_data&.everything&.count || 0) + (@dendro_data&.everything&.count || 0)

# Fetch site data based on the type filter
c14_sites = @c14_data&.xrons&.joins("LEFT JOIN sites ON sites.id = contexts.site_id")
&.select("sites.id AS site_id, sites.lng, sites.lat, sites.name AS site_name")
&.distinct

dendro_sites = @dendro_data&.xrons&.joins("LEFT JOIN sites ON sites.id = contexts.site_id")
&.select("sites.id AS site_id, sites.lng, sites.lat, sites.name AS site_name")
&.distinct

if c14_sites && dendro_sites
combined_sites_query = "((#{c14_sites.to_sql}) UNION (#{dendro_sites.to_sql})) AS combined_sites"
@sites = Site.from(combined_sites_query).select("site_id, lng, lat, site_name").distinct
elsif c14_sites
@sites = c14_sites
elsif dendro_sites
@sites = dendro_sites
else
@sites = Site.none
end
end

def reset_session_key(key)
Expand All @@ -69,7 +91,10 @@ def reset_session_key(key)
end

def filter_params
return {} unless params[:filter].present? # Return an empty hash if :filter is nil or empty

params.fetch(:filter, {}).permit(
type: [],
c14s: [
:lab_identifier, { lab_identifier: [] }, { bp: [] }
],
Expand Down Expand Up @@ -101,16 +126,26 @@ def filter_params
site_types: [
:name, { name: [] }
]
)
)
end

def select_params
params.fetch(:select, {})
end

def render_index_html
@c14_pagy, @c14_xrons = pagy(@c14_data.xrons)
@dendro_pagy, @dendro_xrons = pagy(@dendro_data.xrons)
if @c14_data
@c14_pagy, @c14_xrons = pagy(@c14_data.xrons)
else
@c14_pagy, @c14_xrons = nil, []
end

if @dendro_data
@dendro_pagy, @dendro_xrons = pagy(@dendro_data.xrons)
else
@dendro_pagy, @dendro_xrons = nil, []
end

render layout: "full_page"
end

Expand Down
24 changes: 11 additions & 13 deletions app/views/c14s/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<% content_for :title, Dendro.label.humanize.pluralize %>
<% content_for :meta_description do %>
List of <%= @pagy.count %> dendrochronological dates from archaeological sites around the world, including measurement, sample and contextual information, and bibliographic references.
<% end %>
<% content_for :title, C14.label.humanize.pluralize %>
<% content_for :meta_description do %>List of <%= @pagy.count %> radiocarbon dates from archaeological sites around the world, including laboratory measurement, sample and contextual information, and bibliographic references.<% end %>

<div class="container my-5">

<h1><%= Dendro.label.pluralize.humanize %></h1>
<p>Showing <%= @pagy.count %> <%= Dendro.label.pluralize(@pagy.count) %>.</p>
<h1><%= C14.label.pluralize.humanize %></h1>
<p>Showing <%= @pagy.count %> <%= C14.label.pluralize(@pagy.count) %>.</p>

<turbo-frame id="dendros-table">
<div id="dendros-toolbar" class="d-flex">
<turbo-frame id="c14s-table">
<div id="c14s-toolbar" class="d-flex">
<div class="ms-auto">
<%= link_to bs_icon("download"), dendros_path(format: :csv, **request.query_parameters), class: "btn btn-sm btn-outline-primary", title: "Download CSV" %>
<%= link_to bs_icon("download"), c14s_path(format: :csv, **request.query_parameters), class: "btn btn-sm btn-outline-primary", title: "Download CSV" %>
</div>
</div>
<%= render "table", dendros: @dendros %>
<%= render "table", c14s: @c14s %>
<%== pagy_bootstrap_nav(@pagy) %>
</turbo-frame>

<% if can? :create, Dendro %>
<%= link_to 'New Dendro measurement', new_dendro_path, class: 'btn btn-secondary' %>
<% if can? :create, C14 %>
<%= link_to 'New C14 measurement', new_c14_path, class: 'btn btn-secondary' %>
<% end %>
</div>
</div>
5 changes: 4 additions & 1 deletion app/views/xronos_data/_c14_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

</table>

<%== pagy_bootstrap_nav(@c14_pagy) %>

<% if @c14_pagy %>
<%== pagy_bootstrap_nav(@c14_pagy) %>
<% end %>
<% end %>

5 changes: 4 additions & 1 deletion app/views/xronos_data/_dendro_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
</tbody>
</table>

<%== pagy_bootstrap_nav(@dendro_pagy) %>

<% if @dendro_pagy %>
<%== pagy_bootstrap_nav(@dendro_pagy) %>
<% end %>
<% end %>
17 changes: 17 additions & 0 deletions app/views/xronos_data/_filter_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
</div>
</noscript>

<div class="p-3 border rounded bg-white">
<h5>Data Filters</h5>

<!-- Data Types Filter -->
<div class="mb-3">
<% selected_types = params.dig(:filter, :type) || [] %>
<div class="d-flex gap-3">
<div class="form-check">
<%= check_box_tag 'filter[type][]', 'c14', selected_types.include?('c14'), class: 'form-check-input', id: 'type_c14' %>
<%= label_tag 'type_c14', 'Radiocarbon Data'.html_safe, class: 'form-check-label' %> </div>
<div class="form-check">
<%= check_box_tag 'filter[type][]', 'dendro', selected_types.include?('dendro'), class: 'form-check-input', id: 'type_dendro' %>
<%= label_tag 'type_dendro', 'Dendro Data', class: 'form-check-label' %>
</div>
</div>
</div>

<%= accordion_item "dataFilters-Site", "Sites", (@data.filters.dig("sites", "name") || @data.filters.dig("site_types", "name")) ? false : true do %>

<%= f.fields_for :sites do |f| %>
Expand Down

0 comments on commit 6148d0b

Please sign in to comment.