diff --git a/app/controllers/admin/booth_groups_controller.rb b/app/controllers/admin/booth_groups_controller.rb new file mode 100644 index 00000000..0fadca73 --- /dev/null +++ b/app/controllers/admin/booth_groups_controller.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module Admin + class BoothGroupsController < Admin::BaseController + load_and_authorize_resource :conference, find_by: :short_title + load_and_authorize_resource :program, through: :conference, singleton: true + load_and_authorize_resource :booth_group, through: :program + + def index; end + + def edit; end + + def new + @booth_group = @conference.program.booth_groups.new(color: @conference.next_color_for_collection(:booth_groups)) + end + + def create + @booth_group = @conference.program.booth_groups.new(booth_group_params) + if @booth_group.save + redirect_to admin_conference_booths_path(conference_id: @conference.short_title), + notice: "#{(t 'booth').capitalize} group successfully created." + else + flash.now[:error] = "Creating #{t 'booth'} group failed: #{@booth_group.errors.full_messages.join('. ')}." + render :new + end + end + + def update + if @booth_group.update_attributes(booth_group_params) + redirect_to admin_conference_booths_path(conference_id: @conference.short_title), + notice: "#{(t 'booth').capitalize} group successfully updated." + else + flash.now[:error] = "Update #{t 'booth'} group failed: #{@booth_group.errors.full_messages.join('. ')}." + render :edit + end + end + + def destroy + if @booth_group.destroy + redirect_to admin_conference_booths_path(conference_id: @conference.short_title), + notice: "#{(t 'booth').capitalize} group successfully deleted." + else + redirect_to admin_conference_booths_path(conference_id: @conference.short_title), + error: "Destroying #{t 'booth'} group failed! "\ + "#{@booth_group.errors.full_messages.join('. ')}." + end + end + + private + + def booth_group_params + params.require(:booth_group).permit(:name, :color, :conference_id, + :created_at, :updated_at, booth_ids: []) + end + end +end diff --git a/app/models/booth.rb b/app/models/booth.rb index 71ab6e98..c8c7d0ae 100644 --- a/app/models/booth.rb +++ b/app/models/booth.rb @@ -7,6 +7,7 @@ class Booth < ApplicationRecord attr_accessor :invite_responsible belongs_to :conference + belongs_to :booth_group has_many :booth_requests, dependent: :destroy has_many :users, through: :booth_requests diff --git a/app/models/booth_group.rb b/app/models/booth_group.rb new file mode 100644 index 00000000..0138ce15 --- /dev/null +++ b/app/models/booth_group.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class BoothGroup < ApplicationRecord + belongs_to :program, touch: true + has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id } + has_many :booths, dependent: :restrict_with_error + + accepts_nested_attributes_for :booths + + validates :name, presence: true + validates :color, format: /\A#[0-9A-F]{6}\z/ + before_validation :capitalize_color + + private + + def capitalize_color + self.color = color.upcase if color.present? + end + + def conference_id + program.conference_id + end +end diff --git a/app/models/conference.rb b/app/models/conference.rb index be3c36ad..4ac66ee6 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -52,6 +52,7 @@ class Conference < ApplicationRecord through: :program, source: :events has_many :event_types, through: :program + has_many :booth_groups, through: :program has_many :surveys, as: :surveyable, dependent: :destroy do def for_registration @@ -683,9 +684,10 @@ class Conference < ApplicationRecord # we have different start indices for every collection to generate a # different color for every of them. start_index = { - tracks: (program.tracks.count + 1), - levels: (program.difficulty_levels.count + 51), - types: (program.event_types.count + 101) + tracks: (program.tracks.count + 1), + levels: (program.difficulty_levels.count + 51), + types: (program.event_types.count + 101), + booth_groups: (program.booth_groups.count + 151) } next_color(start_index[collection]) end diff --git a/app/models/program.rb b/app/models/program.rb index 8db24c6c..4e49ebe3 100644 --- a/app/models/program.rb +++ b/app/models/program.rb @@ -9,6 +9,7 @@ class Program < ApplicationRecord has_many :cfps, dependent: :destroy has_many :event_types, dependent: :destroy + has_many :booth_groups, dependent: :destroy has_many :tracks, dependent: :destroy has_many :difficulty_levels, dependent: :destroy has_many :schedules, dependent: :destroy diff --git a/config/routes.rb b/config/routes.rb index 57bfa630..28a5f4dd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -95,6 +95,7 @@ Osem::Application.routes.draw do end end resources :event_types + resources :booth_groups resources :difficulty_levels post 'mass_upload_commercials' => 'commercials#mass_upload' resources :events do diff --git a/db/schema.rb b/db/schema.rb index b199367e..38c4f93b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_06_03_143107) do +ActiveRecord::Schema.define(version: 2019_08_18_135624) do create_table "answers", force: :cascade do |t| t.string "title" @@ -18,6 +18,15 @@ ActiveRecord::Schema.define(version: 2019_06_03_143107) do t.datetime "updated_at" end + create_table "booth_groups", force: :cascade do |t| + t.integer "program_id" + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "color", default: "#000000" + t.index ["program_id"], name: "index_booth_groups_on_program_id" + end + create_table "booth_requests", force: :cascade do |t| t.integer "booth_id" t.integer "user_id" @@ -39,6 +48,8 @@ ActiveRecord::Schema.define(version: 2019_06_03_143107) do t.integer "conference_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "booth_group_id" + t.index ["booth_group_id"], name: "index_booths_on_booth_group_id" end create_table "cfps", force: :cascade do |t|