Skip to content

Commit

Permalink
Merge pull request #84 from cul/hours-128
Browse files Browse the repository at this point in the history
HOURS-128 Reenable wifi density and implement a management UI
  • Loading branch information
barmintor authored Jul 12, 2024
2 parents 64e1620 + 0d4cee5 commit 7d9e453
Show file tree
Hide file tree
Showing 21 changed files with 300 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.7.5
ruby-3.0.3
20 changes: 16 additions & 4 deletions app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def index
path.blank? or path == '/'
end
if home_page
@locations = Location.where(front_page: true).where.not(code: 'all')
@locations = Location.where(front_page: true).where.not(code: 'all', suppress_display: true).includes(:access_points)
else
@locations = Location.where.not(code: 'all')
@locations = Location.where.not(code: 'all', suppress_display: true).includes(:access_points)
end
@locations = @locations.order(:name)
@now = Time.current
Expand Down Expand Up @@ -44,7 +44,12 @@ def create

def update
authorize! :update, @location
if @location.update(update_params)
clean_params = update_params
clean_params.dig(:access_points)&.map! do |ap|
ap.present? ? AccessPoint.find_or_create_by(id: ap.to_i) : nil
end
clean_params.dig(:access_points)&.compact!
if @location.update(clean_params)
flash[:success] = "Location successfully updated"
redirect_to admin_url
else
Expand All @@ -59,6 +64,13 @@ def destroy
end

def show
unless @location && (current_user || !@location.suppress_display)
respond_to do |format|
format.html { render "errors/not_found", status: 404, layout: "public" }
format.json { render json: {}, status: 404 }
end
return
end
@secondary_locations = Location.where(primary_location: @location).load
@date = params[:date] ? Date.parse(params[:date]) : Date.current
respond_to do |format|
Expand Down Expand Up @@ -129,7 +141,7 @@ def create_params

def update_params
if current_user.administrator?
params.require(:location).permit(:name, :code, :comment, :comment_two, :url, :summary, :primary, :primary_location_id, :front_page, :short_note, :short_note_url)
params.require(:location).permit(:name, :code, :comment, :comment_two, :url, :summary, :primary, :primary_location_id, :front_page, :short_note, :short_note_url, :suppress_display, :wifi_connections_baseline, access_points: [])
else
params.require(:location).permit(:comment, :comment_two, :url, :summary, :short_note, :short_note_url)
end
Expand Down
27 changes: 27 additions & 0 deletions app/helpers/edit_location_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module EditLocationHelper
# <label for="location_access_points_2"><input type="checkbox" value="2" name="location[access_points][]" id="location_access_points_2" />2 (Location 2)</label>
def access_point_checkboxes(access_point_branches, count_iterator, current_values = [])
content_tag(:ul) do
access_point_branches.map do |branch|
branch_tag = content_tag(:li, class: ['form-check']) do
cb_index = count_iterator.next
content_tag(:label, for: "location_access_points_#{cb_index}", class: ['form-check-label']) do
checked = current_values.include?(branch.id)
cb_attrs = {
type: 'checkbox', value: branch.id, name: "location[access_points][]",
id: "location_access_points_#{cb_index}", checked: checked, class: ['form-check-input']
}
tag(:input, cb_attrs).safe_concat("#{branch.name}").safe_concat(content_tag(:em, branch.updated_at ? "updated: #{branch.updated_at}" : "aggregate data", class: "ml-1"))
end
end
if branch.children.size > 0
child_tag = content_tag(:li) do
access_point_checkboxes(branch.children.values, count_iterator)
end
branch_tag.safe_concat child_tag
end
branch_tag
end.join.html_safe
end
end
end
4 changes: 4 additions & 0 deletions app/models/access_point.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class AccessPoint < ApplicationRecord
has_many :access_points_locations
has_many :locations, through: :access_points_locations
end
4 changes: 4 additions & 0 deletions app/models/access_points_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class AccessPointsLocation < ApplicationRecord
belongs_to :location
belongs_to :access_point
end
4 changes: 4 additions & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ class Location < ApplicationRecord
validate :primary_location_must_be_primary

has_many :timetables
has_many :access_points_locations
has_many :access_points, through: :access_points_locations

belongs_to :primary_location, class_name: 'Location', foreign_key: 'primary_location_id', optional: true
accepts_nested_attributes_for :access_points

# TODO: when a location is deleted any permissions related to that location should also be deleted

Expand Down
5 changes: 5 additions & 0 deletions app/views/errors/internal_server_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<h2>Internal Server Error</h2>

<p>Please contact a site administrator to report an error at this location.</p>
</div>
5 changes: 5 additions & 0 deletions app/views/errors/not_found.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<h2>Location not found</h2>

<p>Contact a site administrator if you think this is an error.</p>
</div>
50 changes: 36 additions & 14 deletions app/views/locations/_location_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,32 @@
<% end %>
</div>

<% if current_user.administrator? %>
<div class="form-group">
<%= f.label :front_page %>
<%= f.check_box :front_page %>
</div>
<div class="form-group">
<%= f.label :primary %>
<%= f.check_box :primary %>
</div>
<div class="form-group">
<%= f.label :primary_location %>
<%= collection_select(:location, :primary_location_id, Location.where(primary: true), :id, :name, {include_blank: 'None'}) %>
</div>
<% end %>
<div class="form-group">
<h3>Location Display Options</h3>
<% if current_user.administrator? %>
<ul>
<li class="form-check"><%= f.label(:suppress_display, class: ['form-check-label']) { f.check_box(:suppress_display, class: ['form-check-input']).safe_concat("Suppress Display") } %></li>
<li class="form-check"><%= f.label(:front_page, class: ['form-check-label']) { f.check_box(:front_page, class: ['form-check-input']).safe_concat("Front Page") } %></li>
<li class="form-check"><%= f.label(:primary, class: ['form-check-label']) { f.check_box(:primary, class: ['form-check-input']).safe_concat("Primary") } %></li>
</ul>
<div class="form-group">
<%= f.label :primary_location %>
<%= collection_select(:location, :primary_location_id, Location.where(primary: true), :id, :name, {include_blank: 'None'}) %>
</div>
<% else %>
<ul>
<% if @location.suppress_display %>
<li><span class="warn static-control-form"><%= t("location.suppressed") %></span></li>
<% end %>
<li>Front Page: <%= (!!@location.front_page).to_s %></li>
<li>Primary: <%= (!!@location.primary).to_s %></li>
<% if @location.primary_location %>
<li>Primary Location: <%= @location.primary_location.name %></li>
<% end %>
</ul>
<% end %>
</div>

