diff --git a/app/models/geo_entity.rb b/app/models/geo_entity.rb index a39a4d4c..08fe7357 100644 --- a/app/models/geo_entity.rb +++ b/app/models/geo_entity.rb @@ -56,7 +56,7 @@ def occurrences hash = {} - occurrences.uniq.map do |occurrence| + occurrences.map do |occurrence| next if hash[occurrence['name']] == 'present' hash[occurrence['name']] = occurrence['occurrence'] diff --git a/app/serializers/serializers/habitat_country_change_serializer.rb b/app/serializers/serializers/habitat_country_change_serializer.rb index 46e83be8..d85b111d 100644 --- a/app/serializers/serializers/habitat_country_change_serializer.rb +++ b/app/serializers/serializers/habitat_country_change_serializer.rb @@ -6,7 +6,7 @@ def initialize(country, habitats_presence_data) end def serialize - Habitat.where(id: [1, 2, 3, 4, 5]).map{ |h| get_habitat_change(h) }.compact + Habitat.all.map{ |h| get_habitat_change(h) }.compact end def get_habitat_change habitat diff --git a/app/serializers/serializers/representation_habitats_serializer.rb b/app/serializers/serializers/representation_habitats_serializer.rb index 261a78f5..89839bd5 100644 --- a/app/serializers/serializers/representation_habitats_serializer.rb +++ b/app/serializers/serializers/representation_habitats_serializer.rb @@ -29,7 +29,7 @@ def serialize private def add_habitats_stats - Habitat.where(id: [1, 2, 3, 4, 5]).each_with_index do |habitat, idx| + Habitat.all.each_with_index do |habitat, idx| habitat_stat = @geo_stat.find_by(habitat_id: habitat) value = habitat_stat.coastal_coverage || 0 diff --git a/app/views/global/_downloads.html.erb b/app/views/global/_downloads.html.erb index f228ce74..92ef3d7d 100644 --- a/app/views/global/_downloads.html.erb +++ b/app/views/global/_downloads.html.erb @@ -45,6 +45,7 @@ <% else %> <%= link_to downloads[:statistics][:button], "downloads/global_statistics.zip", class: 'downloads__button button--download', + download: 'global_statistics', data: { download_type: 'statistics', download_label: 'global' diff --git a/db/seeds.rb b/db/seeds.rb index bf7cee55..83650970 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,19 +1,4 @@ -habitats_config = YAML.load(File.open("#{Rails.root}/config/habitats.yml", 'r')) - -habitats_config['habitats'].each do |name, data| - Habitat.where(name: data['name']).first_or_create do |habitat| - habitat.update_attributes(data) - end - Rails.logger.info "#{name.capitalize} habitat created!" -end - -# Commented as we are temporarily using static data -#Habitat.all.each do |habitat| -# habitat.calculate_global_coverage -# Rails.logger.info("Global coverage calculate for #{habitat.name}!") -#end - -%w(countries regions prebakedstats new_redlist_data coastalstats global_change +%w(habitats countries regions prebakedstats new_redlist_data coastalstats global_change sources country_citations habitat_sources_per_country bounding_boxes).each do |import_type| Rake::Task["import:#{import_type}"].invoke end diff --git a/lib/tasks/import_habitats.rake b/lib/tasks/import_habitats.rake new file mode 100644 index 00000000..773260e2 --- /dev/null +++ b/lib/tasks/import_habitats.rake @@ -0,0 +1,13 @@ +namespace :import do + desc "import CSV data into database" + task :habitats, [:csv_file] => [:environment] do + habitats_config = YAML.load(File.open("#{Rails.root}/config/habitats.yml", 'r')) + + habitats_config['habitats'].each do |name, data| + Habitat.where(name: data['name']).first_or_create do |habitat| + habitat.update_attributes(data) + end + Rails.logger.info "#{name.capitalize} habitat created!" + end + end +end \ No newline at end of file diff --git a/lib/tasks/import_refresh.rake b/lib/tasks/import_refresh.rake index f9233d4b..32eb68e3 100644 --- a/lib/tasks/import_refresh.rake +++ b/lib/tasks/import_refresh.rake @@ -4,7 +4,7 @@ namespace :import do models = %w( GeoEntityStat ChangeStat GeoRelationship CoastalStat GeoEntity GeoEntitiesSpecies Species GeoEntityStatsSources Source CountryCitation - GlobalChangeCitation + GlobalChangeCitation GlobalChangeStat Habitat ) models.each do |m| @@ -12,7 +12,7 @@ namespace :import do m.constantize.send(:delete_all) end - tasks = %w(countries regions prebakedstats new_redlist_data coastalstats + tasks = %w(habitats countries regions prebakedstats new_redlist_data coastalstats global_change sources country_citations habitat_sources_per_country bounding_boxes ) diff --git a/lib/tasks/import_sources.rake b/lib/tasks/import_sources.rake index 4976f12e..7897dab6 100644 --- a/lib/tasks/import_sources.rake +++ b/lib/tasks/import_sources.rake @@ -52,7 +52,7 @@ namespace :import do next unless geo_entity - Habitat.where(id: [1, 2, 3, 4, 5]).each do |habitat| + Habitat.all.each do |habitat| # Fetch the source IDs from the relevant column camelcased_habitat_title = habitat.title.gsub(/[\s-]/, '_')