Refactoring rooms
This commit is contained in:
parent
2c98ba9711
commit
6a373b5f6c
11 changed files with 135 additions and 52 deletions
|
|
@ -1,26 +1,55 @@
|
|||
module Admin
|
||||
class RoomsController < Admin::BaseController
|
||||
load_and_authorize_resource :conference, find_by: :short_title
|
||||
authorize_resource through: :conference
|
||||
load_and_authorize_resource through: :conference
|
||||
|
||||
def index
|
||||
authorize! :index, Room.new(conference_id: @conference.id)
|
||||
end
|
||||
|
||||
def show
|
||||
render :rooms_list
|
||||
def edit; end
|
||||
|
||||
def new
|
||||
@room = @conference.rooms.new
|
||||
end
|
||||
|
||||
def create
|
||||
@room = @conference.rooms.new(room_params)
|
||||
if @room.save
|
||||
redirect_to(admin_conference_rooms_path(conference_id: @conference.short_title),
|
||||
notice: 'Room successfully created.')
|
||||
else
|
||||
flash[:error] = "Creating Room failed: #{@room.errors.full_messages.join('. ')}."
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if @conference.update_attributes(params[:conference])
|
||||
if @room.update_attributes(room_params)
|
||||
redirect_to(admin_conference_rooms_path(
|
||||
conference_id: @conference.short_title),
|
||||
notice: 'Rooms were successfully updated.')
|
||||
notice: 'Room successfully updated.')
|
||||
else
|
||||
redirect_to(admin_conference_rooms_path(
|
||||
conference_id: @conference.short_title),
|
||||
notice: 'Room update failed.')
|
||||
flash[:error] = "Update Room failed: #{@room.errors.full_messages.join('. ')}."
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @room.destroy
|
||||
redirect_to(admin_conference_rooms_path(conference_id: @conference.short_title),
|
||||
notice: 'Room successfully deleted.')
|
||||
else
|
||||
redirect_to(admin_conference_rooms_path(conference_id: @conference.short_title),
|
||||
error: 'Destroying room failed! ' \
|
||||
"#{@room.errors.full_messages.join('. ')}.")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def room_params
|
||||
params[:room]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,13 +6,16 @@ class Room < ActiveRecord::Base
|
|||
|
||||
before_create :generate_guid
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
validates :size, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
|
||||
|
||||
validates :size, presence: true, if: :public?
|
||||
|
||||
private
|
||||
|
||||
def generate_guid
|
||||
guid = SecureRandom.urlsafe_base64
|
||||
# begin
|
||||
# guid = SecureRandom.urlsafe_base64
|
||||
# end while Person.where(:guid => guid).exists?
|
||||
self.guid = guid
|
||||
end
|
||||
end
|
||||
|
|
|
|||
4
app/views/admin/rooms/_form.html.haml
Normal file
4
app/views/admin/rooms/_form.html.haml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
= f.input :name
|
||||
= f.input :size, :input_html => {:size => 5}
|
||||
= f.input :public, :as => :boolean
|
||||
= f.action :submit, as: :button, button_html: { class: 'btn btn-primary' }
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
<div class="nested-fields">
|
||||
<%= f.inputs do %>
|
||||
<%= f.input :name %>
|
||||
<%= f.input :size, :input_html => {:size => 5} %>
|
||||
<%= f.input :public, :as => :boolean %>
|
||||
<%= remove_association_link :room, f %>
|
||||
<% end %>
|
||||
</div>
|
||||
8
app/views/admin/rooms/edit.html.haml
Normal file
8
app/views/admin/rooms/edit.html.haml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
%h1 Editing Room
|
||||
|
||||
.row
|
||||
.col-md-8
|
||||
= semantic_form_for @room, url: admin_conference_room_path(conference_id: @conference.short_title, id: @room.id) do |f|
|
||||
= render 'form', f: f
|
||||
|
||||
= link_to 'Back', admin_conference_rooms_path
|
||||
|
|
@ -1,5 +1,32 @@
|
|||
.row
|
||||
.col-md-8
|
||||
= semantic_form_for(@conference, :url => admin_conference_room_path(@conference.short_title, @conference.rooms)) do |f|
|
||||
= dynamic_association :rooms, "Rooms", f
|
||||
= f.action :submit, :as => :button, :button_html => {:class => "btn btn-primary"}
|
||||
%h1 Rooms
|
||||
|
||||
- if @conference.rooms.any?
|
||||
.row
|
||||
.col-md-12
|
||||
%table.table
|
||||
%thead
|
||||
%th #
|
||||
%th Name
|
||||
%th Size
|
||||
%th Public
|
||||
%th Edit
|
||||
%th Delete
|
||||
%tbody
|
||||
- @conference.rooms.each_with_index do |room, index|
|
||||
%tr
|
||||
%td
|
||||
= index + 1
|
||||
%td
|
||||
= room.name
|
||||
%td
|
||||
= room.size
|
||||
%td
|
||||
= room.public
|
||||
%td
|
||||
= link_to 'Edit', edit_admin_conference_room_path(@conference.short_title, room.id),
|
||||
method: :get, class: 'btn btn-primary'
|
||||
%td
|
||||
= link_to 'Delete', admin_conference_room_path(@conference.short_title, room.id),
|
||||
method: :delete, class: 'btn btn-danger', data: { confirm: "Do you really want to delete #{room.name}?" }
|
||||
|
||||
= link_to 'New Room', new_admin_conference_room_path(@conference.short_title), class: 'btn btn-success'
|
||||
|
|
|
|||
7
app/views/admin/rooms/new.html.haml
Normal file
7
app/views/admin/rooms/new.html.haml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
%h1 New Room
|
||||
.row
|
||||
.col-md-8
|
||||
= semantic_form_for @room, url: admin_conference_rooms_path(conference_id: @conference.short_title) do |f|
|
||||
= render 'form', f: f
|
||||
|
||||
= link_to 'Back', admin_conference_rooms_path
|
||||
Loading…
Add table
Add a link
Reference in a new issue