diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..30b9997 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,34 @@ +name: Run unit tests +on: + push: + branches: + - main + pull_request: +jobs: + unit_tests: + runs-on: ubuntu-latest + services: + postgres: + image: postgres + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432 + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + - uses: ruby/setup-ruby@v1 + - run: bundle install + - run: | + bundle exec rake db:create db:schema:load + bundle exec rspec + env: + RAILS_ENV: test + DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres diff --git a/Gemfile.lock b/Gemfile.lock index 8d5808f..90a4379 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -239,7 +239,7 @@ GEM stringio (3.1.1) thor (1.3.1) timeout (0.4.1) - turbo-rails (2.0.5) + turbo-rails (2.0.6) actionpack (>= 6.0.0) activejob (>= 6.0.0) railties (>= 6.0.0) diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index 025f7ef..72baf50 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -1,3 +1,5 @@ +require 'csv' + class GuestsController < ApplicationController before_action :set_guest, only: %i[show edit update destroy] @@ -58,6 +60,20 @@ class GuestsController < ApplicationController end end + def import + csv = CSV.parse(params[:file].read, headers: true) + ActiveRecord::Base.transaction do + csv.each do |row| + guest = Guest.create!(first_name: row['name']) + + guest.affinity_group_list.add(row['affinity_group']) + guest.save! + end + end + + redirect_to guests_url + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/app/views/guests/index.html.erb b/app/views/guests/index.html.erb index 436a01b..d71cfa8 100644 --- a/app/views/guests/index.html.erb +++ b/app/views/guests/index.html.erb @@ -29,3 +29,9 @@ <%= link_to "New guest", new_guest_path %> + +<%= form_with url: import_guests_path, method: :post do |form| %> + <%= form.label :file %> + <%= form.file_field :file %> + <%= form.submit "Import" %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 2b43138..67f091d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ Rails.application.routes.draw do - resources :guests + resources :guests do + post :import, on: :collection + end resources :expenses resources :tables_arrangements, only: [:index, :show]