<div class="form-group">
<%= f.label :url %>
<%= f.text_field :url, :class => "form-control" %>
Expand All @@ -57,6 +69,16 @@
<%= f.text_field :short_note_url, :class => "form-control" %>
</div>


<div class="form-group">
<h3>WiFi Configuration</h3>
<div class="form-group">
<%= f.label :wifi_connections_baseline, 'WiFi Connections Baseline Count' %>
<%= f.text_field :wifi_connections_baseline, :class => "form-control" %>
</div>
<% current_aps = @location.access_point_ids.map(&:to_s) %>
<%= access_point_checkboxes(Hours::WifiDensity.access_point_trees(@location), (1...).each, current_aps) %>
</div>
<%= f.submit :class => 'btn btn-primary btn-sm'%>
<% end %>
</div>
3 changes: 3 additions & 0 deletions app/views/locations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<div class="col-md-12">
<h2 class="location-name"><%= link_to @location.name, @location.url %></h2>
<div class="location-primary-comment">
<% if @location.suppress_display %>
<p class="warn"><%= t("location.suppressed") %></p>
<% end %>
<% if @location.primary_location %>
<p>Check hours for: <strong class="text-center"><%= link_to @location.primary_location.name, location_path(@location.primary_location.code) %></strong></p>
<% end %>
Expand Down
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
# available at https://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
location:
suppressed: "Public display of this location is suppressed."
66 changes: 0 additions & 66 deletions config/wifi_density.template.yml
Original file line number Diff line number Diff line change
@@ -1,73 +1,7 @@
development:
data_url: https://hours.library.columbia.edu/wifi-density.json
data_cache_duration: <%= 10.minutes %>
locations:
avery:
cuit_location_ids:
- 124
high: 100
business:
cuit_location_ids:
- 96
high: 100
butler-24:
cuit_location_ids:
- 115
high: 1900
eastasian:
cuit_location_ids:
- 98
high: 300
lehman:
cuit_location_ids:
- 109
high: 1300
math:
cuit_location_ids:
- 141
high: 600
science-engineering:
cuit_location_ids:
- 100
high: 240
social-work:
cuit_location_ids:
- 123
high: 100

test:
data_url: https://hours.library.columbia.edu/wifi-density.json
data_cache_duration: <%= 1.second %>
locations:
avery:
cuit_location_ids:
- 124
high: 100
business:
cuit_location_ids:
- 96
high: 100
butler-24:
cuit_location_ids:
- 115
high: 1900
eastasian:
cuit_location_ids:
- 98
high: 300
lehman:
cuit_location_ids:
- 109
high: 1300
math:
cuit_location_ids:
- 141
high: 600
science-engineering:
cuit_location_ids:
- 100
high: 240
social-work:
cuit_location_ids:
- 123
high: 100
2 changes: 1 addition & 1 deletion db/migrate/20170615203023_add_fields_to_time_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def change
add_column :time_tables, :note, :string
remove_index :time_tables, column: [:code, :date]
remove_column :time_tables, :code
add_reference :time_tables, :library, index: true, foreign_key: true
add_reference :time_tables, :library, index: true, foreign_key: true, type: :bigint
add_index :time_tables, [:library_id, :date], :unique => true
rename_table :time_tables, :timetables
end
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20170627151458_change_library_table_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
remove_index :timetables, name: :index_timetables_on_library_id_and_date
remove_reference :timetables, :library, index: true, foreign_key: true
rename_table :libraries, :locations
add_reference :timetables, :location, index: true, foreign_key: true
add_reference :timetables, :location, index: true, foreign_key: true, type: :bigint
add_index :timetables, [:location_id, :date], :unique => true
end
end
2 changes: 1 addition & 1 deletion db/migrate/20170814194528_add_primary_location.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class AddPrimaryLocation < ActiveRecord::Migration[5.1]
def up
add_reference :locations, :primary_location, foreign_key: {to_table: :locations}
add_reference :locations, :primary_location, type: :bigint, foreign_key: {to_table: :locations}
add_column :locations, :primary, :boolean, default: false
add_index :locations, :primary
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddLocationSuppressDisplaySwitch < ActiveRecord::Migration[6.0]
def up
add_column :locations, :suppress_display, :boolean, default: false
end

def down
remove_column :locations, :suppress_display
end
end
17 changes: 17 additions & 0 deletions db/migrate/20240710210750_add_location_access_points.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddLocationAccessPoints < ActiveRecord::Migration[6.0]
def change
create_table :access_points do |t|
t.integer :client_count
t.timestamps null: false
end
create_table :access_points_locations do |t|
t.timestamps null: false
t.bigint :location_id, null: false
t.bigint :access_point_id, null: false
t.index :location_id
t.index :access_point_id
t.index [:location_id, :access_point_id], unique: true
end
add_column :locations, :wifi_connections_baseline, :int, null: true
end
end
Loading

0 comments on commit 7d9e453

Please sign in to comment.