forked from openstreetmap/openstreetmap-website
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add events and event organizers including tests.
- Loading branch information
Showing
26 changed files
with
1,027 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/*global showMap,formMapInput*/ | ||
|
||
$(document).ready(function () { | ||
if ($("#event_map_form").length) { | ||
formMapInput("event_map_form", "event"); | ||
} else if ($("#event_map_show").length) { | ||
showMap("event_map_show"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
class EventsController < ApplicationController | ||
layout "site" | ||
before_action :authorize_web | ||
before_action :set_event, :only => [:edit, :show, :update] | ||
# This needs to be one before load_and_authorize_resource, so cancancan will be handed | ||
# an event that contains a community, based on the input parameter community_id. | ||
before_action :set_params_for_new, :only => [:new] | ||
|
||
load_and_authorize_resource | ||
|
||
# GET /events | ||
# GET /events.json | ||
def index | ||
if params[:community_id] | ||
@community = Community.friendly.find(params[:community_id]) | ||
@events = @community.events | ||
else | ||
@community = nil | ||
@events = Event.all | ||
end | ||
rescue ActiveRecord::RecordNotFound | ||
@not_found_community = params[:community_id] | ||
render :template => "communities/no_such_community", :status => :not_found | ||
end | ||
|
||
# GET /events/1 | ||
# GET /events/1.json | ||
def show | ||
@community = Community.friendly.find(params[:community_id]) if params[:community_id] | ||
rescue ActiveRecord::RecordNotFound | ||
@not_found_community = params[:community_id] | ||
render :template => "communities/no_such_community", :status => :not_found | ||
end | ||
|
||
# GET /events/new | ||
def new | ||
@title = t ".new" | ||
@event = Event.new(event_params_new) | ||
end | ||
|
||
# GET /events/1/edit | ||
def edit; end | ||
|
||
# POST /events | ||
# POST /events.json | ||
def create | ||
@event = Event.new(event_params) | ||
@event_organizer = EventOrganizer.new(:event => @event, :user => current_user) | ||
|
||
if @event.save && @event_organizer.save | ||
warn_if_event_in_past | ||
redirect_to @event, :notice => t(".success") | ||
else | ||
flash.now[:alert] = t(".failure") | ||
render :new | ||
end | ||
end | ||
|
||
def update | ||
if @event.update(event_params) | ||
redirect_to @event, :notice => t(".success") | ||
else | ||
flash.now[:alert] = t(".failure") | ||
render :edit | ||
end | ||
end | ||
|
||
private | ||
|
||
def warn_if_event_in_past | ||
flash[:warning] = t "events.show.past" if @event.past? | ||
end | ||
|
||
def set_event | ||
@event = Event.find(params[:id]) | ||
end | ||
|
||
def set_params_for_new | ||
@params = event_params_new | ||
end | ||
|
||
def event_params | ||
params.require(:event).permit( | ||
:title, :moment, :location, :location_url, | ||
:latitude, :longitude, :description, :community_id | ||
) | ||
end | ||
|
||
def event_params_new | ||
params.require(:event).permit(:community_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module EventsHelper | ||
def event_location(event) | ||
if event.location_url.present? | ||
link_to event.location, event.location_url | ||
else | ||
event.location | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# == Schema Information | ||
# | ||
# Table name: events | ||
# | ||
# id :bigint(8) not null, primary key | ||
# title :string not null | ||
# moment :datetime not null | ||
# location :string not null | ||
# location_url :string | ||
# latitude :float | ||
# longitude :float | ||
# description :text not null | ||
# community_id :bigint(8) not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_events_on_community_id (community_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (community_id => communities.id) | ||
# | ||
|
||
class Event < ApplicationRecord | ||
belongs_to :community | ||
has_many :event_organizers | ||
|
||
scope :future, -> { where(:moment => Time.now.utc..) } | ||
scope :past, -> { where(:moment => ...Time.now.utc) } | ||
|
||
validates :moment, :datetime_format => true | ||
validates :location, :length => { :maximum => 255 }, :presence => true | ||
# While latitude and longitude below will implicitly convert blanks to nil, | ||
# the string/url here will not and I don't know why. | ||
validates( | ||
:location_url, | ||
:allow_nil => true, :length => { :maximum => 255 }, | ||
:url => { :allow_nil => true, :allow_blank => true, :schemes => ["https"] } | ||
) | ||
validates( | ||
:latitude, | ||
:allow_nil => true, | ||
:numericality => { | ||
:greater_than_or_equal_to => -90, | ||
:less_than_or_equal_to => 90 | ||
} | ||
) | ||
validates( | ||
:longitude, | ||
:allow_nil => true, | ||
:numericality => { | ||
:greater_than_or_equal_to => -180, | ||
:less_than_or_equal_to => 180 | ||
} | ||
) | ||
|
||
def organizers | ||
EventOrganizer.where(:event_id => id) | ||
end | ||
|
||
def past? | ||
moment < Time.now.utc | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# == Schema Information | ||
# | ||
# Table name: event_organizers | ||
# | ||
# id :bigint(8) not null, primary key | ||
# event_id :bigint(8) not null | ||
# user_id :bigint(8) not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_event_organizers_on_event_id (event_id) | ||
# index_event_organizers_on_user_id (user_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (event_id => events.id) | ||
# fk_rails_... (user_id => users.id) | ||
# | ||
class EventOrganizer < ApplicationRecord | ||
belongs_to :event | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "date" | ||
|
||
class DatetimeFormatValidator < ActiveModel::EachValidator | ||
# No need to pull in validates_timeless for just a simple validation. | ||
def validate_each(record, attribute, _value) | ||
# By this point in time, rails has already converted an invalid _value to | ||
# Nil. With built in rails validation, there's no good way to say the | ||
# input is not a valid date. Validate the user input. | ||
before_value = record.read_attribute_before_type_cast(attribute) | ||
return if before_value.is_a? Time | ||
|
||
Date.iso8601(before_value) | ||
rescue ArgumentError | ||
record.errors.add(attribute, options[:message] || I18n.t("validations.invalid_datetime_range")) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<%= javascript_include_tag "event" %> | ||
<%= bootstrap_form_for(@event) do |form| %> | ||
<% if event.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(event.errors.count, "error") %> prohibited this event from being saved:</h2> | ||
<ul> | ||
<% event.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
<div class="row"> | ||
<%= form.text_field :title, :id => :event_title %> | ||
</div> | ||
<div class="row"> | ||
<%= form.datetime_local_field :moment, :pattern => "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}" %> | ||
</div> | ||
<div class="row"> | ||
<%= form.text_field :location, :id => :event_location %> | ||
</div> | ||
<div class="row"> | ||
<%= form.text_field :location_url, :id => :event_location_url %> | ||
</div> | ||
<div class="row"> | ||
<%= form.text_area :description, :id => :event_description %> | ||
</div> | ||
<fieldset> | ||
<div class='row'> | ||
<%= form.text_field :latitude, :id => "event_latitude" %> | ||
</div> | ||
<div class='row'> | ||
<%= form.text_field :longitude, :id => "event_longitude" %> | ||
</div> | ||
<div class="event_set_location"> | ||
<div id="event_map_form" class="content_map"></div> | ||
</div> | ||
</fieldset> | ||
<% if @event&.community_id %> | ||
<%= form.hidden_field(:community_id, :value => @event.community_id) %> | ||
<% else %> | ||
<div class="row"> | ||
<%= collection_select(:event, :community_id, Community.all, :id, :name, :prompt => true) %> | ||
</div> | ||
<% end %> | ||
<%= form.primary %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<% if !events.empty? %> | ||
<h2> | ||
<%= header %> | ||
</h2> | ||
<table class="table table-borderless table-striped"> | ||
<thead> | ||
<tr> | ||
<th><%= t(".moment") %></th> | ||
<th><%= t(".title") %></th> | ||
<th><%= t(".location") %></th> | ||
<th><%= t(".community") %></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% events.each do |event| %> | ||
<tr> | ||
<td><%= l(event.moment, :format => :blog) %></td> | ||
<td><%= link_to event.title, event %></td> | ||
<td><%= event.location %></td> | ||
<td><%= link_to event.community.name, community_path(event.community) %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1><%= t(".edit_event") %></h1> | ||
|
||
<%= render "form", :event => @event %> |
Oops, something went wrong.